also add strings.ncompare() to virtual lib

This commit is contained in:
Irmen de Jong
2025-03-09 14:33:31 +01:00
parent 8f8d99e3ed
commit 8b08895d0f
4 changed files with 51 additions and 23 deletions

2
.idea/misc.xml generated
View File

@@ -22,7 +22,7 @@
<component name="FrameworkDetectionExcludesConfiguration">
<type id="Python" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="openjdk-17" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@@ -123,6 +123,19 @@ strings {
}}
}
sub ncompare(str st1, str st2, ubyte length) -> byte {
; Compares two strings for sorting.
; Returns -1 (255), 0 or 1, meaning: string1 sorts before, equal or after string2.
; Only compares the strings from index 0 up to the length argument.
%ir {{
loadm.w r99000,strings.ncompare.st1
loadm.w r99001,strings.ncompare.st2
loadm.b r99100,strings.ncompare.length
syscall 58 (r99000.w, r99001.w, r99100.b) : r99100.b
returnr.b r99100
}}
}
sub lower(str st) -> ubyte {
; Lowercases the petscii string in-place. Returns length of the string.
; (for efficiency, non-letter characters > 128 will also not be left intact,

View File

@@ -1,34 +1,32 @@
%import textio
%import strings
%zeropage basicsafe
main {
str name1 = "irmen"
str name2 = "irmen de jong"
sub start() {
uword[] texts1 = [ 1,2,3 ]
uword[] @nosplit texts2 = [ 1,2,3 ]
cx16.r4 = texts1
cx16.r5 = texts2
txt.print_b(strings.compare(name1, name2))
txt.spc()
txt.print_b(strings.compare(name2, name1))
txt.nl()
; txt.print_uwhex(&texts1, true)
; txt.spc()
; txt.print_uwhex(cx16.r4, true)
; txt.nl()
; txt.print_uwhex(&texts2, true)
; txt.spc()
; txt.print_uwhex(cx16.r5, true)
; txt.nl()
txt.print_b(strings.ncompare(name1, name2, 6))
txt.spc()
txt.print_b(strings.ncompare(name2, name1, 6))
txt.nl()
cx16.r4 = &texts1
cx16.r5 = &texts2
txt.print_b(strings.ncompare(name1, name2, 5))
txt.spc()
txt.print_b(strings.ncompare(name2, name1, 5))
txt.nl()
; txt.print_uwhex(&texts1, true)
; txt.spc()
; txt.print_uwhex(cx16.r4, true)
; txt.nl()
; txt.print_uwhex(&texts2, true)
; txt.spc()
; txt.print_uwhex(cx16.r5, true)
; txt.nl()
txt.print_b(strings.ncompare(name1, name2, 4))
txt.spc()
txt.print_b(strings.ncompare(name2, name1, 4))
txt.nl()
}
}

View File

@@ -67,6 +67,7 @@ SYSCALLS: DO NOT RENUMBER THESE OR YOU WILL BREAK EXISTING CODE
55 = WRITE_FILE_BYTE
56 = CLOSE_FILE
57 = CLOSE_FILE_WRITE
58 = ncompare strings
*/
enum class Syscall {
@@ -128,6 +129,7 @@ enum class Syscall {
WRITE_FILE_BYTE,
CLOSE_FILE,
CLOSE_FILE_WRITE,
NCOMPARE_STRINGS,
;
companion object {
@@ -290,6 +292,21 @@ object SysCalls {
else
returnValue(callspec.returns.single(), 1, vm)
}
Syscall.NCOMPARE_STRINGS -> {
val (firstV, secondV, lengthV) = getArgValues(callspec.arguments, vm)
val firstAddr = firstV as UShort
val secondAddr = secondV as UShort
val length = (lengthV as UByte).toInt()
val first = vm.memory.getString(firstAddr.toInt()).take(length)
val second = vm.memory.getString(secondAddr.toInt()).take(length)
val comparison = first.compareTo(second)
if(comparison==0)
returnValue(callspec.returns.single(), 0, vm)
else if(comparison<0)
returnValue(callspec.returns.single(), -1, vm)
else
returnValue(callspec.returns.single(), 1, vm)
}
Syscall.MEMCMP -> {
val (firstV, secondV, sizeV) = getArgValues(callspec.arguments, vm)
var firstAddr = (firstV as UShort).toInt()