Egghead.page Logo

How Sinclair ZX81 Handles End of Program Listing

The Sinclair ZX81 managed the termination of a program listing through a specific memory structure rather than a visible command, relying on a zero-length byte marker to signal the conclusion of stored code. This article explores the internal memory organization of ZX81 BASIC, details how the LIST command iterates through program lines, and explains the specific technical marker that informs the interpreter when to stop displaying the program to the user.

Internal Memory Structure

To understand how the ZX81 handled the end of a listing, one must first understand how it stored programs in RAM. Unlike modern text files that use end-of-file characters, the ZX81 stored BASIC programs as a contiguous block of tokenized data. Each line of code was structured sequentially in memory, beginning with a two-byte line number followed by a single byte indicating the length of that specific line. The actual BASIC tokens and text followed this length byte, and each line was terminated by a carriage return character.

The LIST Command Mechanism

When a user typed the LIST command, the ZX81’s ROM routine began reading memory at the start address of the BASIC program area, known as PROG. The interpreter would read the line number to display it, then read the length byte to determine how many bytes of text to decode and print. It would continue this process, moving sequentially through memory, decoding each line number and its corresponding tokens for display on the television screen or monitor.

The Zero-Length Terminator

The critical mechanism for ending the listing was the line length byte. As the interpreter moved from one line to the next, it checked the length byte of the upcoming line. In a valid program, this byte always contained a value greater than zero. The end of the program was marked by a line length value of zero. When the LIST routine encountered this zero-length byte, it recognized that there were no further lines to process. This marker signified the boundary between the user’s program code and the variable storage area that followed in memory, effectively telling the system to stop listing without requiring a specific END command in the code itself.