Shop OBEX P1 Docs P2 Docs Learn Events
Goto in Spin — Parallax Forums

Goto in Spin

TimHTimH Posts: 48
edited 2009-05-13 00:01 in Propeller 1
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.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-05-11 02:14
    There is no GOTO in Spin and you can't perform a GOTO function in assembler. There are procedure/function calls and there's something like catch/throw which in Spin is the ABORT statement and the "\" operator. You have a very flexible REPEAT statement, IF statement, and CASE statement. In REPEAT loops there are specialized goto-like statements, NEXT and EXIT.

    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.
  • W9GFOW9GFO Posts: 4,010
    edited 2009-05-11 03:33
    I must be missing the real question but... isn't 'GOTO' implied whenever you call a method?

     if x == 0
       wait  ' name of method that you want to go to
    
    



    Rich H
  • BradCBradC Posts: 2,601
    edited 2009-05-11 03:39
    W9GFO said...
    I must be missing the real question but... isn't 'GOTO' implied whenever you call a method?

     if x == 0
       wait  ' name of method that you want to go to
    
    


    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!
  • W9GFOW9GFO Posts: 4,010
    edited 2009-05-11 04:01
    Is that because GOSUB will return back to where it was called from but GOTO does not?

    Would this be closer to GOTO then?

    if x == 0
      wait
      Abort
    
    



    Rich H
  • Mike GreenMike Green Posts: 23,101
    edited 2009-05-11 04:03
    When you call a method, you push several longs into the stack including a return address. If you try to use method calls like a GOTO, you'll run out of stack space.
  • Mike GreenMike Green Posts: 23,101
    edited 2009-05-11 04:04
    Again, what are you trying to do? Method calls cannot be substituted for GOTOs.
  • JonnyMacJonnyMac Posts: 9,194
    edited 2009-05-11 05:23
    Mike Green said...
    There is no GOTO in Spin and you can't perform a GOTO function in assembler. <snip>

    Am I missing something? -- while there is no GOTO in Spin, you can JMP in PASM, conditionally or otherwise.
  • jazzedjazzed Posts: 11,803
    edited 2009-05-11 06:01
    Technically JMP and JMPRET (i.e. CALL) are the same instruction except for the R in the ZCRI bits.
    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
  • BradCBradC Posts: 2,601
    edited 2009-05-11 09:31
    jazzed said...

    Now, if you were a spin virtual machine expert, you could modify a bytecode to jump, but that could become very painful.

    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!
  • StefanL38StefanL38 Posts: 2,292
    edited 2009-05-11 14:23
    Hello TimH,

    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
  • Mike GreenMike Green Posts: 23,101
    edited 2009-05-11 14:31
    There are several kinds of jump instruction in assembler, but you can only use them in an assembly program. You can't interleave Spin and assembly instructions, so you can't use the assembly jump instructions to provide a GOTO function for Spin code.

    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.
  • virtuPICvirtuPIC Posts: 193
    edited 2009-05-11 18:19
    You can write any program you want without GOTO in SPIN. Its control structures are powerful enough. You should not miss the GOTO. The keyword is 'structured programming'. GOTO reduces the readability of your program and increases error probability. Almost all high level programming languages of the recent decades have banned GOTO or at least restricted the contexts where you may use it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Airspace V - international hangar flying!
    www.airspace-v.com/ggadgets for tools & toys
  • jazzedjazzed Posts: 11,803
    edited 2009-05-11 19:15
    Too bad someone didn't realize this GOTO business long ago, then we wouldn't have to deal with 20 GOTO 10 BASIC.
    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
  • Mike GreenMike Green Posts: 23,101
    edited 2009-05-11 19:38
    jazzed,
    You missed the GOTO wars (david.tribble.com/text/goto.html).
  • jazzedjazzed Posts: 11,803
    edited 2009-05-11 19:57
    Ya an 8 year old would have been ignored [noparse]:)[/noparse]

    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
  • localrogerlocalroger Posts: 3,452
    edited 2009-05-11 23:03
    I generally appreciate what Parallax is trying to do with the Prop, which is as much an educational platform as a way to do cool industrial things for a tenth of the usual cost. So I see the value of forcing people to learn proper indenting (by making the program not work if they don't get it right) and forcing structure (language #2 after FORTH to really NOT HAVE GOTO) it is occasionally a nuisance. It's particularly annoying that there is no way to construct a jump table so that you can make an array of targets or procedures and call them based on a calculated index. But that's kind of advanced, and there's always PASM when you need sooper dooper performance, so I can see the logic for omitting stuff like that.
  • Mike GreenMike Green Posts: 23,101
    edited 2009-05-11 23:33
    The CASE statement is intended to take the place of an array of targets to call based on a calculated index. It doesn't really use a table though.
  • localrogerlocalroger Posts: 3,452
    edited 2009-05-12 03:11
    Yeah Mike, the funny thing is a lot of C compilers actually construct a jump table if you're doing a SWITCH with a lot of low-value integer results. Being interpreted Spin can't do that, but in the case of string compares the ultrafast PASM backbone mostly makes up the difference for practical purposes.
  • RogerInHawaiiRogerInHawaii Posts: 87
    edited 2009-05-12 05:52
    I am utterly amazed that there is any computer language that still allows GOTO. There is absolutely no need for it. I stopped using it twenty-five years ago. Structured programming superceded GOTO programming long, long ago. If GOTO is being taught in schools then shame on those instructors. If you're never taught about GOTO or if you never learn it on your own you are so much better off and don't have to un-learn bad programming practices. There are always more elegant, more logical, more structured ways of accomplishing what you want to do than by using GOTO.
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2009-05-12 14:56
    I hope our original poster isn't put off by unintentially starting Goto WWIII *grin*

    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?
  • jazzedjazzed Posts: 11,803
    edited 2009-05-12 19:24
    One can not avoid JMP in assembly, but with a good mid-level language one can avoid writing assembly source code entirely (in applications, not systems).
    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
  • bmb!bmb! Posts: 26
    edited 2009-05-12 19:57
    Goto = evil
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2009-05-12 20:26
    GOTO = "The root of all spaghetti coding"

    OBC

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    New to the Propeller?

    Visit the: The Propeller Pages @ Warranty Void.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-05-12 20:31
    Now, now. Not every Goto is evil. smile.gif

    -Phil
  • KyeKye Posts: 2,200
    edited 2009-05-12 20:57
    In structured programing its bad.

    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,
  • RiJoRiRiJoRi Posts: 157
    edited 2009-05-12 21:35
    Now, now! Remember that IF + GOTO is the basis of all the "higher" constructs!

    WHILE_1:
        IF(! Condition) GOTO END_WHILE
           /* do_Something */
        GOTO WHILE_1
    END_WHILE:
    
    DO_1:
           /* do_Something */
        IF(! Condition) GOTO END_DO
        GOTO DO_1
    END_DO:
    
    LIMIT = 10
    N = 1
    FOR_1:
        IF(N = LIMIT) GOTO NEXT_1
         /* do_Something */
        N++
        GOTO FOR_1
    NEXT_1:
    
    



    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
  • virtuPICvirtuPIC Posts: 193
    edited 2009-05-12 22:20
    To bring it to the point, GOTO is the low level control-flow concept. Sure, you can implement every program with GOTO plus conditional jump or predicated execution and or indirect jump. However, it is tedious, error prone and doesn't match the solution concept. GOTO is the control flow implementation of machine language.

    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
  • TimHTimH Posts: 48
    edited 2009-05-12 23:47
    Thanks Everyone - sorry about the time getting back - I was not in hiding.
    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
  • JasonDorieJasonDorie Posts: 1,930
    edited 2009-05-13 00:01
    Tim,

    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
Sign In or Register to comment.