函数调用过程中堆栈运行情况

    最近看到很多资料,都是关于堆栈的,忍不住想亲自对函数调用过程中的堆栈运行情况探个究竟,下面待我细细剖来!
    操作系统:windows xp
    编译器:vc 6.0 sp2
    程序编译版本:debug,注意:release版本生成的程序与debug版本相差甚大,但大致原理相同,因此release版本的堆栈运行情况不在本文关注范围内。

    首先看看《Computer Systems》一书中关于函数调用的描述:

    IA32 programs make use of the program stack to support procedure calls. The stack is used to pass procedure arguments, to store return information, to save registers for later restoration, and for local storage. The portion of the stack allocated for a single procedure call is called a stack frame. Figure 3.16 diagrams the general structure of a stack frame. The topmost stack frame is delimited by two pointers, with register %ebp serving as the frame pointer, and register %esp serving as the stack pointer. The stack pointer can move while the procedure is executing, and hence most information is accessed relative to the frame pointer.

    Suppose procedure P (the caller) calls procedure Q (the callee). The arguments to Q are contained within the stack frame for P. In addition, when P calls Q, the return address within P where the program should resume execution when it returns from Q is pushed on the stack, forming the end of P’s stack frame. The stack frame for Q starts with the saved value of the frame pointer (i.e., %ebp). followed by copies of any other saved register values.
    Procedure Q also uses the stack for any local variables that cannot be stored in registers. This can occur for the following reasons:
    1. There are not enough registers to hold all of the local data.
    2. Some of the local variables are arrays or structures and hence must be accessed by array or structure references.
    3. The address operator ‘&’ is applied to one of the local variables, and hence we must be able to generate an address for it.
    Finally, Q will use the stack frame for storing arguments to any procedures it calls.
    As described earlier, the stack grows toward lower addresses and the stack pointer %esp points to the top element of the stack. Data can be stored on and retrieved from the stack using the pushl and popl instructions. Space for data with no specified initial value can be allocated on the stack by simply decrementing the stack pointer by an appropriate amount. Similarly, space can be deallocated by incrementing the stack pointer.


    下面进入测试过程,我的测试代码如下:

    #include 
    void test(int i)
    {
          char output[8];
          strcpy(output, "abcdefg");
    }

    int main()
    {
          test(10); 
          return 0;
    }