assembly - 推送局部变量

标签 assembly

阿洛

在我从 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

最佳答案

更多的是语义问题而不是技术正确性:pushpop用于保存和恢复寄存器或值;但提供局部变量 对于函数不符合 push 的常规目的/pop .所以,这里的堆栈是手动管理的(除了 push ebppop ebp ,因为在这里我们实际上想要保存和恢复 ebp 真正意义上的 push/pop )。

关于assembly - 推送局部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4308021/

相关文章:

Linux x64 汇编

c - 将 MASM 对象与 C 对象链接会给出错误的结果

assembly - PowerPC 中应该对有效地址 'wrap' 求和吗?如果是这样,怎么办?

assembly - NASM 程序集 16 位 "invalid combination of opcode and operands"

assembly - Linux 性能分析器给出的时间测量的含义

assembly - objdump 输出 - 列代表什么?

c - shellcode:将参数传递给 x86_64 程序集中的 execve

linux - 我将整数添加到 resb 错误吗?以及如何纠正?

assembly - x86 RCL/RCR 指令的实际应用是什么?

c - 爱特梅尔微 Controller : difference between UBRR0H and UBRRnH