Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hello World using x86 assembler on Mac 0SX

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

like image 502
D.C. Avatar asked Nov 26 '10 22:11

D.C.


2 Answers

You'll probably find it easier to build with gcc rather than trying to micro-manage the assembler and linker, e.g.

$ gcc helloWorld.s -o helloWorld

(You'll probably want to change _start to _main if 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.

#include <stdio.h>

int main(void)
{
    puts("Hello world!\n");

    return 0;
}

when compiled with gcc -Wall -O3 -m32 -fno-PIC hello.c -S -o hello.S generates:

    .cstring
LC0:
    .ascii "Hello world!\12\0"
    .text
    .align 4,0x90
.globl _main
_main:
    pushl   %ebp
    movl    %esp, %ebp
    subl    $24, %esp
    movl    $LC0, (%esp)
    call    _puts
    xorl    %eax, %eax
    leave
    ret
    .subsections_via_symbols

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:

$ gcc -m32 hello.S -o hello
$ ./hello 
Hello world!

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 !

like image 59
Paul R Avatar answered Oct 15 '22 00:10

Paul R


Try:

ld -e _start -arch x86_64 -o HelloWorld HelloWorld.S

then:

./HelloWorld

Info:

-e <entry point>
-arch <architecture>, You can check your architecture by uname -a 
-o <output file>
like image 37
abdimuna1 Avatar answered Oct 15 '22 01:10

abdimuna1