功能序幕和尾声用C [英] Function Prologue and Epilogue in C

查看:127
本文介绍了功能序幕和尾声用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在嵌套函数调用的数据转至Stack.The栈自身实现存储和从堆栈中检索数据的函数被调用或这些方法returns.The的名字是最知名的一个一步一步的方法作为序幕和尾声。

I know data in nested function calls go to the Stack.The stack itself implements a step-by-step method for storing and retrieving data from the stack as the functions get called or returns.The name of these methods is most known as Prologue and Epilogue.

我没有成功试图寻找关于这个主题的材料。你们怎么知道的函数开头和结尾是如何工作的一般用C的任何资源(网站,视频,文章)?或者,如果你能解释一下会更好。

I tried with no success to search material on this topic. Do you guys know any resource ( site,video, article ) about how function prologue and epilogue works generally in C ? Or if you can explain would be even better.

P.S:我只是想一些普遍的看法,不是太详细

P.S : I just want some general view, not too detailed.

推荐答案

有很多资源在那里的解释是:

There are lots of resources out there that explain this:

,仅举几例。

基本上,你有所描述,堆叠在一个程序的执行具有多种用途:

Basically, as you somewhat described, "the stack" serves several purposes in the execution of a program:


  1. 在哪里返回饲养轨道,调用函数时

  2. 在函数调用的上下文局部变量的存储

  3. 从调用函数的被调用传递参数。

的prolouge是在函数开头会发生什么。其职责是建立调用函数的的堆栈帧的。结语是完全相反的:它是在一个函数最后会发生什么,其目的是恢复通话(父)函数的栈帧

The prolouge is what happens at the beginning of a function. Its responsibility is to set up the stack frame of the called function. The epilog is the exact opposite: it is what happens last in a function, and its purpose is to restore the stack frame of the calling (parent) function.

在IA-32(x86)的CDECL时, EBP 寄存器所使用的语言来跟踪该函数的栈帧。在尤其寄存器所使用的处理器为指向最近除了(前值)堆栈。

In IA-32 (x86) cdecl, the ebp register is used by the language to keep track of the function's stack frame. The esp register is used by the processor to point to the most recent addition (the top value) on the stack.

呼叫指令做了两件事:首先,它将返回地址压入堆栈,然后跳转到被调用的函数。在呼叫后,立即尤其指向堆栈。

The call instruction does two things: First it pushes the return address onto the stack, then it jumps to the function being called. Immediately after the call, esp points to the return address on the stack.

然后序幕执行:

push  ebp         ; Save the stack-frame base pointer (of the calling function).
mov   ebp, esp    ; Set the stack-frame base pointer to be the current
                  ; location on the stack.
sub   esp, N      ; Grow the stack by N bytes to reserve space for local variables

在这一点上,我们有:

At this point, we have:

...
ebp + 4:    Return address
ebp + 0:    Calling function's old ebp value
ebp - 4:    (local variables)
...

结语:

mov   esp, ebp    ; Put the stack pointer back where it was when this function
                  ; was called.
pop   ebp         ; Restore the calling function's stack frame.
ret               ; Return to the calling function.

这篇关于功能序幕和尾声用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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