From 225bfc4164e88aa281bc88bc04416ee8dc7857d9 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 25 Sep 2020 22:51:59 +0200 Subject: [PATCH] fix 16+8 bit add and sub sign extensions --- .../assignment/AugmentableAssignmentAsmGen.kt | 70 +++++++++++++------ examples/testforloops.p8 | 2 - 2 files changed, 47 insertions(+), 25 deletions(-) diff --git a/compiler/src/prog8/compiler/target/c64/codegen/assignment/AugmentableAssignmentAsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/assignment/AugmentableAssignmentAsmGen.kt index d23e1c614..e865e2586 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/assignment/AugmentableAssignmentAsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/assignment/AugmentableAssignmentAsmGen.kt @@ -840,22 +840,34 @@ internal class AugmentableAssignmentAsmGen(private val program: Program, // the other variable is a BYTE type so optimize for that when (operator) { // note: ** (power) operator requires floats. - "+" -> asmgen.out(""" - lda $name - clc - adc $otherName + "+" -> { + asmgen.out(""" + ldy #0 + lda $otherName + bpl + + dey ; sign extend ++ clc + adc $name sta $name - bcc + - inc $name+1 -+ """) - "-" -> asmgen.out(""" + tya + adc $name+1 + sta $name+1""") + } + "-" -> { + asmgen.out(""" + ldy #0 + lda $otherName + bpl + + dey ; sign extend ++ sty P8ZP_SCRATCH_B1 lda $name sec sbc $otherName sta $name - bcs + - dec $name+1 -+ """) + lda $name+1 + sbc P8ZP_SCRATCH_B1 + sta $name+1""") + } "*" -> { asmgen.out(""" lda $otherName @@ -993,25 +1005,37 @@ internal class AugmentableAssignmentAsmGen(private val program: Program, when(valueDt) { in ByteDatatypes -> { - // the other variable is a BYTE type so optimize for that TODO does this even occur? + // the other variable is a BYTE type so optimize for that when (operator) { // note: ** (power) operator requires floats. - "+" -> asmgen.out(""" - lda $name - clc - adc P8ESTACK_LO+1,x + "+" -> { + asmgen.out(""" + ldy #0 + lda P8ESTACK_LO+1,x + bpl + + dey ; sign extend ++ clc + adc $name sta $name - bcc + - inc $name+1 -+ """) - "-" -> asmgen.out(""" + tya + adc $name+1 + sta $name+1""") + } + "-" -> { + asmgen.out(""" + ldy #0 + lda P8ESTACK_LO+1,x + bpl + + dey ; sign extend ++ sty P8ZP_SCRATCH_B1 lda $name sec sbc P8ESTACK_LO+1,x sta $name - bcs + - dec $name+1 -+ """) + lda $name+1 + sbc P8ZP_SCRATCH_B1 + sta $name+1""") + } "*" -> TODO("mul word byte") "/" -> TODO("div word byte") "%" -> TODO("word remainder byte") diff --git a/examples/testforloops.p8 b/examples/testforloops.p8 index e48789946..0d907a13a 100644 --- a/examples/testforloops.p8 +++ b/examples/testforloops.p8 @@ -3,8 +3,6 @@ ; Note: this program is compatible with C64 and CX16. -; TODO fix byte var in arrayvar fail - main { sub start() {