1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

"Goto" code

Discussion in 'Microcontrollers' started by swapan, Dec 26, 2009.

  1. swapan

    swapan Member

    Joined:
    Aug 29, 2009
    Messages:
    72
    Likes:
    0
    Location:
    KOLKATA (INDIA)
    Dear Friends,
    In a project, I have used two successive "goto" codes. As the project shows erratic operation i.e. sometimes the MCU gets locked, I fear if the two successive "goto" codes make any problem. Please confirm whether it is beyond the rule.

    swapan


    Code (text):
                    MOVLW   D'140'
            SUBWF   AD_VALUE1,W
            BTFSC   STATUS,C
            GOTO    BAT_CHK2
            GOTO    INV_ON
     
  2. WolfGang

    WolfGang New Member

    Joined:
    Nov 1, 2009
    Messages:
    8
    Likes:
    0
    Location:
    Mississippi
    perhaps you could try a "gosub" command...in basic, goto will cause a "jump" to another section of code and will completely exit the current block of code where as the gosub will allow a return to the line of code immediately following the "gosub" command when the subcode is finished executing using the simple "return" command. hope this helps
     
  3. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,016
    Likes:
    317
    Location:
    Brisbane Australia
    ONLINE
    There is nothing wrong with the posted code. You may have a problem elsewhere in your code but as you only posted a tiny bit we will never know.

    Mike.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,218
    Likes:
    641
    Location:
    Derbyshire, UK

    Two goto's like that is perfectly fine, and the normal way of doing it - although, depending on exactly what's going on, it saves a 'goto' by inserting the second 'coto code' actually at the second goto address..
     
  6. swapan

    swapan Member

    Joined:
    Aug 29, 2009
    Messages:
    72
    Likes:
    0
    Location:
    KOLKATA (INDIA)
    Thanks Mr. Pommie. Yes, there was a problem in my code and after rectification of the same the project is working fine with its two successive "goto"s. Thank you again.

    swapan
     
  7. swapan

    swapan Member

    Joined:
    Aug 29, 2009
    Messages:
    72
    Likes:
    0
    Location:
    KOLKATA (INDIA)
    Thanks Mr. Goodwin for your kind reply with some elaboration. It will certainly help me.

    swapan
     
  8. sahu

    sahu Member

    Joined:
    Oct 18, 2009
    Messages:
    468
    Likes:
    3
    dear swapan sir
    can u share ur whole code & ckt diagram here.i hope u don't mind.
     
  9. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    maybe its jumping out of the boundary... im not sure how far a jump can be but that seems like it can be the issue
     
  10. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,218
    Likes:
    641
    Location:
    Derbyshire, UK
    No, Goto doesn't have any such problems.
     
  11. Diver300

    Diver300 Well-Known Member

    Joined:
    Oct 19, 2007
    Messages:
    3,063
    Likes:
    101
    Location:
    England
    Those GOTOs won't be executed one after the other, and there should be no problem with the code.

    I once had a problem with code running three context jumps (GOTOs and Returns) in quick succession. That was caused by insufficient suppression capacitors.
     
  12. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,016
    Likes:
    317
    Location:
    Brisbane Australia
    ONLINE
    Goto does have boundary problems and can only jump within the same 2k block without altering PCLATH.

    Mike.
     
  13. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    thanks... i knew i wasnt crazy
     
  14. colin55

    colin55 Well-Known Member

    Joined:
    Feb 14, 2009
    Messages:
    3,534
    Likes:
    82
    Location:
    Melbourne Australia
    From what I remember, a "goto" does not set a return address in a PIC chip and thus you must put a goto at the end of the sub-rutoutine that you are "going-to".
    You must also put a label at the front of the second "goto" so that the micro goes back to the correct location, after executing the first sub-routine.
     
  15. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,218
    Likes:
    641
    Location:
    Derbyshire, UK
    A 'GOTO' doesn't set a return address in any processor, because it's not a sub-routine. If you want a sub-routine use 'GOSUB' instead.
     
  16. colin55

    colin55 Well-Known Member

    Joined:
    Feb 14, 2009
    Messages:
    3,534
    Likes:
    82
    Location:
    Melbourne Australia
    Where does the PIC instruction set have: "GOSUB?"

    I really don't know what you mean by this statement: " A 'GOTO' doesn't set a return address in any processor, because it's not a sub-routine. If you want a sub-routine use 'GOSUB' instead."
     
  17. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,218
    Likes:
    641
    Location:
    Derbyshire, UK
    I was referring to any generic processor, in PIC assembler the actual instruction is 'call'.

    As for the statement you didn't understand, it's self explanatory - you don't use 'goto' to call a subroutine, you use 'gosub' (or 'call' for a PIC).
     
  18. colin55

    colin55 Well-Known Member

    Joined:
    Feb 14, 2009
    Messages:
    3,534
    Likes:
    82
    Location:
    Melbourne Australia
    You can use "goto" to go to a sub-routine but the last instruction in the sub-routine will always take you back to the location contained in the last instruction.
    The original request was to validate two goto instructions in a routine and I considered the possible problems with these two instructions.
    The possibilities were not addressed by any of the respondents.
     
    Last edited: Dec 31, 2009
  19. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,016
    Likes:
    317
    Location:
    Brisbane Australia
    ONLINE
    You cannot use goto to call a subroutine only to make spaghetti code. The whole point of a subroutine is so it can be called from various locations, putting a goto at the end of it means it is not a subroutine but part of the main code.

    Mike.
     
  20. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,218
    Likes:
    641
    Location:
    Derbyshire, UK
    That's not a sub-routine, it's just a normal routine - a sub-routine can be called from anywhere, and automatically returns to where it came from.

    Sorry, it had been fully covered - your mistake is assuming goto can call sub-routines, which it can't (except for certain specific cases, where a subroutine has already been called, and you're changing the exit point - in which case it doesn't return to the goto, but to the original call).
     
  21. sahu

    sahu Member

    Joined:
    Oct 18, 2009
    Messages:
    468
    Likes:
    3
    dear sir can u expalen meaning of ......generic processor.........
     

Share This Page