diff --git a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt index c416e07ce..162ea47fe 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt @@ -22,6 +22,7 @@ import prog8.compiler.target.c64.codegen.assignment.TargetStorageKind import prog8.compiler.target.generatedLabelPrefix import prog8.functions.BuiltinFunctions import prog8.functions.FSignature +import java.io.CharConversionException import java.nio.file.Path import java.time.LocalDate import java.time.LocalDateTime @@ -231,8 +232,12 @@ internal class AsmGen(private val program: Program, } private fun encode(str: String, altEncoding: Boolean): List { - val bytes = if(altEncoding) Petscii.encodeScreencode(str, true) else Petscii.encodePetscii(str, true) - return bytes.plus(0) + try { + val bytes = if (altEncoding) Petscii.encodeScreencode(str, true) else Petscii.encodePetscii(str, true) + return bytes.plus(0) + } catch(x: CharConversionException) { + throw AssemblyError("There was a problem converting a string to the target machine's char encoding: ${x.message}") + } } private fun zeropagevars2asm(statements: List) { diff --git a/compiler/src/prog8/compiler/target/c64/codegen/assignment/AssignmentAsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/assignment/AssignmentAsmGen.kt index ec7014cdc..d202956f0 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/assignment/AssignmentAsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/assignment/AssignmentAsmGen.kt @@ -132,7 +132,24 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen asmgen.translateFunctionCall(value, preserveStatusRegisterAfterCall) val returnValue = sub.returntypes.zip(sub.asmReturnvaluesRegisters).single { it.second.registerOrPair!=null } if(returnValue.first==DataType.STR) { - TODO("assignment of string => copy string ${assign.position}") + when(assign.target.datatype) { + DataType.UWORD -> { + // assign the address of the string result value + assignRegisterpairWord(assign.target, RegisterOrPair.AY) + } + DataType.STR, DataType.ARRAY_UB, DataType.ARRAY_B -> { + // copy the actual string result into the target string variable + asmgen.out(""" + pha + lda #<${assign.target.asmVarname} + sta P8ZP_SCRATCH_W1 + lda #>${assign.target.asmVarname} + sta P8ZP_SCRATCH_W1+1 + pla + jsr prog8_lib.strcpy""") + } + else -> throw AssemblyError("weird target dt") + } } else { when (returnValue.second.registerOrPair) { RegisterOrPair.A -> assignRegisterByte(assign.target, CpuRegister.A) @@ -156,7 +173,7 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen when(returntype.typeOrElse(DataType.STRUCT)) { in ByteDatatypes -> assignRegisterByte(assign.target, CpuRegister.A) // function's byte result is in A in WordDatatypes -> assignRegisterpairWord(assign.target, RegisterOrPair.AY) // function's word result is in AY - DataType.STR -> TODO("assign string => copy string") + DataType.STR -> TODO("assign string => copy string or assign string address") DataType.FLOAT -> TODO("assign float result from ${sub.name}") else -> throw AssemblyError("weird result type") } diff --git a/examples/test.p8 b/examples/test.p8 index a7948b7ab..32c0d0999 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -7,73 +7,26 @@ ; Note: this program is compatible with C64 and CX16. main { - sub start() { - - uword ss - - ss = %1000000110101010 - ss <<= 8 - txt.print_uwbin(ss, 1) - txt.chrout('\n') - - ss = %1000000110101111 - ss <<= 9 - txt.print_uwbin(ss, 1) - txt.chrout('\n') - - ss = %1000000110101111 - ss <<= 14 - txt.print_uwbin(ss, 1) - txt.chrout('\n') - - ss = %1000000110101111 - ss <<= 15 - txt.print_uwbin(ss, 1) - txt.chrout('\n') - - ss = %1000000110101111 - ss <<= 16 - txt.print_uwbin(ss, 1) - txt.chrout('\n') - - ss = %1000000110101010 - ss <<= 17 - txt.print_uwbin(ss, 1) - txt.chrout('\n') - - main22.testX() - } -} - -main22 { sub start() { - ubyte char - uword ssss - float fl + str string1 = "abcdef" + str string2 = "%=&" + uword sa - ;char = 1+(lsb(ssss) * 2) - ;fl = 2.0*(abs(fl) + 1.0) - - char = abs(char) - char = lsb(ssss) - char++ - char = msb(ssss) - char++ - char = c64.CHRIN() - - txt.print_ub(char) + txt.print(string1) txt.chrout('\n') - - char = chrin() - - txt.print_ub(char) + string1=string2 + txt.print(string1) txt.chrout('\n') void getstr() - ; TODO string assign ssss = getstr() - txt.print_uwhex(ssss, true) + sa = getstr() + txt.print_uwhex(sa, true) + txt.chrout('\n') + + string1 = getstr() + txt.print(string1) txt.chrout('\n') ; fl = getfloat() @@ -82,7 +35,6 @@ main22 { ; txt.chrout('\n') testX() - ;char=strlen(ssss) } sub chrin() -> ubyte { @@ -91,7 +43,7 @@ main22 { sub getstr() -> str { @($d020)++ - return "foo" + return "foobar" } ; sub getfloat() -> float {