Z80 Command and OP Code Reference

See all articles from Sinc-Link v2 n5
Publication: Sinc-Link
Pub Details: Sinc-Link v2 n5 (Volume 2, Issue 5)
Pages: 9-10

The Z80 has eight directly accessible 8 bit registers, A,B,C, D,E,F,H and L, and four sixteen bit registers IX, IY, the stack pointer SP and the programme counter PC. The eight 8 bit registers are sometimes used as four sixteen bit registers AF,BC,DE,HL. A is the accumulator, F holds flags and HL is used to point to an address in memory.

In the following n is a single byte number, nn is a two byte number, d is a displacement and (nn) is the contents of memory location nn e.g (HL) is the contents of the address held in HL.

1 Load

Mnemonic LD. Example LD A,C- copy the contents of C into A.

1.1 8 bit register to register e.g LD A, C

The contents of any of the registers A,B,C,D,E,H,L may be copied one to another.

1.2 8 bit memory to register e.g LD A, (HL)

(HL), (IX+d) or (IY+d), may be copied to any of the registers A,B,C,D,E, H,L. (BC), (DE) or (nn) may be copied to A.

1.3 8 bit register to memory e.g LD (HL), A

A,B,C,D,E,H,L may be copied to (HL), (IX+d), (IY+d). A may be copied to (BC), (DE) or (nn).

1.4 8 bit register or memory immediate e.g LD A, n

A value n may be loaded into A,B,C,D,E,H,L, (HL), (IX+d), (IY+d).

1.5 16 bit register to register e.g LD SP, HL

The contents of HL, IX or IY may be copied to SP.

1.6 16 bit memory to register e.g LD BC, (nn)

(nn) may be copied to BC, DE, HL, IX, IY, SP.

1.7 16 bit register to memory e.g LD (nn), BC

BC, DE, HL, IX, IY, SP may be copied to (nn)

1.8 16 bit register immediate e.g LD BC, nn

nn may be loaded into BC, DE, HL, IX, IY, SP

2 Push and Pop

e.g PUSH HL POP HL

A PUSH instruction copies the contents of a named 16 register to the bottom of the stack and decrements the stack pointer twice. A POP instruction does the reverse. AF,BC,DE,HL,IX,IY may be PUSHed or POPped.

3 Exchange

e.g EX AF AF’

An exchange instruction swops the contents of the named pair of 16 bit registers or memory locations. In the case of EX and EXX the contents are swopped with an otherwise inaccessible set of duplicate registers. Exchanges can be made between HL and DE, HL and (SP), (SP) and IX,(SP) and IY, AF and AF’, BCDEHL and BCDEHL’.

4 8 bit add and subtract

e.g ADD A,E SBC A,D

A,B,C,D,E,H,L,(HL),n,(IX+d), (IY+d) may be added or subtracted to or from the accumulator with or without the carry flag.

5 8 bit AND OR and XOR

e.g AND C

A,B,C,D,E,H,L,(HL),n, (IX+d), (IY+d) may be combined with the accumulator using any of the three logical operators above.

6 Compare

e.g CP C

Compare is like subtract except the flags only and not the contents of the accumulator are affected. A,B,C,D,E,H,L,(HL),n (IX+d), (IY+d) may be compared with the accumulator.

7 8 bit increment and decrement

e.g INC B

A,B,C,D,E,H,L,(HL), (IX+d), (IY+d) may be incremented or decremented.

8 16 bit increment and decrement

e.g INC BC

BC,DE,HL,IX,IY,SP may be incremented or decremented.

9 16 bit add and subtract

e.g ADD HL, BC

BC,DE,HL,IX may be added with or without carry or subtracted with carry only to or from HL. BC,DE,SP,IX may be added without carry to IX. BC,DE,SP, IY may be added without carry to IY.

10 Jump, call and return

The flag register, F, contains a carry flag, C, which is set on carry, a parity flag P, which is set if a result is even, a sign flag, S, which is set if a result is negative, an overflow flag, V, which is set on overflow, and a zero flag, Z, which is set on zero. These flags can be used to control jumps, subroutine calls and returns.

10.1 Jump e.g JP NC, nn

The following jumps to an address nn are possible: absolute jump (JP); jump on zero or not zero (JP Z and JP NZ); jump on carry or not carry (JP C and JP NC); jump on positive or negative (JP P and JP M); jump on P/V = 1 or P/V = 0 (JP PE and JP PO). The following relative jumps to an address d relative to the current position are available where d is interpreted as lying in the range – 128 to 127: absolute relative jump (JR); relative jump on zero or not zero (JR Z or JR NZ); relative jump on carry or not carry (JR C and JR NC). Jumps may be made to the addresses held in HL,IX or IY. (JP (HL), JP(IX), JP(IY)). The DJNZ instruction decrements the B register and jumps to d if B is non zero.

10.2 Call e.g Call Z nn

If the call condition is met the current contents of the programme counter is PUSHed on to the stack and a jump is made to address nn. The following calls may be made: absolute call (CALL); call on zero or not zero (CALL Z or CALL NZ); call on carry or not carry (CALL C or CALL NC); call on positive or negative (CALL P or CALL M); call on P/V = 1 or P/V = 0 (CALL PE or CALL PO).

10.3 Return e.g RET PO

If the return condition is met the value in the stack is POPped into the programme counter causing a jump back to the position following the previous call. Return conditions are available to match each call condition. Returns can also be made from the interrupt and the non-maskable interrupt (RETI and RETN)

11 Bit instructions

The eight bits in each register are numbered from 0 to 7 inclusive from right to left. Each of the following operations may be performed on the A,B,C,D, E,H,L registers and on (HL), (IX+d) and (IY+d).

11.1 Bit test e.g BIT 2, B

The bit test instruction sets the zero flag to the opposite of the named bit. Any bit may be tested.

11.2 Bit set e.g SET 5, D

Any bit may be set.

11.3 Bit reset e.g RES 7, H

Any bit may be reset.

11.4 Rotate left e.g RL B

Bit 7 is copied to the carry, the carry is copied to bit 0 and all other bits are copied one place to the left.

11.5 Rotate right e.g RR D

Bit 0 is copied to the carry, the carry is copied to bit 7 and all other bits are copied one place to the right.

11.6 Rotate left circular e.g RLC E

Bit 7 is copied to the carry and to bit 0. All other bits are copied one place to the left.

11.7 Rotate right circular e.g RRC A

Bit 0 is copied to the carry and to bit 7. All other bits are copied one place to the right.

11.8 Shift left arithmetic e.g SLA (HL)

All bits are copied one place to the left, bit 7 is copied to the carry and bit 0 is reset.

11.9 Shift right arithmetic e.g SRA A

All bits are copied one place to the right, bit 0 is copied to the carry and bit 7 is copied to itself.

11.10 Shift right logical e.g SRL (IX+d)

As shift right arithmetic but with bit 7 reset.

11.11 Rotate left digit and rotate right digit e.g RLD

In a rotate left bits 0 to 3 of A are copied to bits 0 to 3 of (HL); bits 0 to 3 of (HL) are copied to bits 4 to 7 of (HL); bits 4 to 7 of (HL) are copied to bits 0 to 3 of A.

12 Accumulator operations

12.1 Complement accumulator CPL

Every set bit is reset, every reset bit is set.

12.2 Negate accumulator NEG

Complement and add one.

12.3 Complement or set carry CCF or SCF

CCF complements the carry; SCF sets the carry.

12.4 Decimal adjust DAA

Corrects the results of BCD addition and subtraction.

13 Restart

e.g RST 20

Save the programme counter on the stack and jump to location 8*nH where nH is the number in hexa decimal.

14 Block handling

e.g LDI

LDI: Move one byte from (DE) to (HL) and decrement BC. LDIR: As LDI but repeat until BC = 0.

LDD: Move one byte from (DE) to (HL) and decrement BC, DE and HL. LDDR: As LDD but repeat until BC=0.

CPI: Compare A and (HL), increment HL and decrement BC. CPIR: As CPI but repeat until BC = 0. CPD: As CPI but decrement HL. CPDR: As CPD but repeat until BC = 0.

15 Flip – flop

e.g DI

DI: reset interrupt flip – flop.

EI: set interrupt flip – flop.

DEC  HEX   OP CODE

0    00    NOP
1    01nn  LD BC,nn
2    02    LD (BC),A
3    03    INC BC
4    04    INC B
5    05    DEC B
6    06n   LD B,n
7    07    RLCA
8    08    EX AF,AF'
9    09    ADD HL,BC
10   0A    LD A,(BC)
11   0B    DEC BC
12   0C    INC C
13   0D    DEC C
14   0En   LD C,n
15   0F    RRCA
16   10d   DJNZ d
17   11nn  LD DE,nn
18   12    LD (DE),A
19   13    INC DE
20   14    INC D
21   15    DEC D
22   16n   LD D,n
23   17    RLA
24   18d   JR d
25   19    ADD HL,DE
26   1A    LD A,(DE)
27   1B    DEC DE
28   1C    INC E
29   1D    DEC E
30   1En   LD E,n
31   1F    RRA
32   20d   JR NZ,d
33   21nn  LD HL,nn
34   22nn  LD (nn),HL
35   23    INC HL
36   24    INC H
37   25    DEC H
38   26n   LD H,n
39   27    DAA
40   28d   JR Z,d
41   29    ADD HL,HL
42   2Ann  LD HL,(nn)
43   2B    DEC HL
44   2C    INC L
45   2D    DEC L
46   2En   LD L,n
47   2F    CPL
48   30d   JR NC,d
49   31nn  LD SP,nn
50   32nn  LD (nn),A
51   33    INC SP
52   34    INC (HL)
53   35    DEC (HL)
54   36n   LD (HL),n
55   37    SCF
56   38d   JR C,d
57   39    ADD HL,SP
58   3Ann  LD A,(nn)
59   3B    DEC SP
60   3C    INC A
61   3D    DEC A
62   3En   LD A,n
63   3F    CCF
64   40    LD B,B
65   41    LD B,C
66   42    LD B,D
67   43    LD B,E
68   44    LD B,H
69   45    LD B,L
70   46    LD B,(HL)
71   47    LD B,A
72   48    LD C,B
80   50    LD D,B
88   58    LD E,B
96   60    LD H,B
104  68    LD L,B
112  70    LD (HL),B
120  78    LD A,B
128  80    ADD A,B
136  88    ADC A,B
144  90    SUB B
152  98    SBC A,B
160  A0    AND B
168  A8    XOR B
176  B0    OR B
184  B8    CP B
192  C0    RET NZ
193  C1    POP BC
194  C2nn  JP NZ,nn
195  C3nn  JP nn
196  C4nn  CALL NZ,nn
197  C5    PUSH BC
198  C6n   ADD A,n
199  C7    RST 0H
200  C8    RET Z
201  C9    RET
202  CAnn  JP Z,nn
203  CB    see below
204  CCnn  CALL Z,nn
205  CDnn  CALL nn
206  CEn   ADC A,n
207  CF    RST 8H
208  D0    RET NC
209  D1    POP DE
210  D2nn  JP NC,nn
211  D3    OUT A
212  D4nn  CALL NC,nn
213  D5    PUSH DE
214  D6n   SUB n
215  D7    RST 10H
216  D8    RET C
217  D9    EXX
218  DAnn  JP C,nn
219  DB    IN A
220  DCnn  CALL C,nn
221  DD    see below
222  DEn   SBC A,n
223  DF    RST 18H
224  E0    RET PO
225  E1    POP HL
226  E2nn  JP PO,nn
227  E3    EX (SP),HL
228  E4nn  CALL PO,nn
229  E5    PUSH HL
230  E6n   AND n
231  E7    RST 20H
232  E8    RET PE
233  E9    JP (HL)
234  EAnn  JP PE,nn
235  EB    EX DE,HL
236  ECnn  CALL PE,nn
237  ED    see below
238  EEn   XOR n
239  EF    RST 28H
240  F0    RET P
241  F1    POP AF
242  F2nn  JP P,nn
243  F3    DI
244  F4nn  CALL P,nn
245  F5    PUSH AF
246  F6n   OR n
247  F7    RST 30H
248  F8    RET M
249  F9    LD SP,HL
250  FAnn  JP M,nn
251  FB    EI
252  FCnn  CALL M,nn
253  FD    see below
254  FEn   CP n
255  FF    RST 38H

Two byte instructions

Each of the following
codes is preceded by
CB (decimal 203)

DEC  HEX   OP CODE

0    00    RLC B
1    01    RLC C
2    02    RLC D
3    03    RLC E
4    04    RLC H
5    05    RLC L
6    06    RLC (HL)
7    07    RLC A
8    08    RRC B
16   10    RL B
24   18    RR B
32   20    SLA B
40   28    SRA B
56   38    SRL B
64   40    BIT 0,B
72   48    BIT 1,B
80   50    BIT 2,B
88   58    BIT 3,B
96   60    BIT 4,B
104  68    BIT 5,B
112  70    BIT 6,B
120  78    BIT 7,B
128  80    RES 0,B
136  88    RES 1,B
144  90    RES 2,B
152  98    RES 3,B
160  A0    RES 4,B
168  A8    RES 5,B
176  B0    RES 6,B
184  B8    RES 7,B
192  C0    SET 0,B
200  C8    SET 1,B
208  D0    SET 2,B
216  D8    SET 3,B
224  E0    SET 4,B
232  E8    SET 5,B
240  F0    SET 6,B
248  F8    SET 7,B

Op codes 40 to BF (decimal 64 to 191) and CB 00
to CB FF (decimal 203 0 to 203 255) fall naturally
into groups of eight. Successive Op codes in each
group use the next in the sequence B,C,D,E,H,L,
(HL),A as the source or operand as appropriate.
For example Op codes of the form n3 and nB
where n=4,5,6,7,A or B all refer to E.

Op codes 40 to 47 and CB 00 to CB 07 are listed
in their entirety but subsequently the first of
each group only is shown.

Each of the following
codes is preceded by
ED (decimal 237).

DEC  HEX   OP CODE

66   42    SBC HL,BC
67   43nn  LD (nn),BC
68   44    NEG
69   45    RETN
71   47    LD I,A
74   4A    ADC HL,BC
75   4Bnn  LD BC,(nn)
77   4D    RETI
79   4F    LD R,A
82   52    SBC HL,DE
83   53nn  LD (nn), DE
87   57    LD A,I
90   5A    ADC HL,DE
91   5Bnn  LD DE,(nn)
95   5F    LD A,R
98   62    SBC HL,HL
103  67    RRD
106  6A    ADC HL,HL
111  6F    RLD
114  72    SBC HL,SP
115  73nn  LD (nn),SP
122  7A    ADC HL,SP
123  7Bnn  LD SP,(nn)
160  A0    LDI
161  A1    CPI
168  A8    LDD
169  A9    CPD
176  B0    LDIR
177  B1    CPIR
184  B8    LDDR
185  B9    CPDR

Each of the following
codes is preceded by
DD (decimal 221) when
referring to the IX
register or FD (decimal
253) when referring to
the IY register. For
clarity IX only is used
below.

DEC  HEX   OP CODE

9    09    ADD IX,BC
25   19    ADD IX,DE
33   21nn  LD IX,nn
34   22nn  LD (nn),IX
35   23    INC IX
41   29    ADD IX,IX
42   2Ann  LD IX,(nn)
43   2B    DEC IX
52   34d   INC (IX+d)
53   35d   DEC (IX+d)
54   36dn  LD (IX+d),n
57   39    ADD IX,SP
70   46d   LD B,(IX+d)
78   4Ed   LD C,(IX+d)
86   56d   LD D,(IX+d)
94   5Ed   LD E,(IX+d)
102  66d   LD H,(IX+d)
110  6Ed   LD L,(IX+d)
112  70d   LD (IX+d),B
113  71d   LD (IX+d),C
114  72d   LD (IX+d),D
115  73d   LD (IX+d),E
116  74d   LD (IX+d),H
117  75d   LD (IX+d),L
119  77d   LD (IX+d),A
126  7Ed   LD A,(IX+d)
134  86d   ADD A,IX+d)
142  8Ed   ADC A,(IX+d)
150  96d   SUB (IX+d)
158  9Ed   SBC A,(IX+d)
166  A6d   AND (IX+d)
174  AEd   XOR (IX+d)
182  B6d   OR (IX+d)
190  BEd   CP (IX+d)

225  E1    POP IX
227  E3    EX (SP),IX
229  E5    PUSH IX
233  E9    JP (IX)
249  F9    LD SP,IX

Three byte instructions

Each of the following
codes is preceded by
DD CB (decimal 221
203) when referring
to the IX register or
FD CB (decimal 253
203) when referring
to the IY register.
For clarity IX only is
used below.

DEC  HEX   OP CODE

6    d06   RLC (IX+d)
14   d0E   RRC (IX+d)
22   d16   RL (IX+d)
30   d1E   RR (IX+d)
38   d26   SLA (IX+d)
46   d2E   SRA (IX+d)
62   d3E   SRLR (IX+d)
70   d46   BIT 0,(IX+d)
78   d4E   BIT 1,(IX+d)
86   d56   BIT 2,(IX+d)
94   d5E   BIT 3,(IX+d)
102  d66   BIT 4,(IX+d)
110  d6E   BIT 5,(IX+d)
118  d76   BIT 6,(IX+d)
126  d7E   BIT 7,(IX+d)
134  d86   RES 0,(IX+d)
142  d8E   RES 1,(IX+d)
150  d96   RES 2,(IX+d)
158  d9E   RES 3,(IX+d)
166  dA6   RES 4,(IX+d)
174  dAE   RES 5,(IX+d)
182  dB6   RES 6,(IX+d)
190  dBE   RES 7,(IX+d)
198  dC6   SET 0,(IX+d)
206  dCE   SET 1,(IX+d)
214  dD6   SET 2,(IX+d)
222  dDE   SET 3,(IX+d)
230  dE6   SET 4,(IX+d)
238  dEE   SET 5,(IX+d)
246  dF6   SET 6,(IX+d)
254  dFE   SET 7,(IX+d)