The overall function of the stack

Source: Internet
Author: User

This article has been presented to: http://www.cnblogs.com/xmphoenix/archive/2012/04/28/2475399.html

a . The overall function of the stack

(1) Save Site / Context

(2) Passing Parameters : Assembly code calls C function, you pass parameters

(3) Save Temp Variable : includes non-static local variables for the function and other temporary variables that the compiler automatically generates.

two . Why assembly code calls C function needs to set the stack

read a lot about it before Uboot of the analysis, which there is said to be C run the language and prepare the stack. and himself at the start of Uboot . S Assembly code, about the system initialization, also see that there is a stack pointer initialization of this action. However, it was never just seen that the system initialized to initialize the stack, which correctly assigned the stack pointer SP , but never saw anyone explain why the stack was initialized. So, the next thing is to go through a certain exploration, trying to explain why to initialize the stack.
to understand this problem, first understand the role of the stack. About the role of the stack, to be explained in detail, to a very long length, so here is just a brief introduction. In general, the function of the stack is: Save the scene / context, pass parameters, save temporary variables

1.Save Site/Context
site/context, meaning is equivalent to the crime scene, there are always some scene situation, to record down, otherwise destroyed by others, you will not be able to restore the scene. And the scene here, that meansCPUwhen running, some registers are used, such asR0,R1wait, the value of these registers, if you do not save and jump directly to the child function to execute, then it is likely to be destroyed, because the function of the execution also need to use these registers. Therefore, before the function call, these registers should be kept on the spot for the time being.(into the stackpush)after the call function has finished executing and returning(out of the stackPop), and then restore the scene. ThisCPUwill be able to carry out the execution correctly.
Save the value of the register, usually using thePushinstruction, the corresponding value of some registers, put into the stack, the corresponding values into the stack, that is, the so-called stack. Then when the child function that is called is executed, the callPop, the values in the stack are assigned to the corresponding registers that you just started to press the stack, and the corresponding values are popped out of the stack, the so-called stack.
The saved registers are also included in theLRvalue (because theBLcommand to jump, then the previousPCthe value is presentLR), and then when the subroutine is finished, theLRthe valuePopcome out and assign a value toPC, so that the correct return of the child function is implemented.

2. passing Parameters
C when a language makes a function call, it is often passed to some parameters of the called function, for these C language-level parameters, when the compiler is translated into assembly language, it is necessary to find a place to store, and let the function can be called to access, otherwise there is no implementation of the delivery parameters. To find a place to put, in two cases. In one case, The parameters can be passed through the register by using no more than 4 parameters. Because in the previous save scene action, has saved the corresponding register value, then this time, these registers are idle, can let us use, then can put the parameter. In another case, when the parameter is more than 4 , the register is not enough to use the stack.

3. Temporary variables are saved in the stack

Includes non-static local variables for the function and other temporary variables that the compiler automatically generates.

4.Example AnalysisChow a language function call uses the stack's
The effect of the above interpretation of the stack is somewhat abstract, here again with an example to explain briefly, it is easy to understand: with: arm-inux-objdump–d u-boot >dump_u-boot.txtcan getDump_u-boot.txtfile. The file is in and contains theU-bootin the executable assembly code of the program, where we can seeCThe source code of the function of the language, exactly corresponds to those assembly code.
The following is a compilation code of two functions, one isClock_init, and the other is withClock_initin the sameCone of the other functions in the source fileCopycode2ram:
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,
......
33d009f8:e1a0f00e mov pc, LR
(1)Clock_initpart of the code can see the first line of the function:33d009b0:e3a02313 mov r2, #1275068416; 0x4c000000There's nothing we can expect.Pushinstruction, and did not go to put the value of some registers on the stack. This is because, weClock_initThis part of the content, the use ofR2,R3wait for the register, and the previous callClock_initpreviously used registersR0, there is no conflict, so you can use this herePushto save the value of this type of register, but there is a register to note thatR14, i.e.LR, which is called in front of theClock_init, the use ofBLinstructions, so it will automatically turn thePCvalue is assigned to theLR, so there is no needPushinstructions to bePCvalues are saved to the stack. andClock_initthe last line of the code: 33d009f8:e1a0f00e mov pc, LRis what we usually do.MOVPC,LR, putLRThe value of the previous saved function call.PCvalue, assigned to the currentPC, which enables the correct return of the function, returning to the position of the next instruction at the time of the function call. ThisCPUyou can continue to execute the rest of the code in the original function.
(2)Copycode2ramsection of the code whose first line:33d0091c:e92d4070 Push {r4, R5, R6, LR}is what we expect, withPushinstructions to save theR4,r5,ras wellLR. WithPushto saveR4,R5,R6, it is because of the so-called preservation site, after the subsequent function return time to restore the scene, and usePushto saveLR, that's because there are other function calls inside this function:33d0092c:ebffffef bl 33d008f0 <bBootFrmNORFlash>
......
33d00984:ebffff14 bl 33d005dc <nand_read_ll>
......I used it.BLinstructions that will change the way we first enteredClock_initTime ofLRvalue, so we're going to use thePushalso temporarily preserved. and correspondingly,Copycode2ramthe last line:33d009ac:e8bd8070 Pop {r4, R5, r6,pc}is to put the previousPushthe value that givesPopout and back to the corresponding register, where the last one is going to startPushof theLRthe value,Popgive it to me.PC, because the return of the function is implemented. In addition, we note that theCopycode2ramthe second line of the countdown is:33d009a8:e3a00000 mov r0, #0; 0x0is to0assign a value toR0Register, this is what we call the return value of the pass, is throughR0Register. The return value here is0, also corresponds to theClanguage in the source of the "return0 ".
which register to use to pass the return value: Of course, you can also use other temporarily idle unused registers to pass the return value, but these processing methods, in itself is based onARMof theAPCSthe use of the Register of the design of the Convention, you'd better not casually change the use of the way, preferably in accordance with its agreement to deal with, so that the program more in line with the specification.

The overall function of the stack

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.