Discussion on 8-byte stack alignment

Publisher:TechGuru123Latest update time:2017-01-03 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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

Reference address:Discussion on 8-byte stack alignment

Previous article:Initial application of clock
Next article:MSP430 Functional Module Detailed Explanation Series - TimerA

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号