gpt4 book ai didi

macos - os x 中的汇编语言

转载 作者:行者123 更新时间:2023-12-04 12:24:21 32 4
gpt4 key购买 nike

我一步步使用汇编语言在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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com