Opcodes are the actual instructions that will get executed by the Z80 CPU in the Master System or Game Gear. They are carefully structured sets of values for bytes, which makes them easier for the CPU to understand but horrible for humans to deal with.

Thus, we use assembler programs that understand mnemonics. These are small “words” that represent what an opcode does. For the Z80, they are generally (often abbreviated) English words representing what the instruction does, or an initialisation of a phrase describing it. This makes the source code more readable. Here’s an example:

ld bc, $4000    ; Load register pair BC with number $4000
out ($be),a     ; Output to port number $be the number stored in register A
dec bc          ; Decrement register pair BC
ld a,b          ; Load register A with the number stored in register B
or c            ; Perform a logical OR operation between registers A and C
jp nz,SomeLabel ; Jump, if the result is non-zero, to the address of SomeLabel

Once you become familiar with the mnemonics, it is not necessary to add such comments all over the code.

There are more than 1000 possible unique opcodes, but they can be split into 158 “instruction types”, where the operation is really the same but the registers are different. Of these, we will mainly be using a few dozen, so it is not so overwhelming. The instructions cover the groups:

  • 8-bit arithmetic and logic operations
    Adding, subtracting, AND, OR, XOR type operations
  • 16-bit arithmetic
    Similar to the 8-bit ones
  • 8-bit and 16-bit load
    Moving data around
  • Bit set, reset, and test
    Checking and manipulating the individual 1s and 0s
  • Call, return, and restart
    For functions
  • Exchange, block transfer, and search
    These simplify operations that work on lots of data
  • General purpose arithmetic and CPU control
    These are helpers for manipulating data and controlling how the CPU works
  • Input and output
    The CPU can also communicate with other devices in the system through these
  • Jump
    Let us execute different bits of code under different conditions
  • Rotate and shift
    Can be useful for calculations, but quite confusing at first

The Z80 User Manual groups the instructions in this way.

I will be describing each instruction as it appears in this tutorial. You don’t need to memorise anything :) just let them sink in naturally, and look up the ones you forget.


< Labels | Lesson 1 | Memory >