我正在尝试掌握 FreeBSD 中的汇编程序。在手册的 UNIX 过滤器代码示例中,寄存器 esp 在每次系统调用后都会重置。有问题的代码是:
%include 'system.inc'
section .data
hex db '0123456789ABCDEF'
buffer db 0, 0, ' '
section .text
global _start
_start:
; read a byte from stdin
push dword 1
push dword buffer
push dword stdin
sys.read
add esp, byte 12 ; <--------- Is this necessary?
or eax, eax
je .done
; convert it to hex
movzx eax, byte [buffer]
mov edx, eax
shr dl, 4
mov dl, [hex+edx]
mov [buffer], dl
and al, 0Fh
mov al, [hex+eax]
mov [buffer+1], al
; print it
push dword 3
push dword buffer
push dword stdout
sys.write
add esp, byte 12 ; <--------- Is this necessary?
jmp short _start
.done:
push dword 0
sys.exit
这与文档上一页的示例不同:
1: %include 'system.inc'
2:
3: section .data
4: hello db 'Hello, World!', 0Ah
5: hbytes equ $-hello
6:
7: section .text
8: global _start
9: _start:
10: push dword hbytes
11: push dword hello
12: push dword stdout
13: sys.write ; <--------- ESP not adjusted after this. Why?
14:
15: push dword 0
16: sys.exit
为什么这两个例子不同?为什么需要类似的东西add esp, byte 12
?系统调用没有弹出值吗?在没有在堆栈上传递参数的 64 位 FreeBSD 中这是必要的吗?我想象堆栈指针会自行处理。