阿洛
在我从 http://en.wikibooks.org/wiki/X86_Disassembly/Functions_and_Stack_Frames 中阅读了有关函数和堆栈的信息之后我有一个关于局部变量的问题。
文章截图:
push ebp ; save the value of ebp
mov ebp, esp ; ebp now points to the top of the stack
sub esp, 12 ; space allocated on the stack for the local variables
这意味着可以通过引用 ebp 来访问局部变量。考虑以下 C 代码片段和相应的汇编代码:
a = 10;
b = 5;
c = 2;
mov [ebp - 4], 10 ; location of variable a
mov [ebp - 8], 5 ; location of b
mov [ebp - 12], 2 ; location of c
请记住,推送基本上是这样做的:
sub esp, 4 ; "allocate" space for the new stack item
mov [esp], X ; put new stack item value X in
为什么局部变量不会像这样压入堆栈:
push 10
push 5
push 2
代替
sub esp, 12
mov [ebp - 4], 10 ; location of variable a
mov [ebp - 8], 5 ; location of b
mov [ebp - 12], 2 ; location of c
最佳答案
更多的是语义问题而不是技术正确性:push
和 pop
用于保存和恢复寄存器或值;但提供局部变量 对于函数不符合 push
的常规目的/pop
.所以,这里的堆栈是手动管理的(除了 push ebp
和 pop ebp
,因为在这里我们实际上想要保存和恢复 ebp
真正意义上的 push
/pop
)。
关于assembly - 推送局部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4308021/