The Computer Tutor: Syntactic Sum

Author(s): Gene G. Buza
Publication: Synchro-Sette
Pub Details: Synchro-Sette v1 n5 (Volume 1, Issue 5)
Date: August 1982
Pages: 14-16
Computer Tutor illustration, tutor pointing at a computer
Computer Tutor illustration, tutor pointing at a computer

Good morning class. This month’s discussion regards the ubiquitous phrase, SYNTACTIC SUM.

You might have seen this phrase in other publications and wondered what it meant. I see the heads nodding in approval.

Does anyone know what it means? Yes, the gentleman in the back of the room.

No, that’s wrong. It doesn’t mean the sum total of how many times we have tactically sinned, although that is close. Nor does it mean the total of syntax errors in a program or the size of the program or anything else like that.

The number that follows the phrase SYNTACTIC SUM refers to the sum total of the numeric values of all the character set codes in the RAM memory locations when the program in question resides in the ZX-81 computer.

The gentleman in the rear asks what the heck is that good for. Well, I’ll tell you:

Have you ever typed in a program and found it didn’t work? You probably wondered if it was a mistake that you typed in or a mistake in the actual listing of the program.

If the SYNTACTIC SUM was available for that program, you could add up all the values in the RAM locations and compare the total to the SYNTACTIC SUM total, which is what it should be.

If they are the same, the program is probably entered correctly. If they’re not, start looking for errors in the listing.

“How do we know which RAM locations to check?” asks the man in the back. Well, the program listing starts in RAM location 16513. Enter the following line:

10 REM

Now enter PRINT PEEK 16513. The computer responds with the number 234 and if you look on page 141 of the Sinclair manual, you will see that 234 is the character code for the command REM.

If we enter PRINT PEEK 16512, the computer responds with the number 0. This does not, however, correspond with the 0 in the line number 10 in the listing. To prove this, delete line 10 and enter the line – 9 REM.

Now enter PRINT PEEK 16512. The computer still responds with the number 0. We now know that RAM memory location 16512 (and all the locations lower than that number) does not correlate with the listed program but that location 16513 has the character code of the first item after the first line number of the program.

If our only program line was – 10 REM A – and we PEEKed the next RAM location after 16513, which of course is 16514, we would find the number 38, which is the character code for the letter (A).

Each time we add another character to the end of the line, the next available RAM location picks up the character code. However, when we enter another line number, the RAM locations start picking up all sorts of strange character code numbers, which may or may not correlate with what we see in the actual listing, but we won’t get into the mechanics of that now.

If a program is entered properly and we were to add up all the character codes up to the last item listed, the total should be the same, whether it is done on your computer, my computer, or anyone else’s computer. This way we have, so to speak, a common method of checking our spelling, but we have not as of yet, the tool to accomplish this task. Does anyone have an idea on how it can be done? Yes, the hand in the back again?

OK, the gentleman says all we have to do is to write a routine that would be part of the program and that would add up all of the character codes from the first memory location, 16513, to the last available location in memory, depending on whether it was a 1K or 16K machine, and then subtract the character code total of the routine itself.

Wrong again, Sir.

At first glance, this method seems like it would work. The problem is that this type of routine looks at the RAM locations AFTER THE PROGRAM and adds their values too. The values that reside in memory locations not used by the program may be random values. Enter the following program and RUN it:

10 LET N = 16513
20 SCROLL
30 PRINT N; " - "; PEEK N
CHR$ PEEK N
40 LET N = N + 1
50 GOTO 20

This program is actually a mini-machine language monitor and allows you to see three columns of information. The first is the RAM memory location, the second is the character code, and the third is the actual character being held in that location.

In the running mode, the scroll occurs pretty fast, so to turn control over to you, add the following lines:

35 INPUT A$
36 IF A$ = "P" THEN STOP

Now RUN the program again and advance the SCROLL by pressing the ENTER key.

See what I mean? At first you can easily see characters in the third column that correspond to the program items. No line numbers are observed, unless they are following GOTO (or GOSUB) commands. After the program has SCROLLed beyond its last memory location, you start to see the stuff that inhabits the unused RAM memory locations. Kinda gives you an idea what the movie TRON is all about. These memory location values are what we programmers technically refer to as “junk”.

For many of you, this will probably be your first introduction to machine-language.

No, as usual, the answer submitted by the gentleman in the rear won’t work. What we need is to find the last location of the program. There are routines that do that, but depend on knowing how much RAM the computer has. We want a routine that will work on the ZX-81 and not have to worry about how much RAM the machine has. Here is a routine that accomplishes that objective:

9992 REM COPY COPY
(COPY is command)
9993 LET N = 16513
9994 LET X = 0
9995 LET Z = PEEK N
+ PEEK (N + 1)
9996 IF Z = 510 THEN
PRINT X - 234
9997 LET X = X + PEEK N
9998 LET N = N + 1
9999 GOTO 9995

If you enter this routine by itself and RUN it, it will give a total of 0. The man in the back is wildly waving his hand. Yes, what is it?

Oh, yes, line 9992 – the computer does not allow the command COPY to appear after REM. Good point.

Here’s how you do it. After you have entered (9992), enter the command (COPY). Now backspace with the arrow key (shift 5) and enter (COPY) again. Put in the (REM) command the same way.

The purpose of the two (COPY) commands in line 9992 is to provide a situation that would not occur in a normal program.

The character code for (COPY) is 255 and lines 9995 and 9996 search for two adjacent memory locations containing 255 in each of them. If this situation is encountered, the sum of all the character codes before the first of these two locations, less the amount of 234, which represents the character code of the (REM) command that precedes the first RAM memory location that has 255 in it, is displayed.

This is the SYNTACTIC SUM, and once it is displayed you can now BREAK the program. Again, this routine will work no matter what size memory you have in your computer.

If you have attached it to the MINI-MACHINE MONITOR program, you can execute the SYNTACTIC SUM routine by ENTERing (GOTO 9992).

The SYNTACTIC SUM of that program is 5655. If you delete lines 35 and 36, it is 4200.

The SYNTACTIC SUM for the CHECKBOOK program in the July/82 issue is 32445. Unfortunately, the nerd who wrote it made a mistake in line 80 of the program where B$(2) should be B$(1). It’s really degrading having to work with some people !!!

The listed programs in the SYNCHRO-SETTE magazine will have their SYNTACTIC SUMS listed from now on. If any of you are submitting listed programs in the future, include the SYNTACTIC SUM with your listing.

For your homework, I want you to find the SYNTACTIC SUM of the SYNTACTIC SUM routine. Class dismissed.

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