jumping out of subroutine with goto
Hey @ all,
I wonder what happens within the µC when I write code that jumps out of a subroutine with the goto command?
Example:
Code:
main:
GOSUB label
goto main
label:
GOTO somwhereelse
RETURN
somwhereelse:
DOSOMETHING HERE
GOTO main
I know from other BASIC dialects that one should'nt do this, because it messes up the pointers. But what happens in PBP eg. the PIC itself?
Is there a better solution for this problem?
Greetings, helloo
Re: jumping out of subroutine with goto
I recently had a project I was working on that would work fine for a while, then reboot and work fine for a while, then completely lock up. After a power cycle this would repeat. The project had buttons, and I was polling the buttons in a subroutine, if a button press was detected I jumped somewhere else using a goto. I soon realized there was a pattern and it was a certain number of button presses, I forget exactly how many. I moved my button polling routine to the main loop instead of a subroutine and it fixed the problem. I assume a stack was overflowing causing the problems because of using a goto inside of a subroutine.
Re: jumping out of subroutine with goto
Quote:
Originally Posted by
helloo
Hey @ all,
I wonder what happens within the µC when I write code that jumps out of a subroutine with the goto command?
Example:
Code:
main:
GOSUB label
goto main
label:
GOTO somwhereelse
RETURN
somwhereelse:
DOSOMETHING HERE
GOTO main
I know from other BASIC dialects that one should'nt do this, because it messes up the pointers. But what happens in PBP eg. the PIC itself?
Is there a better solution for this problem?
Greetings, helloo
Try:
Code:
main:
GOSUB label
IF bit1 = 1 then
GOTO somwhereelse
Endif
goto main
label:
'GOTO somwhereelse
F x > 3 then
bit1 = 1 'set a flag
Endif
RETURN
...
RETURN
somwhereelse:
DOSOMETHING HERE
GOTO main
Norm
Re: jumping out of subroutine with goto
Quote:
Originally Posted by
helloo
Hey @ all,
I wonder what happens within the µC when I write code that jumps out of a subroutine with the goto command?
Example:
Code:
main:
GOSUB label
goto main
label:
GOTO somwhereelse
RETURN
somwhereelse:
DOSOMETHING HERE
GOTO main
I know from other BASIC dialects that one should'nt do this, because it messes up the pointers. But what happens in PBP eg. the PIC itself?
Is there a better solution for this problem?
Greetings, helloo
Or, you can try this,
Code:
main:
GOSUB label
goto main
label:
GOSUB somwhereelse
RETURN
somwhereelse:
DOSOMETHING HERE
RETURN
Use GOSUB and RETURN instead of GOTO.
Robert
Re: jumping out of subroutine with goto
Thanks for all. That is a solution to the problem. Thank you.