首先先上一段c++代码, 可以看出foo函数参数是引用类型,bar函数参数是值类型
typedef struct A{
int x;
int y;
} A;
void foo(A& a) {
r a.x++;
}
void bar(A a) {
a.x++;
}
int main() {
A a = {1, 2};
foo(a);
bar(a);
return 0;
}
在vscode中开启debug模式,然后在debug console页面查看三个函数的汇编代码
-exec disassemble foo
Dump of assembler code for function foo(A&):
0x00005590410681a9 <+0>: endbr64
0x00005590410681ad <+4>: push rbp
0x00005590410681ae <+5>: mov rbp,rsp
0x00005590410681b1 <+8>: mov QWORD PTR [rbp-0x8],rdi
0x00005590410681b5 <+12>: mov rax,QWORD PTR [rbp-0x8]
=> 0x00005590410681b9 <+16>: mov eax,DWORD PTR [rax]
0x00005590410681bb <+18>: lea edx,[rax+0x1]
0x00005590410681be <+21>: mov rax,QWORD PTR [rbp-0x8]
0x00005590410681c2 <+25>: mov DWORD PTR [rax],edx
0x00005590410681c4 <+27>: nop
0x00005590410681c5 <+28>: pop rbp
0x00005590410681c6 <+29>: ret
End of assembler dump.
-exec disassemble bar
Dump of assembler code for function bar(A):
0x00005590410681c7 <+0>: endbr64
0x00005590410681cb <+4>: push rbp
0x00005590410681cc <+5>: mov rbp,rsp
0x00005590410681cf <+8>: mov QWORD PTR [rbp-0x8],rdi
0x00005590410681d3 <+12>: mov eax,DWORD PTR [rbp-0x8]
0x00005590410681d6 <+15>: add eax,0x1
0x00005590410681d9 <+18>: mov DWORD PTR [rbp-0x8],eax
0x00005590410681dc <+21>: nop
0x00005590410681dd <+22>: pop rbp
0x00005590410681de <+23>: ret
End of assembler dump.
-exec disassemble main
Dump of assembler code for function main():
0x00005590410681df <+0>: endbr64
0x00005590410681e3 <+4>: push rbp
0x00005590410681e4 <+5>: mov rbp,rsp
0x00005590410681e7 <+8>: sub rsp,0x10
0x00005590410681eb <+12>: mov rax,QWORD PTR fs:0x28
0x00005590410681f4 <+21>: mov QWORD PTR [rbp-0x8],rax
0x00005590410681f8 <+25>: xor eax,eax
0x00005590410681fa <+27>: mov DWORD PTR [rbp-0x10],0x1 //rbp-0x10 ~ rbp-0xc 存放的是0x1
0x0000559041068201 <+34>: mov DWORD PTR [rbp-0xc],0x2 //rbp-0xc ~ rbp-0x8 这4个字节存放的是0x2
0x0000559041068208 <+41>: lea rax,[rbp-0x10] //rbp-10是一个内存地址, 把这个内存地址存放在rax寄存器中
0x000055904106820c <+45>: mov rdi,rax //rax寄存器内容移动到rdi中
0x000055904106820f <+48>: call 0x5590410681a9 <foo(A&)> //call 指令会把下一条指令地址0x0000559041068214 push到栈顶
0x0000559041068214 <+53>: mov eax,DWORD PTR [rbp-0x10]
0x0000559041068217 <+56>: mov esi,eax
0x0000559041068219 <+58>: lea rdi,[rip+0x2e20] # 0x55904106b040 <_ZSt4cout@@GLIBCXX_3.4>
0x0000559041068220 <+65>: call 0x5590410680b0 <_ZNSolsEi@plt>
0x0000559041068225 <+70>: mov rax,QWORD PTR [rbp-0x10] //这个是mov指令, 会把[rpb-0x10] 引用的内存里面的值传递给被调用函数
0x0000559041068229 <+74>: mov rdi,rax
0x000055904106822c <+77>: call 0x5590410681c7 <bar(A)>
0x0000559041068231 <+82>: mov eax,DWORD PTR [rbp-0x10]
0x0000559041068234 <+85>: mov esi,eax
0x0000559041068236 <+87>: lea rdi,[rip+0x2e03] # 0x55904106b040 <_ZSt4cout@@GLIBCXX_3.4>
0x000055904106823d <+94>: call 0x5590410680b0 <_ZNSolsEi@plt>
0x0000559041068242 <+99>: mov eax,0x0
0x0000559041068247 <+104>: mov rdx,QWORD PTR [rbp-0x8]
0x000055904106824b <+108>: xor rdx,QWORD PTR fs:0x28
0x0000559041068254 <+117>: je 0x55904106825b <main()+124>
0x0000559041068256 <+119>: call 0x559041068090 <__stack_chk_fail@plt>
0x000055904106825b <+124>: leave
0x000055904106825c <+125>: ret
End of assembler dump.
标签:QWORD,mov,c++,rbp,传递,eax,引用,PTR,rax
From: https://www.cnblogs.com/jasonjunshu/p/18540544