Classic Computer Magazine Archive COMPUTE! ISSUE 70 / MARCH 1986 / PAGE 10

Embedded BASIC Words

I usually pay little or no attention to spacing when typing BASIC programs, but when using "MLX II" to type in a program from the December 1985 issue of COMPUTE!, I ran into a puzzling problem. Everything worked fine until I tried to load data and received the message SYNTAX ERROR IN LINE 830. I rechecked the line and found that everything was correct, except that I hadn't used the same spacing shown in the magazine listing. When I corrected the spacing, the program worked perfectly. After a little further investigation, I discovered that the space causing the problem was between ST and AND. Why does that space make such a difference?

Jim David

Here's what that portion of line 830 looks like:

IF ST AND(I<>B)THEN F = 2

Both AND and ST are reserved words in Commodore BASIC, meaning that BASIC lets you use them for only one purpose. ST (STatus) is a reserved variable that indicates the status of input/output operations like loading or saving to disk. AND is a logical operator which in this case connects the value of ST with the value of the expression (I<>B).

Typing STAND(I<>B) instead of ST AND(I<>B) makes the computer see a third reserved word in the line—the numeric function TAN (TANgent). Since TAN, like other functions, must be followed by something inside parentheses, the computer responds with a syntax error message when it finds the letter D instead of a left parenthesis. That's a nutshell explanation for the error. But you may still wonder why the computer sees TAN inside the word STAND. After all, the words ST and AND seem to be there as well.

The short program below shows exactly why TAN appears. Don't worry about the fact that line 10 looks strange. We're not going to execute that line—it's only there to let us examine how BASIC handles these reserved words.

10 :ST:TAN:AND::STAND::
20 PRINT CHR$(14);CHR$(147)
30 FOR J = 0 TO 19: POKE 1024+J, PEEK(2049+J): POKE 55296+J, 1: NEXT

After typing the program, enter GOTO 20 and press RETURN (don't start the program with RUN). Line 30 PEEKs the first 20 bytes of BASIC program space and displays their contents on the screen, showing you how the computer stores line 10 in memory. As you'll see, the reserved variable ST is stored as the ASCII characters S and T, exactly what you typed in. This is the way all variable names are stored. However, both TAN and AND are changed into one-byte tokens, which appear here as reverse video characters. Most BASIC words are tokenized—compressed into a single numeric value—to save space and make BASIC run faster. Between the double colons we placed in the line as markers, you can see how the computer handles the character sequence S-T-A-N-D. When it tokenizes a BASIC line, the computer reads from left to right, just as you do. The initial S in STAND is left unchanged, since it isn't part of a keyword that can be tokenized. Next, the computer finds the characters T-A-N, which it replaces with the one-character token for TAN. That leaves the character D, which is also left unchanged.

After TAN is tokenized, the computer can't possibly see ST or AND (T and AN are missing), so the line can't work as intended. In this case, it was coincidental that the combination of two reserved words made a third reserved word. However, the same thing would happen if you omitted a space between ST and the logical operator OR. When the computer scans the characters S-T-O-R, it changes the embedded keyword TO into a token. For similar reasons you should be careful not to use variable names like TOP, NOTE, or FORK, which also contain embedded BASIC words (TO, NOT, and FOR).