In the beginning, if you find any mistakes in the technical terms, please remember to correct them in time. This is really important.
1. Function call without parameter passing
This example will actually demonstrate a demo with only function local variable assignment
We will first look at the C language source code and compiled code, and then step by step deduce the space usage of the stack.
1.1 function source code
C language source code:
Note: in this script, the main function calls the F function. There are definitions of local variables in each function. The function call has no parameter passing.
1.2 compile step process
0. Confirm the address of the stack
To facilitate memory and viewing, let’s assume that the actual address of the stack is 9000.
1. Determine the stack frame of main function
2. Save local variables
If there are < 4 variables, they are saved directly to the register.
The part with the number of variables > 4 is put into the stack.
The specific address parameters are
3. Jump to F function
The first step of stack pressing of F function
4. Confirm the stack frame of F function
5. Return parameter variable
The returned value will be directly put in the R0 register
If the value is large, an address unit will be opened to return the address to the main function
6. Return to main function
Then go straight back
With this operation, the function returns to the main function, the previous function.
7. Return execution function
Executable using exec fork subprocess
The last function of main is execve
Give the address of execve to PC, the address of next execution
The starting address of a function on each stack frame, such as 9000
The BL B instruction automatically pushes the next address of the function into the stack
Stack pressing needs to be changed at the next level, or the compiler will optimize it.
2. Function call with parameters
2.1 running instance code
Corresponding assembly code
2.2 execution steps
1. Main function stack and push
2. Store local variables
3. Input of parameters
4. Jump to F function
5. Stack and push of F function
6. Save the parameter in F function
Initialize sum value
7. Assignment of F function variable
It can be found that the change is not the parameter n but the stack address of the parameter.
8. Addition operation
9. Return main function
FP returns the address of the last function stored in the stack frame
And then back to the main function.
So the value of the actual parameter is not changed because it is stored in different location units. Pass the local variable of the parameter definition. It will save different positions of stack or register.
3. Formal and actual parameters
Combined with the analysis of the content parameters and actual parameters in the second paragraph above
It can be seen that when calling functions for parameter passing, stack space or registers will be reallocated for management.
In initialization, the function (such as F function) that is called at the same time will take out the value in the register and put it into its own stack for management.
So the parameters passed in and those in the main function belong to different addresses. Therefore, they will not affect each other.
If not, please refer to Mr. Wang’s wonderful explanation of stack management
In the process of learning, I got a great sense of satisfaction and connected some previous things. Thank you very much, Mr. Wang Litao.
PS: all resources and pictures in this article are from video