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.
Today I ran into my very first stack overflow bug. It was a frustrating experience to find the actual bug as I had to go through all of my code and double-check the logic, buffers, pretty much my entire code before I was finally able to detect that the bug I got was due to ... Read more
In this comprehensive guide, we'll dive deep into the intricacies of 8051 stack operations, exploring how to utilize the microcontroller to its maximum potential.
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?