zoukankan      html  css  js  c++  java
  • How a stack frame works 栈帧的要素与构建步骤

    http://en.citizendium.org/wiki/Stack_frame

    To use a stack frame, a thread keeps two pointers, often called the Stack Pointer (SP), and the Frame (FP) or Base Pointer (BP). SP always points to the "top" of the stack, and FP always points to the "top" of the frame. Additionally, the thread also maintains a program counter (PC) which points to the next instruction to be executed. Then, whenever a function call takes place, the following steps take place in roughly this order:

    1. The caller saves local variables and temporaries, by pushing them onto the stack.
    2. The caller pushes the callee's actual parameters onto the stack.
    3. The caller branches to the callee, pushing PC onto the stack (on most architectures, this is a single instruction called CALL). When on the stack, the saved PC is called the return address.
    4. The callee pushes the value of FP onto the stack.
    5. The callee copies SP to FP.
    6. The callee adjusts SP, creating storage locations for local variables and local temporaries on the stack.

    Steps 4--6 above are referred to as the function prologue, since they are the beginning of every function.

    Within the body of the callee function, formal parameters and local variables can all be accessed at an address relative to the frame pointer. Because of this, a function may recurse, and automatically create a different storage location for each of its local variables.

    Upon exit from the function, those steps are performed in reverse:

    1. The callee restores SP, and in doing so destroys the storage locations reserved for locals and temporaries.
    2. The callee restores FP, and in doing so returns to the previous frame.
    3. The callee branches back to caller by popping PC off of the stack (on most architectures, this is a single instruction called RETURN).
    4. The caller removes the actual parameters from the stack.
    5. The caller resotres local variables and temporaries, by popping them from the stack.

    Steps 1--3 are referred to as the function epilogue, since they are at the end of every function

     
    Contents of a stack frame from a SPARC system (Sun Solaris). Shown are two frames (a function that has called another function). Blue arrows are pointers. Parameters and locals can be addressed as FP ± k. NOTE: Intel/Windows stacks grow upward[4].
  • 相关阅读:
    Python学习笔记捌——面向对象高级编程
    Python学习笔记五,函数及其参数
    Python学习笔记四,dict和set
    Python学习笔记三,数组list和tuple
    Python学习笔记一,输入输出
    Linux 环境下自动化测试工具,Redhat dogtail的安装
    Testlink接口使用方法-python语言远程调用
    Python入门学习之input()与raw_input()的区别
    从客户端(&)中检测到有潜在危险的 Request.Path 值解决方案
    树莓派嵌入式开发第一课笔记
  • 原文地址:https://www.cnblogs.com/feng9exe/p/7885218.html
Copyright © 2011-2022 走看看