View Full Version : Question about GOSUB/RETURN in multi page program on BS2e
09-26-2008, 03:00 AM
If I have a FOR/NEXT loop running and GOSUB out to do some thing that has a jump, via a RUN command to another program page;
when my program comes back to the originating page,
will a RETURN instruction Return to the next instruction line after the original GOSUB?
Thanks in advance,
09-26-2008, 03:08 AM
Yes, It will return to the next line after the gosub as if the code was manually injected in the line that says gosub
hope this helps
09-26-2008, 03:27 AM
I don't think so. As far as I've been able to tell from the documentation:
When you do a RUN any return addresses are lost. The only thing that can be passed to another bank is the variables (and the Scratchpad RAM).
Post Edited (Mike Green) : 9/25/2008 7:34:55 PM GMT
09-26-2008, 03:32 AM
I think destroys both the FOR-NEXT loop counter and also the GOSUB pointers.
If you want to return to a certain point after jumping to another slot with RUN, you will have to do so from the MAIN program level. Your own PBASIC program will have to manage the pointer of where to jump to in the other slot and where to jump to upon return. And if it is part of a looping structure, you have to use DO:LOOP and manage your own loop counter, not FOR:NEXT.
I ran into this sort of problem with my bot and BS2e. I had "subroutines" that I wnated, but they were big stuff. So... I had to make my own form of GOSUB. Basically, at that it does is store a "returnTo" value in the SCRAM, run the next program, and that program looks at that value and decides which slot to return to. Once in that slot a series of If statements allows control to pass back to the calling point. It's not quite as elegant as a real GOSUB command, but it works. See "Swiftsure BS2 Robot (http://forums.parallax.com/showthread.php?p=741023)" in the completed projects forum.
09-26-2008, 09:04 AM
Any run is sort of like starting all over again. I'm assuming your return statement has nowhere to go and thus causes problems.
09-26-2008, 10:18 PM
I have not yet tried to run my program because it is not yet finished.
So, I do not know if there is a problem yet with the GOSUB's return.
Based on the feedback so far, I am redoing the code to implement my own Save the world and restore it using the SPram space.
Thanks to everyone who responded to my question.
P. S. I will let everyone know how my efforts turn out when I finish the program. Hopefully next week.
09-26-2008, 11:16 PM
You can also refer to this Nuts and Volts article:
and I have a writeup at www.emesys.com/BS2SX.htm#Crossbank (http://www.emesys.com/BS2SX.htm#Crossbank).