聘我网

新概念招聘3.0

如何理解Standard Entry Sequence与Standard Exit Sequence?

vote up0vote downstar

Standard Entry Sequence

_function: 
    push ebp       ;store the old base pointer 
    mov ebp, esp   ;make the base pointer point to the current 
                   ;stack location – at the top of the stack is the  
                   ;old ebp, followed by the return address and then 
                   ;the parameters. 
    sub esp, x     ;x is the size, in bytes, of all 
                   ;"automatic variables" in the function 

这里的return address以及parameters是怎么进栈的呢?

Standard Exit Sequence:

mov esp, ebp   ;reset the stack to "clean" away the local variables 
pop ebp        ;restore the original base pointer 
ret            ;return from the function 

这里到pop ebp这行应该已经到return address了,后面ret还有什么作用?

 

4 个答复

vote up0vote downcheck

x86标准调用惯例中,当调用函数之前,会先将参数压栈。

然后call会将下一行地址压栈,并跳到函数。

所以在调用push ebp之前堆栈状态如下:

 ... 
 param2 
 param1 
 param0 
 return_address  <- esp 

调用push ebp之后:

 ... 
 param2 
 param1 
 param0 
 return_address 
 ebp            <- esp 

最后调用mov ebp, espesp存储到ebp中。

Standard Exit Sequence:

ret的作用是将return_address出栈,并跳到那个地址。

链接
vote up0vote down

A few years ago I'd have to pay soonmee for this information.

链接
vote up0vote down

Thanks for the insight. It bigrns light into the dark!

链接
vote up0vote down

Thanks for starting the ball rolling with this iinsght.

链接

您的回答





不是您要找的问题? 浏览其他含有标签 的问题或者 自己问个.