Detection of stack overflow for HCS08 family

Showing results for 
Search instead for 
Did you mean: 

Detection of stack overflow for HCS08 family

Contributor I


I am developing code for MC9SG8S08 microcontroller from HCS08 family. I want to verify that stack is not getting overflowed. I have some of following question w.r.t. stack overflow

1)What is good verification method for that?How to detect stack is not getting overflowed?

2)If stack get  overflow will it MC9SG8S08  cpu generate reset(I couldn't find anything in datasheet about this)?Will be there any indication for that from CPU?



Prasad S Udawant

Tags (2)
0 Kudos
1 Reply

Senior Contributor IV

1. a) At design time increase stack size as much as possible. Prior to running your code fill stack space with some pattern like all 0xFF, 0x55 or smth. Run your code for a while and then check how many contiguous bytes at bottom of stack are still like you initialized them. Stack top minus the most bottom modified stack byte address gives you required stack space. During test time all your stack hungry routines and ISRs should execute at least once, else your measurement may be inaccurate.

b) inside of some periodic interrupt you can TSX (transfer stack pointer to H:X), CPHX against previous value and STHX if lower. Running code for a while should give you value close to the deepest SP. Close and not exact because you may have other ISR's with worse stack usage.

2. Not necessarily. Bad things may happen like sudden unrelated variable change or something. It is always the best to determine the worst stack usage and set stack size accordingly.



0 Kudos