Code after BRANCH doesn't run?
Clock Loop
Posts: 2,069
In the code below, the BOLD items are not being ran. The item in Italic is where the BRANCH command seems to be returning to.
In the IDE manual there is nothing under BRANCH that talks about it skipping the code after branch.
In the IDE manual there is nothing under BRANCH that talks about it skipping the code after branch.
' {$STAMP BS2} ' {$PBASIC 2.5} look VAR Word down VAR Word Main: DEBUG "made it to Main", CR look = 0 DO GOSUB Subone DEBUG "return from subone", CR PAUSE 200 LOOP END '----------SUBS---------------------- Subone: DEBUG "made it to Subone", CR PAUSE 200 GOSUB Subtwo '[i]BRANCH returns here why??????[/i] DEBUG "return from Subtwo", CR PAUSE 200 RETURN Subtwo: DEBUG "made it to Subtwo", CR PAUSE 200 LOOKDOWN look, [noparse][[/noparse]0], down BRANCH down, [noparse][[/noparse]Subthree] [b]DEBUG "return from branch, aka subthree", CR[/b] [b]PAUSE 200[/b] RETURN Subthree: DEBUG "made it to Subthree", CR PAUSE 200 RETURN
Comments
It would be quite helpful to know exactly what was printed out by DEBUG when you ran the program.
I hope you're not mixing up three somewhat similar commands:
1. Branch Offset, [noparse][[/noparse]Address1, Address2, ...AddressN]
2. On Offset GOSUB Address1, Address2, ...AddressN
3. On Offset GOTO Address1, Address2, ...AddressN
ONLY in Example 2 is a GOSUB type branch done, and where a RETURN statement will be necessary.
In Examples 1 and 3 only a GOTO type branch is executed, and program flow will be linear.
Said differently, the BRANCH comand has an implied GOTO, not an implied GOSUB.
The rather liberal use of RETURN statements in your program has caused me to consider this possibility of such a confusion. Perhaps I'm just a bit confused by the rather circuitous logic.
The following example and excerpt from your program is just for reference below:
Example: LOOKDOWN Target, {ComparisonOp} [noparse][[/noparse]Value0, Value1, ...ValueN], Variable
Your code: LOOKDOWN look, [noparse][[/noparse]0], down
I see nothing that says that Target of LOOKDOWN can't be a word in length, but the Help File does make the comment "Target is usually a byte". Since you offered no comparison op, EQUAL will be assumed. Both, just FWIW.
Lastly, and more of a matter of information than anything else, since you never initialized your variable DOWN, it will be impossible for you to detect if LOOKDOWN resulted in a not found condition after it executes. I have no way of knowing if that's of importance to you.
If it IS of importance, here is what's usually done:
You initialize DOWN to some known initial value, then you can check for that value after the execution of LOOKDOWN to see if it met with success or not. SUCCESS is defined as DOWN being anything OTHER THAN the INITIAL VALUE. FAILURE is when you find that DOWN HAS the INITIAL VALUE contined within. SUCESS and FAILURE also very much depend on the particular comparison op ("=", "<", ">", etc) that is used.
Regards,
Bruce Bates
made it to Main
made it to Subone
made it to Subtwo
made it to Subthree
return from Subtwo
return from subone
made it to Subone
And repeats forever.
Heres what it should look like.
made it to Main
made it to Subone
made it to Subtwo
made it to Subthree
return from Subthree
return from Subtwo
return from subone
made it to Subone
In the IDE help the description of BRANCH it says this:
SYNTAX:
BRANCH Offset, [noparse][[/noparse]Address1, Address2, ...AddressN]
As far as the help file, I am doing it correctly.
I set the offset using my variable "down", and "down" then points to "Subthree" Because the variable down = 0.
If down didn't = 0 then BRANCH would infact not do anything, and probably run the code after BRANCH.
I thought BRANCH is the same thing as GOSUB??? Except branch allows you to select one of MANY GOSUB's depending on your OFFSET value.
If BRANCH can't be considered a member of the type: GOSUB, what command is BRANCH most likely related to?
The help file is kinda lacking when it comes to this, if infact BRANCH acts differently than GOSUB.
Post Edited (BPM) : 11/24/2005 2:44:16 PM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Bruce I think I understand what your saying
BRANCH is infact a GOTO family member, and should NOT be used when one wants to return to the sub that started BRANCH.
I think someone should explain this a bit further in the help file.
A simple comment mentioning that BRANCH will NOT RETURN once it successfully branches, and that its a GOTO family member.
And now I understand why my code was returning where it did.
This is because the LAST GOSUB I did was in fact GOSUB Subtwo, NOT BRANCH
And when I say : RETURN in SubThree, it returns to the last Gosub---> Subtwo.
Thanks for your help. (the help file was no help here)
YOU GUYS RAWK!
Post Edited (BPM) : 11/24/2005 2:56:17 PM GMT