Cpp_函数调用过程

函数调用过程

  1. 参数入栈
  2. 函数栈帧开辟
  3. 返回值返回
  4. 函数栈退出

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//4字节入栈
#include<stdio.h>
void fun(int a, int b)
{
int c;
c = a + b;
return c;
}
int main()
{
int a = 10;
int b = 20;
fun(a,b);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//8字节入栈
struct Node
{
int _1;
int _2;
};
int fun(struct Node a, struct Node b)
{
int c = 30;
return c;
}
int main()
{
struct Node a = {10, 20};
struct Node b = {30, 40};
fun(a, b);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//12字节入栈
struct Node
{
int _1;
int _2;
int _3;
};
int fun(struct Node a, struct Node b)
{
int c = 30;
return c;
}
int main()
{
struct Node a = {10, 20, 30};
struct Node b = {40, 50, 60};
fun(a, b);
return 0;
}

image-20211001184239521

寄存器

可以理解为CPU中的变量

如何去标志一个栈?——栈底和栈顶指针。

esp:栈顶寄存器。

ebp:栈底寄存器。

函数调用过程

  1. 参数入栈(C语言)

    1. 4字节参数(dword)入栈

      1. 顺序:从右向左
      2. 方式:使用寄存器push带入
    2. 8字节参数入栈

      1. 顺序:从右向左
      2. 方式:使用寄存器分两次push带入
    3. (>8字节)12字节参数入栈

      image-20211001184251000

      1. 顺序:从右向左
      2. 方式:变了,栈顶先上移,即开辟足够的空间,之后利用寄存器将数据分批次复制进去。
    4. C++中,只要是自定义类型,无论多大字节,都采用先esp上移开辟空间,之后分次赋值的方式(即方式3),即>8字节的情况。

  2. 函数栈帧开辟
    其中,1、2步是为了保存现场。3、4步是开辟新的栈帧。

    1. 函数入参的动作完成后,汇编代码执行call(_fun)语句,esp向上移4位,将调用方函数原来的下一行指令地址存入栈。因为main中调用完成外部函数后,还要回到以前的位置。
      image-20211018205543731
      image-20211018205624299
    2. 调用方函数的栈底寄存器(ebp)入栈
      image-20211018205322518
    3. ebp=esp(让ebp上移到esp的位置)
    4. esp=esp-0**h(上移若干空间,开辟此函数的空间)
    5. 将某些寄存器(线程保护)入栈
    6. 将新开辟的栈帧空间中全部赋为0xcccccccc
  3. 函数返回值

    1. 4字节返回值:将返回值先存到eax再赋给接收变量
      image-20211018210146635
      image-20211018210420665

    2. 8字节返回值:将返回值分开放到两个寄存器,再赋给接收变量
      image-20211018210458734

      image-20211018210558730
      image-20211018210607525
      image-20211018210900593

    3. (>8字节)12字节返回值:

      1. 在函数参数入栈之后,入栈一个调用方(如main)栈帧上的地址(靠近栈顶位置)
      2. 在返回值返回的时候,将返回数据分次写入到之前入栈的调用方地址上。
      3. 返回之后,将从该地址(调用方(如main)栈帧上的地址)将数据分次取出(一次4字节)到接收变量中。

      image-20211001213921252

    4. C++中,自定义类型都按照入栈一个调用方地址的方式

  4. 函数栈退出

    1. 进行当前函数栈帧的校验
    2. 将线程保护的寄存器出栈
    3. esp=ebp(回缩栈帧)
    4. pop操作(即将pop的位置是esp指向的位置)。
      pop ebp,意为:ebp=popesp指向的是原来mainebp地址,赋给ebp,同时esp下移4字节。则ebp回指到原来的位置。形成现场恢复。
      image-20211018233157527
    5. 将下一行指令的地址还原(ret),实际上,也是一个pop,返回保留的地址值,esp下移4字节。
    6. esp再次下移若干,清除参数和之前入栈的调用方地址。下移之后,则esp和ebp回到了main函数调用外部函数之前的原始位置。函数调用结束。
      image-20211018233852672

说明

当前演示的函数调用规则是依赖于C语言默认的调用约定__cdecl,其他的还有__stdcall/__fastcall。三种差异并不大,只是负责的事情不同,清除参数是调用方执行的,stdcall是被调用方执行的。

1
2
3
4
5
struct Node __cdecl fun(int a, int b)
{
struct Node c = {10, 20, 30};
return c;
}

基础数据结构_循环单链表

声明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#ifndef CLIST_H
#define CLIST_H
typedef int ElemType;
typedef struct CNode
{
ElemType data;
struct CNode* next;//指针域
}CNode, * PCNode;
//可执行操作
//初始化
void Init_clist(struct CNode* plist);
//头插
bool Push_Front(PCNode plist, int val);
//尾插
bool Push_Back(PCNode plist, int val);
//按位置插
bool Insert_Pos(PCNode plist, int pos, int val);
//头删
bool Pop_Front(PCNode plist);
//尾删
bool Pop_Back(PCNode plist);
//按位置删
bool Del_Pos(PCNode plist, int pos);
//按值删除
bool Del_Val(PCNode plist, int val);
//查找
struct CNode* Search(PCNode plist, int val);
//找值为val的节点前驱
struct CNode* Get_Prev(PCNode plist, int val);
//找值为val的节点后继
struct CNode* Get_Next(PCNode plist, int val);
//获取其有效节点的个数
int Get_Length(PCNode plist);
//清空
void Clear(PCNode plist);
//销毁1
void Destroy1(PCNode plist);
//销毁2
void Destroy2(PCNode plist);
//判空
bool IsEmpty(PCNode plist);
//打印
void Show(PCNode plist);
#endif


实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"clist.h"

struct CNode* Buynode()
{
CNode* newnode = (CNode*)malloc(sizeof(CNode));
if (NULL == newnode)
{
printf("memory error");
exit(1);
}
return newnode;
}
//可执行操作
//初始化
void Init_clist(struct CNode* head)
{
assert(head != NULL);
head->next = head;
}
//查到ptr下一个
bool Insert_Next(CNode* ptr, int val)
{
assert(ptr);
CNode* pnewnode = Buynode();
pnewnode->next = ptr->next;
ptr -> next = pnewnode;
pnewnode->data = val;
return true;
}
//头插
bool Push_Front(PCNode head, int val)
{
assert(head);
return Insert_Next(head, val);
}
//尾插
bool Push_Back(PCNode head, int val)
{
assert(head);
CNode* p = head;
while (p->next != head)
{
p = p->next;
}
return Insert_Next(p, val);
}
//按位置插
bool Insert_Pos(PCNode head, int pos, int val)
{
assert(head);
if (pos < 0)return false;
CNode* pnode = head;
while (pnode->next != head && pos--)
{
pnode = pnode->next;
}
if (pos != 0)
{
return false;
}
return Insert_Next(pnode, val);
}
//删除下一个
bool Del_Next(CNode* ptr)
{
assert(ptr);
CNode* p = ptr->next;
ptr->next = p->next;
free(p);
return true;
}
//头删
bool Pop_Front(PCNode head)
{
assert(head);
if (head == head->next)//只有头结点不行
{
return false;
}
return Del_Next(head);
}
//尾删
bool Pop_Back(PCNode head)
{
assert(head);
CNode* p = head;
if (head == p->next)//只有头结点不行
{
return false;
}
while (p->next->next != head)
{
p = p->next;
}
return Del_Next(p);
}
//按位置删
bool Del_Pos(PCNode head, int pos)
{
assert(head);
if (pos < 0)return false;
CNode* p = head;
if (NULL == p->next)return false;
int count = pos - 1;
while (p->next != head && count--)
{
p = p->next;
}
if (count != -1)
{
return false;
}
return Del_Next(p);
}
//按值删除
bool Del_Val(PCNode head, int val)
{
assert(head);
CNode* p = head;
if (NULL == p->next)return false;
while (p->next != head && val != p->next->data)
{
p = p->next;
}
if (p->next == head)
{
return false;
}
return Del_Next(p);
}
//查找
struct CNode* Search(PCNode plist, int val);
//找值为val的节点前驱
struct CNode* Get_Prev(PCNode plist, int val);
//找值为val的节点后继
struct CNode* Get_Next(PCNode plist, int val);
//获取其有效节点的个数
int Get_Length(PCNode head)
{
assert(head);
CNode* p = head;
int count = 0;
while (p->next != head)
{
count++;
}
return count;
}
//判空
bool IsEmpty(PCNode head)
{
assert(head);
if (head->next == head)return true;
return false;
}
//清空
void Clear(PCNode head)
{
assert(head);
while (!IsEmpty(head))
{
Pop_Front(head);
}
}
//销毁1
void Destroy1(PCNode head)
{
assert(head);
Clear(head);
}
//销毁2
void Destroy2(PCNode plist);
//打印
void Show(PCNode head)
{
assert(head);
CNode* p = head->next;
while (p != head)
{
printf("%3d ", p->data);
p = p->next;
}
printf("\n");
}
int main()
{
CNode cn;
Init_clist(&cn);
for (int i = 0;i < 10;i++)
{
Insert_Pos(&cn, i,i+1);

}
Show(&cn);
Push_Front(&cn, 100);
Push_Back(&cn, 200);
Show(&cn);

Pop_Front(&cn);
Pop_Back(&cn);
Show(&cn);

Del_Val(&cn, 6);
Show(&cn);
Del_Pos(&cn, 6);
Show(&cn);
Destroy1(&cn);
Show(&cn);
}