【发布时间】:2018-03-25 23:27:51
【问题描述】:
我试图了解当您在 OS X 机器上调用子例程时究竟会发生什么(因为这就是我面前的情况)。我正在使用 nasm 版本 0.98.40。
我阅读了一些主要针对其他平台的教程,并注意到其中一些具有定义
_syscall:
int 0x80
ret
所以,我尝试了一个简单的 hello world,有和没有那个子例程,但其他方面是相同的。
我很难理解为什么calling 一个子例程在这种情况下不“等同于”直接使用它的主体。我认为我不会干扰系统调用所需的任何寄存器。
这是两个汇编文件。
; without_subr.asm
section .text
global start
start:
push dword msg.len
push dword msg
push dword 1
mov eax,4
sub esp,4
int 0x80
add esp,16
push dword 0
mov eax, 1
sub esp, 12
int 0x80
section .data
msg: db 'Hello, world!',10
msg.len: equ $ - msg
和子程序:
; with_subr.asm
section .text
global start
_syscall:
int 0x80
ret
start:
push dword msg.len
push dword msg
push dword 1
mov eax,4
sub esp,4
call _syscall
add esp,16
push dword 0
mov eax, 1
sub esp, 12
int 0x80
section .data
msg: db 'Hello, world!',10
msg.len: equ $ - msg
当我组装、链接和运行 without_subr.asm 时,一切都按预期工作:
$ nasm -f macho without_subr.asm
$ ld -o without_subr without_subr.o
$ ./without_subr
Hello, world!
但是,with_subr.asm 什么也没产生,异常退出
$ nasm -f macho with_subr.asm
$ ld -o with_subr with_subr.o
$ ./with_subr
Exit 1
【问题讨论】:
-
看看这个little tutorial。 MacOS 上的系统调用的实现与 BSD 类似。