SeaGtGruff Posted August 8, 2005 Share Posted August 8, 2005 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 Quote Link to comment Share on other sites More sharing options...
+batari Posted August 8, 2005 Share Posted August 8, 2005 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 906502[/snapback] 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... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.