对应main函数的汇编代码
此时栈及寄存器如下:
对应func函数的汇编代码
结论: 传递基本数据类型,前4个放入寄存器r0-r3中,多于的放入栈上
main函数对应的汇编
结论:传递数组 实际上传递的是数组首地址指针,取数组内容通过首地址偏移
main函数对应汇编
结论:结构体的传递和直接传递参数一样 --> func1(int a, int b, int c, int d, int e)
main对应汇编代码
结论:传递枚举,直接传递的值 类似于 func2(int a)
main函数对应汇编
结论:传递对象指针 传递过来的是地址
main函数汇编,和传递对象地址是一样的
main函数的汇编代码
func5对应的部分汇编代码
结论:传递对象 传递过来的是成员变量值,在被调用方再分配空间。 c++对象内存模型中只有成员变量占对象内存空间
int
func(
int
a,
int
b,
int
c,
int
d,
int
e) {
int
v1
=
1
;
int
r
=
a
+
b
+
c
+
d
+
e
+
v1;
return
r;
}
int
main() {
int
i
=
1
, j
=
2
;
int
r
=
func(i, j,
3
,
4
,
5
);
printf(
"%d \n"
, r);
}
int
func(
int
a,
int
b,
int
c,
int
d,
int
e) {
int
v1
=
1
;
int
r
=
a
+
b
+
c
+
d
+
e
+
v1;
return
r;
}
int
main() {
int
i
=
1
, j
=
2
;
int
r
=
func(i, j,
3
,
4
,
5
);
printf(
"%d \n"
, r);
}
;
int
__cdecl main(
int
argc, const char
*
*
argv, const char
*
*
envp)
PUSH {R11,LR} ;保存现场
ADD R11, SP,
SUB SP, SP,
MOV R3,
STR
R3, [R11,
MOV R3,
STR
R3, [R11,
MOV R3,
STR
R3, [SP,
LDR R0, [R11,
LDR R1, [R11,
MOV R2,
MOV R3,
BL _Z4funciiiii ; func(
int
,
int
,
int
,
int
,
int
)
;
int
__cdecl main(
int
argc, const char
*
*
argv, const char
*
*
envp)
PUSH {R11,LR} ;保存现场
ADD R11, SP,
SUB SP, SP,
MOV R3,
STR
R3, [R11,
MOV R3,
STR
R3, [R11,
MOV R3,
STR
R3, [SP,
LDR R0, [R11,
LDR R1, [R11,
MOV R2,
MOV R3,
BL _Z4funciiiii ; func(
int
,
int
,
int
,
int
,
int
)
; _DWORD __fastcall func(
int
,
int
,
int
,
int
,
int
)
PUSH {R11}
ADD R11, SP,
SUB SP, SP,
STR
R0, [R11,
STR
R1, [R11,
STR
R2, [R11,
STR
R3, [R11,
MOV R3,
STR
R3, [R11,
LDR R2, [R11,
LDR R3, [R11,
ADD R2, R2, R3 ;
1
+
2
LDR R3, [R11,
ADD R2, R2, R3 ;
1
+
2
+
3
LDR R3, [R11,
ADD R2, R2, R3 ;
1
+
2
+
3
+
4
LDR R3, [R11,
ADD R2, R2, R3
; _DWORD __fastcall func(
int
,
int
,
int
,
int
,
int
)
PUSH {R11}
ADD R11, SP,
SUB SP, SP,
STR
R0, [R11,
STR
R1, [R11,
STR
R2, [R11,
STR
R3, [R11,
MOV R3,
STR
R3, [R11,
LDR R2, [R11,
LDR R3, [R11,
ADD R2, R2, R3 ;
1
+
2
LDR R3, [R11,
ADD R2, R2, R3 ;
1
+
2
+
3
LDR R3, [R11,
ADD R2, R2, R3 ;
1
+
2
+
3
+
4
LDR R3, [R11,
ADD R2, R2, R3
void func6(
int
a[],
int
len
) {
for
(
int
i
=
0
; i <
len
; i
+
+
) {
printf(
"index: %d , num: %d\n"
, i, a[i]);
}
}
int
main() {
int
a[]
=
{
0
,
1
,
2
};
int
n
=
sizeof(a)
/
sizeof(
int
);
func6(a, n);
}
void func6(
int
a[],
int
len
) {
for
(
int
i
=
0
; i <
len
; i
+
+
) {
printf(
"index: %d , num: %d\n"
, i, a[i]);
}
}
int
main() {
int
a[]
=
{
0
,
1
,
2
};
int
n
=
sizeof(a)
/
sizeof(
int
);
func6(a, n);
}
PUSH {R11,LR}
ADD R11, SP,
SUB SP, SP,
LDR R2,
=
(dword_20BC
-
0x8C8
)
ADD R2, PC, R2 ; dword_20BC ; r2存储数组首地址
SUB R3, R11,
LDM R2, {R0
-
R2} ; 把数组[
0
,
1
,
2
]内容存到r0,r1,r2
STM R3, {R0
-
R2} ; 再存到r3所指向的栈上
MOV R3,
STR
R3, [R11,
SUB R3, R11,
MOV R0, R3 ;
int
*
LDR R1, [R11,
BL _Z5func6Pii ; func6(
int
*
,
int
)
MOV R3,
MOV R0, R3
SUB SP, R11,
POP {R11,PC}
PUSH {R11,LR}
ADD R11, SP,
SUB SP, SP,
LDR R2,
=
(dword_20BC
-
0x8C8
)
ADD R2, PC, R2 ; dword_20BC ; r2存储数组首地址
SUB R3, R11,
LDM R2, {R0
-
R2} ; 把数组[
0
,
1
,
2
]内容存到r0,r1,r2
STM R3, {R0
-
R2} ; 再存到r3所指向的栈上
MOV R3,
STR
R3, [R11,
SUB R3, R11,
MOV R0, R3 ;
int
*
LDR R1, [R11,
BL _Z5func6Pii ; func6(
int
*
,
int
)
MOV R3,
MOV R0, R3
SUB SP, R11,
POP {R11,PC}
struct st_a {
int
a;
int
b;
int
c;
int
d;
int
e;
};
void func1(st_a stA) {
int
a
=
stA.a;
int
b
=
stA.b;
int
c
=
stA.c;
int
d
=
stA.d;
int
e
=
stA.e;
printf(
"%d, %d \n"
, a, b, c, d, e);
}
int
main() {
st_a stA;
stA.a
=
123
;
stA.b
=
234
;
stA.c
=
345
;
stA.d
=
456
;
stA.e
=
567
;
func1(stA);
}
struct st_a {
int
a;
int
b;
int
c;
int
d;
int
e;
};
void func1(st_a stA) {
int
a
=
stA.a;
int
b
=
stA.b;
int
c
=
stA.c;
int
d
=
stA.d;
int
e
=
stA.e;
printf(
"%d, %d \n"
, a, b, c, d, e);
}
int
main() {
st_a stA;
stA.a
=
123
;
stA.b
=
234
;
stA.c
=
345
;
stA.d
=
456
;
stA.e
=
567
;
func1(stA);
}
PUSH {R11,LR}
ADD R11, SP,
SUB SP, SP,
MOV R3,
STR
R3, [R11,
MOV R3,
STR
R3, [R11,
LDR R3,
=
345
STR
R3, [R11,
MOV R3,
STR
R3, [R11,
LDR R3,
=
567
STR
R3, [R11,
LDR R3, [R11,
STR
R3, [SP,
SUB R3, R11,
LDM R3, {R0
-
R3} ; 将
123
234
345
456
放到寄存器r0,r1,r2,r3
BL _Z5func14st_a ; func1(st_a)
MOV R3,
MOV R0, R3
SUB SP, R11,
POP {R11,PC}
PUSH {R11,LR}
ADD R11, SP,
SUB SP, SP,
MOV R3,
STR
R3, [R11,
MOV R3,
STR
R3, [R11,
LDR R3,
=
345
STR
R3, [R11,
MOV R3,
STR
R3, [R11,
LDR R3,
=
567
STR
R3, [R11,
LDR R3, [R11,
STR
R3, [SP,
SUB R3, R11,
LDM R3, {R0
-
R3} ; 将
123
234
345
456
放到寄存器r0,r1,r2,r3
BL _Z5func14st_a ; func1(st_a)
MOV R3,
MOV R0, R3
SUB SP, R11,
POP {R11,PC}
enum en_a {
p1
=
103
,
p2
=
201
};
void func2(en_a enA) {
printf(
"%d \n"
, enA);
}
int
main() {
func2(p1);
}
enum en_a {
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!
最后于 2020-10-14 19:09
被lanoche编辑
,原因: 修改图片错误