ZX 81 - BASIC Programming

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

The BASIC

Numbers are stored to an accuracy of 9 or 10 digits. The largest number you can get is about 1038, & the smallest (positive) number is about 4 * 10-39.

A number is stored in the ZX81 in floating point binary with one exponent byte e (1 e 255), & four mantissa bytes m (½ m 1). This represents the number m * 2e-128.

Since ½ m 1, the most significant bit of the mantissa m is always 1. Therefore in actual fact we can replace it with a bit to show the sign - 0 for positive numbers, 1 for negative.

Zero has a special representation in which all 5 bytes are 0.

Numeric variables have names of arbitrary length, starting with a letter and continuing with letters and digits. All these are significant, so that for instance LONGNAME & LONGNAMETOO are distinct names. Spaces are ignored.

Control variables for FOR-NEXT loops have names a single letter long.

Numeric arrays have names a single letter long, which may be the same as the name of a simple variable. They may have arbitrarily many dimensions of arbitrary size. Subscripts start at 1.

Strings are completely flexible in length. The name of a string consists of a single letter followed by $.

String arrays can have arbitrarily many dimensions of arbitrary size. The name is a single letter followed by $ and may not be the same as the name of a string. All the strings in a given array have the same fixed length, which is specified as an extra, final dimension in the DIM statement. Subscripts start at 1.

Slicing: Substrings of strings may be specified using slicers.

A slicer can be

    (i) empty
or
    (ii) numerical expression
or
    (iii) optional numerical expression TO optional numerical expression
& is used in expressing a substring either by
    (a) string expression (slicer)
or by
    (b) string array variable (subscript,...,subscript, slicer)
which means the same as

string array variable (subscript,...,subscript)(slicer)

In (a), suppose the string expression has the value s$.

If the slicer is empty, then the result is s$ considered as a substring of itself.

If the slicer is a numerical expression with value m, then the result is the mth character of s$ (a substring of length 1).

If the slicer has the form (iii), then suppose the first numerical expression has the value m (the default value is 1), & the second, n (the default value is the length of s$).

If 1 m n the length of s$ then the result is the substring of s$ starting with the mth character & ending with the nth.

If 0 n m then the result is the empty string.

Otherwise, error 3 results.

Slicing is performed before functions or operations are evaluated, unless brackets dictate otherwise.

Substrings can be assigned to (see LET).

The argument of a function does not need brackets if it is a constant or a (possibly subscripted or sliced) variable.

Function Type of operand Result
  (x)  
ABS number Absolute magnitude.
ACS number Arccosine in radians.

Error A if x not in the range -1 to +1.
AND binary operation, right
operand always a number
Numeric left operand:

A AND B = A if B 0, 0 if B = 0

String left operand:

A$ AND B = A$ if B 0, "" if B = 0
ASN number Arcsine in radians.

Error A if x not in the range -1 to +1.
ATN number Arctangent in radians.
CHR$ number The character whose code is x, rounded to the nearest integer.

Error B if x not in the range 0 to 255.
CODE string The code of the first character in x (or 0 if x is the empty string).
COS number (in radians) Cosine
EXP number ex
INKEY$ none Reads the keyboard. The result is the character representing (in mode) the key pressed if there is exactly one, else the empty string.
INT number Integer part (always rounds down).
LEN string Length
LN number Natural logarithm (to base e) Error A if x 0
NOT number 0 if x 0, 1 if x = 0.

NOT has priority 4.
OR binary operation, both
operands numbers
A OR B = 1 if B 0, A if B = 0

OR has priority 2.
PEEK number The value of the byte in memory whose address is x (rounded to the nearest integer).

Error B if x not in the range 0 to 65535.
PI none (3.14159265..)
RND none The next pseudo-random number y in a sequence generated by taking the powers of 75 modulo 65537, subtracting 1 & dividing by 65536. 0 y 1.
SGN number Signum: the sign (-1, 0 or +1) of x.
SIN number (in radians) Sine
SQR number Square root

Error B if x < 0
STR$ number The string of characters that would be displayed if x were printed.
TAN number (in radians) Tangent
USR number Calls the machine code subroutine whose starting address is x (rounded to the nearest integer). On return, the result is the contents of the bc register pair. Error B if x is not in the range 0 to 65535.
VAL string Evaluates x (without its bounding quotes) as a numerical expression.

Error C if x contains a syntax error, or gives a string value.

Other errors possible, depending on the expression.
- number Negation

The following are binary operations:

+ Additional (on numbers), or concatenation (on strings).
- Subtraction
* Multiplication
/ Division
** Raising to a power.

Error B if the left operand is negative.
= Equals*
> Greater than*
< Less than*
<= Less than or equal to*
>= Greater than or equal to*
<> Not equal to*
* Both operands must be of the same type. The result is a number, 1 if the comparison holds & 0 if it does not.

Functions & operations have the following priorities:

Operation Priority
Subscripting & slicing 12
All functions except NOT and unary minus 11
** 10
Unary minus 9
* , / 8
+ , - (binary -) 6
=, >, <, <=, >=, <> 5
NOT 4
AND 3
OR 2

Sinclair ZX Spectrum

  Previous Page Back Next Page