Hi all,
We can set the size of the stack in the linker file in Codewarrior S12x. Let us say the RAM is from 0x2000 to 0x2100, if the stack size is 0x100. The first variable will be stored at 0x2100 and keeps decreasing. But due to my wrong calculations the the stack exceeds beyond 0x100. There is no RAM below 0x2000 then how does the microcontroller handle this. please explain me.
Thanks in advance.
已解决! 转到解答。
hello rahul,
yes we can read SP register in run time.
I made a small sample code that measures stack usage. see attachment.
Inside the code,
- Function checkStack() stores the current SP value in a global variable and determines what was the lowest value reached by SP in an application.
- Notice the convenient access to C variables from inline-assembly(asm(STS currentStack)). We are forced to use an assembly instruction because there is no access to the CPU registers in C.
- The idea is to have every function in the application call checkStack() to measure the lowest stack address. Thus you can calculate the total stack worst case to allocate.
Have a great day,
Zhang Jun
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Hi rahul,
stack overflow error is always hard to detect.
If the stack grows into the heap/RAM, it will silently start to overwrite the data values in heap/RAM. that destroy contents in those memories.
if stack overflow overwrite some function pointer or PC pointer. the code may jump to unexpected address that we can't control.
does this answer your question?
Have a great day,
Zhang Jun
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
hello rahul,
yes we can read SP register in run time.
I made a small sample code that measures stack usage. see attachment.
Inside the code,
- Function checkStack() stores the current SP value in a global variable and determines what was the lowest value reached by SP in an application.
- Notice the convenient access to C variables from inline-assembly(asm(STS currentStack)). We are forced to use an assembly instruction because there is no access to the CPU registers in C.
- The idea is to have every function in the application call checkStack() to measure the lowest stack address. Thus you can calculate the total stack worst case to allocate.
Have a great day,
Zhang Jun
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
hello rahul,
you are welcome!
Have a great day,
Zhang Jun
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------