Understanding and summary of stack pointer SP settings in C language and ARM

Publisher:创意探险Latest update time:2020-04-06 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Reference address:Understanding and summary of stack pointer SP settings in C language and ARM

Previous article:UART protocol introduction and programming
Next article:Common Linux commands and some skills usage of ARM development board

Recommended ReadingLatest update time:2024-11-15 08:10

ARM byte format
[Microcontroller]
ARM byte format
Architecture comparison between MIPS and ARM
In the embedded field, MIPS K series microprocessors are currently one of the most used processors after ARM (before 1999, MIPS was the most used processor in the world), and its application areas cover game consoles, routers, laser printers, PDAs, etc. The pipeline structure MIPS is one of the simplest architectures
[Microcontroller]
ARM CPU big and small endian
ARM CPU big and small endian: Big-endian mode: low-order bytes are stored at high addresses, and high-order bytes are stored at low addresses.  Little-endian mode: high-order bytes are stored at high addresses, and low-order bytes are stored at low addresses.   STM32 belongs to the little-endian mode. Simply put, fo
[Microcontroller]
ARM CPU big and small endian
Methods to minimize power consumption of ARM Cortex-M CPUs
1Understanding Thumb-2 First, let's start discussing energy-saving technologies from a seemingly non-obvious starting point - the instruction set. All Cortex-M CPUs use the Thumb-2 instruction set, which combines the 32-bit ARM instruction set and the 16-bit Thumb instruction set, and provides a flexible solution for
[Microcontroller]
Methods to minimize power consumption of ARM Cortex-M CPUs
【ARM】ARM interface technology
#0Use of development tools ##0.1Install keil4 Find mdk411.rar in the compressed file of arm data 1, unzip it, and double-click MDK411.exe to install it ##0.2 Run MDK411.EXE to install ##0.3 Select the destination location for installation Click NEXT, the installation interface is as follows ##0.4 Installatio
[Microcontroller]
【ARM】ARM interface technology
Design of campus LED bulletin board system based on ARM
  In recent years, LED  electronic display screens have attracted increasing attention as a high-tech product. They can display text, graphics and image information in real time or in a loop. They have many advantages, such as rich display modes, strong viewing, convenient display content modification, high brightne
[Power Management]
Design of campus LED bulletin board system based on ARM
Arm Linux Kernel build scenario analysis
Overview To build a kernel, you usually configure it first and then compile it. Here we take the Nexus5 kernel, codenamed hammerhead, as an example. Configuration The usual practice is to use the manufacturer's preset configuration as a basis and configure according to your needs. Command: make AR
[Microcontroller]
Arm Linux Kernel build scenario analysis
ARM study notes three (arm instruction set)
arm instruction set Jump instruction to implement process jump Special jump instruction b Jump instruction Format: B{condition} target address The sample code is as follows: When the z condition code in the cpsr register is set, the program jumps to the label label to execute CMP R1,#0
[Microcontroller]
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号