Jump to content

Photo

FOR..NEXT bug


1 reply to this topic

#1 SeaGtGruff OFFLINE  

SeaGtGruff

    Quadrunner

  • 5,566 posts
  • Location:South Carolina, USA

Posted Sun Aug 7, 2005 10:42 PM

First of all, thank you for creating Batari BASIC; it's a great concept!

The compiled code for the FOR..NEXT statement has a bug if the STEP isn't 1, which causes the loop to end before it should.

For example, the following compiled code runs as intended-- three playfield pixels are drawn in a diagonal line:

.L01;  for a=1 to 3 step 1:pfpixel a a on:next
	LDA #1
	STA a
.L01fora
	LDX a
	LDY a
	LDA #0
	jsr pfpixel
	LDA a
	CMP #3
	INC a
	bcc .L01fora

Note that the CMP statement occurs *before* the variable gets incremented, which is okay, because the STEP is 1, so there's no chance the INC statement will go beyond the final desired value.

However, this next compiled code does *not* run as intended-- only one playfield pixel gets drawn:

.L01;  for a=1 to 3 step 2:pfpixel a a on:next
	LDA #1
	STA a
.L01fora
	LDX a
	LDY a
	LDA #0
	jsr pfpixel
	LDA a
	CLC
	ADC #2
	STA a
	CMP #3
	bcc .L01fora

Note that the CMP is done *after* the variable has been increased, which is the way it should be-- except the BCC statement branches only if the result is *less* than the final desired value, but the loop should repeat if the result is less than or *equal* to the final desired value. So to correct the bug, the compiled code needs to include a BEQ to the label, either before or after the BCC:

	bcc .L01fora
	beq .L01fora

Until the bug gets fixed, the workaround is to add 1 to the final desired value (i.e., "FOR A=1 TO 4 STEP 2" when we actually want to stop after 3). The only problem would be if the final desired value were 255.

Michael Rideout

#2 batari OFFLINE  

batari

    )66]U('=I;B$*

  • 6,680 posts
  • begin 644 contest

Posted Sun Aug 7, 2005 11:58 PM

Note that the CMP is done *after* the variable has been increased, which is the way it should be-- except the BCC statement branches only if the result is *less* than the final desired value, but the loop should repeat if the result is less than or *equal* to the final desired value. So to correct the bug, the compiled code needs to include a BEQ to the label, either before or after the BCC:

	bcc .L01fora
	beq .L01fora

View Post

Thanks for pointing this out - I wasn't aware of it. I will fix it for the next version.

Also, I thought I should mention that there is another bug in the loop code which is unrelated to this one. Reverse loops where the step is larger than the end value will sometimes fail.

For instance:
for l=9 to 3 step -5
l will hold 9, then 4, then 255, but since 255>3, the loop will keep on going. I think I've fixed this in the next version, but for now, be careful...




0 user(s) are browsing this forum

0 members, 0 guests, 0 anonymous users