A machine language program is necessary when sorting large amounts of data. "ML Applesort" is a machine language utility that will quickly sort an array of any length.
In COMPUTE! (September 1982), David Lummis presented an excellent machine language sort routine for PET/CBM computers. "ML Applesort" is a modification of Lummis's program for the Apple.
The original program has been compressed to fit it into page 3 of memory. This is a safe area for ML programs so you do not have to worry about overwriting the routine with a long program or numerous and lengthy string arrays. The zero page locations used for temporary storage, counting, and address indexing were chosen because most of these locations are used primarily in connection with hi-res graphics, and it is unlikely such programs will be used concurrently with a sort utility.
ML Applesort makes use of the Apple's special & command. When the BASIC interpreter encounters the & in a program, it goes to location $3F5 (1013 decimal) and then performs an unconditional jump to the address contained in $3F6 and $3F7. In this program, 0 is placed in $3F6 (1014 decimal) and 3 is placed in $3F7 (1015 decimal). This will cause a jump to location $300 (768 decimal), which is where the machine language routine begins.
The first instruction at $300 is a JSR (Jump to SubRoutine) $F7D9. This is a monitor subroutine that fetches the address of a string and stores it in locations $9B and $9C. By placing the name of the string array we want sorted immediately after the &, this routine will tell us where in memory that array is stored. The correct format for calling the ML sort routine from BASIC is as follows:
where X$ is the name of the array to be sorted. When the routine returns to BASIC, the named array will be sorted alphabetically in ascending order. How the program does the sorting can be understood by studying a disassembly. To enter the program, use the BASIC Loader (Program 1).
After placing the program into memory by running Program 1, save it to disk by typing:
BSAVE APPLESORT, A$300, L$FF
You can then BLOAD the sort routine and use it with any of your own BASIC programs.
Program 2 shows how easily the program can be used and how quickly it can sort an array with strings of varying lengths. I'm sure COMPUTE! readers with Apple machines will find numerous applications for this useful utility.