为什么在此函数代码的序言中从堆栈指针中减去0x20? [英] Why is 0x20 subtracted from the stack pointer in the prologue of this function's code?

查看:181
本文介绍了为什么在此函数代码的序言中从堆栈指针中减去0x20?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void main(){
    int c;
    c = function(1, 2);
}

int function(int a, int b){
      char buf[10];
      a = a+b;
      return a;
}

组装代码:

          main:
08048394:   push %ebp
08048395:   mov %esp,%ebp
08048397:   and $0xfffffff0,%esp
**0804839a:   sub $0x20,%esp   <-----------------------???????**
0804839d:   movl $0x2,0x4(%esp)
080483a5:   movl $0x1,(%esp)
080483ac:   call 0x80483b7 <function>
080483b1:   mov %eax,0x1c(%esp)
080483b5:   leave 
080483b6:   ret 
          function:
080483b7:   push %ebp
080483b8:   mov %esp,%ebp
080483ba:   sub $0x10,%esp
080483bd:   mov 0xc(%ebp),%eax
080483c0:   add %eax,0x8(%ebp)
080483c3:   mov 0x8(%ebp),%eax
080483c6:   leave 
080483c7:   ret 

我知道按16字节对齐,
但是,在main()中是int c(=4 byte) + 1(4byte) + 2(4byte) in function(1 ,2)调用语句.

i know aligned by 16-byte,
but, in main(), int c(=4 byte) + 1(4byte) + 2(4byte) in function(1 ,2) call statement.

所以这是12byte.但是通过内存对齐,我认为是16byte.

so sum of this is 12byte. but by memory aligned, i espect 16byte.

(sub 0x10, %esp)

为什么要使用0x20, %esp?

推荐答案

请考虑以下功能:

void main(){
    int c, d, e, f;
    c = function(1, 2, 3, 4);
    d =1;
    e = 2;
    f = 3;
}

仍然会分配0x20的空间.

Still this will allocate 0x20 space.

但是,如果再添​​加1个局部变量或函数参数,它将立即分配0x30空间.

But if you add even 1 more local variable or function parameter, it will immediately allocate 0x30 space.

现在考虑一下在主函数中什么都没有的时候,只有一个语句:

Now consider when there is nothing in the main function, but only one statement:

int c = 1;

然后在这种情况下,它将分配0x10的空间.

Then in this case, it will allocate 0x10 space.

您看到这里的图案了吗?系统首先为局部变量分配空间.然后它将为功能参数分配空间.分配的空间与0x10对齐.

Do you see the pattern here? The system first allocates space for local variable. Then it will allocate space for function parameters. Space allocated is aligned to 0x10.

这就是为什么您看到0x20的原因. 0x10用于局部变量,另外0x10用于函数参数.

This is why you see 0x20. 0x10 is for local variables, and another 0x10 is for function parameters.

这篇关于为什么在此函数代码的序言中从堆栈指针中减去0x20?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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