subroutines
Timmy
Posts: 10
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
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
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
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
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
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
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
Hope this helps.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sid Weaver
Do you have a Stamp Tester yet?
http://hometown.aol.com/newzed/index.html
·
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.
After My_Subroutine has finish executing, will the program return to the IF...THEN statement or after?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax