The Absolute Beginner’s Guide to Machine Code Programming on the ZX81/TS1000/TS1500

Author(s): Jack Hodgson
Pub Details: Sinclair Timex User Group Newsletter v3 n6 (Volume 3, Issue 6)
Date: August 1984
Pages: 1, 8-10

Over the years I’ve been amazed at the number of beginner programmers who are interested in machine language programming. Others might say that a beginner should avoid this type of stuff because it’s far too complicated for them but I don’t agree. I was only a beginner when I first plowed my way through Toni Baker‘s book. If I learned it, anyone who wants to can.

The biggest problem I had in learning all this was that the Z80 processor chip that’s in the Sinclair is so sophisticated that it had too many instructions for me to deal with. I didn’t know where to start.

What I’m going to do in this series of articles is to present a simplified view of Z80 programming. I won’t show you all the instructions or make you understand all the details of how it works. I will give you enough info to write simple, useful machine language programs.

This series will deal with the ZX81/TS1000/TS1500 series of computers. Many of the priciniples I demonstrate will apply to the TS2068 as well, but some of the details will have to be adjusted. Ask me or another machine language programmer at the meetings about the differences. If there’s enough interest the editor of this letter may allow me to write an updated TS2068 version.

OK let’s get started.

A computer is just a collection of storage places where one puts numbers. The instructions tell the computer to move numbers from one place to another, do arithmetic with them, compare them, and do things as a result of those comparisons.

First let’s look at how these storage places are set up.

The processor has a bunch of storage locations built into it. They are called “registers”. There are alot more storage locations in the area we all call RAM. The registers have names that are letters: like A, B, C, H, and L. The RAM locations have names that are numbers: 0 up to 65,535.

The computer understands instructions that tell it to move a number from one location to another, for example, “move the contents of 16514 to A”. That instructions is actually called “LD A, (16514)”. That means load the A register with the number that currently in location 16514.

The parentheses tell the computer to load A with the contents of the “address” 16514 instead of the number 16514 itself. If we wanted to put a particular number into A we would say “LD A, 16514” without the parenthesis.

An important note here. The computer can only deal with certain sized numbers. Our second example, using 16514, would not work because 16514 is too big for the computer to handle. The computer can only handle numbers up to 255.

What follows is as complicated this is going to get. So please take a deep breath and understand it.

The number “fifteen” can’t be understood by a desktop calculator when it’s written that way. You need to divide it into “digits” between 0-9. We all know that “fifteen”, turned into digits is “15” but understand how that happened. “15” is “1” times 10 plus “5” times 1. That’s called “base ten”. Computers use “base 256” (Please don’t everyone holler at me. I know people always says computers use base two. They do, but you don’t need to know anything about that here. Just THINK BASE 256).

Converting numbers from base ten to base 256 is easy. You look it up in a chart. For the stuff we’ll be doing here I’ll supply the conversions. Next installment I’ll show you how to make a chart of your own.

Anyway, where were we? Oh yah. 16514 is 64-130. That is, 16514 is “64” times 256 plus “130” times 1. LD A, 16514 is written for the computer as LD A, 64-130. LD A, (16514) is written LD A, (64-130).

Sometimes we want to store numbers bigger than 255 in a register. We can do this by combining two registers into a bigger one. Some registers are set up to be used in pairs. B&C and H&L are ready to be combined. They are then called by their combined name: BC or HL. We can put numbers in them by using LD HL, 64-130 or LD BC, 64-130.

Up above we asked to computer to load A with the number stored at adresss 64-130. We can also tell it to load A with the number stored at the address that’s currently in register HL. For example:

LD HL, 63-130
LD A, (HL)

This does the same thing as

LD A, (63-130).

Before the computer can execute these instructions we must convert them into numbers. The computer can’t understand the letters LD A, 28. It only understand numbers.

The number for the instruction LD A, 28 is 62-28. The 62 means LD A and the 28 tells what to load it with. 62-255 would mean LD A, 255.

When your’re loading a register pair you have to do something strange. LD HL, 64-130 becomes 33-130-64. 33 means LD HL and the number to be loaded is tacked on in reverse order. Whenever the computer is reading a two section number like that, it must be given to it in reverse order.

OK. Enough of this fooling around. Let’s write a program. Here’s a BASIC program:

10 PRINT "HELLO WORLD."

How would we do that in machine language? It’s really not too hard.

ADRS      OP CODE       INSTRUCTION
====      =======       ===========
64-130    33-144-64     LD HL, 64-144
64-133    126           LD A, (HL)
64-134    254-255       CP 255
64-136    200           RET Z
64-137    205-8-8       CALL 8-8
64-140    35            INC HL
64-141    195-130-64    JP 64-133

64-144    45-42-49-49-52-0-
          60-52-55-49-41-27-255

ADRS is the RAM location where these instructions are going to be stored. OP CODE is the numbers that the computer understands as instructions.The numbers starting at 64-144 are the code numbers for the letters “HELLO WORLD.”

The first instruction LD HL puts the starting address of these letters into the HL register. LD A, (HL) puts the first “letter” into A. CP 255 compares whatever’s in A to see if it’s equal to 255. RET Z ends the program if the comparison we just did is equal to 255 (notice that the last “letter” is 255).

CALL 8-8 tells the computer to executea routine that is already in the computer, at address 8-8. That routine causes the “letter” in the A register to be printed on the screen. INC HL causes the HL register to be increased by one so it now is the address of the next “letter”. JP 64-130 causes the computer to go back to the LD A, (HL) and get the next “letter”.

It does all this, getting letters and printing them, until it gets to the “letter” 255 then it stops (RETurns).

Originally, I wanted to show you, in this first installment, how to load this program into your computer and run it. But I’ve run out of room and you have enough to chew on anyway.

Next time I’ll tell you about loading machine language programs; getting “letters” from the keyboard, and the infamous, “Hex numbers”.

Machine language is not mysterious. It is a little more complicated than some “high-level” languages because in machine language you must keep track of many details yourself. But it can add to the sophistacation of your programming and speed up you routines. And finally many programmers like to simply “work closer to the machine.”

If you got this far you’ll love installment two.

Jack