# SINCLAIR ZX SPECTRUM BASIC Programming

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

A number is stored in the ZX Spectrum in floating point binary with one exponent byte e (l<=e<=255), and four mantissa bytes m (1/2<=m<l). This represents the number m*2e-128.

Since 1/2<=m<l, 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.

Small integers have a special representation in which the first byte is 0, the second is a sign byte (0 or FFh) and the third and fourth are the integer in twos complement form, the less significant byte first.

Numeric variables have names of arbitrary length, starting with a letter and continuing with letters and digits. Spaces and colour controls are ignored and all letters are converted to lower-case letters.

Control variables of 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

 1 empty or 2 numerical expression or 3 optional numerical expression TO optional numerical expression

and is used in expressing a substring either by

(a) string expression (slicer)

(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, 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), and 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 and ending with the nth. If 0<=n&ltm 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).

If a string quote is to be written in a string literal, then it must be doubled.

#### Functions

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

Function   Type of argument (x)   Result
ABS number Absolute magnitude
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 A\$ if B=0
Error A if x not in the range -1 to +1
ATTR two arguments, x and y, both numbers; enclosed in brackets A number whose binary form codes the attributes of line x, column y on the television. Bit 7 (most significant) is 1 for flashing, 0 for not flashing. Bit 6 is 1 for bright, 0 for normal. Bits 5 to 3 are the paper colour. Bits 2 to 0 are the ink colour. Error B unless 0<=x<=23 and 0<=y<=31
BIN   This is not really a function, but an alternative notation for numbers: BIN followed by a sequence of 0s and 1s is the number with such a representation in binary.
CHR\$ number The character whose code is x, rounded to the nearest integer
CODE string The code of the first character in x (or 0 if x is the empty string)
COS number (in radians) Cosine x
EXP number ex
FN   FN followed by a letter calls up a user-defined function (see DEF). The arguments must be enclosed in brackets; even if there are no arguments the brackets must still be present.
IN number The result of inputting at processor level from port x (0<=x<=FFFFh) (loads the bc register pair with x and does the assembly language instruction in a(c))
INKEY\$ none Reads the keyboard. The result is the character representing (in L or C 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 is not in the range 0 to 65535
PI none (3.14159265 . . . )
POINT Two arguments, x and y, both numbers; enclosed in brackets 1 if the pixel at (x,y) is ink colour. 0 if it is paper colour. Error B unless 0<=x<=255 and 0<=y<= 175
RND none The next pseudorandom number in a sequence generated by taking the powers of 75 modulo 65537, subtracting 1 and dividing by 65536. 0<=y< 1
SCREEN\$ Two arguments, x and y, both numbers; enclosed in brackets The character that appears, either normally or inverted, on the television at line x, column y. Gives the empty string, if the character is not recognised. Error B unless 0<=x<=23 and 0<=y<=31
SGN number Signum: the sign (-1 for negative, 0 for zero or +1 for positive) of x
SQR number Square root.
Error A if x<0
STR\$ number The string of characters that would be displayed if x were printed
USR number Calls the machine code subroutine whose starting address is x. On return, the result is the contents of the bc register pair
USR string The address of the bit pattern for the user-defined graphic corresponding to x.
Error A if x is not a single letter between a and u, or a user-defined graphic
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
VAL\$ string Evaluates x (without its bounding quotes) as a string expression.
Error C if x contains a syntax error or gives a numeric value. Other errors possible, as for VAL
- number Negation

The following are binary operations:

 + Addition (on numbers), or concatenation (on strings) - Subtraction * Multiplication / Division Raising to a power. Error B if the left operand is negative = Equals > Greater than Both operands must be of the same type. The result is a number 1, if the comparison holds and 0 if it does not < Less than <= Less than or equal to >= Greater than or equal to <> Not equal to

Functions and operations have the following priorities:

 Operation Priority Subscripting and slicing 12 All functions except NOT and unary minus 11 10 Unary minus (i.e. minus just used to negate something) 0 *, / 8 +, - (minus used to subtract one number from another) 6 =, >, <, <=, >=, <> 5 NOT 4 AND 3 OR 2

#### Statements

In this list,

 a represents a single letter v represents a variable x, y, z represent numerical expressions m, n represent numerical expressions that are rounded to the nearest integer e represents an expression f represents a string valued expression s represents a sequence of statements separated by colons c represents a sequence of colour items, each terminated by commas, or semi-colons; a colour item has the form of a PAPER, INK, FLASH, BRIGHT, INVERSE or OVER statement.

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

All statements except INPUT, DEF and DATA can be used either as commands or in programs (although they can be more sensible in one than the other). A command or program line can have several statements, separated by colons (:). There is no restriction on whereabouts in a line any particular statement can occur - although see IF and REM.