From the assembly to see the c++ function of the default parameters of the use of instructions
- 2020-04-01 23:29:34
- OfStack
In c++, you can provide default arguments to functions, so that when a function is called, if no arguments are provided, the compiler will provide default values for the arguments to the function. Here's how it works from assembly.
The following is the c++ source code:
int add(int a = 1, int b = 2) {//Parameter a and b have default values
return a + b;
}
int main() {
int c= add();//No parameters
}
Here is the sink code in the mian function:
; 4 : int main() {
push ebp
mov ebp, esp
push ecx; Is a local variable c The distribution of 4 Byte storage space ecx for 32 A register
; 5 : int c= add();
push 2; will 2 Push the stack add In the function b The default value of the parameter Here the parameter push direction is from right to left
push 1; will 1 Push the stack add In the function a The default value of the parameter
call ?add@@YAHHH@Z ; call add function
add esp, 8; Release just for add Storage space when providing parameters
mov DWORD PTR _c$[ebp], eax;eax register add The function returns a value and writes the variable c inside
; 6 :
; 7 : }
xor eax, eax
mov esp, ebp
pop ebp
ret 0
The following is the assembly code of the add function:
?add@@YAHHH@Z PROC ; add
; 1 : int add(int a = 1, int b = 2) {
push ebp
mov ebp, esp
; 2 : return a + b;
mov eax, DWORD PTR _a$[ebp]; The parameter a Is written to the register eax
add eax, DWORD PTR _b$[ebp]; The parameter b The value of and eax The values inside are added and the result is stored eax register
; 3 : }
pop ebp
ret 0
?add@@YAHHH@Z ENDP
The following is the case where only one parameter is provided
First look at c++ source code:
int add(int a = 1, int b = 2) {//Parameter a and b have default values
return a + b;
}
int main() {
int a = 3;
int c= add(a);//Only provide the parameters for a
}
The following is the assembly code in the main function:
; 4 : int main() {
push ebp
mov ebp, esp
sub esp, 8;esp The register moves as a stack pointer 8 Byte, is a local variable a . c Reserved storage space
; 5 : int a = 3;
mov DWORD PTR _a$[ebp], 3; will 3 Write a local variable a Storage space
; 6 : int c= add(a);//No parameters
push 2; will 2 Push, provide b Parameter default
mov eax, DWORD PTR _a$[ebp]; Take out the a , and put it into a register eax inside
push eax; will eax Inside the value of the stack, provide parameters a Is not the default value provided 1
call ?add@@YAHHH@Z ; call add function
add esp, 8; Release just as the calling function add Assigned to the parameter 8byte space
mov DWORD PTR _c$[ebp], eax;eax It holds the result of calling the function and writes to it c In storage space
; 7 :
; 8 : }
xor eax, eax
mov esp, ebp
pop ebp
ret 0
As you can see, the difference from above is that there is no default parameter value for a.
The following is the assembly code of the add function, as in the first case, without change:
?add@@YAHHH@Z PROC ; add
; 1 : int add(int a = 1, int b = 2) {//Parameter a and b have default values
push ebp
mov ebp, esp
; 2 : return a + b;
mov eax, DWORD PTR _a$[ebp]
add eax, DWORD PTR _b$[ebp]
; 3 : }
pop ebp
ret 0
?add@@YAHHH@Z ENDP
Since the explicit value is given for parameter a, the compiler only provides the default value for parameter b. As you can imagine, if you provide explicit values for the add function, the compiler will not provide default values for parameters a and b.