当前位置:网站首页>函数的调用过程
函数的调用过程
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
边栏推荐
猜你喜欢

SQL -- data filtering and grouping

基于pygame库编写的五子棋游戏

Dynamic creation and release, assignment and replication of objects

Basemap库绘制地图

类和对象的初始化(构造函数与析构函数)

A solution to replace not in in SQL

Graduation project, viewing screenshots of epidemic psychological counseling system

【无标题】
![[leetcode 59] spiral matrix II](/img/6e/58e600272797563129d2b325a054b5.png)
[leetcode 59] spiral matrix II

Explanation of login page
随机推荐
Cf1427c the hard work of paparazzi
Storing inherited knowledge in cloud computing
GNU EFI header file
词频统计
SQL -- data definition
基于QQwebAPI 查询昵称和头像的爬虫
Detailed arrangement of knowledge points of University probability theory and mathematical statistics
selenium+PhantomJS破解滑动验证2
【OpenCV】使用 FileStorage 读写 Eigen 向量
Animation - Introduction to keyframes
MySQL groups are sorted by a field, and the first value is taken
爬虫效率提升方法
爬虫之requests基本用法
1007 go running (hdu6808) in the fourth game of 2020 Hangzhou Electric Multi school competition
Introduction to virtualization features
线程和进程的关系和区别是什么
数组旋转
渔网道路密度计算
Record the installation and configuration of gestermer on TX2, and then use GST RTSP server
Swagger2 generates API documents