fix forloop 6502 codegen in case of descending word values

This commit is contained in:
Irmen de Jong 2023-07-10 22:03:48 +02:00
parent 6055b8c3dc
commit 6b6427492d
3 changed files with 49 additions and 47 deletions

View File

@ -249,23 +249,6 @@ $endLabel""")
$loopLabel""") $loopLabel""")
asmgen.translate(stmt.statements) asmgen.translate(stmt.statements)
if(iterableDt==DataType.ARRAY_UW) {
asmgen.out("""
lda $varname
sec
sbc #<${stepsize.absoluteValue}
sta $varname
lda $varname+1
sbc #>${stepsize.absoluteValue}
sta $varname+1
$modifiedLabel cmp #0 ; modified
bcc $endLabel
bne $loopLabel
lda $varname
$modifiedLabel2 cmp #0 ; modified
bcs $loopLabel
$endLabel""")
} else {
asmgen.out(""" asmgen.out("""
lda $varname lda $varname
sec sec
@ -283,7 +266,6 @@ $modifiedLabel sbc #0 ; modified
eor #$80 eor #$80
+ bpl $loopLabel + bpl $loopLabel
$endLabel""") $endLabel""")
}
} }
} }
} }

View File

@ -1,8 +1,6 @@
TODO TODO
==== ====
- codegen: fix endless loop uword n=0 :: for uword i in 10 downto n step -3
- investigate if the hiscores issue on the forum is a compiler bug or not. - investigate if the hiscores issue on the forum is a compiler bug or not.
- IR: instructions that do type conversion (SZ etc, CONCAT, SGN) should put the result in a DIFFERENT register. - IR: instructions that do type conversion (SZ etc, CONCAT, SGN) should put the result in a DIFFERENT register.

View File

@ -1,34 +1,56 @@
%import string
%import textio %import textio
%import conv
%zeropage basicsafe %zeropage basicsafe
%option no_sysinit
main { main {
sub start() {
uword n=0
uword i
txt.print("ascending:\n")
n=10
for i in 0 to n step 3 {
cx16.r0++
txt.print(" i=")
txt.print_uw(i)
txt.spc()
txt.print(" n=")
txt.print_uw(n)
txt.nl()
}
txt.print("descending:\n") str[10] names = ["1??","2??","3??","4??","5??","6??","7??","8??","9??","a??"]
n=0 str[10] scores = ["9000","8000","7000","6000","5000","4000","3000","2000","1000","50"]
for i in 10 downto n step -3 { str p_str = "winner"
cx16.r0++ uword top_score
txt.print(" i=")
txt.print_uw(i) sub start() {
txt.spc() print_hiscores()
txt.print(" n=") ubyte r = check_score(7500)
txt.print_uw(n) print_hiscores()
txt.nl()
}
}
} }
sub check_score(uword score) -> ubyte {
ubyte n
ubyte i
str tmp = "?????"
if score < top_score {
return(10) }
else {
for n in 0 to 9 {
if score > conv.str2uword(scores[n]) {
for i in 8 to n step -1 {
names[i+1] = names[i]
scores[i+1] = scores[i]
}
;string.copy(p_str,names[n])
;conv.str_uw0(score)
;string.copy(conv.string_out,scores[n])
return(n)
}
}
}
}
sub print_hiscores() {
ubyte n
for n in 0 to 9 {
txt.print_ub(n)
txt.spc()
txt.print(names[n])
txt.spc()
txt.print(scores[n])
txt.nl()
}
txt.nl()
}
}