!lm11
!rm76
A String Swapper for Applesoft

Practically every program rearranges data in some way.  Many times you must sort alphanumeric data, and Applesoft makes this relatively easy.  At the heart of most sort algorithms you will have to swap two items.

If the items are numbers, you might do it like this:  T=A(I) : A(I)=A(J) : A(J)=T.  If the items are in string variables, you might use this:  T$=A$(I) : A$(I)=A$(J) : A$(J)=T.  

Before long, Applesoft's wonderful string processor eats up all available memory and your program screeches to a halt with no warning.  You think your computer died.  Just about the time you reach for the power switch, it comes to life again (if you aren't too impatient!);  the garbage collection procedure has found enough memory to continue processing.  If only Applesoft had a command to swap the pointers of two strings, this wouldn't happen.

What are pointers?  Look on page 137 of your Applesoft Reference Manual.  The third column shows how string variables are stored in memory.  Each string, whether a simple variable or an element of an array, is represented by three bytes:  the first byte tells how many bytes are in the string value at this time; the other two bytes are the address of the first byte of the string value.  The actual string value may be anywhere in memory.  I am calling the three bytes which define a string a "pointer".

All right, how can we add a string swap command?  The authors of Applesoft thoughtfully provided us with the "&" command; it allows us to add as many new commands to the language as we want.  (Last month I showed you how to add a computed GOSUB command using the &.)  We could make up our own swap command; perhaps something like &SWAP A$(I) WITH A$(J).  However, to keep it a little simpler, I wrote it this way:  &A$(I),A$(J).

The program is in two sections.  The first part, called SETUP, simply sets up the &-vector at $3F5, $3F6, and $3F7.  It stores a "JMP SWAP" instruction there.  When Applesoft finds an ampersand (&) during execution, it will jump to $3F5; our JMP SWAP will start up the second section.

SWAP calls on two routines inside the Applesoft ROMs:  PTRGET ($DFE3) and SCAN.COMMA ($DEBE).  I found the addresses for these routines in the article "Applesoft Internal Entry Points", by John Crossley, pages 12-18 of the March/April 1980 issue of The Apple Orchard.  I also have disassembled and commented the Applesoft ROMs, so I checked to see if there were any bad side effects.  Both routines assume that Applesoft is about to read the next character of your program.  PTRGET assumes you are sitting on the first character of a variable name.  SCAN.COMMA hopes you are sitting on a comma.

SWAP merely calls PTRGET to get the address of the pointer for the first variable, check for an intervening comma, and then calls PTRGET again to get the pointer address for the second variable.  Then lines 1350-1430 exchange the three bytes for the two pointers.
!np
How about a demonstration?  I have a list of 20 names (all are subscribers to the Apple Assembly Line), and I want to sort them into alphabetical order.  Since I am just writing this to demonstrate using the swap command, I will use one of the WORST sort algorithms: the bubble sort.

Line 100 clears the screen and prints a title line.  Line 110 loads the swap program and calls SETUP at 768 ($0300).  Line 120 reads in the 20 names from the DATA statement in line 130, and calls a subroutine at line 200 to print the names in a column.

Lines 150-170 are the bubble sort algorithm.  If two names are out of order, they are swapped at the end of line 160.  Line 180 prints the sorted list of names in a second column.
