Consequences of leaving and interrupt with GOTO

wapo54001

Senior Member
I've been reading about interrupts and the requirement to exit with a RETURN.

I'm in a situation where rather than returning to the main program where the interrupt occurred, I'd much rather return to the beginning of the main program. What happens if I use a GOTO? Or, maybe I should just ask -- is there a way to exit an interrupt to a different location in the program than where the interrupt happened?
 

Technical

Technical Support
Staff member
You'll eventually get a stack error if you do this, as interrupt pushes onto the gosub/return stack.

However 'reset' will always take you to the start of the program no matter what you were doing.
 

srnet

Senior Member
What happens if I use a GOTO?
For every time the interrupt routine is called you will loose bytes from the gosub\return stack, so eventually the PICAXE will run out of memory for the stack and crash.

This would be bad.
 

srnet

Senior Member
You'll eventually get a stack error if you do this, as interrupt pushes onto the gosub/return stack.

However 'reset' will always take you to the start of the program no matter what you were doing.
And run 0 will do much the same but keep variables intact.
 

wapo54001

Senior Member
Hmm, neither will work for me because I have ongoing tasks that can't be interrupted for as long as it would take for a reset or re-initialization. I'll have to work with using the RETURN. Thanks for the clarification.
 

geoff07

Senior Member
Perhaps you should set a flag in the ISR, and then test that frequently in the main program, using the test to send control to wherever you need it. That way you can return from the ISR without messing up the stack. If the program is a loop (most are) and you set it up right, you could use EXIT (driven by a suitable test) or a series of EXITs, to jump out of the main code and go back to the start. Much depends on what you are doing.
 

bpowell

Senior Member
You'll eventually get a stack error if you do this, as interrupt pushes onto the gosub/return stack.

However 'reset' will always take you to the start of the program no matter what you were doing.
What will the PICAXE do in a stack overflow situation? Freeze? Is the Reset on Stack Overflow bit set? Will the PICAXE just reset?
 

hippy

Technical Support
Staff member
The PICAXE return stack is a circular buffer so you just keep overwriting earlier return addresses. When you come back down that stack with RETURN it will then go to the wrong place.
 

bpowell

Senior Member
The PICAXE return stack is a circular buffer so you just keep overwriting earlier return addresses. When you come back down that stack with RETURN it will then go to the wrong place.
Thanks Hippy!

So, can the Picaxe stack be accessed / cleared with a well placed peek / poke?
 

srnet

Senior Member
Thanks Hippy!

So, can the Picaxe stack be accessed / cleared with a well placed peek / poke?
Well under the description of peeksfr\pokesfr in the manual it says;

"Only SFRs associated with peripherals (e.g. ADC or timers) may be accessed.
Peeking or poking SFRs associated with PICAXE program operation (e.g. FSR,
EEPROM or TABLE registers) will cause the PICAXE chip to immediately reset"
 

Technical

Technical Support
Staff member
There is no reason to ever mess with the gosub stack, its just a bad thing to do. As suggested above you should create flags and use them correctly instead.
 
Top