(Number 5 in a series)
There are more than 100 1-byte instructions for data manipulation between single registers. These are grouped as “LD r,r”; “ADD r,r”; “SUB r” (from A); logical operations (AND r, OR r, and XOR r); and “CP r” (comparisons to A). Here we’ll look at loading, adding and subtracting.
LD r,r is the general instruction for data transfers between single registers. That means that values must be less than 256, because no single register can hold more than 8 bits, or a value of 255. Register pairs are just that–two 8-bit registers.
Before you can transfer data with an instruction such as LD C,E (load data in E into C), the E register must have data in it. In practice, all registers have data in them at all times, but rarely the data you want. So you must load the “source” register (E in our example) with data before moving it to the “destination” register (C in our example).
Adding and subtracting single registers are similar in that all operations are between the A register (Accumulator) and the other single registers (B,C,D,E,H, or L) or the HL register pair. (ADCs and SBCs are identical to ADDs and SUBs except that they include the Carry bit in the Flags register.)
For example, let’s load B with 132, C with 45, and D with 36. Then we’ll add B and C and subtract D twice:
REM ADD. DECIMAL=MNEMONIC COMMENT
xx1 6 LD B,n
xx2 132 (data)
xx3 14 LD C,n
xx4 45 (data)
xx5 22 LD D,n
xx6 36 (data)
xx7 120 LD A,B ;B to A
(A=132)
xx8 129 ADD A,C ;C+A=177
(A=177)
xx9 146 SUB D ;A-D=141
(A=141)
x10 146 SUB D ;A-D=105
(A=105)
REM ADD lists relative addresses. You can find the decimal codes for instructions in your 8K ROM or ZX81 manual, Appendix A (the character set). The first and third columns list decimal code and corresponding Z80 mnemonic. These codes also serve as character codes in BASIC. You can find explanations for instructions in assembly language manuals such as Zilog’s Z-80 Assembly Language Programming Manual.
After “running,” A will contain 105 while B,C and D will all contain their “loaded” values. Except for the A and Flags registers, all registers keep their data intact until specifically altered.
Next month: Exchanging Registers