Analysis of the internal mechanism of C language - based on S3C2440c language lighting

Publisher:painterLatest update time:2022-08-05 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Code

compilation

The previous assembly code is as follows


//These are just assembly syntax, nothing to remember

.text

.global _start

_start:

/*Set memory: sp (stack pointer) stack*/

ldr sp, = 4096 //nand

// ldr sp, = 0x40000000 + 4096/*nor start*/

/*Call main function*/

bl main /*jump to execute main and save the return address*/

halt:

b stop


The process is as follows


1. Set up the stack

2. Call main and save the return address in lr

main.c

int main()

{

unsigned int *pGPFCON = (unsigned int *)0x56000050;

unsigned int *pGPFDAT = (unsigned int *)0x56000054;


/*Configure GPF4 as output pin*/

*pGPFCON = 0x100;


/*Configure GPF4 output 0*/

*pGPFDAT = 0;

return 0;

}


The process is as follows


Define two local variables

Setting variables

return 0

doubt

Why do we need to set up the stack before calling the main function?

——C function needs to use


How to use the stack?

——Save local variables, save lr and other registers

In the assembly, the return value of bl main is stored in the lr register.

Assuming that the main function calls other functions, it should also return to the main function to continue execution after the call.

Obviously, when calling a sub-function, the return address should also be saved in lr. Then the address of the main function in lr will not be overwritten by the return address of the sub-function? ——So lr and other registers will be saved


How does the caller pass parameters to the callee (sub-function)?


How does the callee return the return value to the caller?

These two questions need to be understood. The ATPCS rules followed by assembly calling C functions

ARM-THUMB prodcedure call standard

insert image description here

These four registers are parameter result registers

The caller passes parameters to the callee through these registers

The callee returns the result to the caller through these registers

How to restore those registers from the stack?

insert image description here

In the function, r4~r11 may be used

So, save them at the entry of the function and save them at the exit of the function

Of course, your function may be very simple, so just save whoever you need.


Analyzing disassembled code

insert image description here

Assume we are using nand boot

For the program started by NAND, the hardware will completely copy the first 4k of Nand Flash to the 4

kMemory

That is to say, the machine codes in the second column will be completely saved in the front of the 4k memory.


The state in memory is as follows:

insert image description here

The last address is e89da800

It can be seen that the common segment is not saved in the memory

The common section is the comment section of the program, and these comments do not need to be included in the bin file

insert image description here

process

insert image description here

Upon power-up, execution starts at address 0 and the stack is set at address 4096

Then bl saves the return address at 0x8 and jumps to 0x0c to start executing the main function

insert image description here

Initially, ip = sp = 4096


The second statement saves the values ​​of the four registers in the brackets into the memory corresponding to sp.

stmdb, decrement first and then store, store according to the register number. fp, ip, lr, pc correspond to r11, r12, r14, r15 respectively. High numbers are placed at high addresses. So we store the address of pc first, pc = the address of the current instruction + 8, which is 0x18.

Next, store lr, lr = 0x8

ip = sp = 4096

fp is an undefined value

The above are the values ​​of these four registers. The addresses stored are 4 minus 4 from 4096.

So the final sp' = 4096 - 4*4 = 4080


The third directive

fp = ip - 4 = 4092


Article 4

sp = sp - 8 = 4080 - 8 = 4072


Fifth Sixth Seventh

r3 = 0x56000050

The value of r3 is stored in [fp-16] = [4076-16] = 4060

insert image description here

This is the pGPFCON local variable. Local variables are stored on the stack.


Article 10

Store the value of r3, 0x56000054, into [fp - 20] = 4072

insert image description here

This is the local variable


Article 11

r2 = [fp - 16] = [4076] = 0x56000050 = pGPFCON


Article 12

Store r3—0x100 into the location pointed to by r2-----0x56000050

The corresponding C language is

insert image description here

Article 13

r2 = [fp - 20] = 4072 = 0x56000054


14

Store r3 in the location pointed to by r2, which is 0x56000054

insert image description here

The corresponding C code is here↑


The last two

Assign 0 to r3

Assign r3 to r0

The compiler is not very smart, in fact, it can directly assign 0 to r0

The callee returns the return value to the caller through r0~r3. There is only one return value here, so r0 is enough.


The second to last

Recovery Stack

sp = fp - 12 = 4092 - 12 = 4080


The last one

Restore registers from the stack

fp, sp, pc are labeled r11, r13, r15 respectively

fp = [4080] = originally saved fp

sp = [4084] = 4096

pc = [4088] = 8—jump back to address 0x8

insert image description here

Return here↑


Summarize

From the above analysis, we can see that the stack is the memory pointed to by the sp register, which can be read and written

insert image description here

The previous part is used to save registers, and the registers will be restored before the function returns.

insert image description here

Here are the local variables

It can also be seen here that 8 bytes is the space for local variables

Setting the stack to 4096 is perfectly fine.

The stack is transferred from high address to low address. The program is very small and the stack will not destroy the main function below.


In this program, we only involve the callee returning the return value to the caller, so how does the caller pass parameters to the callee?


Next blog post Writing program tests


Reference address:Analysis of the internal mechanism of C language - based on S3C2440c language lighting

Previous article:S3C2440c language assembly parameter transfer light
Next article:S3C2440 uses bin file machine code to light up

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号