【发布时间】:2016-08-06 09:22:33
【问题描述】:
我正在阅读一本书,其中所有汇编代码示例都是为 32 位 Linux 环境编写的,并且我使用的是 64 位 Mac。在将_start 更改为start 后,我能够使用 NASM 编译以下程序。但是,当我运行可执行文件时,它不会像我期望的那样打印hello world。是否可以选择传递给 NASM 以在 64 位 Mac 上运行?
我试过了:
nasm -f macho32 helloworld.asm
和
nasm -f macho helloworld.asm
接着是:
ld helloworld.o -o helloworld
我的代码是:
section .data ; data segment
msg db "Hello, world!", 0x0a ; the string and newline char
section .text ; text segment
global start ; Default entry point for ELF linking
start:
; SYSCALL: write(1, msg, 14)
mov eax, 4 ; put 4 into eax, since write is syscall #4
mov ebx, 1 ; put 1 into ebx, since stdout is 1
mov ecx, msg ; put the address of the string into ecx
mov edx, 14 ; put 14 into edx, since our string is 14 bytes
int 0x80 ; Call the kernel to make the system call happen
; SYSCALL: exit(0)
mov eax, 1 ; put 1 into eax, since exit is syscall #1
mov ebx, 0 ; exit with success
int 0x80 ; do the syscall
【问题讨论】:
-
int 0x80在 OS/X 和 Linux 上的工作方式不同(系统调用不同)。不建议使用 Linux 代码作为起点。你真的需要 32 位 Mac OS/X 的汇编语言教程一个合理的起点是:filippo.io/making-system-calls-from-assembly-in-mac-os-x -
32 位 OS/X 代码将在 64 位 OS/X 中运行。如果您想创建真正的 64 位 OS/X 代码,那么您需要删除
int 0x80并转而使用syscall指令。这与int 0x80不同。调用约定不同,系统调用号与 32 位 OS/X 代码不匹配。您需要找到针对 64 位 OS/X 代码的教程。您还必须在 NASM 中使用-f macho64。