x86 Assembly:为什么需要堆叠框架? [英] x86 Assembly: Why Do I Need Stack Frames?

查看:122
本文介绍了x86 Assembly:为什么需要堆叠框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在大多数x86汇编代码(尤其是NASM)代码示例中,我看到的(甚至在GCC生成的代码示例中)都看到了所谓的堆栈框架设置".像这样:

On most x86 Assembly (NASM especifically) code samples I see around (even on the ones generated by GCC) I see what's called "setup of stack frame". Like this:

main: 
        /*setting the stack frame*/
        push    ebp     
        mov     ebp,esp

        ...
        code goes here
        ...

        /*removing the stack frame*/
        mov     esp, ebp
        pop     ebp

我对此做法有3个问题:

I have 3 questions about this practice:

  1. 如果我的代码没有碰到堆栈,则如上所述设置/删除堆栈框架是完全没有用的,对吗?

  1. If my code doesn't touch the stack then setting/removing the stack frame as above is completely useless, right?

即使我的代码使用了堆栈,只要弹出我推送的所有内容(基本上保留堆栈),然后再次设置堆栈框架也是完全没有用的,对吗?

Even if my code uses the stack, as long as pop everything I push (leaving the stack as it was essentially) then again setting up a stack frame is completely useless, right?

正如我所看到的,这样做的唯一目的是保存ESP的值,以便我可以在代码中使用它,而不必担心将其弄乱,一旦完成,我就可以简单地恢复它的值.原始值.这是设置堆栈框架的目的还是我缺少什么?

As I see it the only purpose of this would be to save the value of ESP so that I can play around with it on my code without worrying about messing things up, and once I am done I simply restore its original value. Is this the purpose of the stack frame setup or am I missing something?

谢谢

推荐答案

实际上,您不需要堆栈框架.

Well, in fact, you don't need stack frames.

当您保存寄存器并将本地变量存储在堆栈中时,堆栈帧很方便-使编写和调试更加容易:您只需将ebp设置为堆栈中的固定点,然后使用ebp寻址所有堆栈数据.并且最后恢复esp更容易.

Stack frames are convenient when you save registers and store local variables in stack - to make writing and debugging easier: you just set ebp to a fixed point in stack and address all stack data using ebp. And it's easier to restores esp at the end.

此外,调试器通常希望存在堆栈帧,否则您可能会得到不准确的堆栈调用.

Also, debuggers often expect stack frames to be present, otherwise you can get inaccurate stack call for example.

所以,对1的答案是肯定的,对2和3的答案就是上面的.

So, the answer to 1 is yes, the answer to 2 and 3 is above.

这篇关于x86 Assembly:为什么需要堆叠框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆