memory size

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

memory size

Jump to solution
1,435 Views
rahulkrishna
Contributor IV

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.

Labels (1)
Tags (2)
0 Kudos
1 Solution
895 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

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

View solution in original post

0 Kudos
5 Replies
895 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

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

0 Kudos
895 Views
rahulkrishna
Contributor IV

Can we read the SP at run time?

0 Kudos
896 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

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

0 Kudos
895 Views
rahulkrishna
Contributor IV

Thank you very much

0 Kudos
895 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

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

0 Kudos