DYSTOPIANPIXEL

Exercises and next steps

Work through exercises, answers and troubleshooting, then check that you are ready to continue.

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.
TUTORIAL 1 / PART 16

Exercises before going further

Do these slowly. The purpose is to become comfortable with the notation, not to race through them.

  • Convert decimal 10, 15, 16, 42, 64, 127, 128 and 255 to hexadecimal.
  • Convert $0A, $0F, $10, $2A, $40, $7F, $80 and $FF to decimal.
  • Write the binary form of $00, $01, $0F, $10, $42 and $FF.
  • For the 16-bit value $1234, identify the high byte and the low byte.
  • Explain in your own words why one hexadecimal digit maps neatly to one nibble.
  • Explain why an 8-bit register cannot contain the unsigned value 256.
  • Explain the difference between an address such as $6000 and a byte stored at that address.
  • Explain the difference between A and F in the AF display.
  • Explain why LD A,$42 did not give us meaningful new arithmetic flags, but ADD A,$01 did.

Exercise answers

Try the exercises before reading these answers. The aim is not to score yourself; it is to identify anything that deserves another example.

Decimal to hexadecimal

10  = $0A
15  = $0F
16  = $10
42  = $2A
64  = $40
127 = $7F
128 = $80
255 = $FF

Hexadecimal to decimal

$0A = 10
$0F = 15
$10 = 16
$2A = 42
$40 = 64
$7F = 127
$80 = 128
$FF = 255

Binary forms

$00 = 00000000
$01 = 00000001
$0F = 00001111
$10 = 00010000
$42 = 01000010
$FF = 11111111

Concept questions

$1234 has high byte $12 and low byte $34.

One hexadecimal digit maps neatly to one nibble because both represent sixteen possible patterns or values: 0 through 15.

An 8-bit register has 256 possible bit patterns, representing unsigned values 0 through 255. Decimal 256 would require a ninth bit.

An address identifies a memory location; the byte stored at that address is the location's contents. They are not the same thing.

A is the accumulator and holds ordinary 8-bit working values. F is the flags register, whose individual bits report conditions produced by certain operations.

LD A,$42 loads a value but does not recalculate the arithmetic flags. ADD performs arithmetic and updates relevant flags to describe the result.

Common beginner confusions

ConfusionClarification
'$42 means forty-two'$42 is hexadecimal. It equals 66 decimal.
'$6000 is a byte'$6000 is a 16-bit value. In our example it is used as an address.
'AF is one ordinary 16-bit number'Odin displays A and F together, but they have different roles. A is the accumulator; F contains flag bits.
'The assembler executes the code'The assembler translates source into machine code. The Z80 CPU executes the machine code.
'The label start is stored as text for the CPU'The label is resolved by the assembler to an address. The CPU ultimately works with numerical addresses and opcodes.
'Hexadecimal is a different kind of value'Hex, decimal and binary are simply different notations for the same underlying number.
'00 60 means $0060'As two sequential bytes, yes; but in a little-endian 16-bit operand they encode the value $6000 because the low byte is stored first.

If something goes wrong

The first tutorial should teach recovery as well as success. Most early problems are small and local.

SymptomWhat to check
NEW, A, S or R appears not to workCheck that you are inside the Odin Monitor, not at the NextZXOS / NextBASIC prompt.
.odin appears not to work inside Odin.odin belongs at the NextZXOS / NextBASIC prompt; it launches Odin.
LS does not show your new sourceUse S to save the native document to storage, then check again with LS.
A reports an errorRead the diagnostic, return to the Editor, check spelling, register names, labels and numeric syntax, then assemble again.
R behaves unexpectedlyConfirm the most recent A ended successfully, then compare the source with the known-good listing.
T does not show startCheck that the label is spelled correctly and that the current source was assembled.
M $6000 shows different bytesConfirm the latest source assembled successfully and that start still resolves to $6000.
TL loads unexpected sourceCheck that you imported the intended text file.
L and TL show different sourceOne file may be newer. L loads .odn; TL imports the ordinary text file.
The machine becomes unresponsiveReboot if necessary, reload the last saved source, and return to the last known-good program.

A simple recovery routine

1. Stop adding new code.
2. Identify which environment you are in.
3. Save if Odin is still responsive.
4. Reassemble and read the first error carefully.
5. Fix one thing at a time.
6. Return to the last known-good source if necessary.
7. Only run again after ASSEMBLED OK.

Making one small correction at a time is much easier to debug than changing several things at once.

More beginner questions worth answering explicitly

QuestionAnswer
Is hexadecimal stored differently from decimal?No. Hexadecimal and decimal are different ways of writing the same underlying numeric value.
Does $FF mean the computer stores the characters '$', 'F', 'F'?Not when it is used as a numeric constant in assembly source. Odin interprets it as the value 255 and emits whatever bytes the instruction requires.
Does a label take space in the finished machine code?Normally the label name itself is for the assembler. It resolves to an address; the text of the label is not executed by the CPU.
Is RAM the same as the SD card?No. The SD card is persistent storage. RAM is working memory used while programs run.
If assembly succeeds, is the program definitely correct?No. Assembly proves the source can be translated. Logical errors are still possible.
Why use comments if the CPU ignores them?Comments explain intent to humans, including your future self. They make low-level code maintainable.
Do I need to memorise opcodes such as $3E?No. Odin translates mnemonics into opcodes. We inspect bytes to understand what happened, not to replace the assembler.
Do I need to memorise every register use?No. Learn the common patterns gradually and refer back to the register table.

You are ready to finish Tutorial 1 if…

Do not interpret this as a test that must be passed perfectly. It is a self-check. You are ready to move on when you can explain most of these ideas in plain language:

  • Why $42 is hexadecimal 66 rather than decimal 42.
  • Why one byte is usually displayed as two hexadecimal digits.
  • Why $6000 is a 16-bit value and can be held in HL.
  • The difference between an address and the byte stored at that address.
  • What it means to say that HL is being used as a pointer.
  • What A and F mean when Odin displays AF.
  • What the assembler does and what the CPU does.
  • Why LD A,$42 becomes the machine-code bytes 3E 42.
  • Why the next instruction does not necessarily begin one byte later.
  • Why RET is needed in our tiny Odin-run routine.
  • Why saving, assembling and running are three different actions.
  • The difference between tut01.odn and tut01.asm.

Where Tutorial 1 stops

Tutorial 1 stops after the reader can create and preserve source, assemble a tiny routine, inspect its symbols and bytes, run it safely in Odin, read basic register output, and understand the number and memory concepts behind what is displayed.

We do not need to become fluent in the entire Z80 instruction set before finishing this chapter. Tutorial 2 can build on this foundation with registers, flags, the stack and program flow in greater depth.

Why this tiny program matters to the real project

The program is deliberately tiny, but the workflow is the same one we will use for real Next Asset Lab modules:

  1. edit a small module
  2. save it
  3. assemble it
  4. read and fix diagnostics
  5. inspect symbols / memory / registers
  6. run a controlled test
  7. export durable text source

Later modules will be larger and more hardware-specific, but the development discipline remains the same.

Integration into Next Asset Lab

This chapter establishes the development habits that the real application will use: verify the actual Odin build, work in small testable steps, inspect what the assembler produced, and keep a plain-text source representation alongside the native Odin source.

No architecture change is required. This is a teaching-order correction: the foundations are now introduced before the tutorial relies on them.

Complete source listing at this stage

start:
    ld a,$ff
    add a,$01
    ld hl,$6000
    ret

This source is still a teaching probe rather than a production subsystem. Its purpose is to make the accumulator, arithmetic, flags, addresses and machine-code bytes visible in Odin.

Mini-glossary for this chapter

TermMeaning in this chapter
bitOne binary digit: 0 or 1.
nibbleFour bits; exactly one hexadecimal digit.
byteEight bits; two hexadecimal digits.
16-bit valueTwo bytes; four hexadecimal digits.
addressA number identifying a location in memory.
contentsThe value stored at a memory location or in a register.
pointerA value being used as an address to locate data or code.
registerA small storage location inside the CPU.
accumulatorThe A register, commonly used by Z80 arithmetic and logic instructions.
flagA one-bit status indicator inside F.
mnemonicA readable instruction name such as LD, ADD or RET.
operandThe register, value or location an instruction works with.
opcodeThe machine-code value identifying an instruction.
assemblerA program that translates assembly source into machine code.
machine codeThe byte values that the CPU actually fetches and executes.
PCProgram Counter: the address controlling where instruction execution is taking place.
SPStack Pointer: the address of the current top of the stack.

Tutorial 1 completion checklist

Before moving on, you should be able to explain most of these ideas in your own words or know where to find them in this chapter.

  • I can distinguish the NextZXOS / NextBASIC prompt, the Odin Monitor and the Odin Editor.
  • I can launch Odin and identify the Dev.2k environment used by this project.
  • I know that decimal, binary and hexadecimal are different notations for values.
  • I know what a bit, nibble, byte and 16-bit value are.
  • I understand why a byte is naturally shown as two hexadecimal digits.
  • I can identify high and low nibbles and high and low bytes.
  • I can convert simple hexadecimal values to decimal using a written method.
  • I understand the difference between an address, contents and a pointer.
  • I understand that the Z80 sees a 64K address space at one time.
  • I understand that files on storage are not the same as machine-code bytes in RAM.
  • I know that bytes gain meaning from context.
  • I know the beginner roles of A, F, HL, PC and SP.
  • I understand the common uses of the main Z80 registers without treating them as rigid rules.
  • I can identify a label, mnemonic, operand, immediate value and comment.
  • I understand that Odin assembles source and the Z80 executes machine code.
  • I understand that Z80 instructions can occupy different numbers of bytes.
  • I can create, edit and save a native Odin source file.
  • I can assemble it and recognise ASSEMBLED OK.
  • I can use T to inspect a symbol and M to inspect machine-code bytes.
  • I can run the tiny routine with R and recognise the important register results.
  • I understand at a beginner level why RET can return control to Odin.
  • I can export ordinary text with TS and import it with TL.
  • I know the difference between tut01.odn and tut01.asm.
  • I know that an assembly error is useful diagnostic information.

Summary

You now have a foundation for understanding what Odin is showing rather than merely copying commands. The CPU fetches, decodes and executes machine-code bytes from memory. Decimal, binary and hexadecimal are different notations for the same values. Bits form nibbles and bytes; bytes form 16-bit values. Memory locations have addresses, and a pointer is a value used as an address. Registers contain working values inside the CPU. A is the accumulator, F contains individual flag bits, HL can be used as a pointer, PC controls instruction flow, and SP relates to the stack. Odin translates readable mnemonics and operands into the actual bytes that the Z80 executes.

The next stage should build on these foundations carefully: signed versus unsigned interpretation, the role of the main flags, simple comparisons and conditional branching. We should not introduce those topics until the number-system and register concepts in this chapter feel comfortable.