当前位置:网站首页>函数的调用过程
函数的调用过程
2022-04-23 05:48:00 【*陌上花开】
一、概念
寄存器:CPU中的变量
cpu 中有很多寄存器,寄存器进行存取
函数调用的过程会生成一个栈,
esp:栈顶寄存器
ebp:栈底寄存器
二、函数调用过程
1.参数入栈
参数入栈:(c语言)
四字节入栈:
#include<stdio.h>
int fun(int a, int b)
{
int c = 0;
return c = a + b;
}
int main()
{
int a = 10;
int b = 5;
int c = fun(a,b);
printf("%d", c);
return 0;
}
fun(a,b)

4字节参数入栈(顺序:从右向左)
fun(a,b);先b再a
方式:使用寄存器push 带入
八字节入栈:
#include<stdio.h>
struct Node
{
int _1;
int _2;
};
int fun(struct Node a, struct Node b)
{
int c = 20;
return c;
}
int main()
{
struct Node a = { 10,20 };
struct Node b = { 30,40 };
fun(a, b);
return 0;
}

8字节参数入栈:
顺序: 从右往左
方式 使用寄存器push 带入
12字节参数入栈

12字节(大于8字节)参数入栈:
顺序:从右往左
方式:先在栈顶开辟足够该参数的空间,之后将数据复制进去
c++中参数入栈
c++中只要是自定义类型,无论多大字节,都采用先开辟空间,之后赋值的方式
2.函数栈帧开辟
将调用方函数下一行指令地址入栈
将调用方函数的栈底寄存器入栈
让ebp = esp
让esp = esp -***
将其他使用的寄存器入栈,
将新开辟的栈帧空间中全部写为cccc cccc

3.函数返回值
c语言中
4字节返回值
方式:将返回值放入寄存器带回

8字节返回值
方式:将返回值放在两个寄存器,带回

12字节(大于8字节)返回值
方式:
在函数参数入栈之后,入栈一个调用方栈帧上的地址(靠近栈顶位置)
在返回值返回的时候,将返回数据写入到之前入栈的调用方地址上
返回之后,将从该地址上将数据取出
c++中
字节返回值方式:
自定义类型都按照入栈调用方地址的方式
4.函数栈退出
进行当前函数栈帧的校验
将线程保护的寄存器出栈
让esp=ebp
ebp= pop
将下一行指令的地址还原
清除参数
注:此文函数调用过程是依赖于c语言默认的调用约定 _cdecl
还有其他的调用方法,如:_stdcall , _fastcall
版权声明
本文为[*陌上花开]所创,转载请带上原文链接,感谢
https://blog.csdn.net/swint_er/article/details/121546748
边栏推荐
- Easy to use data set and open source network comparison website
- C语言输入和输出(printf和scanf函数、putchar和getchar函数)
- [leetcode 59] spiral matrix II
- [leetcode 401] binary Watch
- The onnx model of yolov5 removes the transfer layer
- Conversion between JS object and string
- 【UDS统一诊断服务】三、应用层协议(1)
- 【UDS统一诊断服务】一、诊断概述(4)— 基本概念和术语
- Cf515b drazil and his happy friends
- Animation - Introduction to keyframes
猜你喜欢
随机推荐
GDB debugger installation and use
Explanation of the second I interval of 2020 Niuke summer multi school training camp
非参数化相机畸变模型简介
scikit-learn sklearn 0.18 官方文档中文版
Type conversion in C #
P1586 solution to tetragonal theorem
H. Are You Safe? Convex hull naked problem
NVIDIA Jetson: GStreamer 和 openMAX(gst-omx) 插件
爬取小米有品app商品数据
SVN简单操作命令
Robocode教程5——Enemy类
Rust 中的指针:Box、Rc、Cell、RefCell
Rust:如何实现一个线程池?
代理服务器
C语言实现2048小游戏方向合并逻辑
安装pyshp库
Detailed arrangement of knowledge points of University probability theory and mathematical statistics
[untitled] database - limit the number of returned rows
Graduation project, curriculum link, student achievement evaluation system
【UDS统一诊断服务】一、诊断概述(3)— ISO 15765体系结构
![[leetcode 19] delete the penultimate node of the linked list](/img/ba/3c73fba8c4b4e3de7e506670144890.png)








![[leetcode 401] binary Watch](/img/a5/538caf3a1a6143a47d79d947717554.png)