Shop OBEX P1 Docs P2 Docs Learn Events
subroutines — Parallax Forums

subroutines

TimmyTimmy Posts: 10
edited 2005-09-22 18:49 in BASIC Stamp
g'day everyone,

just a question regarding the GOSUB command, how many subroutines can you have in a program?

I was reading in Scott Edwards' "Programming and customising the basic stamp", in the BS2 language reference at the back that, quote, "Gosub: store the location of the next instruction and go to a specified program address. May be nested four deep." can anyone explain "four deep"??? Does this refer to having a maximum of four subroutines in the code or four subroutines clumped conseculatively??

Cheers Tim

Comments

  • John R.John R. Posts: 1,376
    edited 2005-09-20 11:55
    Tim;

    It depends on what you mean by "clumped".

    Nesting refers to a given element (such as GOSUB, or IF/THEN/ELSE, etc.) being called from within another of the same element. for example, in the pseudo code below, Sub2 is nested in Sub1, but Sub3 is not nested. Another way of saying this is that Sub2 is called from within Sub1.

    Main:
    [noparse][[/noparse]Code Here]...
    GOSUB Sub1
    GOSUB Sub3

    Sub1:
    [noparse][[/noparse]Code Here]...
    GOSUB Sub2
    Return

    Sub2:
    [noparse][[/noparse]Code Here]...
    Return

    Sub3:
    [noparse][[/noparse]Code Here]...
    Return

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    John R.

    8 + 8 = 10
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2005-09-20 12:01
    G'day Tim -

    The following comes directly from the PBASIC Help file, which is a great source of information:

    "Only a limited number of GOSUBs are allowed per program (as shown in above), but they may be nested only four levels deep. In other words, the subroutine that’s the destination of a GOSUB can contain a GOSUB to another subroutine, and so on, to a maximum depth (total number of GOSUBs before the first RETURN) of four. Any deeper, and the program will "forget" its way back to the starting point (the instruction following the very first GOSUB)."

    In other words the following is okay, but no more than this:

    Main:

    .....
    GOSUB routine1
    .....

    GOTO Main:

    Routine1: 'Depth = 1
    ...
    GOSUB Routine2
    RETURN

    Routine2: 'Depth = 2
    ...
    GOSUB Routine3
    RETURN

    Routine3: 'Depth = 3
    ...
    GOSUB Routine4
    RETURN

    Routine4: Depth = 4
    ...
    RETURN

    END

    I hope that's understandable and not more confusing.

    Regards,

    Bruce Bates
  • TimmyTimmy Posts: 10
    edited 2005-09-20 12:17
    thanks folks,

    I understand your description of "nesting".

    On another question still regarding the gosub, can RETURN be used with the IF THEN set of commnds, ie

    main:
    x var byte
    x = 99
    IF x < 100 THEN dosomething

    {more mystical code here}

    GOTO main

    dosomething:
    HIGH 5
    LOW 4
    return '<<<< will this work????

    I ask because in my project i want to use the "dosomething" routine for several different parts of the code and i can't end the "dosomething" routine with a GOTO command back to just after the IF THEN set as it will be differnt.

    i understand if you find this explanation hard to understand.

    Tim
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2005-09-20 12:28
    Tim -

    You either need to reverse your logic, or use the newer forms of IF ... THEN ...ELSE available in PBASIC 2.5. This is where the Scott Edwards book is out of date, and isn't helpful.

    Reversed Logic example -

    IF x >= 100 THEN BYPASS

    GOSUB dosomething

    BYPASS:

    {more mystical code here}

    GOTO main

    dosomething:
    HIGH 5
    LOW 4
    return '<<<< will this work????

    Now it will work. IF ... THEN address has an implied GOTO before the address.

    For the PBASIC 2.5 method, take a look at the PBASIC Help file for full examples.

    Regards,

    Bruce Bates
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-09-20 12:38
    If you're going to call a subroutine from a single line IF-THEN, you need to do it like this:

    IF (condition) THEN GOSUB My_Subroutine

    If you don't insert the GOSUB, the jump is treated like GOTO and the RETURN in the subroutine will not point the code back to the right place.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • SN96SN96 Posts: 318
    edited 2005-09-20 12:40
    No, that will not work, RETURN must be used with GOSUB, just like IF and ENDIF work in pairs. You can use GOSUB without a RETURN statement, but what will happen is your code will continue on after the jump and not RETURN to where the jump was made.

    Hope this helps.
  • NewzedNewzed Posts: 2,503
    edited 2005-09-20 12:48
    Timmy, to answer your original question, you can have 256 GOSUBS in a program using a BS2 or higher, but they can be nested only four deep.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    Do you have a Stamp Tester yet?
    http://hometown.aol.com/newzed/index.html

    ·
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-09-20 12:50
    To summarize:

    Your code can have up to 256 different subroutine labels -- thus, "256 subroutines". This is because of how the tokenizer represents the subroutine destination address in the code.

    But, you can only CALL (GOSUB) those routines 4-deep. With only 2K of program storage space (about 1500 lines of code) and only 26 bytes of RAM, this should not be too much of a problem.

    "4-deep" means main can call A, A can call B, B can call C, and C can call D. If D tries to call anybody, the return address from A to main will be lost.

    And the new 2.5 PBasic does have an "IF ... THEN GOSUB <SubroutineName>" construct.
    The default construct "IF ... THEN <GotoDestinationName>" does have an implied GOTO, not an implied GOSUB.
  • civstercivster Posts: 17
    edited 2005-09-22 18:13
    Question regarding Jon's statement: "IF (condition) THEN GOSUB My_Subroutine".

    After My_Subroutine has finish executing, will the program return to the IF...THEN statement or after?
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-09-22 18:49
    The program will return to the line that follows IF-THEN.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
Sign In or Register to comment.