Turning Hello World into a Creative ZX81 BASIC Program


A random scrolling hello world program.

The year is almost over and time to close out the year with a short program. Watching some videos on character displays in UNIX, I was inspired to create my own version of Hello World. This version scrolls through a stream of random digits until the message is displayed. How fun is that?

# An old favorite.

Back in the eighties, “Hello World” was often the first program you would write when purchasing a home computer. Note that I said write, not play. As many of us simply loaded up a game to play first. I mean, why else would you buy a computer?

In any case, the first program usually introduced the user to the syntax of the BASIC language for that computer. This was a time when each computer came with its own dialect. Line numbers were common and almost every computer came with the PRINT command. A such it was a good starting point.

10 PRINT "HELLO WORLD"
20 GOTO 10

Of course, this introduces the controversy of the time which is how BASIC managed flow control. The GOTO keeps the program running and accounts for all those bad programmers from that time. Probably not, but it did tend to label BASIC as a toy language. Sad times.

On the ZX81, the program above wouldn’t run forever. When the bottom of the screen was reached, the ZX81 would halt. You could use CONT to continue the program. This would clear the screen and keep going, but it would only end again when it reached the bottom.

# Doing something better.

When I saw a different approach to printing “Hello World,” I’m all in. My version isn’t much longer, but it is more interesting.

To start, the program uses two strings. The first, A$, defines what will be printed. You can actually change this to just about any string you want. I stuck with the classic. The second, C$, uses DIM to create an empty string with it’s size defined by A$.

  20 LET A$="HELLO WORLD/"
  30 DIM C$(LEN A$)

With that, the code gets a bit more esoteric, but not overly so. This time, flow control is provided using a loop. The L determine what character will be updated. Line 50 then sets the character C$(L) with a random character. On the ZX81, the first 64 characters (0-63) are all printable single letters or graphics. Going higher gets you to the ZX81 BASIC tokens. For example, character 64 is RND and would mess up the display.

  40 FOR L=1 TO LEN A$
  50 LET C$(L)=CHR$ INT (RND*64)

Lines 60 and 70 scroll the display and print the current contents of C$. The use of SCROLL ensures the ZX81 doesn’t stop the display. If you wanted to get fancy, you could change up when you scroll and what is printed.

  60 SCROLL
  70 PRINT C$

Last two lines of the routine are both flow control. Line 80 checks if the random character matches the one in A$. If not, it jumps back to 50 (that pesky GOTO again) to try a different character. If it does match, it goes to the next character to test.

  80 IF C$(L)<>A$(L) THEN GOTO 50
  90 NEXT L

The rest of the code pauses for a short time before clearing the display and restarting,

Hello World, ZX81 Screenshot, 2024 by Steven ReidHello World, ZX81 Screenshot, 2024 by Steven Reid

# A fancier beginner’s program.

The result is a fancier display of “Hello World” than most first timers would have attempted. To be honest, I wish this was what was use. In a few lines of code you could teach a lot of coding techniques that would aid in future programming. Bit late to attempt writing my own BASIC book.

After writing this program, I had a thought of changing up the print routine a bit to only scroll when the letter was found. This more closely matches the demo I was inspire by. If you want to try yourself, change lines 60-70 to this:

  60 PRINT AT 21,0;C$
  70 IF C$(L)<>A$(L) THEN GOTO 50
  80 SCROLL

I’m sure you could come up with other variations based on code I’ve written before. I’d love to see what others come up with. Please share if you do!



Comments on this article:

No comments so far.

Write a comment:

Type The Letters You See.
[captcha image][captcha image][captcha image][captcha image][captcha image][captcha image]
not case sensitive