1. Why do we need to ensure that the stack is 8-byte aligned?
The AAPCS rule requires that the stack be 8-byte aligned. If it is not aligned, calling general functions is fine. However, errors may occur when calling functions that strictly follow the AAPCS rule.
For example, when calling sprintf to output a floating point number, the stack must be 8-byte aligned, otherwise the result may be wrong.
Experimental verification:
#include "stdio.h"
#include "string.h"
float fff=1.234;
char buf[128];
int main(void)
{
sprintf(buf,"%.3f\n\r",fff);//A
while(1);
}
1. Set a breakpoint at A and let the program run at full speed to A
2. Modify the value of MSP in MDK so that MSP satisfies 8-byte alignment
3. Run the program at full speed and observe that the character in buf is 1.234. The result is correct
4. Go back to step 2 and modify MSP so that it only satisfies 4-byte alignment but not 8-byte alignment
5. Run the program at full speed and observe that the character in buf is -2.000. The result is wrong.
This experiment proves that when calling sprintf to output a floating point number, the stack must be aligned by 8 bytes.
2. What does the compiler do for us?
Let's take a look at an experiment first
#include "stdio.h"
#include "string.h"
float fff=1.234;
char buf[128];
void fun(int a,int b,int c,int d)
{
int v;
v=v;
}
void test(void)
{}
int main(void)
{
fun(1,2,3,4);
test();//A
//sprintf(buf,"%.3f\n\r",fff);
while(1);
}
0. Ensure that the stack is 8-byte aligned at the beginning
1. Set a breakpoint at A
2. Run at full speed to A and observe that MSP=0x2000025c is not 8-byte aligned
3. Slightly modify the main function code as follows, and the other parts of the code remain unchanged
int main(void)
{
fun(1,2,3,4);
//test();
sprintf(buf,"%.3f\n\r",fff);//A
while(1);
}
4. Also set a breakpoint at A
5. Run at full speed to A, and observe that MSP=0x200002d8, this time it is 8-byte aligned
This experiment shows that if the compiler finds that a function needs to call the floating-point library, it will automatically adjust the compiled assembly
code to ensure that the stack is 8-byte aligned when calling these floating-point library functions. In other words, if we ensure that the stack is 8-byte
aligned at the beginning, then the compiler can ensure that the stack is still 8-byte aligned when calling the floating-point library in the future.
3. How to set the task stack under os
From the above discussion, it can be seen that when allocating a stack to a task, it is necessary to ensure that the stack is 8-byte aligned, otherwise any function that calls sprintf in the task
will fail because the stack is not aligned at the beginning.
4. Stack alignment problem in interrupts
Is it guaranteed that everything will be fine if the stack is initially 8-byte aligned? No! Please look at a special case:
#include "stdio.h"
#include "string.h"
float fff=1.234;
char buf[128];
void fun(int a,int b,int c,int d)
{
int v;
v=v;
}
int main(void)
{
fun(1,2,3,4);
while(1);
}
void SVC_Handler(void)
{
sprintf(buf,"%.3f\n\r",fff);//B
}
The disassembly of the main function is as follows:
0x080001DC B500 PUSH {lr}
0x080001DE 2304 MOVS r3,#0x04 ;A
0x080001E0 2203 MOVS r2,#0x03
0x080001E2 2102 MOVS r1,#0x02
0x080001E4 2001 MOVS r0,#0x01
0x080001E6 F7FFFFF5 BL.W fun (0x080001D4)
0x080001EA BF00 NOP
0x080001EC E7FE B 0x080001EC
0. Ensure that the stack is 8-byte aligned at the beginning
1. Set a breakpoint at A
2. Run at full speed to A, and observe that MSP=0x200002e4 is not aligned
3. Set the suspend position of SVC to 1 in MDK
4. Set a breakpoint at B
5. Run at full speed to B, and observe that MSP=0x200002b4 is not aligned
6. Continue to execute at full speed, and observe that the character in buf is: -2.000 This experiment
shows that even if the stack is initially aligned to 8 bytes, the compiler can only ensure that the stack is aligned to 8 bytes when sprintf is called.
But it cannot guarantee that the stack is 8-byte aligned at any time. If an interrupt happens to occur when the MSP is not 8-byte aligned, and
sprintf is called in the interrupt, an error will still occur in this case .
What does the Cortex-M3 core do for us?
The Cortex-M3 core provides a hardware mechanism to solve the problem of stack misalignment in the above interrupt.
In CM3, the STKALIGN of the NVIC configuration control register can be set to ensure that the stack in the interrupt is 8-byte aligned.
The specific implementation process is as follows:
When an interrupt occurs, the hardware automatically detects whether the MSP is 8-byte aligned. If it is aligned, no operation is performed.
If it is not aligned, the MSP is automatically decremented by 4 to align it. At the same time, the 9th bit of xPSR is set to record
the abnormal change of the MSP. When the interrupt returns, if the 9th bit of xPSR is set, the MSP is automatically adjusted
back to the original value by adding 4.
Experimental verification:
#include "stdio.h"
#include "string.h"
float fff=1.234;
char buf[128];
void fun(int a,int b,int c,int d)
{
int v;
v=v;
}
int main(void)
{
fun(1,2,3,4);
while(1);
}
void SVC_Handler(void)
{
sprintf(buf,"%.3f\n\r",fff);//B
}
The disassembly of the main function is as follows:
0x080001DC B500 PUSH {lr}
0x080001DE 2304 MOVS r3,#0x04 ;A
0x080001E0 2203 MOVS r2,#0x03
0x080001E2 2102 MOVS r1,#0x02
0x080001E4 2001 MOVS r0,#0x01
0x080001E6 F7FFFFF5 BL.W fun (0x080001D4)
0x080001EA BF00 NOP
0x080001EC E7FE B 0x080001EC
1. Set a breakpoint at A
2. Run at full speed to A, and observe that MSP=0x200002e4 is not aligned
3. In MDK, set the suspend position of SVC to 1, and change the value at 0xE000ED14 from 0x00000000 to 0x00000200
(that is, set the STKALIGN of the NVIC configuration control register)
4. Set a breakpoint at B
5. Run at full speed to B, and observe that MSP=0x200002b0 Aligned
6. Observe that MSP=0x200002e4 when the interrupt returns. Adjusted back
7. Continue to execute at full speed, and observe that the characters in buf are: 1.234 Correct
This experiment shows that setting STKALIGN of the NVIC configuration control register can protect the stack from being 8-byte aligned during interrupts
. 6. Summary
To sum up, in order to safely use functions that strictly comply with AAPCS rules (such as sprintf), the following points need to be done:
1. Ensure that MSP is 8-byte aligned at the beginning
2. If OS is used, it is necessary to ensure that the stack allocated to each task is 8-byte aligned
3. If a processor based on the CM3 core is used, the STKALIGN of the NVIC configuration control register needs to be set
Previous article:Initial application of clock
Next article:MSP430 Functional Module Detailed Explanation Series - TimerA
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- New breakthrough! Ultra-fast memory accelerates Intel Xeon 6-core processors
- New breakthrough! Ultra-fast memory accelerates Intel Xeon 6-core processors
- Consolidating vRAN sites onto a single server helps operators reduce total cost of ownership
- Consolidating vRAN sites onto a single server helps operators reduce total cost of ownership
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- Common Problems in PCB Production and Design
- Using PWM technology to build a high performance flow transmitter
- pyboard receives serial port data conversion problem
- Notepad Editor - Verilog Code Snippets and Syntax Checker
- [ESK32-360 Review] + Getting to know the ESK32-360 development board
- TI-83 Premium CE Python Edition Calculator
- Improved support for CAN in the STM32 branch of mpy
- The secret of the longevity of the wireless earbud charging case is that it consumes only 1.6% of power per month
- NMOS application solution!
- Keil common errors/warnings and solutions