Hello World using assembler code

Hello!
I'm trying to write a program in assembler code. Here it is:

.data
hello: .ascii "Hello, World!\n"
len: .short len - hello

.text
.globl _start

_start:

mov $4,%eax # system call number (sys_write)
mov $1,%ebx # first argument: file handle (stdout)
lea hello,%ecx # second argument: pointer to message to write
mov len,%edx # third argument: message length
int $0x80 # call kernel

mov $1,%eax # system call number (sys_exit)
mov $0,%ebx # first argument: exit code
int $0x80 # call kernel

This program should display a "Hello World!" message. But it produces nothing...

Can anyone tell me what's the matter with this program?

Thanks a lot for any help!

Christophe

iMac Intel Core Duo 17", Mac OS X (10.4.7)

Posted on Oct 22, 2006 11:25 AM

Reply
3 replies

Oct 23, 2006 4:11 AM in response to Christophe Enderlin

you could always get the c compiler to help you figure it out. though
the output can be a bit hard to read.

$ cat HelloWorld.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <stdlib.h>



int main(int argc, char *argv[]) {

write(1, "Hello World\n", 12);

exit(0);
}

distills down to the following, after running 'cc -S HelloWorld.c':

$ cat HelloWorld.s
.cstring
LC0:
.ascii "Hello World\12\0"
.text
.globl _main
_main:
pushl %ebp
movl %esp, %ebp
pushl %ebx
subl $20, %esp
call __i686.get_pcthunk.bx
"L00000000001$pb":
movl $12, 8(%esp)
leal LC0-"L00000000001$pb"(%ebx), %eax
movl %eax, 4(%esp)
movl $1, (%esp)
call L_write$stub
movl $0, (%esp)
call L_exit$stub
.section _IMPORT,__jump_table,symbol_stubs,self_modifying_code+pureinstructions,5
L_write$stub:
.indirect_symbol _write
hlt ; hlt ; hlt ; hlt ; hlt
L_exit$stub:
.indirect_symbol _exit
hlt ; hlt ; hlt ; hlt ; hlt
.subsections viasymbols
.section _TEXT,__textcoal_nt,coalesced,pureinstructions
.weak_definition __i686.get_pcthunk.bx
.private_extern __i686.get_pcthunk.bx
__i686.get_pcthunk.bx:
movl (%esp), %ebx
ret

Oct 23, 2006 1:20 PM in response to Nils C. Anderson

Thanks for your answers!

In fact, meanwhile I found the answer. The point is that system calls are different between Linux and OSX.

In Linux, you have to put the arguments in the registers, and then make the call. But in OSX, the aguments have to be put on the stack before actually making the call.

Here is a code that works:

.data
msg: .ascii "Hello, World!\n"
len: .long len - msg

.text
.globl _start

_start:
nop # for gdb breakpoint
push len # length of the message
push $msg # message to be displayed
push $1 # file descriptor (stdout)
movl $0x4,%eax # system call number (sys_write)
call _syscall # system call

add $12,%esp # stack cleaning (3 arguments * 4)

push $0 # exit code

movl $0x1,%eax # system call number (sys_exit)
call _syscall # sysem call

_syscall:
int $0x80
ret

Thanks again and greetings!

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Hello World using assembler code

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.