trouble getting Hello World / gas / Mac OS X to work

I’m trying to write Hello World in GNU assembler for Mac OS X, but it’s bus erroring before it can print “Hello World”.


.global start


.equ stdout, 1

.equ sys_write, 4
.equ sys_exit, 1

.equ kernel, 0x80

msg: .asciz "Hello World!\n"
.equ len, .-msg


	push $len
	push $msg
	push $stdout
	mov $sys_write, %eax
	sub $4, %esp
	int $kernel
	add $4 + $4 * $3, %esp

	push $0
	mov $sys_exit, %eax
	sub $4, %esp
	int $kernel


$ clang -c -o hello.o hello.s
$ ld -o hello -macosx_version_min 10.6 hello.o
$ ./hello
Bus error: 10

$ gdb hello
(gdb) run
Starting program: /Users/andrew/Desktop/src/mcandre/gas/hello/freebsd/hello

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x0000000000002000
0x0000000000002000 in msg ()

I’m basing my code off of a couple things:

I took the semantics from NASM / Mac OS X and the syntax from Gas / Linux.

I also tried the example program on the GNU Assembler Wikipedia page, but it also bus error’s.

I filed a bug report with Apple due to its GNU assembler being horribly out of date (as 1.38). They said to use clang instead, so here I am.

What am I doing wrong?


  • ld64-134.9
  • clang 4.1
  • Xcode 4.5
  • Mac OS X 10.8.2
  • MacBook Pro 2009


$ clang -arch i386 -c -o hello.o hello.s

When I assembled it 32-bit instead of 64-bit, it worked fine.