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?
 
Kiel compiler should warn of the eminent overflow as it will estimate the usage when it compiles.

If you have the basic 256 byte stack and you are passing parameters as if they are going out of fashion, then yes! The limit will be reached fairly quickly. However!!!! if you are using interrupts, the compiler cannot determine stack overflow. that I'm afraid is your lookout!

If you increase the stack then you'll be loosing ram.. If you are efficient with pointers, you should only need a small stack or if you use global variable rather than locals.. On an 8 bit, returning in's and long's also cause headaches. If you use functions as they should be used to pass pointers and return characters. The return should only be boolean ie. did work / didn't work...

If you use printf in your code..

returns a char but, printf( buffer," hello this is a string"); has just used 27 bytes. once you know, you can kerb your stack usage.
 

Latest threads

New Articles From Microcontroller Tips

Back
Top