[learning notes] 2. Stack management of function call

Time:2019-10-22

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:

[learning notes] 2. Stack management of function call

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.

Compiled code:

[learning notes] 2. Stack management of function call

[learning notes] 2. Stack management of function call

Assembly instructions:

[learning notes] 2. Stack management of function call

1.2 compile step process

0. Confirm the address of the stack

[learning notes] 2. Stack management of function call

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

[learning notes] 2. Stack management of function call

2. Save local variables

[learning notes] 2. Stack management of function call

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

[learning notes] 2. Stack management of function call

3. Jump to F function

The first step of stack pressing of F function

[learning notes] 2. Stack management of function call

4. Confirm the stack frame of F function

[learning notes] 2. Stack management of function call

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

[learning notes] 2. Stack management of function call

6. Return to main function

[learning notes] 2. Stack management of function call

[learning notes] 2. Stack management of function call

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

[learning notes] 2. Stack management of function call

Give the address of execve to PC, the address of next execution

[learning notes] 2. Stack management of function call

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

[learning notes] 2. Stack management of function call

Corresponding assembly code

[learning notes] 2. Stack management of function call

2.2 execution steps

1. Main function stack and push

[learning notes] 2. Stack management of function call

2. Store local variables

[learning notes] 2. Stack management of function call

3. Input of parameters

[learning notes] 2. Stack management of function call

[learning notes] 2. Stack management of function call

4. Jump to F function

[learning notes] 2. Stack management of function call

5. Stack and push of F function

[learning notes] 2. Stack management of function call

6. Save the parameter in F function

[learning notes] 2. Stack management of function call

[learning notes] 2. Stack management of function call

Initialize sum value

[learning notes] 2. Stack management of function call

7. Assignment of F function variable

[learning notes] 2. Stack management of function call

[learning notes] 2. Stack management of function call

It can be found that the change is not the parameter n but the stack address of the parameter.

8. Addition operation

[learning notes] 2. Stack management of function call

[learning notes] 2. Stack management of function call

9. Return main function

[learning notes] 2. Stack management of function call

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.

100. salute

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.
Thank you.
PS: all resources and pictures in this article are from video

Recommended Today

Single and multiple buttons are styled with pictures

I’ve always seen people asking how to style and enlarge the buttons of radio buttons and multi buttons? Let’s share an example I did. 1. First make the button into a picture  2.html page Copy code The code is as follows: <!DOCTYPE HTML> <html> <head> <meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ /> <script type=”text/javascript” src=”jquery-1.10.2.min.js”></script> <script type=”text/javascript”> […]