Here's a RISCOS BBCBASIC program to sort a list of ASCII text strings contained in a CSV file using ARM assembler routines for speed:
#Assembler routine for sorting
def AssemblerSort
; Load the address of the strings into R0
ldr r0, =Strings
; Load the address of the lengths into R1
ldr r1, =Lengths
; Load the length of the strings table into R2
ldr r2, =StringsEnd
; Load the length of the lengths table into R3
ldr r3, =LengthsEnd
; Set the index of the strings table to 0
mov r4, #0
; Set the index of the lengths table to 0
mov r5, #0
; Set the flag to indicate if a swap has been made
mov r6, #0
; Loop through the strings
LoopSort
; Load the length of the current string into R7
ldr r7, [r1, r5]
; Load the address of the current string into R8
ldr r8, [r0, r4]
; Loop through the strings backwards
sub r4, r4, #1
sub r5, r5, #1
cmp r4, r5
bge ExitSort
; Load the length of the previous string into R9
ldr r9, [r1, r5]
; Load the address of the previous string into R10
ldr r10, [r0, r4]
; Compare the strings
cmp r8, r10
bge NoSwap
; Swap the strings
str r10, [r0, r4]
str r8, [r0, r5]
; Swap the lengths
str r9, [r1, r4]
str r7, [r1, r5]
; Set the flag to indicate a swap has been made
mov r6, #1
; Loop backwards
sub r4, r4, #1
sub r5, r5, #1
NoSwap
; Loop forwards
add r4, r4, #1
add r5, r5, #1
b LoopSort
ExitSort
; Return the flag to indicate if a swap has been made
mov r0, r6
; Return from the assembler routine
bx lr
This assembler routine will sort the strings in ascending order. It works by comparing each string to the string that comes after it, and swapping them if the first string is greater than the second string. It continues to do this until it reaches the end of the strings table. The routine returns a flag to indicate if a swap has been made, which is used in the main program to determine if another sort is needed.
Here's the main program in RISCOS BBCBASIC that uses the assembler routine to sort the strings:
#Set the addresses of the strings and lengths tables
Define Strings As String : Rem "Address of strings table"
Define Lengths As String : Rem "Address of lengths table"
Define StringsEnd As String : Rem "End of strings table"
Define LengthsEnd As String : Rem "End of lengths table"
#Open the CSV file and read the strings and lengths
Open "strings.csv" For Binary As #1
Read #1, Strings
Read #1, Lengths
Close #1
#Call the assembler routine to sort the strings
Call AssemblerSort
#Check if a swap has been made
If R0 = 1 Then
; If a swap has been made, call the assembler routine again
Call AssemblerSort
EndIf
#Print the sorted strings
For i = 1 To Length(Strings)
Print left$(Strings, Lengths), ","
Next
This program sets the addresses of the strings and lengths tables, opens the CSV file, reads the strings and lengths, calls the assembler routine to sort the strings, checks if a swap has been made, and prints the sorted strings.