<div><div>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".</div><div><br></div><div><a href="https://github.com/mcandre/mcandre/blob/master/gas/hello/freebsd/hello.s">hello.s</a>:</div>
<div><pre style="word-wrap:break-word;white-space:pre-wrap">.global start

.data

.equ stdout, 1

.equ sys_write, 4
.equ sys_exit, 1

.equ kernel, 0x80

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

.text

start:
        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</pre></div><div>Trace:</div><div><br></div><div><div><font face="courier new, monospace">$ clang -c -o hello.o hello.s</font></div><div><font face="courier new, monospace">$ ld -o hello -macosx_version_min 10.6 hello.o</font></div>
<div><font face="courier new, monospace">$ ./hello</font></div><div><font face="courier new, monospace">Bus error: 10</font></div><div><div><font face="courier new, monospace"><br></font></div><div><div><font face="courier new, monospace">$ gdb hello</font></div>
<div><font face="courier new, monospace">(gdb) run</font></div><div><font face="courier new, monospace">Starting program: /Users/andrew/Desktop/src/mcandre/gas/hello/freebsd/hello </font></div><div><font face="courier new, monospace"><br>
</font></div><div><font face="courier new, monospace">Program received signal EXC_BAD_ACCESS, Could not access memory.</font></div><div><font face="courier new, monospace">Reason: KERN_PROTECTION_FAILURE at address: 0x0000000000002000</font></div>
<div><font face="courier new, monospace">0x0000000000002000 in msg ()</font></div></div></div></div></div><div><br></div>I'm basing my code off of a couple things:<div><ul><li><a href="https://github.com/mcandre/mcandre/blob/master/nasm/hello/freebsd/hello.asm">Working NASM / Mac OS X code for Hello World</a></li>
<li><a href="https://github.com/mcandre/mcandre/blob/master/gas/hello/linux/hello.s">Working Gas / Linux code for Hello World</a></li></ul></div><div>I took the semantics from NASM / Mac OS X and the syntax from Gas / Linux.</div>
<div><br></div><div>I also tried the example program on the GNU Assembler Wikipedia <a href="http://en.wikipedia.org/wiki/GNU_Assembler#Example_Programs">page</a>, but it also bus error's.</div><div><br></div><div>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.</div>
<div><br></div><div><div>What am I doing wrong?</div><div><br></div><div>Specs:</div><div><br></div><div>* ld64-134.9</div><div>* clang 4.1</div><div>* Xcode 4.5</div><div>* Mac OS X 10.8.2</div><div>* MacBook Pro 2009</div>
<div><br></div>-- <br><div></div>Cheers,<div><br></div><div>Andrew Pennebaker</div><div><a href="http://www.yellosoft.us" target="_blank">www.yellosoft.us</a></div>
</div>