Shop OBEX P1 Docs P2 Docs Learn Events
Using the Run command inside a Subroutine — Parallax Forums

Using the Run command inside a Subroutine

Bob - FremontBob - Fremont Posts: 11
edited 2006-07-07 15:35 in BASIC Stamp
Hello everyone,

I have what is a simple question to the one who can answer it.

Is it possible to issue a RUN command when inside a subroutine.· And then from the new·program slot, issue a Return command.· Will the Return command bring me back to the original program slot, or do I need to RUN to that slot and then issue the Return.

Thanks in advance for your help.
Bob H.

Comments

  • Bob - FremontBob - Fremont Posts: 11
    edited 2006-07-07 05:10
    Sorry, I'm using the BS2P40 with 2.2.5.
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-07-07 05:33
    BoB -

    You will need to use the RUN commmnad twice. Once to get over and once to get back.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • Bob - FremontBob - Fremont Posts: 11
    edited 2006-07-07 05:35
    Thanks for the reply.
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2006-07-07 06:11
    Bob, the RUN command trashes the return stack. So there is no return automatically from RUN. There are workarounds, which involve a BRANCH instruction at the top of the program slot(s), and a branch return index that is set before RUNning another slot.

    Similarly, RUN trashes FOR-NEXT counters. So a program has to use a DO-LOOP instead and manage its own loop counter.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Bob - FremontBob - Fremont Posts: 11
    edited 2006-07-07 14:35
    Tracy,

    You lost me when you mentioned using a DO_LOOP instead of a FOR-NEXT. What's the difference with regards to my original question?

    Thanks
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2006-07-07 15:18
    Hi Bob,

    The comment about DO-LOOP was tangential.

    I should amplify the answer by saying that cross-bank RUN and return is strictly a do-it-yourself affair, and your program has to manage its own stack. And if your program does that, it cannot return to to a point within a subroutine or a FOR loop and expect their RETURN or NEXT to work properly.

    A main loop might be structured as a DO : LOOP, and that is fine for a RUN. All the normal Stamp RAM and Scratchpad variables can be intact across slots.

    Here are a couple of articles about this topic...
    www.emesystems.com/BS2SX.htm#Crossbank
    www.parallax.com/dl/docs/cols/nv/vol3/col/nv87.pdf



    should be used in the main program, and not from a subroutine nor from within a FOR-NEXT loop. However, that does not matter if you have no intention of returning.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Ryan ClarkeRyan Clarke Posts: 738
    edited 2006-07-07 15:35
    Bob,

    As Tracy said, your stack is 'fresh' when you issue a RUN statement (i.e. isn't preserved across run slot jumps)- A possible work around is to manually keep track of where you need to go in a data statement, and check this location/flag each time you enter a run slot-

    The articles Tracy referenced should help you along.

    Ryan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Ryan Clarke
    Parallax Tech Support

    RClarke@Parallax.com
Sign In or Register to comment.