This the multi-page printable view of this section.Click here to print.

Return to the regular view of this page.

Decoding Instructions

How to decode instructions from binary

Table of Contents

This section lists how the instructions are laid out at the bit level.

Normally if you are manually disassembling code you just need to use the list by Opcodes, however this section will be useful if you are implementing a Z80 emulator as you can see how the instruction decoding works including how the undocumented instructions work due to how the bits are organised.

How to use these decoding tables

To decode an opcode, convert it to binary then run through it from left to right, e.g. start at Bit 7 and move towards Bit 0.

As you run through the bits, start on the table from the top-left and go down then right as you find each bit. Bits are ordered with 0 first, then 1 & finally x which indicates that bit can be either 0 or 1.

When you find a match then go with that. If more than one entry matches then go for the one higher in the table as that will have higher precedence.

Z80 Instruction Decode table

To decode an instruction:

  • If the opcode is CB, DD, ED or FD then go to that prefix page and decode the next byte.
  • Using the table below, locate the pattern that matches the opcode. It's usually best to start from the left (bit 7) and go right until you find the opcode.

    An X means a bit that can be either 0 or 1, however check adjacent rows first and always take an entry that has a 0 or 1 as higher precedence to the X.

76543210
00000000 Nop
00001000 EX
0001x000 Flow
000xx111 Rotate
001xx111 Misc
00xx0001 LD Instructions
00xx0011 Arithmetic Instructions
00xx0100 Arithmetic Instructions
00xx0101 Arithmetic Instructions
00xx0110 LD Instructions
00xx1001 Arithmetic Instructions
00xxx000 Flow
00xxx010 LD Instructions
01110110 Halt
01xxxxxx LD Instructions
10xxxxxx Arithmetic Instructions
11000011 Flow
11001011 CB Prefix
11001101 Flow
11011001 EXX
1101x011 I/O
11101101 ED Prefix
1110x011 EX
11111001 LD Instructions
1111x011 Interrupts
11x01001 Flow
11x11101 DD FD Prefix
11xx0x01 Stack Instructions
11xxx000 Flow
11xxx010 Flow
11xxx100 Flow
11xxx110 Arithmetic Instructions
11xxx111 Flow

Notes:

  1. Halt 0x76 is where the invalid LD (HL),(HL) instruction would have been.

1 - Arithmetic Instructions

How to decode arithmetic instructions from binary

Opcodes with bits 7…5 set to 100 are the arithmetic instructions ADD, ADC, SUB and SBC. As are those starting with 7…6 set to 11 but ending with bits 2…0 set to 110. These instructions take an additional numeric operand after the opcode and use that instead of a register as the source.

Those with 7…5 set to 101 are the logic instructions AND, XOR, OR and CP.

Opcode format
76543210
 
Arithmetic with register as source, e.g. ADD A
100AFr
 
Logic with register as source, e.g. OR A
101AFr
 
8 bit number as source, e.g. SUB 5
11XAF110
n
Lookup for A and F bits
765AFInstructionr
10000 ADC r register or
110 = (HL)
1 ADD r
10 SBC r
1 SUB r
100 AND r
1 XOR r
10 OR r
1 CP r
1000 ADC n Always set to 110
1 ADD n
10 SBC n
1 SUB n
100 AND n
1 XOR n
10 OR n
1 CP n
Registers
Registerr
B000
C001
D010
E011
H100
L101
A111

2 - Program Flow Instructions

How to decode program flow instructions from binary
Opcode format
76543210
0001D000
e-2
 
001cc000JR
e-2
 
11000011JP
7nn0
158
 
11001001RET
 
11001101CALL
7nn0
158
 
11101001JP (HL)
 
11ccc000RET
 
11ccc010JP
7nn0
158
 
11ccc100CALL
7nn0
158
 
11b111RST
Conditions
cccccAbbrev Condition Flag
00000NZ Non Zero Z
01001Z Zero
10010NC No Carry C
11011C Carry
 100 PO Parity Odd P/V
101 PE Parity Even
110 P Sign Positive S
111 M Sign Negative
Bits
Valueb
0000
1001
2010
3011
4100
5101
6110
7111
D
InstructionD
DJNZ0
JR1

3 - Increment Decrement Instructions

How to decode increment and decrement instructions from binary
Opcode format
76543210
 
00XXD01116-bit
 
00r10D8-bit
 
(IX+d) or (IY+d)
11Z11101DD or FD prefix
0011010D
d
 
IX or IY
11Z11101DD or FD prefix
0010D011
7nn0
158
Registers
Registerr
B000
C001
D010
E011
H100
L101
A111
XX Register
InstructionXX
BC00
DE01
HL10
A11
D direction
DirectionD
INC0
DEC1
Z
RegisterZ
IX0
IY1

4 - LD Load instructions

How to decode ld instructions from binary
Opcode format
76543210
 
Set 1
000B010
 
Set 4
00B0001
 
Set 2
001B010
7nn0
158
 
Set 3
00b110
n
 
LD r, r'
01rr'
 
LD r, (HL)
01r110
 
LD SP,HL
11111001
Registers
Registerr
B000
C001
D010
E011
H100
L101
A111
Bits
ValuebB
000000
100101
201010
301111
4100
5101
6110
7111
Set 1 store a in memory
43Instruction
00 LD (BC),A
1 LD (DE),A
10 LD A,(BC)
1 LD A,(DE)
Set 2 store in memory
54Instruction
00 LD (nn),HL
1 LD (nn),A
10 LD HL,(nn)
1 LD A,(nn)
Set 3 set to constant n
432Instruction
000 LD B,n
1 LD C,n
10 LD D,n
1 LD E,n
100 LD H,n
1 LD L,n
10 LD (HL),n
1 LD A,n
Set 4 set to constant nnn
54Instruction
00 LD BC,nn
1 LD DE,nn
10 LD HL,nn
1 LD SP,nn

5 - Miscelaneous Instructions

How to decode IO, EX and Interrupt instructions from binary

Only four rotate instructions are defined in the main opcode set, all the rest require the CB prefix.

Opcode format
76543210
 
IO
1101D011
 
EXX
11011001
 
EX
1110W011
 
Interrupts
1111E011
D I/O Direction
InstructionD
Out0
In1
W EX registers
InstructionW
DE_HL0
(SP)_HL1
E Interrupt Enable
InstructionE
DI0
EI1

6 - Rotate Instructions

How to decode rotate instructions from binary

Only four rotate instructions are defined in the main opcode set, all the rest require the CB prefix.

Opcode format
76543210
000FD111
 
00000111RLCA
00010111RLA
00000111RRCA
00011111RRA
F Carry Flag
InstructionF
With Carry0
Without Carry1
D Direction
InstructionD
Left0
Right1

7 - Decoding CB Prefix

How the CB instruction prefix works

Instructions with the CB prefix consist of instructions that manipulate individual bits in a register or memory.

76543210
00000xxx RLC r
00001xxx RRC r
00010xxx RL
00011xxx RR
00100xxx SLA
00101xxx SRA
00110xxx SLL
00111xxx SRL
01xxxxxx BIT b,r
10xxxxxx RES b,r
11xxxxxx SET b,r

Operations with IX and IY registers

The operations here which operator on the (HL) register do also support use with the IX and IY registers with a relative offset. They are identical to the (HL) operation but with a DD or FD prefix.

Only instructions with the lower nibble set to 6 or E are documented. The other opcodes are undocumented.

Undocumented SLL instruction

Op codes CB30CB37 are undocumented; but they do perform a shift left operation, placing a 1 in bit 0 and setting the carry flag to the original bit 7.

8 - Decoding ED Prefix

How the ED instruction prefix works

Instructions with the ED prefix consist of instructions that are not used as often as those in the main group.

76543210
01000100 NEG
01000101 RETN
01001101 RETI
01010011
01011110 IM2
010x0110 IMx
010x0111 LD
010x1000 LD
01100111 RRD (HL)
01101000 LD
01101111 RLD (HL)
01110001
01111001 OUT (C) A
01xx0010 SBC
01xx1010 ADC
01xxx000 IN r (C)
01xxx001 OUT (C) r
01xxx011 LD
101xx00x Block Memory
101xx01x Block IO

9 - Decoding DD and FD Prefixes

How the DD and FD instruction prefixes work

Instructions with either the DD or FD prefixes affect those instructions that operate against the memory addressed by HL, changing them to use either the IX or IY registers with an offset.

Instructions that refer directly to the HL register will then act directly against either IX or IY. For those that refer to (HL), i.e. the memory pointed to by HL then the instructions use an additional relative offset that's added to either the IX or IY registers, and are written as (IX+d) or (IY+d).

Instructions with the DD prefix use the IX register, whilst the FD prefix uses the IY register.

DDCB and FDCB Prefixes

The DD and FD prefixes extends though the CB prefix as it does for normal instructions. Just like the CB prefix

The format of the instruction also changes slightly as they change the behaviour of the existing instructions with the CB prefix. These instructions are all four bytes long with the third byte consisting of the offset.

For example: The RLC (HL) is encoded as CB06.

With the DD prefix this becomes RLC (IX+d) but the instruction is formatted as DDCBdd06. With the FD prefix this becomes RLC (IY+d), formatted as FDCBdd06.

Note that the offset d is before the final part of the operand, not after as you might expect.

Decoder

All of these have either DD or FD as the previous prefix byte and a displacement immediately after them.

76543210
00100001 LD
0010X011 INC DEC
0010xx10 LD
0011010D IncDec
00110110 LD
00xx1001 ADD
0100010x INC DEC
01110110
01110xxx LD
01xxx110 LD
10xx0110 LD
11001011 CB Prefix
11100011 EX
11100x01 Stack Instructions
11101001 Flow
11111001 LD