Did you think that there's no way to put RETURNS into REM statements? Or into PRINT statements? Or to put backspace characters into REM statements?
This machine language search and replace program opens a universe of options like these. Use your imagination after you type in the BASIC listing. This article also throws some light on how BASIC is stored in your computer.
A machine language program can be stored in your Apple computer three ways: (1) by typing on the keyboard; (2) by loading it from cassette or disk; (3) by LOADing a BASIC program and having it POKE the machine language into place. It's the third method that we'll use here.
When you RUN this program, it will ask you to provide information so that it can set itself up for the particular function you have in mind. Once you have done this, you can LOAD another BASIC program without affecting the machine language program. Then to change the new program, type & and press RETURN. The computer will jump to the machine language program, execute it, and return to BASIC.
Search And Replace
This program will search through your BASIC program until it finds a REM statement, then read the information between REM and the end of the line, and change any control-A's to carriage returns. When it reaches the end of the line (or a colon), it goes on to the next line and continues its search for REM statements until it reaches the end of the BASIC program.
You can change it so that it will look for any other command, and change characters that follow on that line, until the end of the line or the colon is encountered.
For example, say you want to make your REM statements easier to read by inserting carriage returns. When you type the REM statement, type a control-A everywhere you want a carriage return. Then, when you're finished, use the & command to execute the machine language program. You'll see the results when you list your program.
As another example, suppose your printer requires the Escape character to access special functions. It is possible to type your BASIC program with control-E's in place of the Escape character, then later run the machine language program to make a switch.
BASIC uses some space-saving tricks to store a program. For one, it converts commands into tokens. So REM is not stored as the ASCII codes for R, E, and M. Instead, the entire word is converted to the value $B2. (The $ indicates the value is in hexadecimal notation. $B2 is equivalent to 178 in ordinary decimal notation.)
Another trick is using the character that indicates the end of a program line. You would assume (because you hit RETURN to tell the computer you have finished entering a line) that it would store the ASCII code for RETURN, $0D (13). But it doesn't. Instead, it stores $00 (0).
A third trick is the conversion of all line numbers to two bytes. A line number of 1 is stored as $01 00, and a line number of 256 is stored as $00 01. The high-order (more significant) byte is in the second position.
The machine language program puts this information to good use. Every time it encounters $00, it skips over the line number (and two more bytes which hold the location of the next line) to the beginning of the next command sequence. If it finds a value of $B2, the token for REM, when it is looking for REM statements, it jumps to the subroutine that switches one character for another. If the subroutine encounters a $00, or the ASCII token for ":", it ends and the program starts looking for the next REM statement.
Here's a list of some tokens and ASCII values of interest. You can find a list of ASCII codes used by Applesoft on pages 138 and 139 of the Applesoft BASIC Programming Manual. The tokens for the commands can be found on page 121.
You should know that DOS commands in a BASIC program are not tokenized. In
10 PRINT CHR$ (4);"PR# 1"
PR# is stored as the ASCII equivalents for P, R, and #. Take this into consideration when setting up the machine language program. The token to search for in such a situation is $23, the ASCII code for #.
Changing Switch Without Loader
Let's call the BASIC program listed with this article Loader and the machine language program that it produces Switch. Once you have run Loader, you can change Switch, without rerunning Loader, using POKE commands.
To change the command token, use POKE 796, (new token).
To change the byte to be replaced, use POKE 815, (new byte).
To change the replacement, use POKE 821, (new byte).
Here's an example. If you want to change all of the control-B's in all of your PRINT statements to control-G's (bell ringers), you must first know that the token for PRINT is 186, that the ASCII byte for control-B is 2, and that the ASCII byte for the bell character is 7. Then enter:
] 10 POKE 796, 186 : POKE 815, 2 : POKE 821, 7
The equivalent monitor command line is:
* 31C : BA N 32F : 02 N 335 : 07
(The N allows you to put more than one command on a line.) Then enter & to make the change (or 300G in machine language).
Some Quick Facts About The Program
The machine language program can be placed anywhere in memory. Normally it resides at $300-$350 (768 to 848).
Locations $F9 and $FA (249 and 250) are normally unused by BASIC, DOS, or the monitor, but are used by Switch to keep track of its current point in the BASIC program it is changing.
Switch gets its information for the beginning and end locations of the program from $67 and $68 (103 and 104) and $AF and $B0 (175 and 176), respectively.
The & vector must be set to $300 (768). This is done by Loader.