How Do I Know If the Hardware Stack Has Overflowed?

An 8-bit PIC device has a limited hardware stack that is used only for function (and interrupt function) return addresses (see Stacks). If the nesting of function calls and interrupts is too deep, the stack will overflow (wraps around and overwrites previous entries). Code will then fail at a later point – sometimes much later in the call sequence – when it accesses the corrupted return address.

The compiler attempts to track stack depth and, when required, swap to a method of calling that does not need the hardware stack (PIC10/12/16 devices only). You have some degree of control over what happens when the stack depth has apparently overflowed, see Stackcall Option for information on the -mstackcall option.

A call graph shows the call hierarchy and depth that the compiler has determined. This graph is shown in the assembly list file. To understand the information in this graph, see Call Graph.

Since the runtime behavior of the program cannot be determined by the compiler, it can only assume the worst case and can report that overflow is possible even though it is not. However, no overflow should go undetected if the program is written entirely in C. Assembly code that uses the stack is not considered by the compiler and this must be taken into account.