What is a stack:
Baidu says: A stack is a special linear list that only allows insertion or deletion operations at one end of the list. The end of the table that allows insertion and deletion operations is called the top of the stack. The other end of the table is called the bottom of the stack. The current position of the top of the stack is dynamic, and the mark of the current position of the top of the stack is called the top pointer. When there are no data elements in the stack, it is called an empty stack. The insertion operation of the stack is usually called pushing or stacking, and the deletion operation of the stack is usually called unstacking or popping.
Simple understanding:
The stack is a temporary storage place. The stack in the computer is mainly used to store temporary data, local variables and the return address of the interrupt/call subroutine program. The stack in the program is mainly used to store local variables in the function and save register parameters. If you use an operating system, the stack may also store the context of the current thread. One principle of setting the stack size is to ensure that the stack does not overflow into the data space or program space. When the CPU runs the program, it will automatically use the stack, so the stack pointer SP must be set before calling the C program.
The CPU's RAM space is generally segmented, from low address to high address, in order: program segment (.text), BSS segment, there may be heap space above, and then the stack segment at the top. The stack is arranged in this way because of the characteristics of the stack. The stack pointer SP is generally initialized at the high address of the stack segment, that is, the high address of the memory, and then the stack pointer is allowed to grow downward (actually decrement).
The advantage of this is that the stack space is far away from other segments and will not overlap with other segments, causing modification of other segment data and causing unpredictable consequences. There is also a principle for setting the stack size to ensure that the stack does not overflow into the data space or program space. The so-called stack overflow refers to the stack pointer SP growing downward to other segment spaces. If the stack pointer grows downward to other segment spaces, it is called a stack overflow. Stack overflow will modify the values of other spaces, and in severe cases, it can cause a crash.
Stack pointer settings
The stack pointer is set in the internal RAM at the beginning because not every board has external RAM, and the size of external RAM is also different. If it is SDRAM, it needs to be initialized. The boot program that starts running in the internal RAM is usually a small boot program, which basically does not use the stack, so the stack is set in the internal RAM. But this also requires that the boot program cannot use a large number of local variables at will.
The chip has 4K SRAM and 64M SDRAM size, from 0x30000000 to 0x33FFFFFF. When the program runs in the on-chip SRAM, the value of sp is set to 4096. When the program runs in the SDRAM, sp is set to 0x34000000. When the program runs in the internal SRAM, if the SDRAM has been initialized, the stack pointer can also be set to 0x34000000 to further prevent stack overflow.
The overall role of the stack
a. Save the scene;
b. Passing parameters: When the assembly code calls the C function, parameters need to be passed;
c. Save temporary variables: including non-static local variables of the function and other temporary variables automatically generated by the compiler;
1) Save the scene:
The scene is like the crime scene. There are always some scenes that need to be recorded, otherwise you will not be able to restore the scene after it is destroyed by others. The scene here refers to some registers used when the CPU is running, such as r0, r1, etc. If you do not save the values of these registers and directly jump to the sub-function to execute, it is likely to be destroyed because these registers are also used for function execution. Therefore, before calling a function, these registers and other scenes should be temporarily kept (pushed into the stack), and the scene should be restored after the called function is executed and returned (popped from the stack). In this way, the CPU can continue to execute correctly.
To save the value of a register, the push instruction is generally used. The corresponding values of some registers are placed on the stack one by one. The corresponding values are pushed into the stack, which is called pushing. Then when the called subroutine is executed, pop is called again to assign the values in the stack to the corresponding registers that you used when you first pushed the stack, and pop the corresponding values from the stack, which is called popping. The saved registers also include the value of lr (because if the bl instruction is used to jump, the previous value of PC is stored in lr). Then, when the subroutine is executed, the value of lr in the stack is popped out and assigned to PC, so that the correct return of the subroutine is achieved.
2) Passing parameters
When calling a function in C language, some parameters are often passed to the called function. When these C language-level parameters are translated into assembly language by the compiler, they need to be stored somewhere and accessible to the called function, otherwise the parameter passing cannot be realized. There are two situations for finding a place to store them. In one case, if the number of parameters passed is no more than 4, the parameters can be transferred through registers r0~r3. Because the values of the corresponding registers have been saved in the previous action of saving the scene, these registers are free and can be used by us, so we can put the parameters. In another case, if there are more than 4 parameters, the registers are not enough, so we have to use the stack.
3) Temporary variables are stored in the stack
This includes non-static local variables of a function and other temporary variables automatically generated by the compiler.
Previous article:S3C2440 Clock Architecture and Programming
Next article:ARM bare board program and extension_light up LED
Recommended ReadingLatest update time:2024-11-15 08:08
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Analysis of the Design Method of DSP and Slow Peripheral Device Interface
- [ST60 short-distance evaluation] Part 1: First release on the Internet! Detailed evaluation!
- How reliable is Dupont wire???
- Q and D values of capacitors
- TI CC3200 WIFI Training Development Kit——by light-s
- HDMI to MIPI universal driver horizontal screen 1080P HD PC Raspberry Pi projector set-top box PS4 camera sub-screen side-transparent monitor TF card
- FPGA-based RGB to YUV422 digital video conversion.pdf
- "Let's practice together in 2021" + the busy year of 2020
- TMS320C66x study notes universal parallel port interrupt service program example
- How to Use a Network Analyzer for Cable Testing