Macos OSX中的汇编语言
我一步一步地用汇编语言学习linux上的汇编语言编程。我最近买了一台Mac电脑,上面的Macos OSX中的汇编语言,macos,assembly,Macos,Assembly,我一步一步地用汇编语言学习linux上的汇编语言编程。我最近买了一台Mac电脑,上面的int0x80似乎不起作用(非法指令) 因此,我只是想知道是否有一本很好的参考书(书/网页),它给出了标准unix程序集和darwin程序集之间的区别。这个答案表明,出于实际目的 可以按原样为linux编译此代码,但编译此代码的cmd行命令可能会有所不同: section .text global mystart ; make the main function external
int0x80
似乎不起作用(非法指令)
因此,我只是想知道是否有一本很好的参考书(书/网页),它给出了标准unix程序集和darwin程序集之间的区别。这个答案表明,出于实际目的 可以按原样为linux编译此代码,但编译此代码的cmd行命令可能会有所不同:
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
你能发布你的代码和你是如何编译的吗?(有许多方法可以引发非法指令错误) OSX采用了bsd风格的传递参数,这就是为什么您必须做一些稍微不同的事情
不久前,我将此添加到书签中:此问题可能会有所帮助: 不幸的是,这本书似乎是找到答案的唯一途径。至于int0x80,我怀疑它是否能工作,因为它是一个非常特定于Linux的API,直接构建在内核中
当我在一个不熟悉的操作系统上工作时,我所做的妥协是只使用libc调用,但我可以理解,如果你只是想学习,那么即使这样也可能太高。但是系统调用机制与Linux相同吗?我知道Solaris的不同之处。不同的编译器的语法可能会有很大的变化,其中包括不同的指令格式和调用约定。真正的区别在于,您需要在32位mach二进制文件中额外提供4个字节。它来自bsd传统。请参阅我的回复以获取链接