optimized repeat loop for word counts

This commit is contained in:
Irmen de Jong 2021-02-28 21:22:46 +01:00
parent 543efa4299
commit 32d894d6b6
2 changed files with 35 additions and 10 deletions

View File

@ -1011,14 +1011,8 @@ internal class AsmGen(private val program: Program,
// note: A/Y must have been loaded with the number of iterations!
if(constIterations==0)
return
if(constIterations==null) {
out("""
cmp #0
bne +
cpy #0
beq $endLabel ; skip loop if zero iters
+""")
}
// no need to explicitly test for 0 iterations as this is done in the count down logic below
val counterVar = makeLabel("repeatcounter")
out("""
sta $counterVar
@ -1027,7 +1021,7 @@ $repeatLabel lda $counterVar
bne +
lda $counterVar+1
beq $endLabel
+ lda $counterVar
lda $counterVar
bne +
dec $counterVar+1
+ dec $counterVar

View File

@ -2,6 +2,37 @@
%zeropage basicsafe
main {
sub start() {
bench()
txt.nl()
uword total
ubyte bb
uword ww
for bb in 0 to 255 {
total = 0
repeat bb
total++
txt.print_uw(total)
txt.spc()
txt.spc()
}
txt.nl()
txt.nl()
for ww in 0 to 600 {
total = 0
repeat ww
total++
txt.print_uw(total)
txt.spc()
txt.spc()
}
}
sub iter(uword iterations) -> uword {
uword total = 0
@ -26,7 +57,7 @@ main {
return total
}
sub start() {
sub bench() {
uword xx1
uword xx2
uword xx3