Write a RISCOS BBCBASIC programme to sort a list of ascii text strings contained in a CSV file. Use ARM assembler routines ror the sort for speed. Use embedded the ARM assembler that supplied with the RISCOC BBCBASIC interpreter.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here is a RISCOS BBCBASIC program that sorts a list of ascii text strings contained in a CSV file using ARM assembler routines for speed:
bbcbasic
REM Sort CSV string list using ARM assembler routines
OSCLI "LOAD StringSort_ASM"
FILE% = OPENUP("strings.csv")
DIM string$(100)
count% = 0
REPEAT
LINE INPUT #FILE% string$(count%)
IF string$(count%) <> "" count% += 1
UNTIL EOF#FILE%
CLOSE #FILE%
REM Call ARM assembler string sort routine
SYS "StringSort_ASM", string$(), count%
REM Output sorted list
FOR i% = 0 TO count%-1
PRINT string$(i%)
NEXT
REM Embedded ARM assembler routine
DEF StringSort_ASM
STMFD SP!,{R4-R6,LR}
MOV R1, R0 ; R1 = array start address
LDR R2, [SP, #16] ; R2 = number of elements
OuterLoop
MOV R4, #0 ; outer loop index
MOV R5, R4 ; inner loop index
InnerLoop
LDR R6, [R1, R5, LSL #2] ; get string address
LDR R0, [R1, R4, LSL #2] ; get other string
BL strcmp ; compare strings
CMP R0, #1
BLT SkipSwap ; already in order, don't swap
; swap array elements
LDR R0, [R1, R5, LSL #2]
STR R0, [R1, R4, LSL #2]
STR R6, [R1, R5, LSL #2]
SkipSwap
ADD R5, R5, #1 ; increment R5
CMP R5, R2
BLT InnerLoop
ADD R4, R4, #1 ; next outer loop
CMP R4, R2
BLT OuterLoop
Done
LDMFD SP!,{R4-R6,PC}
strcmp
STMFD SP!, {R4-R8,LR}
; R0 and R6 point to strings
MOV R8, #0 ; result (0 = equal, 1 = greater, -1 = less)
loop
LDRB R4, [R0], #1 ; get char from string 1
LDRB R5, [R6], #1 ; get char from string 2
CMP R4, #0 ; end of string 1?
BEQ string1_ended
CMP R5, #0 ; end of string 2?
BEQ string2_ended
CMP R4, R5
BEQ loop ; try next char if equal
; chars not equal
CMN R4, R5 ; set flags
BMI string1_less ; R4 < R5
; R4 > R5
MOV R8, #1
B done_compare
string1_less
MOV R8, #-1
B done_compare
string1_ended
CMP R5, #0 ; is string 2 also at end?
BEQ done_compare ; yes, strings equal
; string 1 ended first
MOV R8, #-1
B done_compare
string2_ended
MOV R8, #1 ; string 1 greater
done_compare
LDMFD SP!, {R4-R8,PC}
END
This loads an ARM assembler routine called StringSort_ASM, reads strings from a CSV file into an array, calls the assembler routine to sort the array, and then prints out the sorted list.
The key parts are the SYS command to invoke the assembler routine, passing the array and count parameters, and the embedded ARM assembler code itself which implements a string sort algorithm. This uses the strcmp subroutine to do string comparisons.
Using ARM assembler provides a significant speed boost over doing the sort in BBC BASIC. The code could be further optimized but this is a basic implementation that demonstrates the technique.