Shop OBEX P1 Docs P2 Docs Learn Events
Subroutines that go to other bs2sx pages — Parallax Forums

Subroutines that go to other bs2sx pages

ArchiverArchiver Posts: 46,084
edited 2003-06-23 19:00 in General Discussion
If I have a subroutine on memory page 1 that runs a second page that
then comes back to the first page with some flag that puts execution
right back into the subroutine, will the subroutine return bring me back
to the right place on page 1?


E.g., let's say page 1 looks like

Return_pointer var nib

If return_pointer =3 then rp100

Loopstart:
command...
command...
gosub memory_routine
command...
Goto loopstart

Memory_routine:
Command
Command
Return_pointer = 3
Run 2
Rp100:
Return_pointer = 0
return

Page 2 does something and then runs page 1.


I guess I'm really wondering if transferring program control out of the
subroutine and to another page and then coming back into the subroutine
will confuse its stack. Or, hopefully, in the above example will
control leave the subroutine memory_routine at the "return" command and
go to the command right after the "gosub memory_routine" command?

Thanks

bob

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2003-06-23 10:37
    As far as I know the RUN command does not alter RAM (except for a
    certain scratchpad RAM location which contains the program slot
    number of the currently running program) and thus compromise the
    GOSUB/RETURN stack. After all it should - to my opinion - be ok to
    run a program from da different memory slot from within a subroutine.

    Regards
    Adrian
  • ArchiverArchiver Posts: 46,084
    edited 2003-06-23 11:50
    Sorry Adrian but I believe the GOSUB/RETURN markers are lost when you switch
    to another program however the rest of the RAM is as you described it.

    You can however work around this, as shown by an example in Scott Edwards
    book;

    This is the program in slot 0

    ' PROGRAM: example3_pgm0
    '{$STAMP BS2SX, Example3_pgm1}
    toLabel VAR Nib
    retLabel VAR Nib
    msg1_ CON 1
    msg2_ CON 2
    msg3_ CON 3
    r1_ CON 1
    r2_ CON 2
    r3_ CON 3

    PAUSE 1000
    BRANCH retLabel,[noparse][[/noparse]start,r1,r2,r3]

    start:

    TOLABel= msg3_ ' "Gosub" message 3
    RETLabel= r1_ ' ..then "Return" to r1
    RUN 1 ' Do it (hop to program 1).

    R1:
    TOLabel= msg2_ ' "Gosub" message 3
    retLabel= r2_ ' ..then "Return" to r1
    RUN 1 ' Do it (hop to program 1).

    R2:
    toLabel= msg1_ ' "Gosub" message 3
    retLabel= r3_ ' ..then "Return" to r1
    RUN 1 ' Do it (hop to program 1).

    r3:
    DEBUG CR,"Done."

    This is the program in slot 1

    ' PROGRAM: example3_pgm1
    toLabel VAR Nib
    retLabel VAR Nib
    msg1_ CON 1
    msg2_ CON 2
    msg3_ CON 3
    r1_ CON 1
    r2_ CON 2
    r3_ CON 3

    BRANCH toLabel,[noparse][[/noparse]null,msg1,msg2,msg3]

    null:
    RUN 0

    msg1:
    DEBUG "Moe",CR
    RUN 0

    msg2:
    DEBUG "Larry",CR
    RUN 0

    msg3:
    DEBUG "Curly",CR
    RUN 0

    I havent actually run it, but it's straight from the book.

    Regards

    Rohan


    Original Message
    From: Adrian Schneider [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=oogaB3V8bB8WA5Fkd4j1-OwK_XVkhP9o_kaEJ6V_v9X1pZlI47DJx-S8Vdi7P30-px1mjhlauADKctA6OyxW16JRWRnmGv4]adrian.schneider@t...[/url
    Sent: Monday, 23 June 2003 7:38 PM
    To: basicstamps@yahoogroups.com
    Subject: [noparse][[/noparse]basicstamps] Re: Subroutines that go to other bs2sx pages


    As far as I know the RUN command does not alter RAM (except for a
    certain scratchpad RAM location which contains the program slot
    number of the currently running program) and thus compromise the
    GOSUB/RETURN stack. After all it should - to my opinion - be ok to
    run a program from da different memory slot from within a subroutine.

    Regards
    Adrian


    To UNSUBSCRIBE, just send mail to:
    basicstamps-unsubscribe@yahoogroups.com
    from the same email address that you subscribed. Text in the Subject and
    Body of the message will be ignored.


    Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
  • ArchiverArchiver Posts: 46,084
    edited 2003-06-23 13:58
    You are absolutely right. It is explicitly stated here:

    http://www.emesystems.com/BS2SX.htm#Crossbank

    always a good source for all kinds of stamp questions that never
    should be missed.

    Regards
    Adrian
  • ArchiverArchiver Posts: 46,084
    edited 2003-06-23 19:00
    No, you have to do all crossbank calls from the top level. The RUN
    command clears the return stack. Also it clears the for-next logic,
    so you can't do crossbank calls from within a for-next loop. I
    haven't tried it yet with most of the new PBASIC 2.5 control
    structures, like DO xxx ...LOOP. It depends on how those were
    implemented in the basic PBASIC.

    I would recommend sticking to a simple branch structure at the top
    program level. That does not look hard at all in your example
    program. If you have to enter that routine from several places in
    your main program, then you can put a crossbank return label at each
    of the possible return points in the main program,preset that return
    label in a variable and GOTO your Memory_routine.



    -- Tracy


    >If I have a subroutine on memory page 1 that runs a second page that
    >then comes back to the first page with some flag that puts execution
    >right back into the subroutine, will the subroutine return bring me back
    >to the right place on page 1?
    >
    >
    >E.g., let's say page 1 looks like
    >
    >Return_pointer var nib
    >
    >If return_pointer =3 then rp100
    >
    >Loopstart:
    > command...
    > command...
    > gosub memory_routine
    > command...
    >Goto loopstart
    >
    >Memory_routine:
    >Command
    >Command
    >Return_pointer = 3
    >Run 2
    >Rp100:
    >Return_pointer = 0
    >return
    >
    >Page 2 does something and then runs page 1.
    >
    >
    >I guess I'm really wondering if transferring program control out of the
    >subroutine and to another page and then coming back into the subroutine
    >will confuse its stack. Or, hopefully, in the above example will
    >control leave the subroutine memory_routine at the "return" command and
    >go to the command right after the "gosub memory_routine" command?
    >
    >Thanks
    >
    >bob
Sign In or Register to comment.