ZX 81 - BASIC Programming

Sinclair ZX Spectrum
Appendix C.3 - The ZX81 for those that understand BASIC

Statements

In this list,
α represents a single letter
v represents a variable
x,y,z represent numerical expressions
e represents an expression
m,n represent numerical expressions that are rounded to the nearest integer
f represents a string valued expression
s represents a statement

Note that arbitrary expressions are allowed everywhere (except for the line numbers at the beginning of a statement).

All statements except INPUT can be used either as commands or in programs (although they may be more sensible in one than the other).

CLEAR Deletes all variables, freeing the space they occupied.
CLS (Clear Screen) Clears the display file. See chapter 27 concerning the display file.
CONT Suppose p/q was the last report with a non-zero. Then CONT has the effect GOTO q if p 9, GOTO q+1 if p = 9 (STOP statement)
COPY Sends a copy of the display to the printer, if attached; otherwise does nothing.

Unlike all other commands, a COPY command does not clear the screen first. There must be no spaces before COPY.

Report D if BREAK pressed.
DIM α$(n1,...,nk) Deletes any array or string with the name α$, & sets up an array α$ of characters with k dimensions n1,...,nk. Initializes all the values to "". This can be considered as an array of strings of fixed length nk, with k-1 dimensions n1,...,nk-1.

Error 4 occurs if there is no room to fit the array in. An array is undefined until it is dimensioned in a DIM statement.
FAST Starts fast mode, in which the display file is displayed only at the end of the program, while INPUT data is being typed in, or during a pause.
FOR α = x TO y FOR α = x TO y STEP 1
FOR α = x TO y STEP z Deletes any simple variable α, & sets up a control variable with value x, limit y, step z, & looping address 1 more than the line number of the FOR statement (-1 if it is a command). Checks if the initial value is greater (if step 0) or less (if step < 0) than the limit, & if so then skips to statement NEXT α at the beginning of a line.

See NEXT α.

Error 4 occurs if there is no room for the control variable.
GOSUB n Pushes the line number of the GOSUB statement onto a stack; then as GOTO n

Error 4 can occur if there are not enough RETURNs.
GOTO n Jumps to line n (or, if there is none, the first line after that).
IF x THEN s If x is true (non-zero) then s is executed. The form 'IF x THEN line number' is not allowed.
INPUT v Stops (with no special prompt) & waits for the user to type in an expression; the value of this is assigned to v. In fast mode, the display file is displayed. INPUT cannot be used as a command; error 8 occurs if you try.

If the first character in the INPUT line is STOP, the program stops with report D.
LET v=e Assigns the value of e to the variable v.

LET cannot be omitted.

A simple variable is undefined until it is assigned to in a LET or INPUT statement.

If v is a subscripted string variable, or a sliced string variable (substring), then the assignment is Procrustean: the string value of e is either truncated or filled out with spaces on the right, to make it the same length as the variable v.
LIST LIST 0
LIST n Lists the program to the television, starting at line n, & makes n the current line.

Error 4 or 5 if the listing is too long to fit on the screen; CONT will do exactly the same again.
LLIST LLIST 0
LLIST n Like LIST, but using the printer instead of the television.

Should do nothing if the printer is not attached.

Stops with report D if BREAK pressed.
LOAD f Looks for a program called f on tape, & loads it & its variables.

If f = "", then loads the first program available.

If BREAK is pressed or a tape error is detected, then

    (i) if no program has yet been read from tape, stops with report D & old program;

    (ii) if part of a program has been read in, then executes NEW.
LPRINT ... Like PRINT, but using the printer instead of the television. A line of text is sent to the printer.

    (i) when printing spills over from one line to the next,

    (ii) after an LPRINT statement that does not end in a comma or a semicolon,

    (iii) when a comma or TAB item requires a new line, or

    (iv) at the end of the program, if there is anything left unprinted.

In an AT item, only the column number has any effect; the line number is ignored (except that the same error conditions arise as for PRINT if it is out of range). An AT item never sends a line of text to the printer.

There should be no effect if the printer is absent. Stops with report D if BREAK is pressed.
NEW Starts the BASIC system off anew, deleting program & variables, & using the memory up to but not including the byte whose address is in the system variable RAMTOP (bytes 16388 & 16389).
NEXT α     (i) Finds the control variable α.

    (ii) Adds its step to its value.

    (iii) If the step 0 & the value > the limit; or if the step < 0 & the value < the limit, then jumps to the looping line.

Error 1 if there is a simple variable α.

Error 2 if there is no simple or control variable α.
PAUSE n Stops computing & displays the display file for n frames (at 50 frames per second) or until a key is pressed.

0 n 65535, else error B. If n 32767 then the pause is not timed, but lasts until a key is pressed.
PLOT m,n Blacks in the pixel ( m , n ); moves the PRINT position to just after that pixel.

0 m 63, 0 n 43, else error B.
POKE m,n Writes the value n to the byte in store with address m.

0 m 65535, -255 n 255, else error B.
PRINT ... The '...' is a sequence of PRINT items, separated by commas or semicolons, & they are written to the display file for display on the television. The position (line & column) where the next character is to be printed is called the PRINT position.

A PRINT item can be

    (i) empty, i.e. nothing


    (ii) a numerical expression.

First, a minus sign is printed if the value is negative. Now let x be the modulus of the value.

If x 10-5 or x 1013, then it is printed using scientific notation. The mantissa part has up to eight digits (with no trailing zeros), & the decimal point (absent if only one digit) is after the first. The exponent part is E, followed by + or -, followed by one or two digits.

Otherwise x is printed in ordinary decimal notation with up to eight significant digits, & no trailing zeros after the decimal point. A decimal point right at the beginning is always followed by a zero, so for instance .03 & 0.3 are printed as such.

0 is printed as a single digit 0.

    (iii) a string expression.

The tokens in the string are expanded, possibly with a space before or after.

The quote image character prints as ".

Unused characters & control characters print as ?.

    (iv) AT m,n

The PRINT position is changed to line m (counting from the top, column n (counting from the left).

0 m 21, else error 5 if m = 22 or 23, error B otherwise.

0 n 31, else error B.

    (v) TAB n

n is reduced modulo 32. Then, the PRINT position is moved to column n, staying on the same line unless this would involve backspacing, in which case it moves on to the next line.

0 n 255, else error B.

A semicolon between two items leaves the PRINT position unchanged, so that the second item follows on immediately after the first. A comma, on the other hand, moves the PRINT position on at least one place, & after that, however many as are necessary to leave it in column 0 or 16, throwing a new line if necessary.

At the end of the PRINT statement, if it does not end in a semicolon or comma, a new line is thrown.

Error 4 (out of memory) can occur with 3K or less of memory.

Error 5 means that the screen is filled.

In both cases, the cure is CONT, which will clear the screen & carry on.
RAND RAND 0
RAND n Sets the system variable (called SEED) used to generate the next value of RND. In n 0, then SEED is given the value n; if n = 0 then it is given the value of another system variable (called FRAMES) that counts the frames so far displayed on the television, & so should be fairly random.

Error B occurs if n is not in the range 0 to 65535.
REM ... No effect. '...' can be any sequence of characters except NEWLINE.
RETURN Pops a line number from the GOSUB stack, & jumps to the line after it.

Error 7 occurs when there is no line number on the stack. There is some mistake in your program; GOSUBs are not properly balanced by RETURNs.
RUN RUN 0
RUN n CLEAR, & then GOTO n.
SAVE f Records the program & variables on tape, & calls it f. SAVE should not be used inside a GOSUB routine.

Error F occurs if f is the empty string, which is not allowed.
SCROLL Scrolls the display file up one line, losing the top line & making an empty line at the bottom.

NB the new line is genuinely empty with just a NEWLINE character & no spaces. (See chapter 27).
SLOW Puts the computer into compute & display mode, in which the display file is displayed continuously, & computing is done during the spaces at the top & bottom of the picture.
STOP Stops the program with report 9. CONT will resume with the following line.
UNPLOT m,n Like PLOT, but blanks out a pixel instead of blacking it in.

Sinclair ZX Spectrum

  Previous Page Back