Why does the assembler call the C function to set the stack? __php

Source: Internet
Author: User

Read a lot of articles about the Uboot analysis class, which mentions running the stack for the C language. And in Uboot start. S assembly code, about system initialization, also see the stack pointer initialization, that is, correctly to the stack pointer SP assignment, but never see someone explain why to do so. Next, I try to explain the problem.

First understand the role of the stack. In this regard, it will take a long time to explain in detail, so it is only a brief introduction. In general, its role is to save the field/context, pass parameters, save temporary variables Save the scene/context

Scene/context equivalent to the crime scene, there are always some crime scene, to record down, otherwise destroyed by others, can not be restored. And here is the site, refers to the CPU runtime, used some registers, such as R0,R1, for these registers, if not saved and directly jump to the child function execution, 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 saves (into the stack push), and so on call function completes after the stack (pop) restores the scene. So that the CPU can continue to execute correctly.

Save the value of the register, generally with the push instruction, will correspond to the value of some registers, each put into the stack, that is, the so-called pressure stack. Then the call to the child function after the completion of the call pop, the stack of a value, assigned to the corresponding those you just began 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.

In which registers are saved, also include the LR value (because the BL command to jump, the previous PC value is in LR), after the completion of the subroutine, and then the stack of LR pop out, assign value to the PC, so that the correct return of the child function. Passing Parameters

C language function call, will pass to the called function some parameters, for these C language level parameters, the compiler translated into assembly language, to find a place to store, and let the call function can access, otherwise cannot pass. Find a place to store it in 2 different situations. First, there are no more than 4 parameters, which can be transmitted through registers. Because in the previous save the scene action, has saved the corresponding register the value, at this time these registers are idle, may allow us to use the storage parameter. Second, there are more than 4 parameters, the register is not enough, you have to use the stack. temporary variables saved on stack

These temporary variables include non-static local variables of the function and other temporary variables that the compiler automatically generates.

Examples of how C-language function calls use stacks

The above explanation is somewhat abstract, here again with the example simple explanation, it is easy to understand:
Use Arm-inux-objdump–d u-boot dump_u-boot.txt to get dump_u-boot.txt files. The file contains the U-boot executable assembly code, from which we can see the corresponding assembly code for the C program.

The following is a list of two function assembly codes, one is Clock_init and the other is a function copycode2ram with clock_init in the same C source file:

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 b bootfrmnorflash ...
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,
...
33d009f8:e1a0f00e  mov pc, LR

(1) First analysis of the clock_init corresponding assembly code, you can see the first line of the function
: 33d009b0:e3a02313 mov r2, #1275068416; 0x4c000000
There is no push instruction we expected, that is, there are no values for some registers to put on the stack. This is because, clock_init use of R2,R3 and other registers, and the previous call Clock_init used before the register r0, there is no conflict, so no push to save, there is a register to note, R14, that is, LR, before the call Clock_init, the use of the BL Directive, So the PC value of the jump is automatically assigned to LR, so there is no need to push to save the PC value to the stack. And clock_init corresponds to the last line of assembly code: 33D009F8:E1A0F00E mov pc, LR is our common mov pc,lr, the LR value, that is, before the function call to save the value of the PC, assigned to the current PC, so that the function of the correct return, Returns to the position of an instruction at the moment the function is called. The CPU can continue to execute the remaining code in the original function.

(2) Copycode2ram corresponding assembly code first line: 33d0091c:e92d4070 push {r4, R5, R6, LR}
Is what we expect, save R4,R5,R6,LR with push because this function also includes other function calls
: 33d0092c:ebffffef bl 33d008f0 b bootfrmnorflash ...
33d00984:ebffff14 BL 33D005DC nand_read_ll
......
Also use the BL directive, which will change the LR value when we first enter the Clock_init, so we have to push to save it for the time being.

And correspondingly, Copycode2ram last line: 33d009ac:e8bd8070 pop {r4, R5, r6,pc} is the value of the previous push to pop out, and also the corresponding registers, the last one is to start push LR value pop out to the PC, The return of the function is implemented. In addition, we note that the penultimate line of Copycode2ram: 33d009a8:e3a00000 movr0, #0; 0x0 is a 0 assignment to the R0 register, which is the return value we say is passed, the return value here is 0, also corresponds to the C code "returns 0".

You can, of course, pass the return value with other registers that are temporarily idle.

For the use of which registers to pass the return value, is based on ARM's APCs register of the use of the contract design, preferably in accordance with its agreed to deal with, do not change it casually. This program will be more standardized.

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.