The first is to reference the c ++ source code:
Copy codeThe Code is as follows: void add (int a, int B, int & c ){
C = a + B;
}
Int main (){
Int a = 1;
Int B = 2;
Int c = 0;
Add (a, B, c );
}
The following is the assembly code corresponding to main:
Copy codeThe Code is as follows:; 6: int main (){
Push ebp
Mov ebp, esp
Sub esp, 12; reserve 12 bytes for the stack space of the called function to store local variables a, B, c
; 7: int a = 1;
Mov dword ptr _ a $ [ebp], 1; initialize a _ a $ as the offset of the bucket a address relative to the ebp base address
; 8: int B = 2;
Mov dword ptr _ B $ [ebp], 2; initialize B _ B $ as the offset of B's bucket address relative to ebp's base address
; 9: int c = 0;
Mov dword ptr _ c $ [ebp], 0; the first test of c _ c $ is the offset of the c bucket address relative to the ebp base address
; 10: add (a, B, c );
Lea eax, dword ptr _ c $ [ebp]; get the offset of the c bucket relative to the ebp base address (that is, the offset address of the c storage unit), and put it in the register eax
Push eax; Save the offset of the c bucket to the stack
Mov ecx, dword ptr _ B $ [ebp]; put the values in bucket B (that is, the values of Bucket B) in register ecx
Push ecx; Save the value of Bucket B to the stack
Mov edx, dword ptr _ a $ [ebp]; put the value (that is, the value of a) in the bucket in the register edx
Push edx; Save the bucket to the stack
The above push eax push ecx push edx stores the values of the original local variables a, B, and c in the stack, but for c, it stores the offset address of the c bucket.
Therefore, for a and B, that is, to copy and save their worth, that is, to pass the value; while c only stores the offset address of its own bucket, that is, the transfer address.
Call? Add @ YAXHHAAH @ Z; call the add function. The preceding statement is ready for passing parameters.
Add esp, 12; since the stack is pressed for the parameters passed by calling function add, the stack space is released here, that is, the release parameter.
This is why the local variables and parameters are invalid after the function is called because their space is released.
; 11:
; 12 :}
Xor eax, eax
Mov esp, ebp
Pop ebp
Ret 0
The assembly code corresponding to function add is as follows:
Copy codeThe Code is as follows:; 1: void add (int a, int B, int & c ){
Push ebp
Mov ebp, esp
; 2: c = a + B;
Mov eax, dword ptr _ a $ [ebp]; take the value of parameter a to register eax
Add eax, dword ptr _ B $ [ebp]; add the value of parameter B to the value of a in eax, and put the result in eax.
Mov ecx, dword ptr _ c $ [ebp]; place the offset address of c to the ecx register.
Mov dword ptr [ecx], eax; write the results in eax to the address unit specified by ecx, that is, the storage unit of c
; 3 :}
Pop ebp
Ret 0
From the above we can see that c ++ does pass a copy of the value for passing the value, while for reference, although it is in the form of passing the value, but what the compiler actually delivers internally is worth the address.
The following is the c ++ source code for pointer scenarios:
Copy codeCode: void add (int a, int B, int * c ){
* C = a + B;
}
Int main (){
Int a = 1;
Int B = 2;
Int c = 0;
Add (a, B, & c );
}
Assembly code corresponding to the mian function:
Copy codeThe Code is as follows:; 6: int main (){
Push ebp
Mov ebp, esp
Sub esp, 12;
; 7: int a = 1;
Mov dword ptr _ a $ [ebp], 1
; 8: int B = 2;
Mov dword ptr _ B $ [ebp], 2
; 9: int c = 0;
Mov dword ptr _ c $ [ebp], 0
; 10: add (a, B, & c );
Lea eax, dword ptr _ c $ [ebp]
Push eax
Mov ecx, dword ptr _ B $ [ebp]
Push ecx
Mov edx, dword ptr _ a $ [ebp]
Push edx
Call? Add @ yaxhhhpa @ Z; add
Add esp, 12;
; 11:
; 12 :}
Xor eax, eax
Mov esp, ebp
Pop ebp
Ret 0
Assembly code corresponding to the add function:
Copy codeThe Code is as follows:; 1: void add (int a, int B, int * c ){
Push ebp
Mov ebp, esp
; 2: * c = a + B;
Mov eax, dword ptr _ a $ [ebp]
Add eax, dword ptr _ B $ [ebp]
Mov ecx, dword ptr _ c $ [ebp]
Mov dword ptr [ecx], eax
; 3 :}
Pop ebp
Ret 0
We can see that the pointer is the same as the referenced assembly code, so the two functions the same.