Skip to content
Stack & Jumps
step 1/5

Reading — step 1 of 5

Read

~1 min readInstructions

Stack & Jumps

Subroutines via CALL/RET:

CALL NNN  (2NNN):
    push pc onto stack
    pc = NNN

RET (00EE):
    pc = pop stack

Implementation:

python

Stack used only for return addresses. No saved registers (caller's responsibility, or all functions run with all 16 globals).

Jumps:

  • 1NNN: pc = NNN.
  • BNNN: pc = V0 + NNN. Computed jump (used by jump tables).

Example: switch statement compiled to:

LD I, table_base
ADD I, VX        # I points to table[VX]
LD V0, [I]       # V0 = table[VX]
LD V1, [I+1]
JP V0, ...       # jump

Or more typical: a jump table laid out as JP target_n instructions consecutively, then JP V0, base to dispatch.

Address space:

  • 12-bit addresses → max 0xFFF (4096).
  • Code typically 0x200-0xFFF (3584 bytes max).
  • A few KB is enough for many CHIP-8 games.

Recursion: with only 16 stack levels, deep recursion crashes. Most CHIP-8 games avoid recursion entirely. Iterative loops via skip + jump.

Common pattern: a "sprite render" function called many times with different (X, Y, sprite) registers.

PC overflow: incrementing past 0xFFF is undefined. Most emulators wrap; some halt.

Halt opcode: not in original CHIP-8. Common informal halt is JP self (1NNN where NNN points to itself, making an infinite loop).

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…