Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

how can we detect a stack overflow

Elctro1

New Member
Hi,
I work with the 8051 microcontroller (using the Keil compiler), and I’m a bit stuck on one thing detecting stack overflow.


I know the stack is pretty limited in 8051, and if it overflows, things can get messy (corrupting SFRs and all that). But how do I actually check if a stack overflow is happening?


Are there any built-in tools in Keil for this? Or maybe some clever trick or best practice to monitor stack usage at runtime?
 
The problem is overhead. There is no hardware mechanism for detecting this condition. For every stack access, you need to check that the SP (Stack Pointer) register is within limits before and after the operation. This will also detect underflow which is equally undesirable. I've spent half a century on embedded systems development and never found a need to do this on the 8051 and its derivatives as well as many other devices.

No compiler that I ever used for developing 8051 code was capable of knowing how to do this check at compile time. I have seen other compilers that check for recursive function calls, but you rarely see them in embedded applications. Analyzing data operations on the stack is more difficult and again not a common feature in 8051 compilers or applications.
 
Last edited:
Some ref material, not 8051 centric



Compiler based detection -



8051 centric -



Regards, Dana.
 
Hi,
I work with the 8051 microcontroller (using the Keil compiler), and I’m a bit stuck on one thing detecting stack overflow.


I know the stack is pretty limited in 8051, and if it overflows, things can get messy (corrupting SFRs and all that). But how do I actually check if a stack overflow is happening?


Are there any built-in tools in Keil for this? Or maybe some clever trick or best practice to monitor stack usage at runtime?
Did you try to set a maximum stack size in Keil?
 

Latest threads

New Articles From Microcontroller Tips

Back
Top