Shop OBEX P1 Docs P2 Docs Learn Events
Controlling Program Segments — Parallax Forums

Controlling Program Segments

spainespaine Posts: 51
edited 2006-09-03 01:50 in BASIC Stamp
Due to program size constraints I have started moving toward multi-bank programs. From the start I had known that the RUN command could not return, but I didn't know what impact that had until now. I would may (or may not) want to return to a previous point in the prior program. I have an idea on how to do that, but I was wondering if someone else has had experience with (and a solution to) this problem?

My idea was to use an identifier variable that gets set by the second. When Slot 0 runs again, it would see that the identifier was set using IF/THEN and skip to the appropriate segment of code within Slot 0. Although this technique would work for simple progs, it might get hairy as the program becomes more involved, and even worse if I need to run from three or more slots.

Any and all advice is greatly appreciated.

Thanks,
Stephen

Comments

  • Tracy AllenTracy Allen Posts: 6,662
    edited 2006-09-02 18:30
    Here are a couple of references...
    http://www.parallax.com/dl/docs/cols/nv/vol3/col/nv87.pdf
    www.emesystems.com/BS2SX.htm#Crossbank

    Your idea with the identifier is quite workable. The first step is to modularize your program so that a minimum of crossbank stuff is necessary and so that you can make the slot structure work for you.

    I set aside one variable in RAM in all slots as the bank switching pointer, and I set aside the last byte or two in memory as return pointer(s). The pointer is broken into two nibbles. The top nibble points to a slot to RUN, and the low nibble is the index of a routine to execute upon arriving in a slot. For example, $42 would point to the second routine in slot 4. Suppose a routine running in slot 1 needs to execute routine index 2 in slot 4, and subsequently return to location index 5 in slot 1 to continue where it left off. It would PUT $15 into the return location in scratchpad, and load the pointer variable in RAM with $42, then RUN 4. In slot 4, the first command is a BRANCH that has several destinations based on the pointer.NIB0. The code jumps to that location.
    BRANCH pointer.NIB0,[noparse][[/noparse]myroutine0,myroutine2,.....,myroutine15]
    When that routine is finished, it GETs the value, $15, it finds in the scratchpad into the pointer variable and RUNs pointer.NIB1.
    GET back,pointer : RUN pointer.NIB1
    That brings it back to slot 1, where another BRANCH is set up to direct it to return location, index 5. That highly symmetric mechanism allows for the 7 slots and 16 possible destinations per slot. It allows for only one level of "CALL" and return. On rare occasions where I have needed more than that, I have resorted to using a second location in scratchpad and "pushing" down in an ad hoc manner. It is possible to set up a formal stack mechanism, but I've found that the extra code and potential for bugs far overburdens its usefulness.

    Note that cross-slot stuff all has to be carried out from within a main loop, not from within subroutines or FOR NEXT loops, because the RUN command destroys all existing stack and loop counter logic. If you need a cross-slot stuff within a loop, it has to be done with DO-LOOP logic.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • boeboyboeboy Posts: 301
    edited 2006-09-03 01:50
    here is my code from a project it is only a snipit·and bed sid program is it all

    Post Edited (boeboy) : 9/11/2006 7:59:41 PM GMT
Sign In or Register to comment.