Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
May, 1982.I$Ue 24 COMPUTEI 149<br />
May, 1982. Issue 24 COMPUTE! 149<br />
are not in the midst of handling a command. If<br />
are not in the midst of handling a command. If<br />
FLAG FLAC is not 0, then the user r has typed in something<br />
like GOTO GOTO, which is a syntax error. Since<br />
like COTO COTO, which is a syntax erro r. Since<br />
Applesoft sy ntax is very clea r on this point - all<br />
Applesoft syntax is very clear on this point — all<br />
commands must be separated by ends of line e (colon<br />
or carriage return) - we return "? SYNTAX ER<br />
ROR'1 ROR" if we find a command token while executing<br />
a command. Since BASIC is nol not in direct control of<br />
program execution at this point, this program<br />
must do its own error checking.<br />
g.<br />
4. Our lookup ta ble diffe rs in format fro m the one<br />
4. Our lookup table differs in format from the one<br />
at $1)000. $0000. We also store jump p locations, less 1, I, and<br />
will get to these via an RTS, as the Applesoft inter<br />
will get to th ese vi a an RTS, as the Applesoft interpreter<br />
does. However our table e also holds the<br />
target tokens themse lves. T he first and second<br />
target tokens themselves. The first and second<br />
locations of CMDTBL (CoMmanD TaBLe) hold<br />
the e low loll' and high bytes of the address of the subroutine<br />
which will execute the command whose<br />
routine which will execute the command whose<br />
token is stored in the third location. Similarly y for<br />
the fourth and fifth locations (addresses) and the<br />
the fourth and fifth loca tions (addresses) and the<br />
sixth location (token), (token). and so on. TheT command<br />
table is stored in this ve rsion of the program at<br />
table is stored in this version of the program at<br />
location $0354. ThereT is no need whatever to store<br />
it here. YouY O LI can n put it in any convenient ni ent place in<br />
memory, as long as you change the four places in<br />
memory, as long as )'O U change th e four places in<br />
the program which refer to the starting address of<br />
the program which refer to the starting address of<br />
CMDTBL (before JMPGOT J MPCOT and after r GOTONE).<br />
COTONE).<br />
5. End of CMDT BL is indicated by a zero va lu e for<br />
5. End of CMDTBL is indicated by a zero value for<br />
the target token. We load the token into register Y<br />
at $3 1 ID 0 in order to test for r end of table. If Y is<br />
zero, the command token n is not one we are looking<br />
g<br />
for, r, so we exil.<br />
exit.<br />
The Labelled GOTO/GOSUB Example<br />
1. Comments on the patch.<br />
We \Ale use Smith's routine as an example partially<br />
all y<br />
because itw"s was published in COMPUTE!, so youu may<br />
be familiar with it, and partiallyy because we have<br />
found it quite usefui. useful. The routine has been completely<br />
rewritten in three ways, once for ROM<br />
Applesoft, once forr RAMCARD Applesoft, and<br />
once for r Applesoftl Ila. la, or TAPERAM. . A more<br />
complete discussionn of the logic of the routine is in<br />
Smith's anicle.<br />
article.<br />
The effect of the patch is as follows: Taking X<br />
to mean any arithmetic expression orr variable, (X<br />
may, but need not, be a literal number), then if the<br />
value of X is 1000, COTO GOTO X will be treated by<br />
Applesoft in the same way as COTO GOTO 1000. Similarly,<br />
COSUB GOSUB X will be treated as COSUB GOSUB 1000.<br />
Thus labels cann be defined forr subroutines (as all<br />
rational programming languages, including nearly<br />
all assemblers, allow) and for COT GOTOO statements<br />
(reminiscent of FORTRAN'S RAN's ASSIGN ICN statement). If<br />
X is a Teal real number, it is rounded down to the nearest<br />
integer, so be wary of arithmetic ti c expressions,<br />
expressions.<br />
This patch does not affect the behavior of<br />
ON .. . COT O and ON ... COSUB, so these must still<br />
use li ne numbers rather than labels. However, you<br />
ca n replace these computed COTO's in your code<br />
by computed COTO's of a very different type,<br />
which are reminiscent of PASCAL's CASE handling.<br />
As an example, at the start of your program you<br />
This patch does not affect the behavior of<br />
ON...GOTO and ON...GOSUB. so these must still<br />
use line numbers rather than labels. However, you<br />
can replace these computed GOTO's in your code<br />
by computed GOTO's of a very different type,<br />
which are reminiscent of PASCAL'S CASE handling.<br />
As an example, at the start of your program you<br />
might DIMension a matrix SELECT(20) and assign<br />
the values of 20 different line numbers to the<br />
values of SELECT(I). To GOTO these lines, you<br />
values of SELECT(I). T o COTO these lines, you<br />
can compute the valu e of I, and then COT O<br />
SELECT (I). With decent commenting on the intention<br />
and conditions of each choice of SELECT's<br />
line numbers (which is bes t done at the place in the<br />
program that the line numbers are actually ass igned<br />
to SELECT's elements,) your program will probably<br />
be much more readable than one with an equally<br />
commented ON ... COTO statement. T his is our<br />
experience with these two d iffe rent forms of computed<br />
COTO and COSUB, and we have stopped<br />
using g ON...GOTO . COTO completely.<br />
can compute the value of I, and then GOTO<br />
SELECT(I). With decent commenting on the in<br />
tention and conditions of each choice of SELECT's<br />
line numbers (which is best done at the place in the<br />
program that the line numbers are actually assigned<br />
to SELECT's elements,) your program will probably<br />
be much more readable than one with an equally<br />
commented ON...GOTO statement. This is our<br />
experience with these two different forms of com<br />
puted GOTO and GOSUB, and we have stopped<br />
Spaghetti Structure structure<br />
A different method of implementing a computed<br />
GOTO COT O is ideal for making your program structure<br />
resemble a plate of spaghetti. . If you change the<br />
value of X at various points in the program and<br />
repeatedly repeateclly use X in GOTO COTO X or GOSUB COSUB X statements,<br />
then, if yo u succeed in debuggin g your<br />
and neighbors with your ability to produce unintel<br />
ments, then, if you succeed in debugging your<br />
program, you will be able to amaze your friends<br />
and neighbors with your ability to produce unintel<br />
ligible, yet functional, code. The labelled GOTO/ COTOI<br />
GOSUB facility as presented here can be used to<br />
COS U B facili ty as presented here can be used to<br />
dramatically increase the readability of your program,<br />
or it can be abused to degrade the structure<br />
of your program. We strongly recommend that<br />
you assign line numbers to specific variables at the<br />
start of the program, use informative names for<br />
those variables, and never change their values once<br />
assigned.<br />
.<br />
2. The actual patches<br />
The ROM Applesoft patch is given in the<br />
appended listing, directlyy after the lookup table.<br />
The program is vinually the same as Smith's, with<br />
The program is virtually the same as Smith's, with<br />
a few more comments. nts, Note that whether er you use<br />
this program or Smith's, IF...THEN X, IF...GOTO IF...COTO<br />
X and IF...THEN .THE COTO GOTO X will not work properly.<br />
We have tried and tried, but cannot fixx this flaw in<br />
a program of reasonable length. A statement of the<br />
a program of reasonable length. A statement of the<br />
form IF...THEN:<br />
E : COTO GOTO X (or COSUB GOSUB X) will<br />
work correctly. There must be a colon between the<br />
THEN and the COTO GOTO or the COSU GOSUB.<br />
The RAM Applesoft patch is much simpler.<br />
For either RAM Applesoft version, you do not<br />
need the subinterpreter. Instead , modify COTO<br />
need the subinterpreter. Instead, modify GOTO<br />
directly. COTO GOTO is a subroutine of CO GOSUB, so this<br />
modifies both. Change the JSR $DA0C (the LINe<br />
modifies both. Change the J SR $DAOC {the LINe