Goto in Spin
TimH
Posts: 48
This is my 2nd day using Spin and am having a hard time because there seems to be no Goto or Jmp type statement in Spin.· Such as "If x = 0 GOTO wait"
I've tried using the repeat loops but it seems very cumbersom with multiple nested return loops and I can't get the required result.
Do I need to perform the Goto function in assembler or is there a trick to using Spin.
I've tried using the repeat loops but it seems very cumbersom with multiple nested return loops and I can't get the required result.
Do I need to perform the Goto function in assembler or is there a trick to using Spin.
Comments
What are you trying to do? Often multiple nested loops with complex exit conditions are best implemented by putting the inner loop into its own procedure, but there may be a better, clearer way to do what you want to do.
Rich H
That is much closer to GOSUB than GOTO
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"VOOM"?!? Mate, this bird wouldn't "voom" if you put four million volts through it! 'E's bleedin' demised!
Would this be closer to GOTO then?
Rich H
Am I missing something? -- while there is no GOTO in Spin, you can JMP in PASM, conditionally or otherwise.
Besides that possibility, JMP is a GOTO. You could also use IF_Z etc... to fake a PASM GOTO.
GOTO usage ... Spin equivalent:
- Conditional goto ... if, if else, case, quit/next in repeat loops
- Loop goto ... repeat, repeat until, repeat while
- Exit sub at any point ... return, abort
- One exit sub point without extra work ... no equivalent
- Writing spin with your eyes closed ... priceless
Now, if you were a spin virtual machine expert, you could modify a bytecode to jump, but that could become very painful.▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Steve
Propalyzer: Propeller PC Logic Analyzer
http://forums.parallax.com/showthread.php?p=788230
I was thinking about this earlier, but there is some judicious stack popping required if you were to jump out of a REPEAT or CASE, so you'd have to be pretty careful or have the compiler do some funky trickery (neither of which I like the sound of).
You don't need to modify any bytecodes, SPIN has jmp and test codes built in. It's how the compiler manages IF constructs.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"VOOM"?!? Mate, this bird wouldn't "voom" if you put four million volts through it! 'E's bleedin' demised!
please write a posting where you describe what you want to do IN THE END
most basic dialects have a GOTO-command.
To say it clear the goto-command destroyes structured programming.
If you are ready to learn structured programming instead of spaghetti-code
I promise if you have switched from goto to method-calls you never want to go back
to gotos because it will be MUCH easier to read and maintain programs that are consequently structured by the use of method-calls.
All you have to do is to give yourself a little bump and say "OK I learn how method-calls work"
best regards
Stefan
TimH is asking if there's some way he can use assembly in-line to do a GOTO since there's no GOTO in Spin and the answer is no.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Airspace V - international hangar flying!
www.airspace-v.com/ggadgets for tools & toys
Now if people would stop pushing it as "a solution," we would be even better off [noparse]:)[/noparse]
BradC, please please don't even try to add a GOTO extension.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Steve
Propalyzer: Propeller PC Logic Analyzer
http://forums.parallax.com/showthread.php?p=788230
You missed the GOTO wars (david.tribble.com/text/goto.html).
Added: The Greeks contrived many wonderful and crazy ideas. The ones that are remembered are the ones that fit closest to reality or were just darned silly. I hope someone here eventually establishes such infamy. Alas, we are just grains of sand.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Steve
Propalyzer: Propeller PC Logic Analyzer
http://forums.parallax.com/showthread.php?p=788230
Post Edited (jazzed) : 5/11/2009 8:17:14 PM GMT
For me, the first program I ever wrote was:
10 print "hello world"
The second program was:
10 print "hello world"
20 goto 10
Then you get hooked on goto. You can't get off it.
Real programs, ie structured ones, are harder to write something simple. Most start with a list of definitions for variables, which is second nature to those who write structured programming but harder to get people hooked with a few lines of simple code.
As has been said above, you don't need goto, but you have to think in a different way. I think it was called "top down programming" or something similar. Start with something like (in pseudo basic generic code):
main:
do
...
loop
Now add the bits of code (eg to flash a led)
main:
do
led_on
pause
led_off
pause
loop
Then you go off and write the bits of code that actually turn the led on, or do a pause. These would probably be subroutines, and it is often easier to leave the main program loop as simple as possible so you can follow the program flow.
If you think about a language in this way, it becomes a lot easier to translate that to C and Spin and other languages and even some versions of Basic (eg a wonderful, ancient and long forgotten CP/M program called sbasic which is as structured as C, and which will soon be running on zicog). And of course the .net family which abandoned the need for goto long ago.
Anyway, that is the theory. I'm off now to tweak a copy of xmodem written in assembler and it is full of JMP instructions.
A rhetorical question to myself: Is a GOTO allowed if we call it a JMP?
Many Linux drivers do use goto in C for the one case where structured programming fails to deliver, but that is mostly a hack to try and fix a fundamental Linux limitation.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Steve
Propalyzer: Propeller PC Logic Analyzer
http://forums.parallax.com/showthread.php?p=788230
Post Edited (jazzed) : 5/12/2009 8:05:40 PM GMT
OBC
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
New to the Propeller?
Visit the: The Propeller Pages @ Warranty Void.
-Phil
But in asm jmp (goto) is your friend. And, well, goto solves driver problems where you have a catastropic failure (like removing the sd card from its socket when writing a file) that structured programing cannot solve without a lot of extra pointless conditional checking.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,
The same can be done for the varieties of REPEAT found in Spin. Hiding the GOTO in pretty words does enforce organized coding, and does make it easier to understand what the programmer was trying to do. However, there may be times when a GOTO is more useful than other constructs. I just ran a pathological test of GOTO vs. BREAK, and saved 3 lines of NBNC* code by using GOTO in lieu of multiple breaks.
Actually, I am chuckling at these "DOWN WITH GOTO!" statements. GOTO the next forum over, where they deal with the BASIC Stamp, which has GOTOs galore.
--Rich
*NBNC == Non-Break, Non-Comment
All control flow constructs of higher language like IF, WHILE, DO...UNTIL, REPEAT, FOR have to be implemented by GOTO and associates to be executed on a processor. But you leave this task to the compiler or interpreter.
As I said before, programming in lower level is more error prone, less understandable, more tedious, more time consuming...
I also made the transition. I wrote my first programs in good old BASIC on C64. Then I switched to 6502 assembler - which had not even indirect jumps - where I did my first professional work. Then at university I learned about structured programming. This gave me some insight. Believe me, you don't need GOTO in higher level language. Believe me, higher level languages should not even provide GOTO, it's too tempting.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Airspace V - international hangar flying!
www.airspace-v.com/ggadgets for tools & toys
I spent my first propeller education day reading the manual and trying to work through the examples. I was looking for something familiar.
I think what I should have asked is about GoSub... Ret instead of Goto (which I do try to avoid as much as possible)
On futher reading it seems that the Method "call" is very similar to the Gosub.... ret - infact the manual even refers to a Method as routine or procedure (similar to a sub-routine). So I think with that understanding I can proceed to the next step.
One other question. When I include another Object (say FullDuplexSerial) are all the methods in that Object compiled or only the methods that I call from the top object. From reading the manual it seems as if the entire Object is compiled even if I only require a small part of it - which seems to be a wast of memory resources.
Once again thanks for the advise
TimH
All of the included object is compiled into your program whether you use it or not. If it becomes a problem, it's easy to make a personal copy of the object and comment out the bits you don't use to shrink the size of your app.
Jason