BUG: Simulation fails to exit loop (also failed on real chip)

grim_reaper

Senior Member
I ran across a weird issue last night, and re-coded to avoid it at the time. Now I've got more time on my hands, I managed to re-produce the issue using the code below. What I think is the problem is that the code fails to exit a loop-within-a-loop, when another loop is also present. It's difficult to explain, so try the code below in the simulator (I was using 6.0.7.1 before realising I needed to upgrade to 6.0.7.3, so I've tested in both). If you run it as is, the code loops once, then on the second pass it gets stuck on the 'Exit' command once the condition is satisfied. If you then comment out the 'For...Next' loop, it runs fine. Weird, eh?!

Code:
#PICAXE 20X2

; Variables
Symbol vTest	= b0
Symbol vLoop	= b1

Do
	; This can be set to any value
	vTest = 4

	Do
		; 'Exit' hangs when the condition is met - IF another loop (For/Next in this case) is present
		If vTest = 5 Then Exit

		For vLoop = 1 To 5
			' Do something else in a loop
		Next

		Inc vTest
	Loop

	Pause 500
Loop
I've tested with all sorts of numbers/passes through the loop. I couldn't reproduce it for a while, but then when I copied my 'real' code back into the middle loop it started doing it again and I realised it was the presence of the third loop that did it. I guess the stack gets a bit confused with all the Loop/Next commands?
Just to throw some more info in here, the first time this happened I assumed it was the simulator (the condition only occurs rarely in the real application), so I programmed a 20X2 with it and after a few minutes it hung - I have an LED that flashes when data is transmitted, about once per second, so I could test this quite easily. Loading a particular value into EEPROM and letting it run a few passes before that value was used to exit the loop confirmed this happened within the chip as well.

I've tried simulating with various chip types, so it seems to be a generic problem rather than anything specific. I hope that's everything, of course let me know if you need anything else.

Thanks.
 

Technical

Technical Support
Staff member
Thanks, we'll look into it but it would probably be an issue with the compiler rather than the simulator/chip firmware.

Exit is a pseudo command, it is simply translated by the compiler as 'goto out of the loop' so a simple workaround is to do that manually instead

e.g.

do
if vtest = 5 then exit
loop

simply becomes

do
if vtest = 5 then goto exit_target_position
loop
exit_target_position:
 

Technical

Technical Support
Staff member
After analysing this we have found a clear issue in the on-screen simulator code that has now been fixed for the next PE6 release, thanks for identifying this with a clear example. This issue would stop the simulation at the word 'exit' within these nested loop examples.

However we can find no issue in the compiler or real life chips, all real programs running in a chip work as expected. As you say your program hung after several minutes we strongly suspect the issue is elsewhere - the 'exit' would simply either work or not, it would not become unreliable after a few minutes.
 
Top