CPU Instruction Set

If you are looking for textual explanations of what each each instruction does, please read gbz80(7); if you want a compact reference card/cheat sheet of each opcode and its flag effects, please consult the optables (whose octal view makes most encoding patterns more apparent).

The Game Boy’s SM83 processor possesses a CISC , variable-length instruction set. This page attempts to shed some light on how the CPU decodes the raw bytes fed into it into instructions.

The first byte of each instruction is typically called the “opcode” (for “operation code”). By noticing that some instructions perform identical operations but with different parameters, they can be grouped together; for example, inc bc , inc de , inc hl , and inc sp differ only in what 16-bit register they modify.

In each table, one line represents one such grouping. Since many groupings have some variation, the variation has to be encoded in the instruction; for example, the above four instructions will be collectively referred to as inc r16 . Here are the possible placeholders and their values:

01234567
r8 b c d e h l [hl] a
r16 bc de hl sp
r16stk bc de hl af
r16mem bc de hl+ hl-
cond nz z nc c
b3A 3-bit bit index
tgt3 rst 's target address, divided by 8
imm8The following byte
imm16The following two bytes, in little-endian order

These last two are a little special: if they are present in the instruction’s mnemonic, it means that the instruction is 1 ( imm8 ) / 2 ( imm16 ) extra bytes long.

[hl+] and [hl-] can also be notated [hli] and [hld] respectively (as in increment and decrement).

Groupings have been loosely associated based on what they do into separate tables; those have no particular ordering, and are purely for readability and convenience. Finally, the instruction “families” have been further grouped into four “blocks”, differentiated by the first two bits of the opcode.

Block 0

76543210
nop 00000000
76543210
ld r16, imm16 00Dest (r16)0001
ld [r16mem], a 00Dest (r16mem)0010
ld a, [r16mem] 00Source (r16mem)1010
ld [imm16], sp 00001000
76543210
inc r16 00Operand (r16)0011
dec r16 00Operand (r16)1011
add hl, r16 00Operand (r16)1001
76543210
inc r8 00Operand (r8)100
dec r8 00Operand (r8)101
76543210
ld r8, imm8 00Dest (r8)110
76543210
rlca 00000111
rrca 00001111
rla 00010111
rra 00011111
daa 00100111
cpl 00101111
scf 00110111
ccf 00111111
76543210
jr imm8 00011000
jr cond, imm8 001Condition (cond)000
76543210
stop 00010000

stop is often considered a two-byte instruction, though the second byte is not always ignored.

Block 1: 8-bit register-to-register loads

76543210
ld r8, r8 01Dest (r8)Source (r8)

Exception: trying to encode ld [hl], [hl] instead yields the halt instruction:

76543210
halt 01110110

Block 2: 8-bit arithmetic

76543210
add a, r8 10000Operand (r8)
adc a, r8 10001Operand (r8)
sub a, r8 10010Operand (r8)
sbc a, r8 10011Operand (r8)
and a, r8 10100Operand (r8)
xor a, r8 10101Operand (r8)
or a, r8 10110Operand (r8)
cp a, r8 10111Operand (r8)

Block 3

76543210
add a, imm8 11000110
adc a, imm8 11001110
sub a, imm8 11010110
sbc a, imm8 11011110
and a, imm8 11100110
xor a, imm8 11101110
or a, imm8 11110110
cp a, imm8 11111110
76543210
ret cond 110Condition (cond)000
ret 11001001
reti 11011001
jp cond, imm16 110Condition (cond)010
jp imm16 11000011
jp hl 11101001
call cond, imm16 110Condition (cond)100
call imm16 11001101
rst tgt3 11Target (tgt3)111
76543210
pop r16stk 11Register (r16stk)0001
push r16stk 11Register (r16stk)0101
76543210
Prefix (see block below)11001011
76543210
ldh [c], a 11100010
ldh [imm8], a 11100000
ld [imm16], a 11101010
ldh a, [c] 11110010
ldh a, [imm8] 11110000
ld a, [imm16] 11111010
76543210
add sp, imm8 11101000
ld hl, sp + imm8 11111000
ld sp, hl 11111001
76543210
di 11110011
ei 11111011

The following opcodes are invalid, and hard-lock the CPU until the console is powered off: $D3, $DB, $DD, $E3, $E4, $EB, $EC, $ED, $F4, $FC, and $FD.

$CB prefix instructions

76543210
rlc r8 00000Operand (r8)
rrc r8 00001Operand (r8)
rl r8 00010Operand (r8)
rr r8 00011Operand (r8)
sla r8 00100Operand (r8)
sra r8 00101Operand (r8)
swap r8 00110Operand (r8)
srl r8 00111Operand (r8)
76543210
bit b3, r8 01Bit index (b3)Operand (r8)
res b3, r8 10Bit index (b3)Operand (r8)
set b3, r8 11Bit index (b3)Operand (r8)