Shop OBEX P1 Docs P2 Docs Learn Events
Jump to LABEL command in propeller Spin — Parallax Forums

Jump to LABEL command in propeller Spin

at_jayat_jay Posts: 16
edited 2011-08-21 09:41 in Propeller 1
Hello,

My question is quite basic and is regarding the JUMP (JMP) command as we normally use to jump to some LABEL. Is there any similar command or routine exist for propeller spin? or some one have already had some implementation/object of similar kind?
Please also post if there any and its limitations e.g. we can use quit and next command only in repeat loop etc.

Thanks for your support

Cheers
Jay

Comments

  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-08-20 19:12
    Does not exist in Spin (does in PASM). Many contend that with good coding practices you don't need JUMP or GOTO in high-level languages. I'm not weighing in on that, but the lack of JUMP or GOTO in Spin has not prevented me from doing what I need to do.

    You might have a look at the code for ABORT -- that may support what you're wanting to do.
  • RavenkallenRavenkallen Posts: 1,057
    edited 2011-08-20 21:51
    Do you mean jumping to a "method"? If so all you do is declare a public(pub) or private(pri) method(And then add your code) and have your main method "call" it simply by stating it's name(And passing parameters sometimes)... Like this.

    Pub main
    'Do something here
    othermethod

    Pub othermethod
    'Do something different here


    Once the othermethod has executed it's last instruction, program control will return to the method that called it and continue from there...C and other languages refer to this useage as "Functions". BASIC refers to it as a subroutine.
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-08-21 06:36
    Where do you use jumps?

    1. To skip some code: Use an if statement
    2. To branch into some code maybe depending on a variable: Use the switch statement
    3. To call a subroutine: Make it a function and call it
    4. To create loops: use repeat

    In the end no-jump code is easier to read and maintain (and believe me ... I have seen some goto driven code 20 years ago) because you are forced to structure your code. With Jump driven code you can create labels wherever you want and create wild pathes through your code, where only the programmer knows what happens exactly.
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-08-21 07:00
    There are rare occasions when a jump is useful in structured programming, such as a way to jump to a common exit point when an error condition occurs. Since Spin doesn't have a jump you would have do this by using the abort instruction, or you could set a flag that would be tested by downstream code to prevent it from executing.

    Spin is compiled to Spin bytecodes, which does contain a jump instruction. However, there's no direct way to generate this jump instruction. You could use the bytecode() primitive in BST to insert a jump instruction, but then you would need to determine the number of instruction bytes to jump over, which would be difficult.

    So the only way to do the equivalent of a jump in Spin is to set a flag to avoid downstream processing or use the abort instruction.

    Dave
  • StefanL38StefanL38 Posts: 2,292
    edited 2011-08-21 09:41
    small programming languages that have no possability of defining subroutines force the coder to produce "spagheti-code" that is hard to understand and hard to maintain.

    Structured programming means pack each part of the code that belongs to a senseful unit into its own subroutine.
    And equal important each subroutine should do only one thing.

    extremly contrary examples: a subrotine for entering a number for rpm should not do the details of creating stepperpulses to drive the steppermotor. These are two complete different things which should be coded each in its own subroutine.

    So the main thing that changes is

    instead of writing label "MyName" and somewhere a "goto MyName"
    you write

    PUB MyName

    and instead of "goto MyName" you write simply

    "MyName"

    Subroutines mean you have a basic set of commands and are able to expand the available commands through defining subroutines
    keep the questions coming
    best regards

    Stefan
Sign In or Register to comment.