ARM Foundation: Why C language (function call) need stack, but assembly language does not need to stack __ function

Source: Internet
Author: User
/************************************************************************************************************** ****
Original address: http://bbs.chinaunix.net/thread-2304248-1-1.html
Description: Why the C language (function call) requires a stack, but assembly language does not need a stack.

*************************************************************************************************************** ***/
Why the C language (function call) requires a stack, while assembly language does not need a stack

Before read a lot about Uboot analysis, which has said to the C language, ready to run the stack.

And in Uboot's START.S assembly code, for system initialization, you also see a stack pointer that initializes this action. But I've never seen anyone say system initialization to initialize the stack, that is, assign values to the stack pointer sp correctly, but never see anyone explain why the stack is initialized. So, the next thing is, after a certain amount of exploration, trying to explain why to initialize the stack, that is:

Why function calls to the C language use the stack, but the assembly does not need to initialize the stack.

To understand this problem, you first need to understand the role of the stack.

On the role of the stack, to be explained in detail, a long space, so here is just a brief introduction.

In general, the function of the stack is to save the field/context and pass the parameters.

1. Save the site/context

The scene, meaning is equivalent to the crime scene, there are always some scene of the situation, to record down, otherwise destroyed by others, you will not be able to restore the scene. And here's where it says the scene, that is, when the CPU is running, use some registers, such as R0,R1, and so on, for the value of these registers, if you do not save and jump directly to the child function to execute, it is likely to be destroyed, because its function to execute also use these registers.

Therefore, before the function call, should these registers and so on the scene, temporarily maintains, waits for the call function to perform completes returns, then restores the scene. So that the CPU can continue to execute correctly.

In the computer, you can often see the word context, the corresponding English is the contextual. So: 1.1. What is called the contextual context

Save the scene, also called the Save context.

Context, the English language is called the contextual, is the above article, and the following article, that is, with you at the moment, the current CPU operation is related to the content, that is, those you use the register. So, and above the scene, is a meaning.

Save the value of the register, generally with the push instruction, will correspond to the value of some registers, one by one into the stack, the corresponding value into the stack inside, that is, the so-called pressure stack.

Then the call to the completion of the child function, and then call the pop, the stack of values, assigned to the corresponding those you just start to press the stack used in the register, the corresponding value from the stack shot out, that is, the so-called out of the stack.

The saved registers, also including the LR value (because with the BL instruction to jump, then the value of the previous PC is in LR, then when the subroutine is finished, then pop out the value of the LR in the stack and assign it to the PC, thus realizing the correct return of the child function.
2. Pass Parameters

C Language for function calls, often passed to the called function some parameters, for these C language level parameters, the compiler translated into assembly language, it is necessary to find a place to store, and let the function can be called access, otherwise it is not sent to implement the parameters. To find a place to put, in two cases.

In one case, the parameters passed by themselves are very few, and the parameters can be transmitted through registers.

Because in the previous save the scene of the action, has saved the corresponding register value, so at this point, these registers are idle, we can use, that can be put parameters, and fewer parameters, sufficient to store parameters, such as 2 of parameters, then use R0 and R1 storage can be. (about parameter 1 and parameter 2, which is placed in the r0, which is placed in the R1, and APCs in the "transfer/return parameters between function calls" related, APCs will have detailed agreement. Interested in their own to study. )

But if there are too many parameters and the registers are not enough, then you have to put the extra parameters in the stack.

That is, you can use the stack to pass all the extra arguments that are not in place for the registers.
3. An example of how the C language function call uses the stack

The function of the stack for the above explanation is somewhat abstract, and here's an example to explain it easily:

Use:

Arm-inux-objdump–d u-boot > Dump_u-boot.txt Copy Code

can get dump_u-boot.txt files. The file is the executable assembly code that contains the program in U-boot,

In which we can see the C language function source code, in the end corresponds to those assembly code.

The following is a two-function assembly code,

One is Clock_init,

The other is in the same C-source file as Clock_init, and another function Copycode2ram:

33D0091C <copycode2ram>:
33d0091c:e92d4070 Push {r4, R5, R6, LR}
33d00920:e1a06000 mov r6, r0
33D00924:E1A05001 mov R5, r1
33D00928:E1A04002 mov r4, r2
33D0092C:EBFFFFEF BL 33d008f0 <bBootFrmNORFlash>
... ...
33d00984:ebffff14 BL 33D005DC <nand_read_ll>
... ...
33d009a8:e3a00000 mov r0, #0; 0x0
33d009ac:e8bd8070 Pop {r4, R5, R6, PC}

33d009b0 <clock_init>:
33d009b0:e3a02313 mov r2, #1275068416; 0x4c000000
33D009B4:E3A03005 mov r3, #5; 0x5
33d009b8:e5823014 STR R3, [R2, #20]
... ...
33D009F8:E1A0F00E mov pc, LR copy code


(1) Clock_init part of the code

You can see the first line of the function:

33d009b0:e3a02313 mov r2, #1275068416; 0x4c000000 Copy Code

There is no push instruction that we expected, and we did not put the values of some registers on the stack. This is because we clock_init this part of the content, the use of R2,R3 and so on registers, and the previous call Clock_init before the register used to r0, there is no conflict, so you can not push to save the value of such registers, but there is a register to note that, That is R14, that is, LR, it is in front of the call Clock_init, with the BL directive, so will automatically jump when the value of the PC to the LR, so also do not need to push the command to save the PC value to the stack.

And the last line of the Clock_init code:

33D009F8:E1A0F00E mov pc, LR copy code

Is our common MOV pc, LR, the value of LR, that is, the previous saved function call when the PC value, assigned to the current PC,

This enables the correct return of the function, which is returned to the position of the next instruction when the function is called.

So that the CPU can continue to execute the code left in the original function.

(2) Copycode2ram part of the code

Its first line:

33d0091c:e92d4070 Push {r4, R5, R6, LR} copy Code

That is what we expect, with the push command, to preserve the r4,r5,r and LR.

Save R4,R5,R6 with push

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.