Sinclair BASIC
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.
History
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 minimal BASIC standard with integer arithmetic only, termed the 4K BASIC for the ZX80. It evolved through the floating-point 8K BASIC for the ZX81 and TS1000, and became an almost complete version in the 16 KB ROM ZX Spectrum. It is present in all ZX Spectrum compatibles.
As of 2015, interpreters exist for modern operating systems, and older systems, that allow Sinclair Basic to be used easily.
Syntax
New BASIC programmers might start with a simple program, perhaps using the language's PRINT statement to display a message on the screen; a well-known and often-replicated example is Kernighan and Ritchie's Hello world program:10 PRINT "Hello, World!"
Keywords
On the 16K/48K ZX Spectrum, there are 88 keywords in Sinclair BASIC, denoting commands, functions and logical operators, and other keywords :Keyword | Parameters | Entered using | Type | Summary |
ABS | number | then | Function | Returns the absolute value of number |
ACS | number | then | Function | Returns the arccosine of number |
AND | Logical operator | Returns true if both conditions on either side of the AND keyword are true, else returns false | ||
ASN | number | then | Function | Returns the arcsine of number |
AT | line, column; | Other | Used in a PRINT statement to print at the line and column specified; for example, PRINT AT 5,10;"*" puts a star in column 10 of line 5. | |
ATN | number | then | Function | Returns the arctangent of number |
ATTR |
| then | 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 colour, the fourth, fifth and sixth bits the paper colour, the seventh bit whether the colours are bright or not, and the eight, whether they are flashing |
BEEP | duration, pitch | then | Command | Produces sound from the computer's speaker; duration is in seconds, pitch is in semitones above or below middle C |
BIN | number | then | Other | Indicates number is in binary notation |
BORDER | number | Command | Sets the outer border of the screen to the colour specified by the number | |
BRIGHT | number | then | Command/other | Makes all following colours brighter if number is 1, or its normal shade if 0 |
CAT | number | then | Command | Displays contents of ZX Microdrive specified by number |
CHR$ | number | then | Function | Returns the character corresponding to the decimal number in the computer's character set |
CIRCLE | x, y, r | then | Command | Draws a circle with its centre at coordinates and radius r |
CLEAR | address | Command | Clears the screen, all variables and the GO SUB stack, and optionally sets the maximum RAM address to be used by BASIC | |
CLOSE # | number | then | Command | Closes the specified stream number for access |
CLS | Command | Clears all text and graphics from the screen | ||
CODE | string | then | Function/other | Returns the number corresponding to the first character in string in the computer's character set; also used to save arbitrary chunks memory to tape, disk, etc. and load them back in — see LOAD , SAVE and VERIFY for details |
CONTINUE | Command | Restarts a program after it has stopped due to an error or the user pressing the or keys | ||
COPY | Command | Sends the currently displayed screen to the printer | ||
COS | number | then | Function | Returns the cosine of number |
DATA | comma-separated values | then | Command/other | Provides numbers and/or strings to use with the READ command and allows saving the contents of an array to tape when used with the SAVE command |
DEF FN | name = operation | then | Command | Defines a custom function that can be used with the FN command;function definitions must be of the form f=operations , for example f=x*2 and the function name may not consist of more than one letter, plus a $-symbol if the function returns a string |
DIM | variable | Command | Declares an array with the specified dimensions , which may be multi-dimensional ; if used with strings, the last dimension indicates the length of each of the strings is an array of two strings each of five characters long, and DIM b$ | |
DRAW | x, y | Command | Draws a line in the current INK colour to coordinates 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 | |
ERASE | drive;"filename" | then | Command | Deletes the specified file from a ZX Microdrive |
EXP | number | then | Function | Returns e to the power number |
FLASH | number | then | Command/other | Makes all following text alternate its foreground and background colours |
FN | function | then | Function | Calls the function defined earlier in the program using DEF FN |
FORMAT | drive;"name" | then | Command | Formats the cartridge in the indicated Microdrive and assigns it the identifier name |
FOR | variable = start TO end | Command | Starts a FOR -NEXT loop; the variable name may only be one character long | |
GO SUB | number | 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 | |
GO TO | number | Command | Makes the program jump to the BASIC line specified by number | |
IF | condition THEN | Command | Evaluates the condition , and if true, executes the statement that follows the keyword THEN that must come after the condition, for example IF a=1 THEN LET b=2 | |
IN | address | then | Function | Returns a byte read from the hardware input/output port corresponding to the address |
INK | number | then | Command/other | Sets the foreground colour for text and graphics |
INKEY$ |
| then | 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, but does not wait for a keypress |
INPUT | variable | 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 | |
INT | number | then | Function | Returns the integer value of number , rounding down to the nearest whole number |
INVERSE | number | then | 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 |
LEN | string | then | Function | Returns the number of characters in string |
LET | variable=value | Command | Assigns value to the named variable | |
LINE |
| then | Other |
|
LIST |
| Command | Outputs the current BASIC program to the screen; if the optional number is provided, it omits all lines with a lower number | |
LLIST |
| then | Command | As LIST except the listing is output to the printer |
LN | number | then | Function | Returns the natural logarithm of number |
LOAD | "" | Command | Loads a program or data into RAM from tape, ZX Microdrive, disk, etc., deleting any existing BASIC program and variables; 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 ; if the optional DATA variable is provided, will load the data from the tape into the array named variable | |
LPRINT | text | then | Command | As PRINT except output is sent to the printer |
MERGE | "" | then | 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 |
MOVE | stream1 TO stream2 | then | Command | Moves data from one stream to another |
NEW | Command | Erases the current BASIC program and all variables | ||
NEXT | variable | Command | Closes a FOR -NEXT loop; the variable must match that of the corresponding FOR command — “empty” NEXT s to refer to the immediately preceding FOR in the program are not allowed | |
NOT | condition | Logical operator | Returns true if the condition is false, else returns false | |
OPEN # | stream | then | Command | Opens a stream for reading from and/or writing to |
OR | Logical operator | Returns true if either of the conditions on either side of the OR keyword are true, else returns false | ||
OUT | address, value | then | Command | input/output port] corresponding to the address |
OVER | number | then | 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 |
PAPER | number | then | Command/other | Sets the background colour for text and graphics |
PAUSE | delay | Command | Halts program execution for the specified delay, in of a second in Europe or in North America | |
PEEK | address | then | Function | Returns a byte representing the contents of the memory location pointed to by address |
PI |
| then | Function | Returns the value of pi |
PLOT | x, y | Command | Draws a pixel in the current INK colour on the screen at the coordinates | |
POINT |
| then | Function | Returns 1 if the pixel pointed at graphical coordinates is currently in the ink colour, else returns 0 |
POKE | address, value | Command | Sets the contents of address in RAM to value | |
PRINT | text | Command | Prints text to the screen; 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 | |
RANDOMIZE |
| Command | Initializes the random number generator; if used without a number | |
RUN |
| Command | Starts the current BASIC program, from its first line if no number is specified, else from the line with that number | |
SAVE | LINE number] | 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 | |
SCREEN$ |
| then + | Function/other | As a function, identifies the character at the specified line and column on the screen. 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 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; |
SGN | number | then | Function | Returns 1 if number is positive, 0 if it is 0, and −1 if it is negative |
SIN | number | then | Function | Returns the sine of number |
STEP | number | Other | Indicates the interval used by a FOR statement, for example FOR n=2 TO 6 STEP 2 will skip n=3 and n=5 in the loop | |
STOP | 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; once the program is stopped, it can be resumed with CONTINUE | ||
SQR | number | then | Function | Returns the square root of number |
STR$ | number | then | Function | Returns the character from the computer's character set corresponding to number |
TAB | column | then | 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 |
TAN | number | then | Function | Returns the tangent of number |
THEN | statement | Other | Follows the condition in an IF statement to indicate what should happen when the condition evaluates to true | |
TO |
| Other | Indicates a range from the number to the left of TO to the number of the right of it, inclusive; 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 | then | 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. If called with an address, it starts machine code execution at that address and returns the contents of the Z80’s BC register pair. |
VAL | string | then | Function | Evaluates the string as a number and returns the result; 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""" also returns 3 |
VAL$ | string | then | Function | Similar to VAL but evaluates the string as a string |
VERIFY | "" | then | Command | Reads a program from tape or other storage, much like LOAD , but instead of loading it into memory, compares it to the program that is currently in memory; this is intended to make sure the program, has been SAVE d correctly |
PRINT
. Less frequent commands require more complex key sequences: BEEP
is keyed by pressing plus to access extended mode, keeping held down and pressing. Keywords are colour-coded on the original Spectrum keyboard to indicate which mode is required:- : key only
- on the key itself: plus the key
- above the key: followed by the key
- below the key: followed by plus the key
GOTO
, GOSUB
, CONT
and RAND
, whereas the Spectrum 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 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.
<=
, >=
, <>
, ""
, **
. 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 SIN x
was sufficient, no SIN
needed. 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.The 128K Spectrum models, the ZX Spectrum 128, +2, +3, +2A, and +2B, also stored keywords internally in one-byte code points, but used a conventional letter-by-letter BASIC input system. They also introduced two new commands:
-
PLAY
, which operated the 128k models' General Instrument AY-3-8910 music chip -
SPECTRUM
, which switched the 128k Spectrum into a 48k Spectrum compatibility mode
-
EDIT
-
RENUM
-
DELETE
-
WIDTH
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, 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 is equivalent to LEFT$
. Further, a$
alone is enough to replace MID$
.Variable names
and array variable names must consist of only one alphabetical character. Thus,The long variable names allowed for numeric variables can include alphanumeric characters after the first character, so
Official versions
- 4K BASIC is the original ZX80 BASIC with integer-only arithmetic, by John Grant of Nine Tiles for the ZX80, so named for residing in 4 KiB read-only memory.
- 8K BASIC is the ZX81 BASIC, updated with floating-point arithmetic by Steve Vickers, so named for residing in 8 KiB ROM.
- 48 BASIC is the BASIC for the original 16/48 kB random-access memory ZX Spectrum, with colour and more peripherals added by Steve Vickers and John Grant. It resides in 16 KiB ROM and began to be called 48 BASIC with the introduction of the ZX Spectrum 128 at which time the 16 kB Spectrum was no longer sold and most existing ones in use had been upgraded to 48 kB
- 128 BASIC is the BASIC for the ZX Spectrum 128. It offers extra commands and uses letter-by-letter input.
- +3 BASIC is the BASIC with disk support for the ZX Spectrum +3.
- T/S 2000 BASIC was used on the Spectrum-compatible Timex Sinclair 2068 and has the following six keywords and the ordinary Sinclair BASIC ones:
- *
DELETE
deletes BASIC program line ranges. with theK
cursor produces the commandDELETE
. - *
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 asON ERR GO TO
orON ERR CONT
. - *
RESET
can be used to reset the behaviour ofON 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. - BASIC64 by Timex of Portugal, is a software extension to allow better Basic programming with the 512×192 graphic mode available only on Timex 2000 series computers. This extension adds commands and does a complete memory remap to avoid the system overwriting the extended screen memory area. Two versions exist due to different memory maps - a version for TC2048 and a version for TS/TC2068.
Other versions, extensions, derivatives and successors
Interpreters for the ZX Spectrum family
Several ZX Spectrum interpreters exist.- Beta BASIC by Dr. Andy Wright, was originally a BASIC extension, but became a full interpreter.
- YS MegaBasic by Mike Leaman.
- ZebraOS by Zebra Systems in New York, a cartridge version of T/S 2000 BASIC that used the 512×192 screen mode.
- by Steve Vickers and Ian Logan, modified by Geoff Wearmouth, a replacement ROM with an enhanced Sinclair BASIC.
- Gosh Wonderful by Geoff Wearmouth, a replacement ROM that fixes bugs and adds a tokenizer, stream lister, delete and renumber commands.
- OpenSE BASIC by Andrew Owen, a replacement ROM with bug fixes and many enhancements including ULAplus support, published as open source in 2011
Compilers for the ZX Spectrum family
- HiSoft COLT Compiler
- HiSoft BASIC, an integer and floating-point capable compiler
- Laser Compiler
- Softek 'IS' Integer Compiler
- Softek 'FP' Full Compiler
- ZIP Compiler
Derivatives and successors for other computers
- SuperBASIC, a much more advanced BASIC dialect introduced with the Sinclair QL personal computer, with some similarities to the earlier Sinclair BASICs
- SAM Basic, the BASIC on the SAM Coupé, generally considered a ZX Spectrum clone
- ROMU6 by Cesar and Juan Hernandez - MSX
- Spectrum 48 by Whitby Computers - Commodore 64
- Sparky eSinclair BASIC by Richard Kelsh, an operating system loosely based on ZX Spectrum BASIC - Zilog eZ80
- Sinbas by Pavel Napravnik - DOS
- Basic by Philip Kendall - Unix
- BINSIC by Adrian McMenamin, a reimplementation in Groovy closely modelled on ZX81 BASIC - Java
- BASin by Paul Dunn, a complete Sinclair BASIC integrated development environment based on a ZX Spectrum emulator - Windows
- SpecBAS by Paul Dunn, an integrated development environment providing an enhanced superset of Sinclair BASIC - Windows, Linux, Pandora, and Raspberry Pi
- ZX-Basicus by Juan-Antonio Fernández-Madrigal, a synthesizer, analyzer, optimizer, interpreter and debugger of Sinclair BASIC 48K for PCs, freely downloadable for Linux and Windows.