Shop OBEX P1 Docs P2 Docs Learn Events
Question about GOSUB/RETURN in multi page program on BS2e — Parallax Forums

Question about GOSUB/RETURN in multi page program on BS2e

JonmarJonmar Posts: 9
edited 2008-09-26 15:16 in BASIC Stamp
Greetings all,

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,
Jonmar

Comments

  • GaudwareGaudware Posts: 18
    edited 2008-09-25 19:08
    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

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Diamonddredges.com
    Gaudware.net84.net
  • Mike GreenMike Green Posts: 23,101
    edited 2008-09-25 19:27
    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
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2008-09-25 19:32
    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.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • SRLMSRLM Posts: 5,045
    edited 2008-09-25 20:04
    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" in the completed projects forum.
  • Jesse HastyJesse Hasty Posts: 29
    edited 2008-09-26 01:04
    Any run is sort of like starting all over again. I'm assuming your return statement has nowhere to go and thus causes problems.
  • JonmarJonmar Posts: 9
    edited 2008-09-26 14:18
    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.

    Jonmar

    P. S. I will let everyone know how my efforts turn out when I finish the program. Hopefully next week.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2008-09-26 15:16
    You can also refer to this Nuts and Volts article:
    www.parallax.com/dl/docs/cols/nv/vol3/col/nv87.pdf

    and I have a writeup at www.emesys.com/BS2SX.htm#Crossbank.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
Sign In or Register to comment.