Kitchen Sync

See all articles from SYNC v1 n5

We introduce in this issue the “Kitchen SYNC” by Alan Groupe, Michael Tardiff, and Ivan Zatkovich. This is not a column on cooking with the ZX80/1 (although conceivably that could be covered). Rather, the wide ranging interests of this trio are suggested.

Furthermore, they are open to suggestion for topics you want to hear more about. We will pass along all requests.

Expression Evaluators at Work

The three of us work for Digital Equipment Corporation, and, in the course of our work, we have unlimited access to a great number of large computers. Yet we each recently bought ZX80s. Why?

We were warned that the ZX80 was a relatively tiny computer, almost a toy, with limited capabilities and a “very small” amount of memory. But it was the restrictions and the limitations of the machine that interested us. The computers we use every day have millions of bytes of memory; operating systems occupy tens of thousands of bytes of memory; programs have all the RAM they need and more. There is comparatively little need to “economize” in writing software. When such a need arises, we talk of shrinking a 20K program to fit into 16K. We decided that it would be fun to see just how far a “little” machine could be pushed.

Since then, we have become impressed with some of the features this compact machine does offer, and we would like to share some of our observationss and discoveries with you.

One of the first unusual (to us) properties of the ZX80 version of Basic that we “discovered” is that anywhere you need to enter a number, you can enter an equation instead. Or, to put it more impressively, if less comprehensibly, the expression evaluator is called at each instance of a value-required context.

What does that mean, and why is it good?

If a computer language is to accept and solve equations, which we will call “expressions,” it must have the ability to take an expression as input and return a numeric value as output. This portion of the language is called the “expression evaluator.”

The expression evaluator first gets values for the variables in an expression and then performs the indicated arithmetic operations to end up with a single value. For example, enter the following commands into your ZX80 in the immediate mode (that is, without typing line numbers first).

LET A=5
LET B=3
LET X=A+B
PRINT X

You should have seen an “8” at the top of your screen. When you entered the third statement, the expression evaluator looked in memory and found the value of A (which was 5) and the value of B (set to 3 in the second LET statement), then added them together and filed the result under X.

While normally the expression evaluator is only used to handle arithmetic statements, like LET; on the Sinclair machine it is used anywhere a number can be entered. For example, in a GO TO statement, you could insert an expression in place of the statement number of the GO TO. Instead of:

GO TO 40

you could write

GO TO X+10

If X equals 30, the expression evaluator would first search out the value of X, add 30 and 10, and then “GO TO” the result: statement number 40.

In a machine like the ZX80, it is not much trouble for Basic to use the expression evaluator often, but it certainly can be very handy for us in writing programs.

For an example, enter and run the following small program:

10 INPUT LAST
20 PRINT LAST
30 GO TO 10

When you are prompted for input, enter the number 3. The number 3 will appear on the screen. Enter an 11 and an 11 appears. On lesser machines (like the TRS-80, etc.) this is all the program will do. But on the Sinclair, you have just written a simple calculator! Enter 3+9 and 12 appears on the screen. 4*7 gives you a 28. In fact, you can even use the previous answer in an expression (assuming you have typed in at least one expression previously). Enter LAST-5 and the Sinclair responds with 23 (assuming you been entering all the examples).

When you entered 3, the expression evaluator was used. It evaluated the expression and returned the result (3), which was stored in the variable LAST. When you entered 4*7, the expression evaluator evaluated the expression to 28 which was stored in LAST. And when you entered LAST-5, the expression evaluator recalled the value for LAST (28), subtracted 5 from it, and returned the result (23) which was stored in LAST.

Another posible use of this technique is in the following rather crude telephone directory:

 10 PRINT "ALAN GROUPE"
20 PRINT "ALANS ADDRESS"
30 PRINT "ALANS PHONE"
40 STOP
50 PRINT "MICHAEL TARDIFF"
60 PRINT "MICHAELS ADDRESS"
70 PRINT "MICHAELS PHONE"
80 STOP
90 PRINT "IVAN ZATKOVICH"
100 PRINT "IZZIES BAR AND GRILL"
110 PRINT "IVANS PHONE"
120 STOP

Running this program by entering “RUN” is of little value, since it will always print only the first person’s information. So you must run the program with the GO TO command rather than the RUN command. If you want Alan’s information, you enter GO TO 10. If you want Ivan’s, you enter GO TO 90.

This is all well and good, except that remembering the numbers is probably harder than remembering the addresses and phone numbers. However, after typing in the program, you can enter the following commands in immediate mode (that is, without typing line numbers first):

LET GROUPE=10
LET TARDIFF=50
LET ZATKOVICH=90

Now, if you want information on Ivan Zatkovich, you only need to enter GO TO ZATKOVICH. Again the expression evaluator is used. It determines that the variable ZATKOVICH has the value of 90 and then GOes TO statement 90. When you save a program on tape, the values of the variables are also saved; so when you load your telephone directory again you will not have to re-enter the LET statements. A word of warning though—the RUN command clears the values of all the variables. If you accidentally enter RUN, you will have to re-enter all the LET statements or LOAD the tape again.

The expression evaluator will also work with strings as well as with numbers, although we do not see a use for this feature at the moment. Run the following program:

10 INPUT A$
20 PRINT A$,CODE(A$)
30 GO TO 10

You will notice that the cursor appears on the screen between a pair of quotation marks(“”). Enter the letter A. On the screen will appear the letter A and the number 38, which is the ZX80’s internal numeric code for representing the character A. Now, using the RUBOUT and arrow keys, rubout the two quotation marks. You will notice that the familiar syntax error symbol appears. This is because the INPUT statement is looking for a string, and strings are delimited by quotation marks. If you enter “A” (including the quotation marks) you will see that it is accepted, because “A” is a valid string no matter whether you typed the quotation marks or whether the machine did it for you.

Certain internal functions of the ZX80 (those whose names end in $, such as CHR$), return strings as their outputs. As before, rubout the two quotation marks, but this time enter CHR$(38) (no quotation marks). Once again, the expression evaluator is used and evaluates the expression, returning the string “A” as its output. Since this is a valid string, it satisfies the INPUT statement and is accepted.

Now it is up to you to figure out a use for this feature of the ZX80. Send your discoveries to SYNC.