Skip to content

Make the Spectrum answer

Enter a greeting, turn it into a program, and change what it says.

  • Sinclair ZX Spectrum
  • BASIC

We can give the Spectrum an instruction and see its answer straight away. Then we’ll keep instructions together as a program we can run again and change.

You can start here without having programmed before. We’ll explain the words and keys as we use them.

Start the Spectrum

On a phone or tablet? Open the editable listing, change the greeting and press Run this code. Your device’s keyboard works in that listing. The Spectrum-screen instructions below use a physical keyboard.

These examples use a 48K ZX Spectrum, and there is one in this page. Press Start the Spectrum below. The 48K ROM is included with the player, so there is nothing to install. When the Sinclair copyright message appears, click the Spectrum’s screen so that it receives your keys. Tab returns to the page.

Start an empty 48K Spectrum, or run the finished program. No installation or ROM file needed.

Using a touchscreen? Edit and run the listing below.

The computer you are reading this on is our host; the Spectrum the page recreates is our target. The BASIC program runs on that target. If you would rather use a desktop emulator, see work locally. There, use the left Alt or Option key for SYMBOL SHIFT and Home for EDIT; the other keys below work the same way.

Find the keyboard

The Spectrum keyboard has words as well as letters. A keyword is a word BASIC recognises as an instruction or part of one. At the start of an instruction, one key enters a whole keyword: P enters PRINT.

The Spectrum also has two shift keys of its own. CAPS SHIFT gives capital letters and editing combinations. SYMBOL SHIFT gives the red symbols and words printed on the keys, such as the quotation mark on P. In the page, your keyboard’s Shift is CAPS SHIFT and Alt (Option on a Mac) is SYMBOL SHIFT. Enter finishes a line.

Give one instruction

Press P. PRINT appears, followed by a flashing cursor showing where the next character will go. The cursor changes from K, for keyword entry, to L, for ordinary letters. It is telling us how the next key will be interpreted.

Enter this instruction:

Sinclair BASIC
PRINT "Hello"

After pressing P for PRINT, hold Alt and press P for the opening quotation mark. Hold Shift and press H for a capital H, type ello, then use Alt+P again for the closing mark. Press Enter.

PRINT displays the text between the quotation marks. The marks tell BASIC where the text starts and ends; they do not appear in the greeting. BASIC supplies the space after its keyword, so you needn’t type an extra one there.

The Spectrum displays Hello, with the report 0 OK at the bottom.
An instruction without a line number runs when we press Enter.

The report at the bottom begins 0 OK. It says the instruction finished without a reported error. It cannot tell whether we wrote the message we intended: we check that ourselves.

If a character is wrong before you press Enter, use Backspace to delete the character immediately before the cursor, then type the replacement. Backspace stands in for the Spectrum’s DELETE: CAPS SHIFT and 0 together.

Keep the instruction

This time, type 10 before pressing P for PRINT:

step-01.bas
  10 PRINT "Hello"

Press Enter. The Spectrum lists the numbered instruction instead of carrying it out. The number tells BASIC to store it as a program line.

At the empty input line, press R. The keyword RUN appears. Press Enter to run the stored program. You should see Hello again. Repeat R, Enter: the program is still there. Running it does not use it up.

The Foundations explanation of instructions explores how precise instructions produce a result. Here, PRINT is the particular word Sinclair BASIC uses for displaying something.

Add another line

Keep line 10 and enter line 20. Use P for PRINT again, then enter the quoted text. The complete program is now:

step-02.bas
  10 PRINT "Hello"
  20 PRINT "from the Spectrum"

Run it. The two messages appear on separate lines. Each of these PRINT instructions finishes its output with a new line.

Now enter one more instruction, numbered 15:

step-03.bas
  10 PRINT "Hello"
  15 PRINT "to you"
  20 PRINT "from the Spectrum"
We entered line 15 last. Where will its message appear?

Between the other two messages. In this program, BASIC follows increasing line numbers: 10, 15, 20. The order in which we typed them does not decide the running order. Run it and compare the result with your prediction.

Three lines read Hello, to you, and from the Spectrum.
Leaving gaps between numbers gives us room to insert instructions.

The numbers are labels, not a count of how many lines exist. The Foundations unit on order develops why changing the order of instructions can change a result.

Change a line

Enter 10 PRINT "Welcome", using the same keyword key and Alt+P for each quotation mark. There is already a line 10, so BASIC replaces it. Lines 15 and 20 remain:

step-04.bas
  10 PRINT "Welcome"
  15 PRINT "to you"
  20 PRINT "from the Spectrum"

Press K at the empty input line for LIST, then Enter. LIST shows the stored program; RUN carries out its instructions. List it, check the new line 10, then run it and look for Welcome.

For these short lines, entering a replacement is a useful way to edit. To bring an existing line back into the editor, enter LIST 10, then press Shift+1 for EDIT and release both keys. The line appears at the bottom. The left and right arrow keys move within it; Backspace deletes the character before the cursor. Press Enter to store it again. Pressing Enter without a change keeps the same line.

A line number entered on its own deletes that stored line. The next lesson starts from these three lines and gives them in full, so it does not matter if your Spectrum has been cleared by then.

Run an editable listing

Change Welcome in the listing below, then press Run this code to see your greeting on the Spectrum. Choose Back to the listing to change it again.

Original code

Opens in your Spectrum.

Edit this listing, then run it. Changes are marked against the original code.

Listing options

Original code

  10 PRINT "Welcome"
  15 PRINT "to you"
  20 PRINT "from the Spectrum"

Your Spectrum

48K · PAL

Click the screen to send it keys. Tab returns to the controls.

Not started.

On-screen keys
Keyboard and Session

Shift is CAPS SHIFT. Alt or Option is SYMBOL SHIFT. Comma and full stop use their Spectrum key combinations automatically. Escape sends BREAK while the screen has focus. Enter submits a line. Restart clears this session. Returning to the lesson pauses it.

To type into this screen, use a physical keyboard. On a touchscreen, edit the listing in the lesson and choose Run this code. Open On-screen keys here to answer numeric INPUT prompts or send BREAK.

Runs the Sinclair 48K ROM. Amstrad have kindly given their permission for the redistribution of their copyrighted material but retain that copyright.

Download Emu198x for desktop
Screen text
The Spectrum has not started yet.

When the screen disagrees

If Enter leaves an error marker in the input line, the Spectrum has rejected its syntax: the arrangement of symbols does not form an instruction it accepts. Check the quotation marks and the keyword. The cursor or marker is a clue to where checking failed, not proof that the character at that position caused it.

If you see a listing instead of your greeting, the line was stored successfully; use R, Enter to run it. If the wrong message appears, use K, Enter to inspect what is stored. A new line number adds a line; reusing the old number replaces it.

We now have a small working cycle: enter an instruction, run the program, inspect its result, and make a deliberate change. Next we’ll give the greeting a name to use, and keep a copy outside the running Spectrum.

Sources

Steven Vickers, edited by Robin Bradbeer, ZX Spectrum BASIC Programming, second edition (Sinclair Research, 1983), introductory keyboard and editing notes, and chapter 2, “Basic programming concepts”. These describe keyword modes, numbered lines, editing and running a stored program.