开发者

Frame pointer, epb, and return address

The following image is from wikipedia entry on call stack and there is something that I don't understand completely:

Frame pointer, epb, and return address

I thought the frame pointer which is stored in ebp register is initialized as such in the 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. 

If so, then shouldn't the frame pointer in the image be pointing to after the return address and before it should be the previous frame pointer address and before that the return address? What am I missing?

Thanks!

*Taken from: What is exactly the base pointer and stack pointer? To what do they poi开发者_如何学Cnt?


Yes, you are right, frame pointer points to an address where is stored previous frame pointer, before return address. The correct picture would be

               | locals
               +---------
frame pointer->| prev frame pointer
               +--------
               | return address
               +--------


When the function is called. The return address is pushed onto stack and the stack pointer now points to return address. This is what happens inside the function:

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

The result is: EBP is pointing to previous value of EBP. ESP is pointing further 20 bytes from ESP. These 20 bytes will be used for local vars.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜