X86为局部变量分配堆栈空间

时间:2017-10-27 02:20:42

标签: c++ assembly x86 stack

我有一个用C ++编写的基本功能:

int __cedcl add(int a, int b){
       return a + b;
}

在练习中,我尽力在IDA中扭转它。这是我的结果:

push    ebp             ; Store EBP Register
mov     ebp, esp        ; Adjust EBP to be Stack Pointer (Becomes reference to paramaters and such)
sub     esp, 0C0h       ; Allocate C0h Space on Stack
push    ebx             ; Save EBX register
push    esi             ; Save ESI Register
push    edi             ; Save EDI Register
lea     edi, [ebp+var_C0] ; Sets Location to Start Copying at Beginning of Allocated Space
mov     ecx, 30h        ; Stores #Repitions
mov     eax, 0CCCCCCCCh ; Value to store
rep stosd               ; Fill 30h space with CCCCCCCC
mov     eax, [ebp+arg_0] ; Store Argument 1 in EAX Return Register
add     eax, [ebp+arg_4] ; Add argument 2 to EAX Return Register
pop     edi             ; Restore EDI Register
pop     esi             ; Restore ESI Register
pop     ebx             ; Restore EBX Register
mov     esp, ebp        ; Restore Stack Pointer
pop     ebp             ; Restore Base Pointer
retn                    ; Return

然而,我感到困惑的是,当没有使用局部变量时,为什么它在堆栈上保留0x30空间,因为它只是利用EAX寄存器,因为它可以使用返回寄存器执行必要的操作。另外,为什么它默认存储函数中未使用的寄存器的值。即。 ebx,esi和edx注册?

如果有人能够澄清这些问题或者注意我在发现它们时发现的任何错误,我会感激不尽。谢谢!

0 个答案:

没有答案