According to ARM Architecture Procedure Call Standard (AAPCS) on the ARMv6-M, and ARMv7-M architecture in it says:
"Although the processor hardware allows SP to be at any word aligned address at function boundaries, standard programming practice requires C program code to ensure that the SP is at a 64-bit (doubleword) aligned address."
What does it mean that the Stack pointer has to be at a 64 bit aligned address?
Hi Yasuhiko,
In Cortex-M0 and Cortex-M0+ processors, CCR.STKALIGN is fixed to 1. So it always enforce 8 byte stack alignment for exception stack frames.
When setting up the initial SP values, the values should be 8 bytes aligned.
When having function calls, the SP value at function call boundaries should be 8 byte aligned. It is okay to have 4 byte alignment in the middle of a function, as long as the SP value is adjust back to 8 bytes aligned before the function is ended or a function call is made.
If a function is coded in assembly language, and inside this function it calls another assembly function which you knew that it does not require 8 byte stack alignment, you can have 4 byte stack alignment at function call in such case. But if you are calling a C function, then 8 byte stack alignment is required, as the C compiler might make assumption of SP value in pointer handling.
Hope this helps.
regards,
Joseph
Hello Joseph Yiu,
thank you for your detailed explanation.
I have been misunderstood.
My understanding had been that the case of CCR.STKALIGN=1 made the stack pointer at interrupt or exceptions aligned to 8 byte boundary.
Best regards,
Yasuhiko Koumoto.
That's correct. CCR.STKALIGN only affect exception stack frames. In Cortex-M0, Cortex-M0+ and Cortex-M7 processors, this bit is fixed to 1 which always ensure that the exception stack frames are double word aligned.
However, software still have to implemented correctly to be AAPCS compliant.