fix string compare in expressions

This commit is contained in:
Irmen de Jong 2020-11-06 22:59:56 +01:00
parent aaa20093ef
commit 661c757236
4 changed files with 43 additions and 21 deletions

View File

@ -945,12 +945,26 @@ strcpy .proc
sty P8ZP_SCRATCH_W2+1
ldy #$ff
- iny
inc $d020
lda (P8ZP_SCRATCH_W2),y
sta (P8ZP_SCRATCH_W1),y
bne -
rts
.pend
strcmp_expression .proc
; TODO expression call args not via stack
inx
lda P8ESTACK_LO,x
ldy P8ESTACK_HI,x
sta P8ZP_SCRATCH_W2
sty P8ZP_SCRATCH_W2+1
inx
lda P8ESTACK_LO,x
ldy P8ESTACK_HI,x
jmp strcmp_mem
.pend
strcmp_mem .proc
; -- compares strings in s1 (AY) and s2 (P8ZP_SCRATCH_W2).

View File

@ -1696,34 +1696,35 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
}
private fun translateCompareStrings(operator: String) {
asmgen.out(" jsr prog8_lib.func_strcmp") // result of compare is on stack but also in A
asmgen.out(" jsr prog8_lib.strcmp_expression") // result of compare is in A
when(operator) {
"==" -> asmgen.out(" and #1 | eor #1 | sta P8ESTACK_LO+1,x")
"!=" -> asmgen.out(" and #1 | sta P8ESTACK_LO+1,x")
"==" -> asmgen.out(" and #1 | eor #1 | sta P8ESTACK_LO,x")
"!=" -> asmgen.out(" and #1 | sta P8ESTACK_LO,x")
"<=" -> asmgen.out("""
bpl +
lda #1
bne ++
+ lda #0
+ sta P8ESTACK_LO+1,x""")
+ sta P8ESTACK_LO,x""")
">=" -> asmgen.out("""
bmi +
lda #1
bne ++
+ lda #0
+ sta P8ESTACK_LO+1,x""")
+ sta P8ESTACK_LO,x""")
"<" -> asmgen.out("""
bmi +
lda #0
beq ++
+ lda #1
+ sta P8ESTACK_LO+1,x""")
+ sta P8ESTACK_LO,x""")
">" -> asmgen.out("""
bpl +
lda #0
beq ++
+ lda #1
+ sta P8ESTACK_LO+1,x""")
+ sta P8ESTACK_LO,x""")
}
asmgen.out(" dex")
}
}

View File

@ -287,9 +287,9 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
DataType.STR -> {
asmgen.out("""
lda #<${target.asmVarname}
ldy #>${target.asmVarname}
sta P8ZP_SCRATCH_W1
lda #>${target.asmVarname}
sta P8ZP_SCRATCH_W1+1
sty P8ZP_SCRATCH_W1+1
inx
lda P8ESTACK_HI,x
tay
@ -430,17 +430,17 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
DataType.UWORD -> {
asmgen.out("""
lda #<$sourceName
ldy #>$sourceName
sta ${target.asmVarname}
lda #>$sourceName
sta ${target.asmVarname}+1
sty ${target.asmVarname}+1
""")
}
DataType.STR, DataType.ARRAY_UB, DataType.ARRAY_B -> {
asmgen.out("""
lda #<${target.asmVarname}
ldy #>${target.asmVarname}
sta P8ZP_SCRATCH_W1
lda #>${target.asmVarname}
sta P8ZP_SCRATCH_W1+1
sty P8ZP_SCRATCH_W1+1
lda #<$sourceName
ldy #>$sourceName
jsr prog8_lib.strcpy""")
@ -451,9 +451,9 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
TargetStorageKind.STACK -> {
asmgen.out("""
lda #<$sourceName
ldy #>$sourceName+1
sta P8ESTACK_LO,x
lda #>$sourceName+1
sta P8ESTACK_HI,x
sty P8ESTACK_HI,x
dex""")
}
else -> throw AssemblyError("string-assign to weird target")

View File

@ -46,27 +46,33 @@ main {
; memsetw(ADDR2+24*40, 19, $4241)
; memcopy(ADDR2, ADDR, 200)
str result = "." *10
str result = "?" *10
str s1 = "irmen"
str s2 = "hello"
str dots = "....."
ubyte ub
byte bb
ubyte zero=0
bb = strcmp(s1, s2)
txt.print_b(bb)
txt.chrout('\n')
bb = strcmp(s2, s1)
txt.print_b(bb)
txt.chrout('\n')
txt.print_ub(s1==s2)
txt.chrout('\n')
txt.print_ub(s1<s2)
txt.chrout('\n')
txt.print_ub(s1>s2)
txt.chrout('\n')
bb = zero+strcmp(s1,s2)*1+zero
txt.print_b(bb)
txt.chrout('\n')
bb = zero+strcmp(s2,s1)*1+zero
txt.print_b(bb)
txt.chrout('\n')
ub = strlen(s1)
txt.print_ub(ub)
@ -81,8 +87,9 @@ main {
leftstr(s1, result, len(s1))
txt.print(result)
txt.chrout('\n')
txt.chrout('\n')
result = "."*10 ; TODO doesn't work??
result = "x"*8
rightstr(s2, result, 3)
txt.print(result)
txt.chrout('\n')
@ -90,7 +97,7 @@ main {
txt.print(result)
txt.chrout('\n')
result = "."*10 ; TODO doesn't work??
result = "y"*10
substr(s2, result, 1, 3)
txt.print(result)
txt.chrout('\n')