Writing a compiler in school, last milestone is generating assembly code. Trying to learn NASM. Starting at the beginning, http://www.cs.lmu.edu/~ray/notes/nasmexamples/, trying to compile a Hello World.
; ----------------------------------------------------------------------------
; helloworld.asm
;
; This is a Win32 console program that writes "Hello, World" on one line and
; then exits. It needs to be linked with a C library.
; ----------------------------------------------------------------------------
global _main
extern _printf
section .text
_main:
push message
call _printf
add esp, 4
ret
message:
db 'Hello, World', 10, 0
To assemble, link and run this program under Windows:
nasm -fwin32 helloworld.asm
gcc helloworld.obj
a
Under Linux, you'll need to remove the leading underscores from function names, and execute
nasm -felf helloworld.asm
gcc helloworld.o
./a.out
But I'm on OSX. Found this little resource: http://salahuddin66.blogspot.com/2009/08/nasm-in-mac-os-x.html. In Mac OS X we should use format macho...
nasm -f macho -o hello.o hello.asm
...and for the linker (we need to specify the entry point)...
ld -e main -o hello hello.o
But when I do this...
Undefined symbols:
"printf", referenced from:
_main in hello.o
ld: symbol(s) not found for inferred architecture i386
Sorry, I know it's a lot to read. And I doubt there are many NASM coders around these parts, but worth a try right? I'd appreciate any help I can get.
Function printf
is defined in some C library (on Linux, that would be in e.g. /lib/libc.so.6
or /lib/x86_64-linux-gnu/libc.so.6
) so you need to link to that library (I don't know what it is on MacOSX)
You could do directly system calls i.e. syscalls (I don't know the details for MacOSX, and I don't know if they are publicly available). On Linux, the Linux Assembly Howto give the details. You need to find equivalent details for your operating system.
(BTW, using entirely free software is definitely easier for such tasks, because their specification and source code is available; with proprietary software like probably MacOSX is, you need to get the from the software provider, sometimes it is very very expensive)