我逐步使用汇编语言来学习Linux上的汇编语言编程。我最近有一台Mac,在int 0x80上似乎不起作用(非法指令)。

所以只是想知道是否有一个很好的引用书(书/网页),它给出了标准unix程序集和darwin程序集之间的区别。

最佳答案

出于实际目的,此答案显示how to compile a hello world application using nasm on OSX

可以按原样为linux编译此代码,但是用于编译它的cmd-line命令可能会有所不同:

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

将源(hello.nasm)组装到目标文件:
nasm -f macho hello.nasm

链接以生成可执行文件:
ld -o hello -e mystart hello.o

关于macos - OS X中的汇编语言,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6990885/

10-11 18:42