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!
-----------------------------------------------------------------------------------------------------------------------