Skip to content

Instantly share code, notes, and snippets.

@vishaltelangre
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vishaltelangre/0b5e8dd46da49ca8d01a to your computer and use it in GitHub Desktop.
Save vishaltelangre/0b5e8dd46da49ca8d01a to your computer and use it in GitHub Desktop.
Hello NASM on x86_64 Mac OSX
;; BUILD AS:
; nasm -f macho64 -o hello.o 1.asm && ld -macosx_version_min 10.7.0 -lSystem -o hello hello.o && ./hello
; ^ ^ ^
; [generate object file from assembly] [link object file] [execute]
%define SYSCALL_WRITE 0x2000004
%define SYSCALL_EXIT 0x2000001
;; initialised data section
section .data
; Define constants
num1: equ 100
num2: equ 50
; initialize message
msg: db `Fantastic, boy!\n` ; to use escape sequences, use backticks
msglen: equ $ - msg
section .text
global start
;; entry point
start:
; set num1's value to rax
mov rax, num1
; set num2's value to rbx
mov rbx, num2
; get sum of rax and rbx, and store it's value in rax
add rax, rbx
; compare rax and 150
cmp rax, 150
; go to .exit label if rax and 150 are not equal
jne .exit
; go to .rightSum label if rax and 150 are equal
jmp .rightSum
; Print message that sum is correct
.rightSum:
;; write syscall
mov rax, SYSCALL_WRITE
;; file descritor, standard output
mov rdi, 1
;; message address
mov rsi, msg
;; length of message
mov rdx, msglen
;; call write syscall
syscall
; exit from program
jmp .exit
; exit procedure
.exit:
; exit syscall
mov rax, SYSCALL_EXIT
; exit code
mov rdi, 0
; call exit syscall
syscall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment