7

(avery) #1

Getting retro with the Z80: software


TUTORIAL


hello:
defm ‘Hello, World!’
defb 13,10,0
We assemble this using z80asm:
>:z80asm -l hello.asm
Z80 - Assembler Release 1.8, Copyright (C) 1987-
2017 by Udo Munk
0 error(s)

Assembling is much like compiling, in that it
converts our human readable program into computer
executable code.
We can see the result of this conversion by looking
at the hello.lis file which was generated because we
gave the -l option to z80asm.
>:more hello.lis

Z80-Assembler Release 1.8 Mon Apr 23
11:47:26 2018 Page 1
Source file: hello.asm
Title: Hello, world

LOC OBJECT CODE LINE STMT SOURCE CODE
1 1 title
‘Hello, world’
2 2
0000 01 10 00 3 3 ld bc,
hello
4 4 loop:

0003 0a 5 5 ld a,
(bc)
0004 f6 00 6 6 or 0
0006 ca 0f 00 7 7 jp z, end
0009 d3 01 8 8 out (1), a
000b 03 9 9 inc bc
000c c3 03 00 10 10 jp loop
11 11 end:
000f 76 12 12 halt
13 13
14 14 hello:
0010 48 65 6c 6c 15 15 defm
‘Hello, World!’
0014 6f 2c 20 57 15 16
0018 6f 72 6c 64 15 17

WHERE IN THE WORLD?


There are different ways to specify where data is
located. These are called the CPU’s addressing
modes. The ld instruction on line 3 loaded
the literal value 0x0010 into BC. This is called
immediate addressing; the value to use is included
in the machine code immediately following the
opcode. The or instruction on line 6 also uses
immediate addresssing mode. The ld on line 5 is
different than the one on line 3 in a couple of ways:
1) it references the BC register pair as the source
of the data to be used, and 2) bc is surrounded by
parentheses. This means that the value to load
is read from memory, specifically by using the
contents of BC as an address, and reading the
byte at that location. This is the register indirect
addressing mode. It uses a register, not as the
data, but where the data can be found.
In total, the Z80 features ten different
addressing modes.

Above
The ZX Spectrum
Next includes a Z80
implemented in
an FPGA

The simulator has
the pleasing feature
that anything output
to port 1 is sent
to stdout.

QUICK TIP

Free download pdf