PDA

View Full Version : Exit from an IF/ENDIF loop with a GOTO



aratti
- 16th February 2014, 07:37
In a time sensitive application I did try to gain time jumping out of a IF/ENDIF loop with a goto instruction. Here a snippet as example.


Main_Loop

If A > B then
A = B
Gosub one
Goto skip_00
ENDIF

If C > D then
C = D
Gosub two
Goto skip_00
ENDIF
.
.
.
.
.
.
.
If Z > W then
Z = W
Gosub ten
ENDIF

Skip_00:

Goto Main_Loop



The code seems working without any problems and does what is suppose to do.
The question is: Exiting any loops with a goto instruction can produce unpredictable results?

Thank you for any clarification on the subject.

Al.

HenrikOlsson
- 16th February 2014, 08:55
Hi Al,
The only scenarios where problems can (and will) pop up that I can think of right now are:

A) Jumping out of an ISR with a GOTO without eventually reaching a RETFIE or INT_RETURN or whatever is appropriate for the type of interrupt you're using.

B) Jumping out of subroutine to which you've GOSUBed without eventually reaching a RETURN.

Using GOTO within an IF/ENDIF block or a WHILE/WEND, DO/LOOP type of loop structure should be a problem - as far as I can see.

/Henrik.

aratti
- 16th February 2014, 11:09
Hi Henrik, thank you for the answer.

Let me add few more information, the code with the GOTO within several IF/ENDIF loops is now running past the 48 hours. Thre program has the watchdog off and is monitored via serial connection by a pc which sends every 60 secs a series of bytes with random numbers and check the answer. So far no errors has been detected and no hangs of the pic has been experienced (this was my first worry).

So far it seems that you can exit the IF/ENDIF loop with a goto without any consequence, but I don't consider at all, this test of mine as a complete test. I will let the pic running for a whole week and see what heppens.

Cheers

Al

HenrikOlsson
- 16th February 2014, 11:41
Hi Al,
I wouldn't call an IF/ENDIF construct a loop but anyway, I certainly wouldn't worry about it. I mean, what reason could there possibly be preventing it from working? I do admire your desire to test and verify though, nice job!

Again, if that IF/ENDIF construct was within a subroutine to which you jumped with a GOSUB, THEN a GOTO someplace could (will)mess it up if it doesn't eventually end up at a "matching" RETURN.

/Henrik.

Charlie
- 16th February 2014, 14:25
To repeat what Henrik said using slightly different words, there is nothing wrong with using a GOTO to exit a loop as you are using it because that is pretty much what it was intended for. Where you can get into trouble is when you use GOTO to exit a subroutine and thereby miss a RETURN. Your code snippet does not show what's inside the subroutines "one" and "two" but for example, perhaps "one" contains the code:

one:
IF something = value THEN GOTO Main_Loop
something = different
RETURN

In this case, whenever the program goes to "one" it puts whatever it was doing on the stack. When it hits the RETURN, it takes the values off the stack and puts them back where they were before the GOSUB, and life is good. When the condition of "something = value" is met, the clean up doesn't happen, and the old register values are left on the stack. Each time it happens, a little more memory is not released. Eventually you run out of stack space and crash.

In your example, the GOTO is not inside a subroutine, so you don't have to worry about it. It should run forever.

The same is true of ISRs which are just a special kind of subroutine.

dw_picbasic
- 17th February 2014, 03:58
Is it possible for you to use a WHILE loop instead of the IF/ENDIF?
It can perform much the same function except you can modify the controlling variables upon which the WHILE test is performed, within the WHILE loop, or within a GOSUB/RETURN, and then it will exit gracefully.

Heckler
- 17th February 2014, 04:52
I always thought that the ENDIF statement was more for the PBP compiler and that you could Jump out of an IF/ENDIF pair without problem.

Charlie
- 17th February 2014, 13:05
Guys this has nothing to do with IF / ENDIF, WHILE, LOOP, or EIEIO (I made that one up). This has to do with GOTO.
Please read the content of the posts instead of adding to the confusion.