4

下图来自调用堆栈上的维基百科条目,有些我不完全理解:

替代文字

我认为存储在 ebp 寄存器中的帧指针在 prologue* 中是这样初始化的:

push ebp  ; Preserve current frame pointer 
mov ebp, esp ; Create new frame pointer pointing to current stack top 
sub esp, 20 ; allocate 20 bytes worth of locals on stack. 

如果是这样,那么图像中的帧指针不应该指向返回地址之后,它应该是前一个帧指针地址,然后是返回地址吗?我错过了什么?

谢谢!

*取自:基指针和堆栈指针到底是什么?他们指向什么?

4

2 回答 2

6

是的,你是对的,帧指针指向一个地址,在返回地址之前存储前一帧指针。正确的图片应该是

               | locals
               +---------
frame pointer->| prev frame pointer
               +--------
               | return address
               +--------
于 2010-12-13T06:36:48.200 回答
0

当函数被调用时。返回地址被压入堆栈,堆栈指针现在指向返回地址。这是函数内部发生的事情:

push ebp  ; Push the ebp; The ebp address will pushed on stack and sp will be decremented
mov ebp, esp ;  EBP will now point the same as ESP which is previous value of EBP
sub esp, 20 ;    ESP will be subtracted further to create frame for local variables

结果是:EBP 指向 EBP 的先前值。ESP 指向 ESP 的另外 20 个字节。这 20 个字节将用于本地变量。

于 2010-12-13T06:37:34.020 回答