PDA

View Full Version : jumping out of subroutine with goto



helloo
- 31st December 2011, 06:10
Hey @ all,
I wonder what happens within the µC when I write code that jumps out of a subroutine with the goto command?

Example:



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

spcw1234
- 2nd January 2012, 01:36
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.

Normnet
- 2nd January 2012, 12:02
Hey @ all,
I wonder what happens within the µC when I write code that jumps out of a subroutine with the goto command?

Example:



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:


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

rsocor01
- 2nd January 2012, 12:30
Hey @ all,
I wonder what happens within the µC when I write code that jumps out of a subroutine with the goto command?

Example:



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,




main:
GOSUB label
goto main

label:
GOSUB somwhereelse
RETURN

somwhereelse:
DOSOMETHING HERE
RETURN


Use GOSUB and RETURN instead of GOTO.

Robert

helloo
- 3rd January 2012, 10:37
Thanks for all. That is a solution to the problem. Thank you.