Skip to content
BetterDL

Opcode

Also called: operation code, opcode field

The field of an instruction that says which operation to perform. A field of n bits can name up to 2ⁿ different operations.

The opcode (operation code) is the part of an instruction that names the operation: load, add, jump and so on. The control unit decodes it to decide which steps and control signals the instruction needs.

In Tiny8 the opcode is the top 4 bits, IR[7:4], which is the left hex digit:

  • 0 HALT, 1 LOAD, 2 STORE, 3 ADD, 4 SUB, 5 JMP, 6 JZ;
  • 7 to F are unused.

A 4-to-16 decoder turns the opcode into one line per instruction.

Sizing rule: with n opcode bits there are 2ⁿ possible codes. To support k instructions you need the smallest n with 2ⁿ ≥ k, rounding up. Tiny8's 7 instructions would fit in 3 bits; it uses 4, which leaves room for nine more.

Unused opcodes are useful when designing the decode logic: they never occur, so they're don't-cares. But if you later give one a meaning, any logic that relied on it as a don't-care must be rechecked.

0
128
1
64
0
32
1
16
1
8
1
4
0
2
0
1

Worked examples

Example

Decoding an opcode

The IR holds 0101 1100 (shown above).

  1. 1.

    Opcode = 0101 = 5 = JMP.

  2. 2.

    Address field = 1100 = C.

  3. 3.

    So it's JMP C: the next instruction is fetched from address C, always.

Example

How many opcode bits?

A new machine needs 20 instructions.

  1. 1.

    2⁴ = 16 is too few.

  2. 2.

    2⁵ = 32 is enough.

  3. 3.

    So 5 opcode bits, with 12 codes spare.

Common mistakes

  • Rounding down: 20 instructions need 5 bits, not 4.

  • Reading the opcode from the right-hand digit. In Tiny8 it's the left digit.

Practice Opcode

Interactive questions with instant feedback and a worked solution for every wrong answer.

Learn it step by step

Opcode is taught in Basic CPU / Computer Architecture.