FizzBuzz in BASIC on a Sinclair ZX81
The Context
I went to the History of Computing museum in Cambridge on Mar 10, 2024 and there were lots of old computers, including a Sinclair ZX81.
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fce4ae3a6-c14e-4216-8a53-12ebf58dd203%2F53f73f5c-3613-4830-bfd4-d74561bfcffe%2FUntitled.png?table=block&id=08efc210-a0a4-4554-a933-1f2c9e481af7&cache=v2)
The entire computer is tiny - the keyboard (which was not mechanical and horrible to type on) comes connected so you only need to connect a monitor.
The keyboard has the reserved keywords hardcoded which self-insert depending on the first key of a line you pressed. There were some other reserved keywords which you needed to press
shift
to access (e.g. to do an if/then clause you press u
, which inserts the word IF
, followed by the condition, then shift+3
to get a THEN
printed - you cannot simply type out IF ... THEN ...
). You can also hold shift+new line
to get into function mode to access functions like INT
.To start programming, you type the number of the line you want to overwrite and then press
space
. As there is no way to rearrange the lines dynamically (that I found), you typically increment your line numbers in 10s so you have room to insert new code you might need later without needing to re-write your entire program.The Solution
I didn’t spend too long on it, but I ran into some problems:
- There was no modulo operator as far as I could find in the manual, which meant I ended up using the
INT
operator to check if there was a remainder.
I could have used an algorithm to take away the divisor (3, 5, or 15) from
I
(the number I’m testing) until the remainder is less than the divisor and check if the remainder is 0, but the former solution seemed clearer and was easier to type.- As far as I could see, there was no way to have an
if
block of more than one command. To run something like:
if (/* condition */) { // command one // command two }
You needed to do:
IF /* condition */ THEN /* command one */ IF /* condition */ THEN /* command two */
- To do a for loop, you need to call
NEXT I
to jump back to the top of the next iteration.
The Code
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fce4ae3a6-c14e-4216-8a53-12ebf58dd203%2Fa992fab0-a2ee-4593-8316-327e61c5ee21%2FUntitled.png?table=block&id=3601dd54-b896-4161-8883-3e9d0fd64c70&cache=v2)