Jump (JP)
Introduction
Previously you learned how to load values, print letters, and do basic addition. Now it’s time to take more control of how your program executes. This lesson introduces a new concept: Flow Control, specifically, how to jump around inside your program using the JP instruction
What Does JP Do?
JP stands for Jump.
The Z80 normally reads instructions one after another in sequence. The JP instruction interrupts that flow and tells the CPU:
“Stop reading the next instruction, and instead jump to a specific address and continue from there.”
This gives you control to:
- Skip parts of your program
- Repeat instructions (loops)
- Jump to common routines (e.g., print blocks, subroutines)
In future lessons, you’ll see conditional jumps, where the jump only happens if certain conditions are true
Example Program: Using JP to Skip Instructions
ORG 256
LD A, 83 ; Load ASCII 'S'
JP 262 ; Skip the RET
RET ; This line is skipped
RST 8
DEFB 158
RET
Walkthrough
- LD A, 83 loads ASCII 'S' into register A.
- JP 262 jumps ahead to address 262.
- The RET at 261 is skipped.
- At 262, RST 8 and DEFB 158 print 'S'.
- The final RET ends the program
How the Program Assembles in Memory
If the program started at 256, why does it jump to 258 after just one instruction? It’s because the address is counting individual bytes. When you assemble your program, each instruction is translated into one or more bytes of machine code. These bytes are then placed sequentially in memory, starting from the program’s load address. The first instruction ‘LD A, 83’ is two object codes, two bytes, so the address increases by two. Here’s how your example looks once assembled:
Assembly Breakdown
- Line 20
Source: LD A, 83
Memory (hex): 0100
Address (decimal): 256
Object Code: 3E 53
- Line 30
Source: JP 262
Memory (hex): 0102
Address (decimal): 258
Object Code: C3 06 01
- Line 40
Source: RET
Memory (hex): 0105
Address (decimal): 261
Object Code: C9
- Line 50
Source: RST 80
Memory (hex): 0106
Address (decimal): 262
Object Code: CF
- Line 60
Source: DEFB 158
Memory (hex): 0107
Address (decimal): 263
Object Code: 9E
- Line 70
Source: RET
Memory (hex): 0108
Address (decimal): 264
Object Code: C9
Object Codes
When you write an assembly program, each instruction is translated into object code — the actual machine instructions that the processor runs. These are written as hexadecimal values, which is how we humans usually represent machine code bytes.
Here’s what that looks like for your example program:
- LD A, 83
Object Code: 3E 53
Explanation: 3E is the instruction for load the accumulator (A) with a value. The next byte 53 is the value itself — in this case, 83 in decimal, which is the ASCII code for the character S.
- JP 262
Object Code: C3 06 01
Explanation: C3 means jump. The next two bytes represent the address to jump to (0106) in little-endian format — meaning the low byte (06) comes first, followed by the high byte (01).
- RET
Object Code: C9
Explanation: C9 tells the processor to return from the current subroutine or program.
- RST 8
Object Code: CF
Explanation: CF calls the routine dispatcher — a built-in function at a fixed address in the system’s ROM.
- DEFB 158
Object Code: 9E
Explanation: 9E is a data byte that selects the print character routine, which will output the character currently stored in register A.
Why This Matters
Understanding how assembly instructions translate into object code is an important skill. It helps you:
- Debug programs more effectively — especially if you’re looking at raw memory dumps or tracing execution.
- Read .lst files — assembler listing files that show the original source code side-by-side with the generated machine code.
- Understand the hardware — you’ll see exactly how your instructions map to bytes the processor executes.
The Program Counter (PC)
The Program Counter (PC) is a register that stores the address of the current instruction.
- Normally, PC auto-increments after each instruction.
- With JP, you manually set the PC to a new location.
So:
JP 262 means “Set PC to 262 and continue from there.”
Finding the Right Address for JP
There are two methods for find the specific address to jump to:
Option 1: Manual Calculation
You would calculate the quantity of object codes / bytes manually
If ORG 256:
- LD A, 83 → 256–257
- JP → 258–260
- RET → 261
- Next instruction starts at 262
Option 2: Use z80asm Listing File
You could you the z80asm command line tool output the .com program along with all the memory address locations.
z80asm -l -o program.com program.asm > program.lst
Output shows addresses like 0106 (hex) = 262 (decimal). This is the most reliable method for larger programs
Don’t Worry
In reality, when wiring assembly code, you’ll be using concepts like Labels, Subroutines, and Jump Relative. So you probably won’t need to manually calculate specific memory address to use with the JP instruction.
Exercises
Exercise 2.1 – Jump To It
Write a program that:
- Loads a value into A.
- Uses JP to skip a RET.
- Prints the value with RST 8 and DEFB 158.
- Ends with RET.
Then try:
- Removing the JP — does the program stop too early?
- Changing the destination — does printing still work?
- Adding multiple JPs — can you make it loop endlessly?
- Module2 - Jump
Summary
- JP lets you jump to a memory address.
- This changes program flow by resetting the Program Counter.
- Instructions compile into multi-byte object code, so addresses can appear to “skip.”
- Use assembler listing files to find exact addresses.
- Later you’ll replace numbers with labels for readability