This starts our series on machine language programming for beginners. You’ll need to review our previous colums on binary and hex number systems and see the story on addresses this issue.
Last month we published the routine to check how much ZX80 memory is connected, courtesy of Sinclair Research:
1 POKE 17000,33
2 POKE 17001,11
3 POKE 17002,0
4 POKE 17003,57
5 POKE 17004,201
6 PRINT USR (17000)-16383
–A machine language program in disguise.
Some of these BASIC commands may be unfamiliar to you. POKE places decimal values between 0 and 225 in specified addresses (see p.15 about addresses). USR begins at the address specified and executes a machine language subroutine until it reaches a code (201 or C9 hex) that tells it to return to the point in the BASIC program from which it left, much like a subroutine’s GO SUB and RETURN commands.
Machine language controls the computer by manipulating numbers in registers and memory. A register is the computer’s scratchpad for holding and calculating numbers, usually between 0-225. Memory is used to store the numbers, which can represent the numbers themselves, characters in the ZX80 character code, or opcodes (machine language commands). Opcodes are like computer shorthand.
The BASIC system uses some RAM space, called the stack, to evaluate expressions (like A+B/C), save the original value of changing variables (as in a FOR-NEXT statement) and execute subroutines. Register SP, the stack pointer, tells the computer where the stack ends and spare memory begins. The plan behind this program is to check where the stack pointer is and add back the number of bytes in the stack. This will give the total bytes of RAM available.
Let’s look at the values that this BASIC routine pokes into memory:
33 11 0 57 201 (decimal) 21 0B 00 39 C9 (hex)
Opcode 33 tells the computer to load the next two bytes, 11 and 0, into a working register pair, HL.
The integer loaded into HL is 11 decimal, or 000B hex. 11 and 0 represent only one integer value. The ZX80 stores all integers in two bytes. The programmer knew by testing a working ZX80 that the stack has 11 bytes in it when this program executes. Because the stack pointer is at the top of memory and moves down as the stack above fills, it will be 11 bytes from the top of memory. To load HL, you need to use 2 bytes (remember, HL is a register pair), so you add 11 then 0 to compensate for the 11 bytes in the stack. The first byte loaded is called the lower byte, the second is called the higher byte. “Lower” and “higher” refer to the byte’s position in the register, not to the magnitude of the number loaded.
Opcode 57 tells the Z80 to add the contents of HL (000B hex) to the value of SP and places the answer back in HL. So HL’s new value is that of SP plus 11.
Opcode 201 is the return to BASIC command.
These codes are processor-specific, that is, all Z80 processors (like the ones in ZX80s and MicroAces) use the same codes, but other processors like the 6502 or 6800 do not. You can find these codes in books on machine language programming or code listings. Two such books are Rodnay Zaks‘ Programming the Z80, published by Sybex, and the Z80-Assembly Language Programming Manual from Zilog (who first developed the Z80 processor). See p.12 for information on ordering the Zilog manual.
The computer cannot distinguish opcodes from data except in context. That is, it must be told where to begin reading, or it will execute the wrong instructions (if it can read the codes at all). The BASIC USR command sends the computer to the correct address to start reading so the machine language codes will make sense.
In the routine above, the ZX80 pokes five integers into addresses starting at 17000. USR(17000) tells it to begin reading at address 17000 and execute machine language instructions until it hits the return at 17004. Then the computer pops back into BASIC and evaluates USR(17000)-16383. USR(17000) evaluates to the number in HL when the computer returns to BASIC. Register HL now contains the sum of SP and 11, or 17407, the highest consecutive RAM address. Subtracting 16383 removes the possible ROM locations, giving the answer 1024 RAM bytes for a 1K machine.