Darwin(xnu)のシステムコールを直で呼び出してHello, world #2

実際にコードを書いてみる

ここらへんでだれてきたこともあり、linux版のサンプルを改造してやってみることに

http://tldp.org/HOWTO/Assembly-HOWTO/hello.html#AEN865

をベースに改造したのがこちら、
http://websvn.nyaxtstep.com/viewvc.cgi/sandpit/asm/x86/helloworld/hello.S?revision=748&view=markup

.text                                   # section declaration

                        # we must export the entry point to the ELF linker or
        .globl _main # loader. They conventionally recognize _start as their
                        # entry point. Use ld -e foo to override the default.


_main:
# write our string to stdout

        pushl   $len # third argument: message length
        pushl   $msg # second argument: pointer to message to write
        pushl   $1 # first argument: file handle (stdout)
        movl    $0x4, %eax # system call number (sys_write)
        movl    $(SYSEXIThere), %edx
        leal    -0x4(%esp), %ecx
        # movl  %esp, %ecx
        sysenter
SYSEXIThere:

# and exit

        pushl   $0              # first argument: exit code
        movl    $1,%eax         # system call number (sys_exit)
        leal    -0x4(%esp), %ecx
        sysenter

.data                                   # section declaration

msg:
        .ascii  "Hello, world!\n"       # our dear string
        len = . - msg                   # length of our dear string

lealとするべきところをmovlにしてしまっていて、しばらく悩んでしまった。

実行結果:

kouhei@snowshoe:~/svnwork/sandpit/asm/x86/helloworld$ ./hello; echo $?
Hello, world!
0