简体   繁体   English

为什么在转到下一个堆栈帧时存储旧的帧指针

[英]Why store the old frame pointer when go to next stack frame

I study the x86 assembly recently, and not understand why we have to do push ebp when entering a new function. 我最近研究了x86程序集,但不了解为什么在输入新功能时我们必须执行push ebp

From the survey, I can see the need for ebp is because that it can easily access the arguments of function call and the local variables in this new callee. 从调查中可以看出,需要ebp是因为它可以轻松访问此新被调用方中函数调用的参数和局部变量。

But I don't know why we have to store the old frame pointer when go to the new stack frame? 但是我不知道为什么在转到新的堆栈帧时我们必须存储旧的帧指针?

Is that because doing so will make stack trace easier for debugging? 那是因为这样做会使堆栈跟踪更易于调试吗?

The following is my test code: 以下是我的测试代码:

foobar:
.LFB0:
    .cfi_startproc
    push ebp                 #Why do this here??
    ....
    ....
    mov ebp, esp

Thanks in advance 提前致谢

You don't even have to use a frame pointer, the calling convention doesn't mandate that and optimized code frequently doesn't. 您甚至不必使用帧指针,调用约定也不需要这样做,而优化的代码通常也不需要。 What the calling convention does prescribe though is that some registers are callee-saved, that is they must be preserved for the caller. 但是,调用约定所规定的是某些寄存器是被调用者保存的,即必须为调用者保留它们。 This normally includes ebp . 通常包括ebp This requirement may be fulfilled by pushing/popping it. 可以通过推动/弹出来满足此要求。

As a side effect, if you do use frame pointers and you do know the structure of the caller, you may use it to access its frame. 作为副作用,如果您确实使用了框架指针并且知道调用者的结构,则可以使用它来访问其框架。 This is however not typically used. 但是,通常不使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM