Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
anderscarling committed Aug 22, 2011
0 parents commit 20bb553
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
*.o
*.bin
18 changes: 18 additions & 0 deletions 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 changes: 14 additions & 0 deletions 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 changes: 22 additions & 0 deletions 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 changes: 21 additions & 0 deletions 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

0 comments on commit 20bb553

Please sign in to comment.