Skip to content

Instantly share code, notes, and snippets.

@kohyama
Created August 20, 2012 08:18
Show Gist options
  • Save kohyama/3402152 to your computer and use it in GitHub Desktop.
Save kohyama/3402152 to your computer and use it in GitHub Desktop.
'Hello World' in MacOS X with 'as' and 'ld'
# Usage:
# Save this file as 'hello.s'.
# $ as -o hello.o hello.s
# $ ld -o hello hello.o
# $ ./hello
#
# How to call a system call
# 1) Set rax to the index of the system call.
# 2) Set rdi, rsi, rdx, ... to arguments in this order.
# 3) Call 'syscall'.
#
# The entry point of an executable is 'start' ('_start' in Linux)
# The index of write(2) is '0x2000004' ('0x1' in Linux)
# The index of _exit(2) is '0x2000001' ('0x3c' in Linux)
.text
.globl start
start:
movq $0x2000004, %rax # write(2)
movq $1, %rdi # fildes <- 1 == stdout
leaq msg(%rip), %rsi # buf <- address of msg as relative address from rip
movq $14, %rdx # nbyte <- 14 == strlen("Hello world!\n")
syscall
movq $0x2000001, %rax # _exit(2)
movq $0, %rdi # status <- 0
syscall
.data
msg:
.ascii "Hello world!\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment