函数活动记录

来源:互联网 发布:天谕业刹女捏脸数据 编辑:程序博客网 时间:2024/05/01 01:42
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.

 活动记录:过程的一次执行所需要的信息用一块连续的存储区来管理,这块存储区叫做活动记录。

活动记录的各个域的用途:

1、临时数据域:如计算表达式出现的中间结果,若寄存器不足以存放所有这些中间结果时,可以把它们存放在临时数据域中

2、局部数据域:保存局部于过程执行的数据

3、机器状态域:保存刚好在过程调用前的机器状态信息,包括机器计数器的值和控制从这个过程返回必须恢复的机器寄存器的值

4、控制链:用来指向调用者的活动记录,控制链也称动态链

5、参数域:用来存放调用过程提供的实在参数

6、返回值域:用来存放被调用过程返回给调用过程的值

活动记录其实比没有包含过程一次执行所需的全部信息,比方说非局部数据就不在活动记录中,另外,过程运行时生成的动态变量也不在活动记录中,对它们通常采用堆式分配