DYSTOPIANPIXEL

Your first source file

Create, edit and save a tiny Odin programme, with each label and instruction explained.

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 09

Your first Odin source file

Before typing: how to read an assembly source line

Assembly source has a small vocabulary of parts. Learning the names helps us discuss code clearly.

start:
    ld a,$42      ; put hexadecimal $42 into A
PartMeaning
start:A label. It gives a name to an address.
ldThe instruction mnemonic — a readable name for a Z80 operation.
aAn operand: here it names the A register.
$42Another operand: an immediate hexadecimal value written directly in the instruction.
; commentA note for the human reader. It is not executed by the CPU.

What does 'immediate' mean?

An immediate value is a literal value written directly in the instruction. In LD A,$42, the value $42 is part of the instruction itself. This is different from loading a value from a memory address, which we will learn later.

Comments are part of good assembly programming

Comments explain intent. Assembly instructions can be very compact, so a short comment often saves time later. Comments do not become machine code. As the Next Asset Lab grows, clear comments will be important, but we will avoid comments that merely repeat the instruction.

Create the source

From the Odin Monitor:

NEW tut01.odn

NEW creates a new Odin source document. We then use the physical EDIT key to switch from the Monitor to the full-screen editor.

Enter the first small program

start:
    ld a,$42
    ld hl,$6000
    ret

Do not rush past these four lines. Each one introduces an idea.

start:

start is a label. A label gives a human-readable name to an address. Labels make assembly source much easier to understand than hard-coding every address.

ld a,$42

This tells the Z80 to place the value $42 into the 8-bit A register. Because A is eight bits wide, a two-digit hexadecimal value is a natural fit.

ld hl,$6000

This tells the Z80 to place the 16-bit value $6000 into the HL register pair. In this exercise we deliberately point HL at the same address where Odin places our code.

ret

RET means return. It tells the CPU to return to the code that called our routine. The exact stack mechanism behind RET will be taught properly later; for now it is enough to know that Odin can call our short routine and RET allows control to come back.

How can RET know where to return?

For now we only need a simple model. When Odin runs our routine, it arranges for a return address to be remembered on the stack. RET takes that saved address and puts it back into the Program Counter, so execution continues in Odin.

  1. Odin calls our routine
  2. return address is remembered on the stack
  3. our code runs
  4. RET retrieves the saved address
  5. execution returns to Odin

The stack, SP, PUSH, POP and CALL deserve a proper lesson of their own. At this stage we only need enough understanding to know that RET is not magic: it uses an address that was previously saved.

Save the native Odin source

Return to the Monitor with EDIT, then enter:

S

Because the document already has the name tut01.odn, S saves it using that name. LS confirms that the file exists.