Attempting to assemble and run nasm on mac OSX - macos

So for awhile I have been using the Irvine library to kind of dip my toes into assembly (masm) language. Moving up in the world I want to get off the training wheels and let go of the Irvine library and jump into the pool with nasm on mac OSX. The problem here is I cannot even get terminal to open up the simple nasm hello world file.
The scene looks like this:
simple nasm hello world file written and saved to desktop
terminal is opened up and
nasm -f macho64 -o hello.o hello.asm (I have also tried .nasm and macho and macho32)
is inserted into the terminal.
A fatal error occurs which does not allow me to continue the learning process.
nasm: fatal: unable to open input file 'hello.asm'
Any help would be greatly appreciated because I have looked up a solution but cannot find any "helpful" advice. (Quotation marks because the advice is just not specifically helpful to my situation, or does not work to solve the problem.)

So the problem here was very simple and I hate myself a little for not realizing my mistake. The answer to this was very simple: to allow permissions with sudo.
`sudo nasm -f macho32 -o hello.o hello.asm`
You live and you learn I suppose.

Related

Automating GCC Compiler arguments to create easier compilations - Windows OS?

Goal
When I run the command:
gcc -ggdb -std=c99 -Wall -Werror hello.c -lcs50 -o test.exe from the root directory
I am able to build the test.exe file and when I run test.exe all is well (thanks to this post by Manohar Reddy Poreddy)
However all of those flags are a little bit cumbersome and I think it would great to condense them into a 'make' command or similar. How would I do this on windows?
Context
GCC, G++ and GDB all seem to be correctly linked (I used chocolatey which paths everything automatically)
Okay so I found what I was looking for.
I hope this answer can help others. Turns out the utility is called 'make' (no surprises). In your directory you essentially create a 'makefile' where you can include your command line arguments which saves on repeated typing in the command line for each compile.
Here is an excellent response on how to install 'make' for windows and was perfect for my use case as a Chocolatey user.
I also found this resource which helps newcomers begin to get their head round GCC which I highly recommend if you're coming into this like I was and felt completely out of your depth.

getting error while executing code of assembly language on nasm Ubuntu Terminal

I tried it so many time, but while executing the assembly language code on terminal through nasm to make an executable file, then I stuck here, any one can help me to get out of this problem..
i tried these command line to execute it.
nasm -f elf helloworld.asm
to make run the file then i want to make an object file to execute it so I used this command
id -s -o helloworld helloworld.o
but the second command is not running because there are no command options such as -s or -o.
Please help me and follow the image thanks

How to get started with gfortran on a Mac?

Just finished installing the compiler gfortran 6.1. El Capitan. Next, I want to know how to run a file. So, I have a few questions:
Which extension should I use to save the file? Is it .f90 or something else?
What kind of software can be used to edit and save the source code?
Once I save the file, how do I compile it? Is is gfortran followed by the file name (with path) in the Terminal? Also, how does the path look like on a Mac?
A step by step guide would be a great help. I am a first time Mac user. :)
.f90 for free formatted sources is commonly used
Any texteditor you are comfortable with and used to, vim for example
gfortran /your/source/file that would be with an absolute path. If you are in the directory of the source file already you do not need to specify the complete path.
Step by step "hello world":
Create a text file with your Fortran program
Just to give a command line example without the need for an editor (this is not what you typically would do):
Open the terminal, then enter
cat >hello.f90 <<EOF
program hello
implicit none
write(*,*) 'hello world'
end program hello
EOF
Compile your program in the terminal with: gfortran hello.f90
Execute it in the terminal with ./a.out
If you want another name use the -o option:
gfortran -o hello hello.f90
./hello

How to do source level debugging of x86 code with GDB inside QEMU?

I wrote a x86 assembly program for MBR section.
I compile it as follows:
nasm hellombr.asm -f bin -o hellombr.img
Then I run it in qemu:
qemu -fda hellombr.img -boot a
The question is how can I debug my program at source level?
You should let nasm create the debugging symbols in an ELF file and then dump this to a flat binary to be used in the MBR. You can then instruct GDB to read the necessary symbols from the ELF file.
The complete procedure would then become something like this:
$ nasm hellombr.asm -f elf -g -o hellombr.elf
$ objcopy -O binary hellombr.elf hellombr.img
$ qemu -s -S -fda hellombr.img -boot a
$ gdb
(gdb) symbol-file hellombr.elf
(gdb) target remote localhost:1234
For an explanation of the flags I pass to qemu see this answer.
Instead of using qemu, use bochs. It is completely compatible, albeit slower. It is also an emulator but if you make it from sources, using these flags and build it like this:
./configure --enable-debugger --enable-disasm --disable-docbook
make
make install
you can place breakpoints in your code, step through it, view GDT, IDT and everything you needed to know.
A really good (and simple) way is to use IDA with bochs, you find an excellent blog post on it here, along with some other hints/suggestions for bootloader development.

Debugging information not included with -g

Currently attempting to debug with KDbg / gdb the source code for Towers of Hanoi from http://www.kernelthread.com/projects/hanoi//html/asm.html (great resource)
Since I wanted to review how the stack is used within this problem, I assembled it with NASM and used GCC to link it. However, I noticed that in KDbg, the current point of execution was not updating (i.e, I could not tell where I was within the file). Since KDbg relies on gdb, I ran the code within gdb to see if I experienced similar issues.
If I set a breakpoint on line #30 in the program (which is a line within the main function), I get the following:
(gdb) break 30
Breakpoint 2 at 0x804840b: file hanoi.asm, line 30.
(gdb) next
Single stepping until exit from function main,
which has no line number information.
I'm currently compiling the assembly with the following little script I've written (I should probably migrate to a make file, but this has been working up until now)
bschlinker#net1develop02:~/.scripts$ cat asmgcc
# /usr/bin/sh
nasm -f elf -g -F stabs $1.asm -l $1.lst
gcc -g $1.o -o $1
I just migrated from CentOS to Ubuntu, so I'm not sure if this is an OS environment issue I'm not familiar with, or another issue.
As always, thanks in advance for any assistance.
Try -F dwarf instead of -F stabs.
You can assemble with as -o tmp.o something.s && ld -s -o something tmp.o && rm tmp.o.
In gdb just display/8i *$eip (or rip if 64 bit), it will display 8 instructions after instruction pointer with every step. So you don't need debugging info at all ;-)

Resources