Egghead.page Logo

How Sinclair ZX81 BASIC Handled String Variables

The Sinclair ZX81, launched in 1981, was renowned for its affordability but limited by just 1KB of RAM. This article explores how its version of BASIC managed string variables within such tight constraints. We will examine the memory allocation strategies, the lack of dedicated string arrays, and the specific syntax users employed to manipulate text data on this iconic home computer.

To identify a string variable, Sinclair BASIC required a dollar sign suffix, such as A$ or NAME$. This syntax distinguished text data from numeric variables, which was standard for BASIC dialects of the era. When a string variable was assigned a value, the system stored the characters in a dedicated variable storage area located at the top of the available RAM. The interpreter maintained pointers to track the start and end of this memory region, ensuring that program code and variables did not overwrite each other.

Memory management was the most critical aspect of string handling on the ZX81. Due to the severe limitation of 1KB of random-access memory, string storage was dynamic and precarious. Each time a string was created or modified, the system had to shuffle memory to accommodate the new data length. If a string variable was reassigned with a longer value, the interpreter moved existing variables to make room. This process could lead to memory fragmentation, where free space became scattered, potentially causing an “Out of Memory” error even if the total free RAM theoretically exceeded the requirement.

A significant limitation of the ZX81’s ROM BASIC was the absence of string arrays. Users could not declare dimensions like DIM A$(10) to store multiple strings under a single variable name. Instead, programmers had to create individual variables for every text entry they wished to store, such as A$, B$, and C$. This restriction heavily influenced game design and data storage, forcing developers to encode information numerically or use clever printing routines to simulate text databases.

String manipulation functions were also minimal compared to later microcomputers. While users could concatenate strings using the plus operator, advanced functions for substring extraction or length checking were either absent or required complex workarounds. The LEN function was available to determine the length of a string, which was vital for managing memory usage, but operations like finding a character within a string required custom subroutines. These constraints defined the programming style of the ZX81 era, emphasizing efficiency and brevity in every line of code.