Skip to content

Remember a name; keep the program

Give a name a place in memory, use it in a greeting, and keep your work.

Our greeting currently contains fixed words. We’ll give it a name it can use, then change that name without rewriting the instruction that prints it.

Start with the greeting

If you have the three-line greeting from the opening lesson, keep it: we’ll replace lines 10 and 15 and leave line 20 in place.

If you have been trying other programs, clear them before entering this lesson’s listing. Keep a saved copy of anything you want to retain first. At an empty input line, press A to enter NEW, then press Enter. This clears the stored program and its variables; clearing the screen alone would leave them in memory. You do not need to restart the emulator.

Starting from that empty program, enter all three lines below. Numbers store lines, P enters PRINT, and R at an empty input line enters RUN. The opening lesson explains keyword entry and editing in detail.

Give the name a place

Here is the complete greeting with its name stored in a variable:

10 LET n$="Sam"
15 PRINT "Hello, "; n$
20 PRINT "from the Spectrum"

At line 10, press L for LET. In Host Keyboard mode, type the remaining characters with your usual keys, including $, =, quotes and the semicolon. Select Machine → Keyboard → Host Keyboard if the window title shows Original Keyboard. The opening lesson explains the distinction.

LET n$="Sam" stores the text Sam in a variable named n$. A variable gives us a name by which to retrieve a stored value. LET assigns the value on the right of = to the variable on its left.

A string is text: a sequence of characters. Sinclair BASIC names a string variable with one letter followed by $. Here we chose n for “name”. The $ is part of the variable’s name; it does not mean money.

The quotation marks distinguish the literal text "Sam" from the variable name n$. Without quotes, BASIC looks for a value under a name. With quotes, it uses the characters themselves.

In line 15, PRINT displays the fixed text Hello, , then the value of n$. The semicolon makes the next item follow immediately. The space after the comma, inside the quotes, leaves a gap before the name.

Run the program:

The Spectrum displays Hello, Sam followed by from the Spectrum.
Line 10 supplies the name. Line 15 uses its value.

Foundations: A Place to Remember explores variables with pseudocode. This is Sinclair BASIC’s syntax for the same idea. Showing Your Work separates fixed text from a value being displayed. Its pseudocode punctuation is not a Sinclair BASIC rule: we use a semicolon here to keep the items together.

Change the value

Replace line 10 with 10 LET n$="Jo". Leave the other lines alone:

10 LET n$="Jo"
15 PRINT "Hello, "; n$
20 PRINT "from the Spectrum"
Line 15 has not changed. What will it print now?

Hello, Jo. When we run the program, line 10 assigns Jo to n$ before line 15 reads it. A variable is not a permanent label for its first value.

Run it to check. Then enter this separate instruction without a line number:

PRINT m$

The report 2 Variable not found means BASIC could not find that variable. We assigned n$, but asked for m$. The report names the immediate problem; checking our spelling explains this example. The stored program is intact: run it again to see the greeting.

Keep a copy outside the Spectrum

The program currently lives in the Spectrum’s working memory. Resetting the machine or closing an unsaved session can lose it. Keeping a copy lets us come back later and makes experiments less costly.

There are three different things we might keep:

  • A source listing, such as the .bas text shown here, records the numbered instructions in a file we can read in an editor on the host.
  • A tape image, such as a .tap file, holds data a Spectrum can load through its tape interface. BASIC’s SAVE stores the program and its variables.
  • An emulator snapshot preserves a machine state so the emulator can resume it. It is useful, but it is different from saving a program to tape.

On a Spectrum, the command is:

SAVE "greeting"

Press S for SAVE, enter the quoted name, then Enter. The Spectrum asks Start tape, then press any key. On original hardware this is the point to start recording on the cassette recorder before pressing a key. In an emulator, the recording needs to be kept as a file on the host.

The Spectrum asks Start tape, then press any key.
BASIC is waiting to send the program through its tape output.

In Emu198x, press Enter at the tape prompt and wait for the save to finish with a 0 OK report. Choose Tape → Export Recording… (or press Ctrl+Shift+E on Linux). Select a folder you can find again and name the file greeting.tap. The confirmation tells you where it was written. If that filename already exists, choose a new one: export does not overwrite existing files.

greeting in the BASIC command is the name stored on the tape. greeting.tap is the host file containing the tape data. Export keeps every recording made in the current tape session; it does not empty the recorder.

Reopen your saved greeting

After the export confirmation, close Emu198x and start it again. Choose File → Open Tape Deck… and select greeting.tap. This puts the tape in the deck; BASIC still needs to read it. On Linux, launch ./emu198x-spectrum --tape "path/to/greeting.tap" instead, using your exported file’s path and the same --rom option if needed.

At an empty input line, press J for LOAD, then enter two quotation marks with nothing between them:

LOAD ""

Press Enter, then choose Tape → Play (F9 on Linux). Wait for loading to finish. The empty name asks BASIC to load the next program it finds on the tape.

Now we can check what came back. Seeing a successful save report alone would not prove that we could recover our work.

Change the recovered program

After reopening the saved greeting, use K, Enter to list it. Check for Jo in line 10, then run it. Replace line 10 with 10 LET n$="Alex" and run again:

10 LET n$="Alex"
15 PRINT "Hello, "; n$
20 PRINT "from the Spectrum"

The result should begin Hello, Alex. The recovered copy is a program we can continue developing. Keep the changed version too when you want to resume from this point.

Next we’ll let the person running the program supply a name, using INPUT.

Sources

Steven Vickers, edited by Robin Bradbeer, ZX Spectrum BASIC Programming, second edition (Sinclair Research, 1983), chapter 2, “Basic programming concepts” (PRINT punctuation and the missing-variable example); chapter 7, “Expressions” (variable names); and chapter 20, “Tape storage”, pp. 141–145 (SAVE, LOAD, names and the recording prompt).