This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages)(Learn how and when to remove this template message)
|Designed by||John Grant, Steve Vickers|
|Developer||Nine Tiles Networks,|
|Platform||ZX80, ZX81, ZX Spectrum|
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.
It was initially an incomplete implementation of the 1978 American National Standards Institute (ANSI) minimal BASIC standard with integer arithmetic only, termed the 4K BASIC (for its ROM size) for the ZX80. It evolved through the floating-point 8K BASIC for the ZX81 and TS1000 (which was also available as an upgrade for the ZX80), 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.
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!"
On the 16K/48K ZX Spectrum, 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|
||EXTENDED MODE then G||Function||Returns the absolute value of |
||EXTENDED MODE then SYMBOL SHIFT+W||Function||Returns the arccosine of |
||SYMBOL SHIFT+Y||Logical operator||Returns true if both conditions on either side of the |
||EXTENDED MODE then SYMBOL SHIFT+Q||Function||Returns the arcsine of |
||SYMBOL SHIFT+I||Other||Used in a |
||EXTENDED MODE then SYMBOL SHIFT+E||Function||Returns the arctangent of |
||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 |
||EXTENDED MODE then SYMBOL SHIFT+Z||Command||Produces sound from the computer's speaker; |
||EXTENDED MODE then B||Other||Indicates |
||B||Command||Sets the outer border of the screen to the colour specified by the |
||EXTENDED MODE then SYMBOL SHIFT+B||Command/other||Makes all following colours brighter if |
||EXTENDED MODE then SYMBOL SHIFT+9||Command||Displays contents of ZX Microdrive specified by |
||EXTENDED MODE then W||Function||Returns the character corresponding to the decimal |
||EXTENDED MODE then SYMBOL SHIFT+H||Command||Draws a circle with its centre at coordinates (|
||X||Command||Clears the screen, all variables and the |
||EXTENDED MODE then SYMBOL SHIFT+5||Command||Closes the specified stream number for access[note 6]|
||V||Command||Clears all text and graphics from the screen|
||EXTENDED MODE then I||Function/other||Returns the number corresponding to the first character in |
||C||Command||Restarts a program after it has stopped due to an error or the user pressing the CAPS SHIFT+SPACE or BREAK keys|
||Z||Command||Sends the currently displayed screen to the printer|
||EXTENDED MODE then W||Function||Returns the cosine of |
||EXTENDED MODE then D||Command/other||Provides numbers and/or strings to use with the |
||EXTENDED MODE then 1||Command||Defines a custom function that can be used with the |
||D||Command||Declares an array with the specified |
||W||Command||Draws a line in the current |
||EXTENDED MODE then SYMBOL SHIFT+7||Command||Deletes the specified file from a ZX Microdrive[note 6]|
||EXTENDED MODE then X||Function||Returns e to the power |
||EXTENDED MODE then SYMBOL SHIFT+V||Command/other||Makes all following text alternate its foreground (|
||EXTENDED MODE then SYMBOL SHIFT+2||Function||Calls the |
||EXTENDED MODE then SYMBOL SHIFT+0||Command||Formats the cartridge in the indicated Microdrive and assigns it the identifier |
||F||Command||Starts a |
||H||Command||Makes the program jump to the BASIC line specified by |
||G||Command||Makes the program jump to the BASIC line specified by |
||U||Command||Evaluates the |
||EXTENDED MODE then SYMBOL SHIFT+I||Function||Returns a byte read from the hardware input/output port corresponding to the |
||EXTENDED MODE then SYMBOL SHIFT+X||Command/other||Sets the foreground colour for text and graphics[note 4][note 5]|
||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, but does not wait for a keypress|
||I||Command||Halts program execution until the user types in something on the keyboard and presses the |
||EXTENDED MODE then R||Function||Returns the integer value of |
||EXTENDED MODE then SYMBOL SHIFT+M||Command/other||Reverses the colours on all following text if |
||EXTENDED MODE then K||Function||Returns the number of characters (bytes) in |
||EXTENDED MODE then SYMBOL SHIFT+3||Other|
||K||Command||Outputs the current BASIC program to the screen; if the optional |
||EXTENDED MODE then SYMBOL SHIFT+V||Command||As |
||EXTENDED MODE then SYMBOL SHIFT+Z||Function||Returns the natural logarithm of |
||J||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 (|
||EXTENDED MODE then SYMBOL SHIFT+C||Command||As |
||EXTENDED MODE then SYMBOL SHIFT+T||Command||As |
||EXTENDED MODE then SYMBOL SHIFT+6||Command||Moves data from one stream (keyboard, screen, file, printer, network, etc.) to another[note 6]|
||A||Command||Erases the current BASIC program and all variables|
||N||Command||Closes a |
||SYMBOL SHIFT+S||Logical operator||Returns true if the |
||EXTENDED MODE then SYMBOL SHIFT+4||Command||Opens a stream for reading from and/or writing to[note 6]|
||SYMBOL SHIFT+Y||Logical operator||Returns true if either of the conditions on either side of the |
||EXTENDED MODE then SYMBOL SHIFT+O||Command||Sends the |
||EXTENDED MODE then SYMBOL SHIFT+N||Command/other||Will make following text overprint with an XOR operation what is already on the screen if |
||EXTENDED MODE then SYMBOL SHIFT+C||Command/other||Sets the background colour for text and graphics[note 4][note 5]|
||M||Command||Halts program execution for the specified delay, in of a second in Europe or in North America (thus, |
||EXTENDED MODE then O||Function||Returns a byte representing the contents of the memory location pointed to by |
||EXTENDED MODE then M||Function||Returns the value of pi|
||Q||Command||Draws a pixel in the current |
||EXTENDED MODE then SYMBOL SHIFT+8||Function||Returns 1 if the pixel pointed at graphical coordinates (|
||O||Command||Sets the contents of |
||T||Command||Initializes the random number generator; if used without a |
||EXTENDED MODE then A||Command||Takes a value from a |
||text||E||Command||Begins a comment in the source code, meaning that everything after the |
||EXTENDED MODE then S||Command||Resets where |
||Y||Command||Returns execution to the first statement following the last |
||EXTENDED MODE then T||Function||Returns a pseudo-random number with eight significant figures in the range [0,1)|
||R||Command||Starts the current BASIC program, from its first line if no |
||LINE number]||S||Command||Saves the current BASIC program to tape or other storage device, with the |
||EXTENDED MODE then SYMBOL SHIFT+||Function/other||As a function, identifies the character at the specified line and column on the screen. Used after the filename in a |
||EXTENDED MODE then F||Function||Returns 1 if |
||EXTENDED MODE then Q||Function||Returns the sine of |
||SYMBOL SHIFT+D||Other||Indicates the interval used by a |
||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 |
||EXTENDED MODE then H||Function||Returns the square root of |
||EXTENDED MODE then Y||Function||Returns the character from the computer's character set corresponding to |
||EXTENDED MODE then P||Other||In a |
||EXTENDED MODE then E||Function||Returns the tangent of |
||SYMBOL SHIFT+G||Other||Follows the condition in an |
||SYMBOL SHIFT+F||Other||Indicates a range from the number to the left of |
||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. 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.|
||EXTENDED MODE then J||Function||Evaluates the |
||EXTENDED MODE then SYMBOL SHIFT+J||Function||Similar to |
||EXTENDED MODE then SYMBOL SHIFT+R||Command||Reads a program from tape or other storage, much like |
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
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:
- White: key only
- Red on the key itself: SYMBOL SHIFT plus the key
- Green above the key: EXTENDED MODE followed by the key
- Red below the key: EXTENDED MODE followed by SYMBOL SHIFT plus the key
The ZX81 8K BASIC used the shorter forms
RAND, whereas the Spectrum used the longer forms
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
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 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 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
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
The original Spanish ZX Spectrum 128 included four additional commands in Spanish, one of which was undocumented. These can be translated as:
EDIT(to edit a line number or invoke the full screen string editor)
RENUM(to renumber the program lines)
DELETE(to delete program lines)
WIDTH(to set the column width of the RS232 device, but undocumented as the code was broken)
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
RIGHT$() commands by simply omitting the left or right array position respectively; for example
a$( TO 5) is equivalent to
a$(5) alone is enough to replace
String and array variable names must consist of only one alphabetical character. Thus,
DIM a(10) and
DIM a$(10) are all good, while
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
- 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 (ROM).
- 8K BASIC is the ZX81 BASIC (also available as an upgrade for the ZX80), 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 (RAM) ZX Spectrum (and clones), 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 (TS2068) and has the following six keywords and the ordinary Sinclair BASIC ones:
DELETEdeletes BASIC program line ranges. CAPS SHIFT+0 with the
Kcursor produces the command
FREEis a function that gives the amount of free RAM.
PRINT FREEwill show how much RAM is free.
ON ERRis an error-handling function mostly used as
ON ERR GO TOor
ON ERR CONT.
RESETcan be used to reset the behaviour of
ON ERR. It was also intended to reset peripherals.
SOUNDcontrols the AY-3-8192 sound chip.
STICKis a function that gives the position of the internal joystick (Timex Sinclair 2090).
- 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.
- Sea Change ROM 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 (formerly SE 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
Several ZX Spectrum compilers exist.
- HiSoft COLT Compiler (a.k.a. HiSoft COLT Integer Compiler)
- HiSoft BASIC (a.k.a. HiSoft BASIC Compiler), an integer and floating-point capable compiler
- Laser Compiler
- Softek 'IS' Integer Compiler (successor to Softek 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 (and CheckBasic) 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 (IDE) based on a ZX Spectrum emulator - Windows
- SpecBAS (a.k.a. SpecOS) by Paul Dunn, an integrated development environment (IDE) 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.
- List of computer system emulators § Sinclair ZX80
- List of computer system emulators § Sinclair ZX81
- List of computer system emulators § Sinclair ZX Spectrum and clones
- Optional parameters are enclosed in [square brackets]
- These assume the computer is in K (keyword) mode, which it normally is at the start of a line when entering BASIC. On the Spectrum 16K and 48K, Extended Mode is entered by pressing CAPS SHIFT and SYMBOL SHIFT simultaneously rather than the EXTENDED MODE key that is present on the Spectrum+ and later models.
- “False” in Sinclair BASIC equates to 0 (zero), everything else equates to “true”. Functions that return true-or-false values thus actually return 0 for false and 1 for true, while
ANDusually returns the first of the conditions supplied for true, or 1 if no numerical values were given. For example,
6 AND 7returns 6, while
NOT 6=7returns 1.
- The available numbers for colours are:
1 2 3 4 5 6 7 0 blue red magenta green cyan yellow white black
In all colour-related commands, the number 8 may be used to indicate “transparent” while in
PAPERmay also be set to 9 for “contrast” — that is, to put a dark colour on a light background or vice versa automatically.
INVERSEset attributes for outputting text and graphics to the screen. They can be used either as commands, to apply to all subsequent output until set again, or within a
MOVEwere originally designed to be used with peripherals, but at the launch of ZX Spectrum, they had not been completely implemented, such that their use generated an error message (Invalid Stream). Later with the aid of the ZX Interface 1 shadow ROM, they were used for the ZX Microdrive. (The shadow ROM was paged when the BASIC interpreter detected a syntax error, which is why most ZX Microdrive commands use a
- Unlike many other BASIC dialects, Sinclair Basic did not include the
ELSEoperator in the
ELSE] clause. A workaround would be to use an
GO TOconstruct instead, bypassing the lines that would have been in an
ELSEclause with the
- The Spectrum’s display memory starts at address 16384 and is 6912 bytes long
- A string inside a string must have its quotes doubled in Sinclair BASIC
- Garfield, Simon (2010-02-28). "Sir Clive Sinclair: "I don't use a computer at all"". The Guardian. Guardian Media Group. Retrieved 2011-05-23.
He is keen to credit [...], not least Nine Tiles, the company that made the Basic operating software.
- "ZX80 - 8K BASIC ROM UPGRADE".
- Vickers 1983, p. 59.
- Vickers 1983, p. 70.
- Vickers 1983, p. 85.
- Vickers 1983, p. 69–70.
- Vickers 1983, p. 101.
- Vickers 1983, p. 116.
- Vickers 1983, p. 135.
- Vickers 1983, p. 93.
- Vickers 1983, p. 113.
- Vickers 1983, p. 110–111.
- Cambridge Communication 1983, p. 15.
- Vickers 1983, p. 91.
- Vickers 1983, p. 123.
- Vickers 1983, p. 103.
- Vickers 1983, p. 168.
- Vickers 1983, p. 19.
- Vickers 1983, p. 151.
- Vickers 1983, p. 68.
- Vickers 1983, p. 41.
- Vickers 1983, p. 142.
- Vickers 1983, p. 60.
- Vickers 1983, p. 79–81.
- Vickers 1983, p. 11–123.
- Cambridge Communication 1983, p. 18.
- Vickers 1983, p. 66.
- Cambridge Communication 1983, p. 19.
- Vickers 1983, p. 31.
- Vickers 1983, p. 32.
- Vickers 1983, p. 37.
- Vickers 1983, p. 25.
- Vickers 1983, p. 159.
- Vickers 1983, p. 110.
- Vickers 1983, p. 131.
- Vickers 1983, p. 16.
- Vickers 1983, p. 112.
- Vickers 1983, p. 57.
- Vickers 1983, p. 13.
- Vickers 1983, p. 144.
- Vickers 1983, p. 15.
- Vickers 1983, p. 67.
- Vickers 1983, p. 142–143.
- Vickers 1983, p. 147.
- Cambridge Communication 1983, p. 39.
- Vickers 1983, p. 31–32.
- Cambridge Communication 1983, p. 22.
- Vickers 1983, p. 129.
- Vickers 1983, p. 130.
- Vickers 1983, p. 121.
- Vickers 1983, p. 163.
- Vickers 1983, p. 74.
- Vickers 1983, p. 42.
- Vickers 1983, p. 73.
- Vickers 1983, p. 14.
- Vickers 1983, p. 143.
- Vickers 1983, p. 58.
- Vickers 1983, p. 69.
- Vickers 1983, p. 32, 51.
- Vickers 1983, p. 180.
- Vickers 1983.
- Vickers 1983, p. 141.
- Vickers 1983, p. 7–8.
- "Picture of ZX80".
- Spectrum 128 ROM Disassembly - Spanish Spectrum 128
- "Sinclair BASIC history".
- "The Incomplete Spectrum ROM Assembly". Archived from the original on 2015-09-01.
- Ardley, Neil (1984). Sinclair ZX Spectrum+ User Guide. Dorling Kindersley in association with Sinclair Research. ISBN 0-86318-080-9.
- Vickers, Steven (1982). Sinclair ZX Spectrum BASIC Programming. Sinclair Research.
- Vickers, Steven (1983). Sinclair ZX Spectrum BASIC Programming (2 ed.). Sinclair Research.
- Cambridge Communication (1983). Sinclair ZX Spectrum Microdrive and Interface 1 manual. Sinclair Research.
- Sinclair ZX Spectrum BASIC Programming: The original 1982 manual by Steven Vickers (referenced above)
- Sinclair ZX81 Basic Programming: also by Vickers
- The History of Sinclair BASIC: By Andrew Owen
- Timex Computer World: Basic 64 user manual for Timex Computer 2048
- Sinclair BASIC grammar: A LL(1) grammar specification for parsing Sinclair BASIC 16/48K