1,安装nasm

brew install nasm

2,编写hello world代码,保存为helloworld.asm

SECTION .text
msg: db "hello world!",0x0a
len: equ $-msg
global _main
kernel:
syscall
ret
_main:
mov rax,0x2000004
mov rdi,1
mov rsi,msg
mov rdx,len
call kernel
mov rax,0x2000001
mov rdi,0
call kernel

3, 编译

nasm -f macho64 -o helloworld.o helloworld.asm

4, 链接

ld -o helloworld -e _main helloworld.o

此时有以下报错,

ld: dynamic main executables must link with libSystem.dylib for architecture x86_64

解决办法:

ld -o helloworld -e _main helloworld.o -lSystem

成功,运行hellowworld

➜ ./helloworld
hello world!