Skip to content

Instantly share code, notes, and snippets.

@FiloSottile
Last active January 31, 2025 03:22
Show Gist options
  • Save FiloSottile/7125822 to your computer and use it in GitHub Desktop.
Save FiloSottile/7125822 to your computer and use it in GitHub Desktop.
NASM Hello World for x86 and x86_64 Intel Mac OS X (get yourself an updated nasm with brew)
; /usr/local/bin/nasm -f macho 32.asm && ld -macosx_version_min 10.7.0 -o 32 32.o && ./32
global start
section .text
start:
push dword msg.len
push dword msg
push dword 1
mov eax, 4
sub esp, 4
int 0x80
add esp, 16
push dword 0
mov eax, 1
sub esp, 12
int 0x80
section .data
msg: db "Hello, world!", 10
.len: equ $ - msg
; /usr/local/bin/nasm -f macho64 64.asm && ld -macosx_version_min 10.7.0 -lSystem -o 64 64.o && ./64
global start
section .text
start:
mov rax, 0x2000004 ; write
mov rdi, 1 ; stdout
mov rsi, msg
mov rdx, msg.len
syscall
mov rax, 0x2000001 ; exit
mov rdi, 0
syscall
section .data
msg: db "Hello, world!", 10
.len: equ $ - msg
@Shvabrik
Copy link

Shvabrik commented Jan 25, 2025

I don't recommend using syscall too, they are not stable in macOS and may change, use dynamic linking with libsystem to read

Code on NASM here:

global main

extern _write
extern _exit

section .data
text:   db      "Bebebe", 0Ah

section .text
main:
        mov     rdi, 1
        lea     rsi, [rel +  text]
        mov     rdx, 07h
        call    _write
        xor     rdi, rdi
        call    _exit

Commands here:

assembling: nasm -f macho64 -o name.o name.asm
linking: ld -o name name.o -syslibroot `xcrun -sdk macosx --show-sdk-path` -l System -e "main"

Change name to the file name

My info:

get nasm: brew install nasm
get ld: Xcode-select --install
my macOS: Ventura 13.7.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment