我正在尝试计算将数据推送到栈中的大小。在尝试时,我收到了一个分段错误。我的目标是在 push
任何数据到栈上之前将 ebp
指向 esp
。在我将数据推入栈后,我用 ebp - esp
计算已推入数据的大小(以字节为单位),并将其存储在 ebx
中,并使用 printf
将其输出到标准输出。
例如:
; compile with:
; nasm -f elf test.asm && gcc -m32 -o test test.o
section .text
global main
extern printf
main:
; set the frame pointer to the beginning of the stack before-
; data is pushed.
push ebp
mov ebp, esp
push ebx
push 0x00 ; <- null terminating byte/string truncation
push 0x64636261 ; <- data
mov ebx, ebp
sub ebx, esp ; start of stack - end of stack = sizeof(data) stored in ebx
push ebx
push format_str
call printf
add esp, 8
pop ebp
pop ebx
ret
section .data
format_str db "%s", 2, 0
当我编译此代码时,输出如下:
Segmentation fault (core dumped)
预期输出:
5
%d
... - CherryDTmov esp, ebp
语句。 - Jester12
是正确的。你推了3个东西,每个4字节。ebx
也是4字节,而push 0x00
将推送4个零而不是一个。 - Jester