Better
Branching
In Applesoft
Mark Russinovich
Branching
In Applesoft
Mark Russinovich
Are you ready to update the Applesoft BASIC on your Apple II-series computer? This handy utility adds extra flexibility by letting you branch to line numbers computed by variables or even complex expressions. For both DOS 3.3 and ProDOS.
Though it's been used to write a tremendous number of programs, Applesoft BASIC has some significant shortcomings compared to more recent versions of BASIC. One of these is the inability to use a variable or BASIC expression as the object of a GOTO, GOSUB, or RESTORE command. Applesoft BASIC requires you to use a line number as the destination of a GOTO, GOSUB, or RESTORE.
There are two disadvantages to this. First, line numbers contain no clue to the purpose of the branch: GOSUB DELAY makes the purpose of a subroutine more obvious to everyone than GOSUB 1000. Second, branching statements that are limited to constants can't be modified while a program is executing. Unlike line numbers, variables can change as a program runs, letting you modify the destination of a command just by changing the value of the variable. For example, you could use GOSUB CHOICE*1000 to branch to subroutines at lines 1000, 2000, or 3000 depending on whether the variable CHOICE equals 1, 2, or 3.
Improved GOTO And GOSUB
"Enhancer" lets you substitute variables and even complex expressions as the object of GOTO, GOSUB, and RESTORE in Applesoft BASIC. To use it, first enter Program 1 and be sure to save a copy. Program 1 is a BASIC program that creates the machine language routine for Enhancer on disk, using the filename APPLE.ENHANCER. (Be careful to use some name other than APPLE.ENHANCER for Program 1 itself; otherwise, you'll get a FILE TYPE MISMATCH error when you run it.)
After you've created the APPLE.ENHANCER file, you won't need Program 1 again, except to make additional copies of the machine language. To load and activate the utility, add this line to the beginning of any BASIC program:
10 PRINT CHR$(4)"BRUN APPLE.
ENHANCER"
Make sure the Enhancer machine language file is on the disk in the current drive. As soon as your program executes this line, it can use the features of APPLE.ENHANCER. You can still use the normal form of GOTO and GOSUB (GOTO 100, GOSUB 20005, or whatever), but in addition, you can also use this format:
& GOTO expression
& GOSUB expression
Note the ampersand (&) symbol that precedes the GOTO or GOSUB. In place of expression, you can substitute any legal Applesoft variable name or expression. Here's a short example to show how the new commands work:
AB 10 BEGIN = 20
7B 20 INPUT "CHOOSE 1 OR 2 ";CHO
ICE
B5 30 IF CHOICE < 1 OR CHOICE >
2 THEN & GOTO BEGIN
6D 40 & GOSUB CHOICE * 1000
FA 50 PRINT "DONE."
F1 60 END
DI 1000 PRINT "FIRST ROUTINE"
D5 1010 RETURN
2B 2000 PRINT "SECOND ROUTINE"
D6 2010 RETURN
Notice how the improved GOSUB serves as a substitute for ONGOSUB. Depending on what value CHOICE has, the program branches to line 1000 or 2000. Likewise, the improved GOTO command can replace an ON-GOTO command.
RESTORE To A Destination
The RESTORE statement in Applesoft normally stands by itself; it simply resets the READ pointer to the beginning of the program. If you wish to READ a particular piece of data midway through the list, you first READ past every preceding DATA item in the program. With APPLE.ENHANCER in place, you can use this more convenient command:
& RESTORE expression
Again, you must include the ampersand before the command; replace expression with any legal Applesoft expression or variable. Now you can access any DATA line in the program instantly.
For instance, say that a program has many DATA statements containing information for three different graphics screens. Ordinarily, you'd have to READ through all the DATA for screens 1 and 2 before reaching the DATA for screen 3-a process that takes time and increases the possibility of errors. The improved RESTORE command allows you to zero in on the precise DATA line you want, without time-consuming delays.
Program 2 demonstrates all the features of these new commands. Without these features, the program would be considerably longer and less flexible.
For instructions on entering these listings,
please refer to "COMPUTE!'s Guide to Typing
In Programs" in this issue of COMPUTE!.
Program 1: APPLE.
ENHANCER Filemaker
ED 10 FOR I = 768 TO 887: READ A
: POKE I,A:CK = CK + A: NE
XT
7A 20 IF CK < > 14482 THEN PRINT
"ERROR IN DATA STATEMENTS
.": STOP
El 30 PRINT CHR$ (4)"BSAVE APPLE
.ENHANCER,A$300,L$77"
EE 40 DATA 169,76,141,245,,169,
16,141
AB 50 DATA 246,3,169,3,141,247,3
,96
E9 60 DATA 160,0,177,184,217,115
,3,240
88 70 DATA 11,200,192,3,240,3,76
,20
C2 80 DATA 3,32,201,222,140,118,
3,230
BA 90 DATA 184,208,2,230,185,32,
103,221
95 100 DATA 172,118,3,192,1,240,
10,192
75 110 DATA 2,240,35,32,82,231,7
6,65
A4 120 DATA 217,169,3,32,214,211
,165,185
AA 130 DATA 72,165,184,72,165,11
8,72,165
BF 140 DATA 117,72,169,176,72,32
,82,231
63 150 DATA 32,65,217,76,210,215
,32,82
96 160 DATA 231,32,26,214,56,165
,155,233
43 170 DATA 1,164,156,176,1,136,
133,125
A5 180 DATA 132,126,96,171,176,1
74,0,0
Program 2: Better Branching
Demo
ED 10 PRINT CHR$ (4)"BRUN APPLE.
ENHANCER"
53 20 TEXT : HOME
EF 30 & RESTORE 100
CD 40 FOR I = 1 TO 3: READ A: &
GOSUB 50 + A ; 10: NEXT I
F8 50 END
FC 60 PRINT "THIS IS STATEMENT 2
": RETURN
7E 70 PRINT "THIS IS STATEMENT 3
": RETURN
7E 80 PRINT "THIS IS STATEMENT 1
": RETURN
FB 90 DATA THIS IS UNWANTED DATA
55 100 DATA 3,1,2