一个函数调用期间堆栈的内容 [英] Stack contents during a function call

查看:137
本文介绍了一个函数调用期间堆栈的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这将是一个函数调用期间堆栈上present。

I'm trying to understand what will be present on the stack during a function call.

据我所知,争论到被叫方(如果有的话),调用者和基地址的返回地址会在堆栈上调用另一个函数之前推。

As far as I have learnt, arguments to the callee (if any), the return address of the caller and the base address would be pushed on the stack before calling another function.

所以,我写了一个简单的C程序

So, I wrote a simple C program

#include <stdio.h>

void
foo()
{
}

int
main()
{
    foo();
    return 0;
}

和相应的存款保险计划组装机code是

and the corresponding dis-assembled machine code is

08048334 <foo>:
 8048334:   55                      push   %ebp
 8048335:   89 e5                   mov    %esp,%ebp
 8048337:   c9                      leave  
 8048338:   c3                      ret    

08048339 <main>:
 8048339:   55                      push   %ebp
 804833a:   89 e5                   mov    %esp,%ebp
 804833c:   83 ec 08                sub    $0x8,%esp
 804833f:   83 e4 f0                and    $0xfffffff0,%esp
 8048342:   b8 00 00 00 00          mov    $0x0,%eax
 8048347:   83 c0 0f                add    $0xf,%eax
 804834a:   83 c0 0f                add    $0xf,%eax
 804834d:   c1 e8 04                shr    $0x4,%eax
 8048350:   c1 e0 04                shl    $0x4,%eax
 8048353:   29 c4                   sub    %eax,%esp
 8048355:   e8 da ff ff ff          call   8048334 <foo>
 804835a:   b8 00 00 00 00          mov    $0x0,%eax
 804835f:   c9                      leave  
 8048360:   c3                      ret    
 8048361:   90                      nop    
 8048362:   90                      nop    
 8048363:   90                      nop    

虽然富()的code是有道理的,我不明白主的()code。这是为什么有这么多的业务?我只是希望里面以下操作的main()

While foo()'s code makes sense, I couldn't understand main's() code. Why is that there are so many operations? I was only expecting the following operations inside main()

    1. Push the frame pointer
    2. Call foo (which will inturn save the return address)

有人可以解释我的main()的code?谢谢!

Can someone please explain me main()'s code? Thanks!

推荐答案

前三行的主要

8048339:   55                      push   %ebp
804833a:   89 e5                   mov    %esp,%ebp
804833c:   83 ec 08                sub    $0x8,%esp

称为功能序幕。这组指令推压基指针入堆栈,然后分配基指针从而当前堆栈创建新的堆栈帧的值。然后堆栈指针下降预留空间的函数的局部变量(你没有,但由于调用约定仍然完成做)。接下来的指令

are called the function prologue. This set of instructions pushes the base pointer onto the stack, then assigns the base pointer the value of the current stack thus creating a new stack frame. The stack pointer is then decreased to reserve space for the function's local variables (which you don't have but is still done due to the calling convention). The next instruction

804833f:   83 e4 f0                and    $0xfffffff0,%esp

对齐堆栈到下一个较低的16字节的边界。下面的说明

aligns the stack to the next lower 16 byte boundary. The following instructions

8048342:   b8 00 00 00 00          mov    $0x0,%eax
8048347:   83 c0 0f                add    $0xf,%eax
804834a:   83 c0 0f                add    $0xf,%eax
804834d:   c1 e8 04                shr    $0x4,%eax
8048350:   c1 e0 04                shl    $0x4,%eax
8048353:   29 c4                   sub    %eax,%esp

已经出现在SE几次已经(<一个href=\"http://stackoverflow.com/questions/19552816/gcc-on-windows-generating-garbage-windows-vs-linux\">here,由保罗·R,和<一指出, href=\"http://stackoverflow.com/questions/3155257/gdb-what-is-the-mysterious-assembly-$c$c\">here,and 在这里再次)。这个程序似乎是在栈上预留更多的空间,但是这是否在一种奇怪的方式无效。这部分可能取决于gcc版本和操作系统似乎并没有必要。

have appeared on SE a few times already (here, as pointed out by Paul R, and here,and again here). This routine seems to reserve additional space on the stack but does this in a strangely ineffective way. This section may depend on gcc version and OS and does not seem to be necessary.

的其他指令调用foo和退出程序。

The rest of the instructions calls foo and exits the program.

这篇关于一个函数调用期间堆栈的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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