Đối với mục đích thực tế, câu trả lời này hiển thị how to compile a hello world application using nasm on OSX.
Mã này có thể được biên dịch cho Linux như là, nhưng lệnh cmd-line để biên dịch nó có lẽ sẽ khác nhau:
section .text
global mystart ; make the main function externally visible
mystart:
; 1 print "hello, world"
; 1a prepare the arguments for the system call to write
push dword mylen ; message length
push dword mymsg ; message to write
push dword 1 ; file descriptor value
; 1b make the system call to write
mov eax, 0x4 ; system call number for write
sub esp, 4 ; OS X (and BSD) system calls needs "extra space" on stack
int 0x80 ; make the actual system call
; 1c clean up the stack
add esp, 16 ; 3 args * 4 bytes/arg + 4 bytes extra space = 16 bytes
; 2 exit the program
; 2a prepare the argument for the sys call to exit
push dword 0 ; exit status returned to the operating system
; 2b make the call to sys call to exit
mov eax, 0x1 ; system call number for exit
sub esp, 4 ; OS X (and BSD) system calls needs "extra space" on stack
int 0x80 ; make the system call
; 2c no need to clean up the stack because no code here would executed: already exited
section .data
mymsg db "hello, world", 0xa ; string with a carriage-return
mylen equ $-mymsg ; string length in bytes
Lắp nguồn (hello.nasm) đến một tập tin đối tượng:
nasm -f macho hello.nasm
liên kết để sản xuất thực thi:
ld -o hello -e mystart hello.o
Nhưng cơ chế gọi hệ thống có giống với Linux không? Tôi biết rằng Solaris cho một khác. –
Cú pháp có thể thay đổi rất nhiều từ trình biên dịch này sang trình biên dịch khác và bao gồm các định dạng lệnh và quy ước cuộc gọi khác nhau. – karlphillip
sự khác biệt thực sự là bạn cần phải cung cấp thêm 4 byte trong 32-bit mach nhị phân. Nó xuất phát từ di sản bsd. Xem câu trả lời của tôi cho một liên kết –