Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 20bb5530767718e483e3308b0de9a99b453396cd 0 parents
@anderscarling authored
2  .gitignore
@@ -0,0 +1,2 @@
+*.o
+*.bin
18 Makefile
@@ -0,0 +1,18 @@
+NASM=/usr/local/bin/nasm
+LD=ld
+
+all: hello_world.bin hello_world64.bin
+
+hello_world.o:
+ $(NASM) -f macho hello_world.asm
+hello_world.bin: hello_world.o
+ $(LD) -e main -macosx_version_min 10.7.0 -lSystem -o hello_world.bin hello_world.o
+
+hello_world64.o:
+ $(NASM) -f macho64 hello_world64.asm
+hello_world64.bin: hello_world64.o
+ $(LD) -e main -macosx_version_min 10.7.0 -lSystem -o hello_world64.bin hello_world64.o
+
+clean:
+ rm -f *.o
+ rm -f *.bin
14 README.md
@@ -0,0 +1,14 @@
+# Hello World in NASM assembly for OS X (32 & 64 bit)
+
+I won't promise this is done "the right way" (tm), I've never really
+used assembly but wanted to play around with it some.
+Everything seems to work under OS X Lion.
+
+To assemble the 64 bit version you'll need to build your own nasm (use
+homebrew).
+
+## Thanks
+These, among other sources, where helpful:
+* http://thexploit.com/secdev/mac-os-x-64-bit-assembly-system-calls/
+* http://www.x86-64.org/documentation/abi.pdf
+* http://michaux.ca/articles/assembly-hello-world-for-os-x
22 hello_world.asm
@@ -0,0 +1,22 @@
+ SECTION .data
+msg: db "Hello World!", 0x0a
+len: equ $-msg
+
+ SECTION .text
+ global main
+
+kernel:
+ int 0x80
+ ret
+
+main:
+ mov eax, 4 ; SYS_write
+ push dword len
+ push dword msg
+ push dword 1 ; stdout
+ call kernel
+ add esp, 12
+
+ push dword 0
+ mov eax, 1
+ call kernel
21 hello_world64.asm
@@ -0,0 +1,21 @@
+ SECTION .data
+msg: db "Hello World!", 0x0a
+len: equ $-msg
+
+ SECTION .text
+ global main
+
+kernel:
+ syscall
+ ret
+
+main:
+ mov rax, 0x2000004 ; SYS_write
+ mov rdi, 1
+ mov rsi, msg
+ mov rdx, len
+ call kernel
+
+ mov rax, 0x2000001
+ mov rdi, 0
+ call kernel
Please sign in to comment.
Something went wrong with that request. Please try again.