Let A = …
This installment is going to show you how to assign values to a number of variables–or, as computer jargon has it, to load registers. But first, our little digression.
Here is a way you can build up a huge block of nulls–thousands of them, if you wish (and if your RAM is big enough). We’ll find a practical use for them in our next issue, when we learn how to use machine language to store a screenful of material in a single REM (or PRINT, or LPRINT) statement.
But first, one caution. After you create the MEGA-REM, you must never DELETE the line immediately following it. You may EDIT that line however much you wish, even to the point of reducing it to a naked 2 REM. But if you try to DELETE it, you’re in grave danger of a crash.
With that one caution in mind, here’s what to do. First, enter the machine code routine shown in Figure 8-1, and then build up a number of sub-assemblies that will be joined to make the MEGA-REM. These sub-assemblies will look like this:
50 REM...............
Once you have one of them entered, you can easily make lines 49, 48, etc. identical to this line, just by bringing it down into EDIT position and changing the line number. And then establish an end point for the series by
51 REM "
There are a couple of restrictions on the subassembly lines:
1. There must be no line number 11.
2. No line should contain exactly 9 nulls. (They may be as long as you can conveniently handle – or as short as 1 byte.)
3. No line should contain a quotation mark, except for the last one.
4. The lowest-numbered line should be number 2 or above.
When you think you have the number of nulls that you want, then PRINT USR 16517 (note the new number) will tell you how many nulls will be in the final MEGA-REM. You can adjust that number by adding or deleting lines, or adding or deleting nulls within any of the lines (always observing the provisos listed above). When the number is right, you can consolidate them all by RAND USR 16514.
16514 195 ? JP
16515 150 ▒ 16534
16516 64 RND
16517 62 Y LD A,
16518 11 " 11
16519 33 5 LD HL,
16520 191 ▒ 16575
16521 64 RND
16522 17 ) LD DE,
16523 180 ▒ 16564
16524 64 RND
16525 1 ▒ LD BC,
16526 0 0
16527 0
16528 35 7 INC HL
16529 3 ▒ INC BC
16530 190 ▒ CP(HL)
16531 32 4 JR NZ,
16532 251 CLS -5
16533 201 TAN RET
16534 205 LN CALL
16535 133 ▒ 16517
16536 64 RND
16537 3 ▒ INC BC
16538 3 ▒ INC BC
16539 3 ▒ INC BC
16540 235 FOR EX DE,HL
16541 113 ? LD(HL),C
16542 35 7 INC HL
16543 112 ? LD(HL),B
16544 11 " DEC BC
16545 11 " DEC BC
16546 11 " DEC BC
16547 62 Y LD A,
16548 0 0
16549 35 7 INC HL
16550 35 7 INC HL
16551 54 Q LD(HL)
16552 27 . 27
16553 11 " DEC BC
16554 184 ▒ CP B
16555 32 4 JR NZ,
16556 249 RAND -7
16557 185 ▒ CP C
16558 32 4 JR NZ,
16559 246 PLOT -10
16560 201 TAN RET
FIG 8-1. BUILDING A MEGA-REM
And while you’re wondering what to do with that huge block of nulls, let’s go back to the lesson.
ZX81 BASIC lets you define a virtually unlimited number of variables–anything that starts with a letter and contains only letters and numbers. So a variable in BASIC could be A, AARDVARK, A1,…on through ZYMURGY, ZZZ, and beyond.
Not so with machine code. We only have seven variables to work with (well, we do have a few others, that we’ll hold in reserve for the the moment), and we can’t be arbitrary about their names. Somebody has already given them a rather prosaic series of names: A, B, C, D, E, H, and L. At least they showed a little bit of creativity–the last two could have been F and G. (As we mentioned earlier, the H and L stand for High and Low, in some contexts.)
The “in” group of machine codists don’t talk about them as variables–they speak in terms of “registers”, which are “loaded”, instead of variables which are assigned a value. Since this term is used by the highbrows, we’ll use it, alongside our BASIC analogies. And then, after a while we’ll drop the analogies. After you’ve seen the two systems side by side a couple of times, you’ll start to feel at home with the new convention. Sort of like watching an English-language movie with foreign subtitles.
We already know how to set two variables:
LET B = or LD B, is 6 (06h)
LET C = or LD C, is 14 (0Eh)
The symbols LD B, and LD C, which of course stand for LOAD B and LOAD C, are known as MNEMONICS (and that’s a great letter for your phonetic alphabet–just think: P as in pneumonia, M as in mnemonic…). The comma is a part of the mnemonic, and serves to separate the instruction from its argument.
The seven registers can be loaded by means of the instructions given in Table 8-1. You’ll also find these, and the rest of the Z80’s instruction set, in the back of the ZX81 manual, under the section on the Character Set.
MNEMONIC MEANING INSTRUCTION --------------------------------- LD A,N * LET A=N 62 LD B,N * LET B=N 6 LD C,N * LET C=N 14 LD D,N * LET D=N 22 LD E,N * LET E=N 30 LD H,N * LET H=N 38 LD L,N * LET L=N 46 * EACH INSTRUCTION REQUIRES A SINGLE BYTE OF DATA TABLE 8-1. LOADING SINGLE-BYTE REGISTERS
So now you can load any of several registers with a number from 0 to 255. Only you can’t look at any of these registers except B and C. Remember, all you can get the computer to say is “256*B+C=…”
Obviously, we have to find some way to shift data from one register to another. And that’s where Table 8-2 comes in.
LD R1,R2 (LET R1=R2) (LET = ) A B C D E H L --------------------------------------------- A, 127 120 121 122 123 124 125 B, 71 64 65 66 67 68 69 C, 79 72 73 74 75 76 77 D, 87 80 81 82 83 84 85 E, 95 88 89 90 91 92 93 H, 103 96 97 98 99 100 101 L, 111 104 105 106 107 108 109 TABLE 8-2. SHIFTING DATA BETWEEN REGISTERS
It’s a little puzzling, by the way, why they provide instructions that will LET A=A, LET B=B, and so on, but there they are.
So let’s try out what we know. Enter the program of Figure 8-2, using the loader program. Did you get 1? Now try Figure 8-3. Your output should be 256.
OPERATION MNEMONIC INSTRUCTION ---------------------------------- LET A= LD A, 62. 1 1 1. LET B= LD B, 6. 0 0 0. LET C=A LD C,A 79. RETURN RET 201. (PRINT PRINT USR 16514 256*B+C) FIGURE 8-2. LOADING REGISTERS
OPERATION MNEMONIC INSTRUCTION ---------------------------------- LET H=1 LD H,1 38.1. LET L=0 LD L,0 46.0. LET B=H LD B,H 68. LET C=L LD C,L 77. RETURN RET 201. (PRINT PRINT USR 16514 256*B+C) FIGURE 8-3. LOADING REGISTERS (CONTINUED)
Try several other combinations: LET D=1 (LD D,1) and LET E=0 (LD E,0), for instance. Shift them into B and C respectively, and see if you come out with 257. Then with the same values in D and E, shift them into C and B respectively, and see if you come out with 1.
If you want to get really fancy, try passing a variable from E to H before bringing it back to B or C. Do the same with the other register of the BC pair. Sort of like Tinker to Evers to Chance. Or, in 1984, like Sandberg to Bowa to Durham. Remember, though, you have to end up with a defined value for B and C in order to get any meaningful output.
And remember, as we learned in Chapter 6 (SWN 2:6), if you don’t assign any value to BC, it just takes on the starting address of the USR routine.
Doubling Up
Fine, you say, but what if I want to use numbers bigger than 255? Well, as the candidate says, I’m glad you asked that question. And you may have already guessed the answer–use 256-imal. Remember that BC will handle any number up to 65535–one less than 256*256. So why not treat other register combinations as pairs?
Actually, you’ve already been doing that. Remember when you LET H=1 and LET L=0? That was the same as Loading HL with 256. To pair two registers, all you have to do is load one of them with all the 256’s in your number, and load the other register with what’s left over. Sort of reminds me of the man who went to a hotel for a rest and a change, but Room Service got all the change, while the hotel got all the rest.
Or, in mathematical language, to Load H with 258, you
LET H=INT(258/256) =1
and LET L=258-256*H =2
Three pairs of the Z80’s registers are set up to work AS PAIRS. Look at Table 8-3. Unfortunately, Zilog, the company that designed the chip, didn’t provide any way for shifting data from one pair to the other–you have to move data one register at a time.
MNEMONIC MEANING INSTRUCTION ------------------------------------- LD BC,NN * LET BC=NN 1 LD DE,NN * LET DE=NN 17 LD HL,NN * LET HL=NN 33 * NOTE THAT 2 BYTES OF DATA MUST BE SUPPLIED, LEAST SIGNIFICANT BYTE FIRST TABLE 8-3. LOADING REGISTER PAIRS
Remember, a register pair must be loaded with two bytes of data, in 256-imal. If you tell the computer to load a register pair, you’ve implicitly promised it two bytes of data to chomp on. If you don’t provide two of them, the computer will take a byte out of the next instruction, and your program will be in real trouble. Table 8-3 reminds you of this fact by using the symbol NN to represent the two-byte data unit.
Double Trouble
Let’s use the loader program (also in SWN 2:6-ed.) to enter
100 LET P$="1.1.0.201."
LET B=1, and LET C=0, no? Try it, and see. You’d expect the printout of BC to give you 256, wouldn’t you? So what do you get? 1! Try
100 LET P$="1.0.1.201."
And there’s the 256 you wanted.
When you Load a two-byte register, the computer wants the Least Significant Byte first, and the Most Significant Byte second. Say that over and over again to yourself. If it’ll help you to think of loading LH instead of HL, remember it that way. Or you might remember that the bytes are loaded in alphabetical order: LSB followed by MSB.
Remember it any way you want–any mnemonic that works is a good mnemonic. But one way or another, remember it.
That’s all for now. There will be more.

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.