Programming

stack & function

An important control transfer mechanism is function invocation. It works thanks to a data structure known as the call stack.

栈从下往上堆积,是函数执行作用域生成的过程,从上往下丢弃,则是函数执行完成的过程。这两个过程组成了 函数调用 的过程。

the caller pushes the return address which points to the next instruction after the callee returns. 被调用者(callee )返回时(当前函数执行完成),调用者(caller )将 指向下一个指令(上一层函数作用域) 的 返回地址 (return address)推出 这段感觉有歧义

依赖堆栈的执行权转换发生是隐式的(对于高级编程语言来说),依据这个过程进行的流程编排也叫 direct style,常见的函数遍码,if/else,loop (control flow)然后 return。

也就是当一个函数执行完成时,可以理解为有一个 return address 的指针,指向外层,也就是返回外层,以继续执行外层函数。
image.png1_XYU1PXi_pWxjX18WV2MsaQ.gif

汇编

caller 和 callee 都有自己的 帧
从 caller 进入 callee 的 stack frame 时(压栈),参数(paramater),返回地址(return address) push,然后会保存 ebp(此时是 caller 的帧指针),之后 ebp 被置为 callee 的帧指针 (粗略理解)
https://miro.medium.com/max/1400/1*XYU1PXi_pWxjX18WV2MsaQ.gif
frame point:ebp
stack point:esp

ESP就是一直指向栈顶的指针,而EBP只是存取某时刻的栈顶指针,以方便对栈的操作,如获取函数参数、局部变量等。