I am trying to dive into some x86 assembly programming on my Mac, but am having trouble producing an executable. The problem seems to be at the linking stage.
helloWorld.s:
.data
HelloWorldString:
.ascii "Hello World\n"
.text
.globl _start
_start:
# load all the arguments for write()
movl $4, %eax
movl $1, %ebx
movl $HelloWorldString, %ecx
movl $12, %edx
# raises software interrupt to call write()
int $0x80
# call exit()
movl $1, %eax
movl $0, %ebx
int $0x80
Assemble the program:
as -o helloWorld.o helloWorld.s
Link the object file:
ld -o helloWorld helloWorld.o
The error I get at this point is:
ld: could not find entry point "start" (perhaps missing crt1.o) for inferred architecture x86_64
Any advice on what I’m doing wrong / missing would be very helpful. thanks
You’ll probably find it easier to build with gcc rather than trying to micro-manage the assembler and linker, e.g.
(You’ll probably want to change
_startto_mainif you go this route.)Incidentally, it can be instructive to start with a working C program, and study the generated asm from this. E.g.
when compiled with
gcc -Wall -O3 -m32 -fno-PIC hello.c -S -o hello.Sgenerates:You might want to consider using this as a template for your own “Hello world” or other experimental asm programs, especially given that it already builds and runs:
One final comment: beware of taking examples from Linux-oriented asm books or tutorials and trying to apply them under OS X – there are important differences !