Skip to content

Instantly share code, notes, and snippets.

@liamgriffiths
Last active August 29, 2015 14:10
Show Gist options
  • Save liamgriffiths/1f9898d60608d6897ed6 to your computer and use it in GitHub Desktop.
Save liamgriffiths/1f9898d60608d6897ed6 to your computer and use it in GitHub Desktop.
hello world nasm
; hello world in intel 64bit x86 assembly
; drawn on from examples here:
; https://en.wikipedia.org/wiki/Netwide_Assembler
; http://www.nasm.us/xdoc/2.11.06/html/nasmdoc0.html
; the `start` global defined the entry point to the program
global start
; osx system call numbers
%define SYSCALL_EXIT 0x2000001
%define SYSCALL_READ 0x2000003
%define SYSCALL_WRITE 0x2000004
; i/o file descriptors
%define STDIN 0
%define STDOUT 1
%define STDERR 2
; exit codes
%define OK 0
%macro print 1
; execute a system call to write (`man 2 write`)
; first line, puts the syscall number into the `rax` register
; the next lines fill in the params for `write` - see the signature
; ssize_t write(int fildes, const void *buf, size_t nbyte);
mov rax, SYSCALL_WRITE
mov rdi, STDOUT ; file descriptor
mov rsi, %1 ; buffer
mov rdx, %1.len ; size
syscall
%endmacro
%macro wait_for_enter 0
mov rax, SYSCALL_READ
mov rdi, STDIN
mov rdx, 1
syscall
%endmacro
%macro exit 1
mov rax, SYSCALL_EXIT
mov rdi, %1
syscall
%endmacro
section .data
message db "hello hello, world!"
.len equ $-message
newline db 0x0a
.len equ $-newline
section .text
start:
print message
print newline
wait_for_enter
exit OK
# osx nasm compile using macho64
NASM=/usr/local/bin/nasm
LD=/usr/bin/ld
all: hello
@./hello
hello: hello.o
@$(LD) -macosx_version_min 10.0 -o hello hello.o
hello.o:
@$(NASM) -f macho64 hello.asm -o hello.o
clean:
@rm -f hello hello.o
.PHONY: clean hello hello.o
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment