DYSTOPIANPIXEL

Z80/Z80N programme structure, decisions and the stack

Build on your first Odin programme with comparisons, branches, loops, the stack and reusable routines.

Next Asset Lab concept artwork showing sprite patterns, palette colours and animation frames
Next Asset Lab concept artwork · Begin with Odin Dev.2k and the foundations of Z80 assembly.

Complete Tutorial 1: Odin Dev.2k and Z80 foundations before starting this lesson.

Overview

A complete beginner’s second lesson. We are not building the editor interface yet. We are learning how a real assembly programme decides what to do next, repeats work, enters a subroutine, protects its registers and returns safely.

About this tutorial

In Tutorial 1, we discovered the Odin Dev.2k environment on the real ZX Spectrum Next, learned what bytes and hexadecimal numbers mean, and entered a small programme whose machine-code bytes began at $6000. We learned to inspect its registers and flags rather than simply assuming it worked. We also learned that RET returns control to Odin by using a saved address.

Now we can ask a more interesting question: how does a programme perform several related jobs in the correct order? Next Asset Lab will eventually need to respond to input, select tools, choose resources and call reusable routines. Even before we write its screen renderer or editor, we need to understand how those pieces will connect.

We will begin with signed and unsigned values, then learn comparisons and branches, loops, the stack, subroutines and a simple routine contract. Every working example is deliberately short, has an explicit expected result, and ends in a way intended to return control to Odin. The final example is a small reusable helper that demonstrates the beginnings of a disciplined Next Asset Lab codebase.

Learning objectives. By the end, you should be able to explain the difference between signed and unsigned interpretations of a byte; distinguish Carry, Zero and Overflow; use CP without losing the value in A; follow a conditional JR; use DJNZ for a counted loop; describe why CALL and RET need the stack; match a PUSH with a POP; identify which registers a routine may change; and test and export a structured source file from Odin.

The boundary of this chapter. We are using ordinary Z80 instructions which also run on the Spectrum Next’s Z80N processor. We are not yet teaching Z80N-specific instructions, NextRegs, Turbo mode, MMU paging, NextZXOS services, interrupts or NEX packaging. Those belong to later parts of the frozen implementation roadmap. Nor will we overwrite the machine’s stack pointer, touch hardware control registers, or turn a teaching example into a production application prematurely.

Explore the sections

NEXT ASSET LAB / PART 01

Reconnect with Tutorial 1

Review the first programme and Odin workflow.

Read section
NEXT ASSET LAB / PART 02

Signed and unsigned bytes

See how the same bits can represent positive or negative values.

Read section
NEXT ASSET LAB / PART 03

Flags and operations

Distinguish Carry, Zero and Overflow and learn when flags change.

Read section
NEXT ASSET LAB / PART 04

Comparing with CP

Compare values without changing A.

Read section
NEXT ASSET LAB / PART 05

Branches and jumps

Follow the programme counter and explore relative and absolute jumps.

Read section
NEXT ASSET LAB / PART 06

Your first decision

Type and test both paths of a conditional decision.

Read section
NEXT ASSET LAB / PART 07

Unsigned ranges

Use Carry to test whether an unsigned value is below a limit.

Read section
NEXT ASSET LAB / PART 08

Counted loops

Repeat work with DJNZ and understand the zero-counter case.

Read section
NEXT ASSET LAB / PART 09

Labels and structure

Organise entry points and reusable routines with clear names.

Read section
NEXT ASSET LAB / PART 10

Understanding the stack

Learn last-in, first-out storage and stack balance.

Read section
NEXT ASSET LAB / PART 11

CALL and RET

Trace a call, its saved return address and the return to Odin.

Read section
NEXT ASSET LAB / PART 12

PUSH and POP

Protect register pairs and restore them in reverse order.

Read section
NEXT ASSET LAB / PART 13

Routine contracts

Document inputs, outputs and register changes, then repair a helper contract.

Read section
NEXT ASSET LAB / PART 14

A reusable clamp helper

Build and trace a helper that clamps an unsigned byte to 0–15.

Read section
NEXT ASSET LAB / PART 15

Testing and downloads

Download the examples and record ordinary and boundary-case results.

Read section
NEXT ASSET LAB / PART 16

Debugging safely

Recognise common mistakes and use the instruction reference.

Read section
NEXT ASSET LAB / PART 17

Practice exercises

Predict results and compare your reasoning with worked answers.

Read section
NEXT ASSET LAB / PART 18

Next steps and checklist

Connect the lesson to Next Asset Lab and prepare for Tutorial 3.

Read section