On Tue, 2 Sep 1997 03:50:13 +0000 Mike Smith writes: >On 1 Sep 97 at 13:55, Steve Smith wrote: > >> I have a program which needs to return to stack level 0 from >> different stack levels normally 2 to 5. Is there any way of purging >> the stack or is it not necessary ? When in simulation MPLAB gives >> stack overflow after a couple of itterations. The timer 0 and a >> counter is used to recover to the main program loop if there has >> been no user input for 30 seconds. The object was to stop user >> leaving the module in a state where other inputs are ignored while >> user sets up opperational parameters. This however exits to 'MAIN' >> via a goto not a through clearing the stack properly. An exit to >> 'DIRTY_EXIT' could be made then goto 'MAIN' but how do I clear the >> stack from an unknown level (2-5) ? It is OK to go back to main and abandon the addresses on the stack. MPLAB-SIM has an option to turn off the halt on stack overflow if your program is intended to work this way. If (when in your case) the real (or simulated, I think) PIC stack overflows it will roll over cleanly. The stack pointers work mod-8 on a dedicated stack register, so unlike many processors that implement a stack of indefinite size in general-purpose RAM, the stack cannot expand out of control and cause a crash. There isn't a way to purge the stack (a 'pop' or no-return RETURN). So if the program corrupts the stack this way it must go back to the topmost level of program and start calling anew. Interrupts will still work though. Reagrdless of anything done previous, the stack always has room to handle 8 calls or interrupts and returns.