Paradigm | Imperative |
---|---|
Designed by | John Grant, Steve Vickers |
Developer | Nine Tiles Networks, Sinclair Research |
First appeared | 1979 |
Platform | ZX80, ZX81, ZX Spectrum, +, 128, +2, +3, T/S 2068, TC 3256 |
License | Proprietary |
Sinclair BASIC is a dialect of the programming language BASIC used in the 8-bit home computers from Sinclair Research and Timex Sinclair. The Sinclair BASIC interpreter was made by Nine Tiles Networks Ltd.[1]
Sinclair BASIC was originally developed in 1979 for the ZX80 by Nine Tiles. The programmers were John Grant, the owner of Nine Tiles, and Steve Vickers.
It was initially an incomplete implementation of the 1978 American National Standards Institute (ANSI) minimal BASIC standard with integer arithmetic only, termed 4K BASIC (for its ROM size) for the ZX80. It evolved through the floating-point 8K BASIC for the ZX81 and T/S 1000 (which was also available as an upgrade for the ZX80[2]), and became an almost complete version in the 16 KB ROM ZX Spectrum (known as 48K BASIC).
It is present in all ZX Spectrum compatibles and clones, with more advanced systems also offering expanded versions like 128K BASIC, +3 BASIC, T/S 2000 BASIC, BASIC64 or Timex Extended Basic.
As of 2015, interpreters exist for modern operating systems,[3] and older systems,[4] that allow Sinclair Basic to be used easily.
On the 16K/48K ZX Spectrum (48K BASIC), there are 88 keywords in Sinclair BASIC, denoting commands (of which there are 50), functions and logical operators (31), and other keywords (16, including 9 which are also commands or functions):
Keyword | Parameters[note 1] | Entered using[note 2] | Type | Summary |
---|---|---|---|---|
ABS
|
number
|
EXTENDED MODE then G | Function | Returns the absolute value of number [5]
|
ACS
|
number
|
EXTENDED MODE then SYMBOL SHIFT+W | Function | Returns the arccosine of number [6]
|
AND
|
SYMBOL SHIFT+Y | Logical operator | Returns true if both conditions on either side of the AND keyword are true, else returns false[note 3][7]
| |
ASN
|
number
|
EXTENDED MODE then SYMBOL SHIFT+Q | Function | Returns the arcsine of number [8]
|
AT
|
line, column;
|
SYMBOL SHIFT+I | Other | Used in a PRINT statement to print at the line and column specified;[9] for example, PRINT AT 5,10;"*" puts a star in column 10 of line 5.
|
ATN
|
number
|
EXTENDED MODE then SYMBOL SHIFT+E | Function | Returns the arctangent of number [6]
|
ATTR
|
(line, column)
|
EXTENDED MODE then SYMBOL SHIFT+L | Function | Returns a byte containing information on the colours of the text cell on the screen, corresponding to the specified line and column ;note that, unlike most Sinclair BASIC keywords, the parentheses are required; the first three bits indicate the ink (foreground)colour, the fourth, fifth and sixth bits the paper (background) colour, the seventh bit whether the colours are bright or not, and the eight, whether they are flashing[10]
|
BEEP
|
duration, pitch
|
EXTENDED MODE then SYMBOL SHIFT+Z | Command | Produces sound from the computer's speaker; duration is in seconds, pitch is in semitones above (positive value) or below (negative value) middle C[11]
|
BIN
|
number
|
EXTENDED MODE then B | Other | Indicates number is in binary notation[12]
|
BORDER
|
number
|
B | Command | Sets the border of the screen to the colour specified by the number [note 4][13]
|
BRIGHT
|
number
|
EXTENDED MODE then SYMBOL SHIFT+B | Command/other | Makes all following colours brighter if number is 1, or its normal shade if 0[note 5][14]
|
CAT
|
number
|
EXTENDED MODE then SYMBOL SHIFT+9 | Command | Displays contents of ZX Microdrive specified by number [note 6][15]
|
CHR$
|
number
|
EXTENDED MODE then U | Function | Returns the character corresponding to the decimal number in the computer's character set[16]
|
CIRCLE
|
x, y, r
|
EXTENDED MODE then SYMBOL SHIFT+H | Command | Draws a circle with its centre at coordinates (x ,y ) and radius r [17]
|
CLEAR
|
address
|
X | Command | Clears the screen,[18] all variables and the GO SUB stack,[19] and optionally sets the maximum RAM address to be used by BASIC[20]
|
CLOSE #
|
number
|
EXTENDED MODE then SYMBOL SHIFT+5 | Command | Closes the specified stream number for access[note 6] |
CLS
|
V | Command | Clears all text and graphics from the screen[18] | |
CODE
|
string
|
EXTENDED MODE then I | Function/other | Returns the number corresponding to the first character in string in the computer's character set;[16] also used to save arbitrary chunks memory to tape, disk, etc. and load them back in — see LOAD , SAVE and VERIFY for details
|
CONTINUE
|
C | Command | Restarts a program after it has stopped due to an error or the user pressing the CAPS SHIFT+SPACE or BREAK keys[21] | |
COPY
|
Z | Command | Sends the currently displayed screen to the printer[22] | |
COS
|
number
|
EXTENDED MODE then W | Function | Returns the cosine of number [23]
|
DATA
|
comma-separated values
|
EXTENDED MODE then D | Command/other | Provides numbers and/or strings to use with the READ command[24] and allows saving the contents of an array to tape when used with the SAVE command[25] (as SAVE filename DATA array name()
|
DEF FN
|
name(variable) = operation
|
EXTENDED MODE then 1 | Command | Defines a custom function that can be used with the FN command;function definitions must be of the form f(x)=operations , for example f(x)=x*2 and the function name may not consist of more than one letter, plus a $-symbol if the function returns a string[26]
|
DIM
|
variable(dimensions)
|
D | Command | Declares an array with the specified dimensions , which may be multi-dimensional (for example, DIM a(10,10) ; if used with strings, the last dimension indicates the length of each of the strings (thus, DIM a$(2,5) is an array of two strings each of five characters long, and DIM b$(5) is one string of five characters)[27]
|
DRAW
|
x, y [, r]
|
W | Command | Draws a line in the current INK colour to coordinates (x ,y ) from the coordinates used by the previous PLOT or DRAW command; if the optional r is supplied, it indicates the radius of the circle segment to be drawn, in radians[28]
|
ERASE
|
drive;"filename"
|
EXTENDED MODE then SYMBOL SHIFT+7 | Command | Deletes the specified file from a ZX Microdrive[note 6][29] |
EXP
|
number
|
EXTENDED MODE then X | Function | Returns e to the power number [30]
|
FLASH
|
number
|
EXTENDED MODE then SYMBOL SHIFT+V | Command/other | Makes all following text alternate its foreground (INK ) and background (PAPER ) colours[note 5]
|
FN
|
function(value)
|
EXTENDED MODE then SYMBOL SHIFT+2 | Function | Calls the function defined earlier in the program using DEF FN [26]
|
FORMAT
|
drive;"name"
|
EXTENDED MODE then SYMBOL SHIFT+0 | Command | Formats the cartridge in the indicated Microdrive and assigns it the identifier name [note 6][31]
|
FOR
|
variable = start TO end
|
F | Command | Starts a FOR -NEXT loop;[32] the variable name may only be one character long[33]
|
GO SUB
|
number
|
H | Command | Makes the program jump to the BASIC line specified by number ; when the program encounters the command RETURN , it will jump back to the statement after the GO SUB [34]
|
GO TO
|
number
|
G | Command | Makes the program jump to the BASIC line specified by number
|
IF
|
condition THEN
|
U | Command | Evaluates the condition , and if true, executes the statement that follows the keyword THEN that must come after the condition,[35] for example IF a=1 THEN LET b=2 [note 7]
|
IN
|
address
|
EXTENDED MODE then SYMBOL SHIFT+I | Function | Returns a byte read from the hardware input/output port corresponding to the address [36]
|
INK
|
number
|
EXTENDED MODE then SYMBOL SHIFT+X | Command/other | Sets the foreground colour for text and graphics[note 4][note 5][37] |
INKEY$
|
|
EXTENDED MODE then SYMBOL SHIFT+Z | Function | Returns a string representing the key being pressed on the keyboard at the moment the function is called, or an empty string if none is,[38] but does not wait for a keypress |
INPUT
|
[prompt,] variable
|
I | Command | Halts program execution until the user types in something on the keyboard and presses the Enter key, then stores the entered value in the specified variable ; if the optional prompt is supplied, this will be shown on the screen[39]
|
INT
|
number
|
EXTENDED MODE then R | Function | Returns the integer value of number , rounding down to the nearest whole number[5] (thus, INT -1.1 returns −2, not −1)
|
INVERSE
|
number
|
EXTENDED MODE then SYMBOL SHIFT+M | Command/other | Reverses the colours on all following text if number is 1, so that it uses the current ink colour for the background and the current paper colour for the text, or sets them back to normal if number is 0[note 5][40]
|
LEN
|
string
|
EXTENDED MODE then K | Function | Returns the number of characters (bytes) in string [41]
|
LET
|
variable=value
|
L | Command | Assigns value to the named variable[42]
|
LINE
|
|
EXTENDED MODE then SYMBOL SHIFT+3 | Other | |
LIST
|
[number]
|
K | Command | Outputs the current BASIC program to the screen; if the optional number is provided, it omits all lines with a lower number[44]
|
LLIST
|
[number]
|
EXTENDED MODE then SYMBOL SHIFT+V | Command | As LIST except the listing is output to the printer[22]
|
LN
|
number
|
EXTENDED MODE then SYMBOL SHIFT+Z | Function | Returns the natural logarithm of number [45]
|
LOAD
|
"[filename]" [CODE [address[, length]]| DATA variable()]
|
J | Command | Loads a program or data into RAM from tape, ZX Microdrive, disk, etc., deleting any existing BASIC program and variables;[39] if an empty string ("" ) is provided, this loads the first program found, else it will search the tape for the program named in the string; if the optional CODE is provided, will load the program into memory at the address it had when it was saved, or at the specified address (length is intended as a safety, to try and load the right program in case there are multiple on the tape with the right name but of different lengths);[46] if the optional DATA variable() is provided, will load the data from the tape into the array named variable() [25]
|
LPRINT
|
text
|
EXTENDED MODE then SYMBOL SHIFT+C | Command | As PRINT except output is sent to the printer[22]
|
MERGE
|
"[filename]"
|
EXTENDED MODE then SYMBOL SHIFT+T | Command | As LOAD , except it does not delete the current program and variables; if a line number exists in both, that of the newly loaded program overwrites the existing one. Using MERGE prevents a program saved using LINE from auto-running once loaded[47]
|
MOVE
|
stream1 TO stream2
|
EXTENDED MODE then SYMBOL SHIFT+6 | Command | Moves data from one stream (keyboard, screen, file, printer, network, etc.) to another[note 6][48] |
NEW
|
A | Command | Erases the current BASIC program and all variables[39] | |
NEXT
|
variable
|
N | Command | Closes a FOR -NEXT loop; the variable must match that of the corresponding FOR command[49] — "empty" NEXT s to refer to the immediately preceding FOR in the program are not allowed
|
NOT
|
condition
|
SYMBOL SHIFT+S | Logical operator | Returns true if the condition is false, else returns false[note 3][7]
|
OPEN #
|
stream
|
EXTENDED MODE then SYMBOL SHIFT+4 | Command | Opens a stream for reading from and/or writing to[note 6][50] |
OR
|
SYMBOL SHIFT+Y | Logical operator | Returns true if either of the conditions on either side of the OR keyword are true, else returns false[note 3][7]
| |
OUT
|
address, value
|
EXTENDED MODE then SYMBOL SHIFT+O | Command | Sends the value (a byte) to the hardware [Memory-mapped I/O|input/output port] corresponding to the address [36]
|
OVER
|
number
|
EXTENDED MODE then SYMBOL SHIFT+N | Command/other | Will make following text overprint with an XOR operation what is already on the screen if number is 1, instead of erasing it, or erase it if number is 0[note 5][40]
|
PAPER
|
number
|
EXTENDED MODE then SYMBOL SHIFT+C | Command/other | Sets the background colour for text and graphics[note 4][note 5][37] |
PAUSE
|
delay
|
M | Command | Halts program execution for the specified delay, in of a second in Europe or in North America[51] (thus, PAUSE 50 halts for one second in Europe)
|
PEEK
|
address
|
EXTENDED MODE then O | Function | Returns a byte representing the contents of the memory location pointed to by address [52]
|
PI
|
|
EXTENDED MODE then M | Function | Returns the value of pi[45] |
PLOT
|
x, y
|
Q | Command | Draws a pixel in the current INK colour on the screen at the coordinates (x ,y )[53]
|
POINT
|
(x,y)
|
EXTENDED MODE then SYMBOL SHIFT+8 | Function | Returns 1 if the pixel pointed at graphical coordinates (x ,y ) is currently in the ink (foreground) colour, else returns 0[17]
|
POKE
|
address, value
|
O | Command | Sets the contents of address in RAM to value [54]
|
PRINT
|
[AT x,y;] text
|
P | Command | Prints text (which must be a string or a number) to the screen;[42] if used with AT , will print at the specified text coordinates, else in the first column of the line after that used by the last PRINT statement[9]
|
RANDOMIZE
|
[number]
|
T | Command | Initializes the random number generator; if used without a number (or with 0), it does this based on the computer's internal clock, else it uses the number supplied, which must be in the range [1,65535][55]
|
READ
|
variable
|
EXTENDED MODE then A | Command | Takes a value from a DATA statement and stores it in the named variable : the first time READ is used, it gets the first value after the first DATA , the second time it gets the next one, and so on[24]
|
REM
|
text | E | Command | Begins a comment in the source code, meaning that everything after the REM statement is ignored, until the end of the line[39] — note this includes everything after a colon, which normally begins a new segment: 10 REM Nothing to see here : PRINT "Unprintable" will not produce any output, for example
|
RESTORE
|
[number]
|
EXTENDED MODE then S | Command | Resets where READ commands look for values in DATA statements: if used without a number, the next READ will use the first DATA in the program, with a number it will use the first DATA on or after the line whose number is indicated[56]
|
RETURN
|
Y | Command | Returns execution to the first statement following the last GO SUB command that was executed[34]
| |
RND
|
|
EXTENDED MODE then T | Function | Returns a pseudo-random number with eight significant figures in the range [0,1)[57] |
RUN
|
[number]
|
R | Command | Starts the current BASIC program, from its first line if no number is specified, else from the line with that number (or the first one after, if it does not exist)[58]
|
SAVE
|
"filename" [DATA variable() | LINE number]
|
S | Command | Saves the current BASIC program to tape or other storage device, with the filename specified; if the optional LINE followed by a line number is used, then the program will start automatically at the indicated line number when it is LOAD ed back in; with the optional DATA , the command saves the contents of the array named by the variable instead of the current BASIC program[25]
|
SCREEN$
|
[(line, column)]
|
EXTENDED MODE then SYMBOL SHIFT+ | Function/other | As a function, identifies the character at the specified line and column on the screen.[9] Used after the filename in a LOAD or SAVE command, indicates that the contents of the display memory should be loaded or saved; this essentially makes it a shortcut for CODE 16384,6912 [note 8] but does not work with VERIFY because the contents of the display memory will be different by the time that command reads back the saved data;[59]
|
SGN
|
number
|
EXTENDED MODE then F | Function | Returns 1 if number is positive, 0 if it is 0, and −1 if it is negative[5]
|
SIN
|
number
|
EXTENDED MODE then Q | Function | Returns the sine of number [23]
|
STEP
|
number
|
SYMBOL SHIFT+D | Other | Indicates the interval used by a FOR statement,[33] for example FOR n=2 TO 6 STEP 2 will skip n=3 and n=5 in the loop
|
STOP
|
SYMBOL SHIFT+A | Command | Ends execution of the current program, exiting to the BASIC editor; can also be given when the computer is waiting for input using the INPUT command;[58] once the program is stopped, it can be resumed with CONTINUE
| |
SQR
|
number
|
EXTENDED MODE then H | Function | Returns the square root of number [26]
|
STR$
|
number
|
EXTENDED MODE then Y | Function | Returns the character from the computer's character set corresponding to number [60]
|
TAB
|
column
|
EXTENDED MODE then P | Other | In a PRINT statement, makes sure that the text to be output begins in the column specified, wrapping to the next line as necessary, but never more than one line[18]
|
TAN
|
number
|
EXTENDED MODE then E | Function | Returns the tangent of number [61]
|
THEN
|
statement
|
SYMBOL SHIFT+G | Other | Follows the condition in an IF statement to indicate what should happen when the condition evaluates to true[note 7][35]
|
TO
|
|
SYMBOL SHIFT+F | Other | Indicates a range from the number to the left of TO to the number of the right of it, inclusive;[62] when used with FOR both numbers must be supplied, while if used to slice strings, either may be left off to indicate the start or end of the string
|
USR
|
string or address
|
EXTENDED MODE then L | Function | When called with a single-character string, this returns the memory address at which the glyph for the user-defined graphic character corresponding to that character is defined.[12] If called with an address, it starts machine code execution at that address (thus making it one of the few Sinclair BASIC functions to have a Side effect) and returns the contents of the Z80’s BC register pair.[63] |
VAL
|
string
|
EXTENDED MODE then J | Function | Evaluates the string as a number and returns the result;[64] this can perform calculations: VAL "1+2" returns 3, for example, and also evaluates variables and even other VAL statements: LET a=1: VAL "a+VAL ""2""" [note 9] also returns 3
|
VAL$
|
string
|
EXTENDED MODE then SYMBOL SHIFT+J | Function | Similar to VAL but evaluates the string as a string[5]
|
VERIFY
|
"[filename]"
|
EXTENDED MODE then SYMBOL SHIFT+R | Command | Reads a program or data from tape or other storage, much like LOAD , but instead of loading it into memory, compares it with the program or data that is already in memory. This is intended to be used immediately after a SAVE command, to make sure the program or data has been saved without corruption.[65]
|
In 48K models and older, the keywords are entered via Sinclair's unique keyword entry system, as indicated on the table. The most common commands need one keystroke only; for example, pressing only P at the start of a line on a Spectrum produces the full command PRINT
. Less frequent commands require more complex key sequences: BEEP
(for example) is keyed by pressing CAPS SHIFT plus SYMBOL SHIFT to access extended mode (later models include an EXTENDED MODE key), keeping SYMBOL SHIFT held down and pressing Z. Keywords are colour-coded on the original Spectrum keyboard to indicate which mode is required:[66]
The ZX81 8K BASIC used the shorter forms GOTO
, GOSUB
, CONT
and RAND
, whereas the Spectrum 48K BASIC used the longer forms GO TO
, GO SUB
, CONTINUE
and RANDOMIZE
. The ZX80 4K BASIC also used these longer forms but differed by using the spelling RANDOMISE
. The ZX81 8K BASIC was the only version to use FAST
, SCROLL
, SLOW
and UNPLOT
. The ZX80 4K BASIC had the exclusive function TL$()
; it was equivalent to the string operator (2 TO )
in later versions.
Unique code points are assigned in the ZX80 character set, ZX81 character set and ZX Spectrum character set for each keyword or multi-character operator, i.e. <=
, >=
, <>
, ""
(tokenized on the ZX81 only), **
(replaced with ↑
on the Spectrum). These are expanded by referencing a token table in ROM. Thus, a keyword uses one byte of memory only, a significant saving over traditional letter-by-letter storage. This also meant that the BASIC interpreter could quickly determine any command or function by evaluating one byte, and that the keywords need not be reserved words like in other BASIC dialects or other programming languages, e.g., it is allowed to define a variable named PRINT
and output its value with PRINT PRINT
. This is also related to the syntax requirement that every line start with a command keyword, and pressing the one keypress for that command at the start of a line changes the editor from command mode to letter mode. Thus, variable assignment requires LET
(i.e., LET a=1
not only a=1
). This practice is also different from other BASIC dialects. Further, it meant that unlike other BASIC dialects, the interpreter needed no parentheses to identify functions; SIN x
was sufficient, no SIN(x)
needed (though the latter was allowed). The 4K BASIC ROM of the ZX80 had a short list of exceptions to this: the functions CHR$()
, STR$()
, TL$()
, PEEK()
, CODE()
, RND()
, USR()
and ABS()
did not have one-byte tokens but were typed in letter-by-letter and required the parentheses. They were listed as the INTEGRAL FUNCTIONS on a label above and to the right of the keyboard.[67]
128 BASIC, present on ZX Spectrum 128, +2, +3, +2A, and +2B, stored keywords internally in one-byte code points, but used a conventional letter-by-letter BASIC input system. It also introduced two new commands:
PLAY
, which operated the 128k models' General Instrument AY-3-8910 music chipSPECTRUM
, which switched the 128k Spectrum into a 48k Spectrum compatibility modeThe original Spanish ZX Spectrum 128 included four additional BASIC editor commands in Spanish,[68][69] one of which was undocumented:
EDITAR
(to edit a line number or invoke the full screen string editor)NUMERO
(to renumber the program lines)BORRAR
(to delete program lines)ANCHO
(to set the column width of the RS-232 device, but undocumented as the code was broken)Unlike the LEFT$()
, MID$()
and RIGHT$()
functions used in the ubiquitous Microsoft BASIC dialects for home computers, parts of strings in Sinclair BASIC are accessed by numeric range. For example, a$(5 TO 10)
gives a substring starting with the 5th and ending with the 10th character of the variable a$
. Thus, it is possible to replace the LEFT$()
and RIGHT$()
commands by simply omitting the left or right array position respectively; for example a$( TO 5)
is equivalent to LEFT$(a$,5)
. Further, a$(5)
alone is enough to replace MID$(a$,5,1)
.
Variables holding numeric values may be any length, while string and array variable names must consist of only one alphabetical character. Thus, LET a=5
, LET Apples=5
, LET a$="Hello"
, DIM a(10)
and DIM a$(10)
are all good, while LET Apples$="Fruit"
, DIM Apples(10)
and DIM Apples$(10)
are not.
The long variable names allowed for numeric variables can include alphanumeric characters after the first character, so LET a0=5
is allowed but not LET 0a=5
. The long variable names can also include spaces, which are ignored, so LET number of apples = 5
is the same as LET numberofapples = 5
NEW
RUN
LIST
LOAD
SAVE
GOTO
IF
THEN
GOSUB
STOP
RETURN
FOR
TO
NEXT
CONTINUE
PRINT
INPUT
LET
CLEAR
CLS
DIM
REM
RANDOMIZE
POKE
PRINT
RAND
LET
CLEAR
RUN
LIST
GOTO
CONT
INPUT
NEW
REM
PRINT
STOP
BREAK
IF
STOP
FOR
NEXT
TO
STEP
SLOW
FAST
GOSUB
RETURN
SAVE
LOAD
CLS
SCROLL
PLOT
UNPLOT
PAUSE
LPRINT
LLIST
COPY
DIM
POKE
NEW
ABS
SGN
SIN
COS
TAN
ASN
ACS
ATN
LN
EXP
SQR
INT
PI
RND
FUNCTION
LEN
VALSTR$
NOT
CODE
CHR$
INKEY$
AT
TAB
INKEY$
PEEK
USR
LOAD !
SAVE !
MERGE !
ERASE
PLAY
SPECTRUM
FORMAT
COPY
DELETE
deletes BASIC program line ranges.FREE
is a function that gives the amount of free RAM. PRINT FREE
will show how much RAM is free.ON ERR
is an error-handling function mostly used as ON ERR GO TO
or ON ERR CONT
.RESET
can be used to reset the behaviour of ON ERR
. It was also intended to reset peripherals.SOUND
controls the AY-3-8192 sound chip.STICK
is a function that gives the position of the internal joystick (Timex Sinclair 2090).PRINT #
Prints to a specific output channel.LIST #
Lists the program to a specific output channel.CLS*
Clears both display areas.INK*
Sets ink colour for both display areasPAPER*
Sets paper colour both display areasSCREEN$
Selects the high / normal resolution modes.PLOT*
Plots a pixel and updates the drawing position.LINE
Draws a line from the previous PLOT position, supporting arc drawingCIRCLE*
Draws a circle or oval, depending on screen mode.LOAD!
SAVE!
CAT!
MERGE!
ERASE!
CLEAR!
FORMAT!
LPRINT
LLIST
BEEP!
SCREEN$
DRAW!
PLOT!
CIRCLE!
Several ZX Spectrum interpreters exist.[76]
Several ZX Spectrum compilers exist.[76]