Search and Replace Routines

See all articles from SYNC v4 n1

The routines in Figure 1 and Figure 2 are designed to search through a program and replace all occurrences of one character or keyword with another (keywords are represented with one byte, too). The routines can all be entered directly from the keyboard into a 1 REM line.

All characters and most keywords can be accessed directly by pressing the correct key or combination of keys except the keywords printed above the key outlines. To enter these, first enter the keyword THEN, followed by the desired keyword. Then backtrack with shift 5, hit DELETE to erase THEN, press shift 8, and continue. Figure 1 is a print-out of each routine, while Figure 2 has the assembly, along with the characters used for each instruction.

Figure 1.

VERSION I, ROUTINE A

1 REM 5[inv 2]RNDYV[gra 3]7[inv Y]C[gra D]YA[inv Y]4"QB/[gra E]7[inv Y]
C[gra 5]777/ SCROLL TAN
2 LET A$=""
3 LET A=L

Two versions of the routines are given; version I is for replacing any character, such as the letter A or keyword PRINT with another letter or keyword. Version II is for replacing only string variable names. Version I will do this too, but version II will not replace numeric variable identifiers with the same letter as the string variable identifier.

Three routine variations—A, B, and C—are included for each version. The A routines are for one-time type use. The character to be replaced (marked with a single asterisk in the Figure 2 assembly), and its replacement (double asterisk), have to be entered manually, either by editing line 1 or POKEing (addresses are given in the footnotes).

Figure 2.

version I
routine A
LD HL,16542 5, inv 2, RND
LD A,59 Y, V
ADD A,A gra 3
INC HL 7
CP (HL) inv Y
JRZ,+9 C, gra D
LD A, * Y, A
CP (HL) inv Y
JRNZ,+11 4, inv Y
LD (HL), ** Q, B
JR,+7 /, gra E
INC HL 7
CP (HL) inv Y
JRZ,+5 C, gra 5
INC HL 7
INC HL 7
INC HL 7
JR,-25 /, SCROLL
RET TAN

routine B
LD HL,16557 5, inv H, RND
(Same as Routine A)
LD A, (16530) U, inv >, RND
PUSH AF PRINT
LD A,(16525) U, inv $, RND
LD (16530),A M, inv >, RND
POP AF LET
LD (16525),A M, inv $, RND
RET TAN

routine C
LD HL,16548 5, inv 8, RND
(Same as Routine A)
LD A,(16530) U, inv >, RND
LD (16525),A M, inv $, RND
RET TAN

* Character to be replaced (16525)
** Replacement character (16530)

version II
routine A
LD HL,16549 5, inv 9, RND
LD A,59 Y, V
ADD A,A gra 3
INC HL 7
CP (HL) inv Y
JRZ,+16 C, (
LD A, * Y, A
CP (HL) inv Y
JRNZ,+18 4, >
INC HL 7
LD A,$ Y, $
CP (HL) inv Y
DEC HL F
JRNZ,+11 4, gra S
LD (HL), ** Q, B
JR,+6 /, gra T
INC HL 7
CP (HL) inv Y
JRZ,+5 C, gra 5
INC HL 7
INC HL 7
INC HL 7
JR,-32 /, STEP
RET TAN

routine B
LD HL,16566 5, inv Q, RND
(Same as Routine A)
LD A, (16537) U, inv ;, RND
PUSH AF PRINT
LD A,(16525) U, inv $, RND
LD (16537),A M, inv ;, RND
POP AF LET
LD (16525),A M, inv $, RND
RET TAN

routine C
LD HL,16555 5, inv F, RND
(Same as Routine A)
LD A,(16537) U, inv ;, RND
LD (16525),A M, inv $, RND
RET TAN

* String variable letter to be replaced (16525)
** Replacement letter (16537)

The B routine will flip-flop two characters each time it is run, without requiring any editing. If you want a routine to change all PRINTs to LPRINTs, and vice-versa, then routine B (version I) is best for this application.

The C routines keep track of the last character changed to, but the replacement character must be entered manually by editing, or by POKEing. If you want to interchange A$, B$, and C$ in your program (for differently formatted files, for instance), then add a Basic routine that will let you pick one and will POKE its code into address 16537 (for version II). When routine C is run, it will change whatever string variable is presently used to the one specified. If you use numeric variables A, B, and C to hold the format limits, you can use routine C, version I, to change string and numeric identifiers simultaneously.

I will run through version I quickly (version II is basically the same, except as noted). Enter 1 REM from version I, routine A—use Figure 2 and check yourself with Figure 1. Enter lines 2 and 3 as well.

All routines are run by entering RAND USR 16514 in the immediate mode (without a line number). Do this, and the A$ in line 2 and A in line 3 should now read B$ and B. Edit line 1 and change it to routine B (also change lines 2 and 3 back to A$ and A). Try running routine B several times, stopping with A$ and A. Now edit line 1 and enter routine C, and run it. Note that now both 16525 and 16530 hold the letter B. If routine C is run again, it will not change any characters. Enter POKE 16530, CODE “C” and run routine C again. Now enter LET L$-“E” and POKE 16530, CODE L$, and run the routine once more. Version II is the same as version I, except the replacement character is held at address 16537 instead of 16530.

Cartoon. A family gathered around a computer: "Beats the heck out of bridge, doesn't it?"
Cartoon. A family gathered around a computer: "Beats the heck out of bridge, doesn't it?"

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.