Continue to Site

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.

  • 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.

"Goto" code

Status
Not open for further replies.

swapan

Member
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:
                MOVLW	D'140'
		SUBWF	AD_VALUE1,W
		BTFSC	STATUS,C
		GOTO	BAT_CHK2
		GOTO	INV_ON
 
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
 
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.
 
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:
                MOVLW	D'140'
		SUBWF	AD_VALUE1,W
		BTFSC	STATUS,C
		GOTO	BAT_CHK2
		GOTO	INV_ON

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..
 
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.

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
 
dear swapan sir
can u share ur whole code & ckt diagram here.i hope u don't mind.
 
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.
 
Goto does have boundary problems and can only jump within the same 2k block without altering PCLATH.

Mike.
 
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.
 
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."
 
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).
 
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:
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.
 
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.

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.

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.

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).
 
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).

dear sir can u expalen meaning of ......generic processor.........
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top