Skip to content

Sinclair BASIC

One key, one keyword

The BASIC in the ROMs of the ZX80, ZX81 and ZX Spectrum, written for Sinclair by the Cambridge firm Nine Tiles. Each keyword went in with a single keypress and every line was checked as it was typed; the Spectrum version added colour, sound and graphics commands, and the Spectrum 128 finally let owners spell keywords out.

Switch on a ZX Spectrum and a flashing K sits in the bottom corner of the screen. Press the P key and the whole word PRINT appears. That single keypress is Sinclair BASIC’s signature. It began on the ZX80 in 1980, carried on through the ZX81, and reached the ZX Spectrum in 1982, where it became the first programming language of a very large number of British children.

It was not written by Sinclair Research. The BASIC came from Nine Tiles, a software firm near Cambridge. John Grant wrote the ZX80’s 4K version; Steven Vickers turned it into the ZX81’s 8K ROM, adding floating-point arithmetic, and then wrote most of the Spectrum’s 16K ROM with Grant. Vickers also wrote the manual that came in the Spectrum’s box, Sinclair ZX Spectrum BASIC Programming, which is still the clearest description of the language.

Fast facts

  • Machines: ZX80 (4K ROM, whole numbers only), ZX81 (8K ROM), ZX Spectrum (16K ROM), and the Spectrum 128, +2 and +3, which keep the 48K BASIC and add 128 BASIC.
  • Authors: John Grant and Steven Vickers of Nine Tiles, under contract to Sinclair.
  • Input: single-key keyword entry, with every line checked for syntax as it is entered.
  • Numbers: five-byte floating point, with a special compact form for whole numbers.
  • Built-in extras (Spectrum): colour, sound and graphics commands, and user-defined graphics.

Keywords at a keypress

The keyboard carries the language. The Spectrum manual explains that keywords and function names are “entered from the keyboard rather than being spelled out”, so “some keys have five or more distinct meanings”, chosen by the two shift keys and by the mode the machine is in. The cursor shows the mode:

  • K (keywords) appears at the start of a line, after THEN, and after a : that separates statements. The next key gives the keyword printed on it, or a digit.
  • L (letters) appears everywhere else, and a key gives its letter.
  • E (extended) follows both shift keys pressed together, for the keywords printed in green above and red below the keys.
  • C is capitals, and G is graphics mode, for block graphics and user-defined characters.

Each keyword is stored in memory as a single character code, one of the “tokens” in the Spectrum’s 256-character set. So a keyword could never be misspelt, a program took less memory, and the ROM never had to recognise words as they were typed. The cost was learning where every keyword lived. CRASH’s reviewer in February 1986 praised a language where “most of the simplest keywords can be entered at a single key press”, but complained that “the less common symbols and keywords are hidden in the obscure corners of the keyboard”.

Keyword entry also explains a rule that surprises people coming from other BASICs: every statement starts with its keyword, so assignment always needs LET. GO TO and GO SUB are single tokens, displayed with a space.

The ROM checked each line when you pressed ENTER. If it could not make sense of the line, a flashing question mark appeared at the problem and the line was refused until you fixed it, an idea inherited from the ZX80.

What the Spectrum added

The Spectrum’s ROM kept the ZX81’s language and added commands for the new hardware:

Command What it does
PLOT x,y Sets one pixel. (0,0) is the bottom left and (255,175) the top right; the two bottom lines of the screen are kept for input.
DRAW x,y Draws a line from the last point plotted, by the offsets x and y.
DRAW x,y,a Draws an arc, turning through a radians: PI gives a semicircle.
CIRCLE x,y,r Draws a circle.
INK, PAPER, BRIGHT, FLASH, INVERSE, OVER Set colours and printing effects for later PRINT and drawing commands, or for a single statement.
BORDER c Sets the border colour.
BEEP d,p Plays a note for d seconds, p semitones above middle C (negative for notes below).

A short program shows how directly they work:

BASIC
10 BORDER 0: PAPER 0: INK 7: CLS
20 FOR n=0 TO 175
30 PLOT n,n
40 DRAW 255-n*2,0
50 NEXT n
60 BEEP 0.5,0: BEEP 0.5,12

The colour commands work within the Spectrum’s attribute system: each 8×8 character square has one ink and one paper colour, so a line drawn in a new ink recolours every lit pixel in the squares it crosses. The manual’s own line-drawing example warns that “the lines seem to get broader as the program goes on” for exactly this reason.

Names, numbers and strings

Sinclair BASIC’s variable rules are strict, and they are the first thing to check when a listing from another machine fails. The manual sets them out in chapter 7:

  • Numeric variables can be any length. They “can use any letters or digits as long as the first one is a letter”. Spaces do not count and capitals do not matter, so now we are six and nOWeaReSiX are the same variable.
  • String variables must be a single letter followed by $: a$ to z$. A name such as name$ is rejected.
  • FOR loop control variables must be a single letter.
  • Arrays, numeric or string, must have single-letter names: DIM b(10), DIM c$(20,5).
  • Only letters and digits are allowed, so no underscores, hyphens or other symbols.

Commodore 64 BASIC also accepts long names, but its manual says “only the first two characters are considered significant” and forbids keywords inside names. On the Spectrum every character of a numeric name counts.

Every number is held in five bytes as floating point. There is no separate integer type, but whole numbers from −65535 to 65535 are stored in a special form, flagged by a zero first byte. The floating-point routines came from the ZX81.

Strings can be sliced with TO: "abcdef"(2 TO 5) is "bcde", and a$(3) is the single character "c". Joining uses +. Memory stays tidy because the ROM keeps every area only as large as its contents. In the manual’s words, when anything is inserted “space is made by shifting up everything above that point”, and when it is deleted “everything is shifted down”. There is no garbage collection, and no fragmentation.

What was missing

There is no ELSE. IF controls the rest of its line, so an either-or decision is written with GO TO:

BASIC
10 LET n=INT (RND*10)+1
20 INPUT "Guess? ";g
30 IF g=n THEN GO TO 60
40 PRINT g;" is wrong"
50 GO TO 20
60 PRINT "Right!"

There are no named procedures either: every subroutine is a GO SUB to a line number, with every variable global. Programs also slowed as they grew. CRASH called the language “very slow, especially when large programs are being run”, and noted that FOR loops, GO TO, GO SUB and RETURN “slow up alarmingly as program size increases”.

Machine code from BASIC

PEEK and POKE read and write memory, and IN and OUT read and write the processor’s I/O ports. USR with a number “calls the machine code subroutine whose starting address is x”, and hands back the contents of the Z80’s BC register pair. Because USR is a function, it has to be used inside a statement. Listings usually wrote RANDOMIZE USR 32768, which uses the returned value as a new random-number seed and otherwise throws it away. That line is how countless hybrid programs moved from a BASIC loader into machine code.

Extended BASICs

Programmers who stayed with BASIC could buy more of it. CRASH’s “Battle of the BASICs” in February 1986 compared three extensions, all loaded from tape:

  • Beta BASIC by Betasoft (£9.95), with procedures, ELSE, better loops and a toolkit;
  • Mega BASIC (£9.95), sold by the publishers of Your Spectrum, with windows, sprites and a choice of character sets;
  • Laser BASIC (£14.95) from Ocean IQ, written by Oasis Software, a sprite and graphics system aimed at games.

The reviewer preferred Beta BASIC, “mainly because of its elegance”. Both Beta BASIC and Mega BASIC used about 20K of memory.

128 BASIC

The Spectrum 128, launched in Spain in 1985 and in the UK in 1986, kept the old language as “48 BASIC” and added a new mode with “a full screen editor”. Its introductory manual put the change plainly: in 128 BASIC “none of these special key entries apply. Instead, you type each word in full, letter by letter”, and the machine converts keywords to capitals when it accepts the line. Amstrad’s +2 manual adds that GO TO can be typed as one word.

The language underneath changed little. CRASH’s Simon N Goodwin counted “only two extra BASIC commands” in August 1987: PLAY, for the three-channel sound chip and MIDI, and SPECTRUM, which drops back to 48K mode. He found the editor “good in theory, but annoying in practice”: it did not show whether you were typing capitals or graphics, and it paused while converting each line back into the tokens “the 1982-vintage interpreter” understood.

Sources

  • Steven Vickers, Sinclair ZX Spectrum BASIC Programming, edited by Robin Bradbeer (Sinclair Research, 1982; second edition 1983): keyboard modes and “five or more distinct meanings” (chapter 1, p. 7); FOR control variables (p. 32); variable names (chapter 7, pp. 45–46); string slicing (chapter 8); RND and RANDOMIZE (p. 75); arrays (pp. 79–80); tokens (chapter 14, p. 91); the flashing question mark (p. 47); PLOT and the coordinate range (chapter 17, p. 121); DRAW with an angle and the broadening lines (p. 122); BEEP (chapter 19, p. 135); memory “shifting up” and “shifting down” (chapter 24, p. 164); number storage and the whole-number form (p. 170); USR and IN (appendix, p. 200).
  • Sinclair User issue 46, January 1986, pp. 87–88 (extract from Rodney Dale’s The Sinclair Story): the ZX80’s launch, and Nine Tiles as the company “which had originally provided Sinclair Basic”.
  • Chris Smith, The ZX Spectrum ULA: How to Design a Microcomputer (ZX Design and Media, 2010), chapter 7: the Spectrum ROM as “the work of John Grant and Steven Vickers, contracted from Nine Tiles Information Handling Ltd”.
  • CRASH issue 25, February 1986, pp. 99–102, “Battle of the BASICs” (Tech Niche): the verdict on ZX BASIC, prices and publishers of Laser BASIC, Mega BASIC and Beta BASIC, their memory use, and the reviewer’s preference.
  • CRASH issue 43, August 1987, pp. 65–67, Simon N Goodwin’s Tech Niche: the Spectrum 128’s extra commands and editor.
  • Sinclair ZX Spectrum 128 Introduction (Sinclair Research, 1986), pp. 1 and 6: the full-screen editor and letter-by-letter typing in 128 BASIC.
  • Sinclair ZX Spectrum +2 User Guide (Amstrad, 1986), chapters 6–8: 128 BASIC and 48 BASIC modes, and GO TO typed as one word.
  • Commodore 64 Programmer’s Reference Guide (Commodore, 1983), p. 7: C64 variable names.

See also

Checked. Verified against primary sources and signed off by a person.