www.tobobobo.co.uk home




Z80 Machine Code and Assembly Language

This is a pure bucket list item for me - let's face it, why else would I be bothering with an 8-bit microprocessor that was released in the mid 1970s?.

I'm going to see if I can finally get the ZX Spectrum to do things that I could only dream of achieving when I was a kid. I tried so many times to master the skills but never got past the most basic of instructions. I just wanted to put graphics on the screen and move them pixel-by-pixel, but Machine Code books of the day always seemed to spend chapters and chapters telling you about the Spectrum architecture and why 1 + 1 =0.

This is not a tutorial - and it isn't in any strict order. You'll find bits, pieces and snippets dotted all over the place; however, top-down will generally follow my progress.

(This, however, is a tutorial - 'How To Write ZX Spectrum Games' - and a very good one at that. Reproduced here with the kind permission of Jonathan Cauldwell)


 

This is why I'm doing this

30 years ago, this would have looked like gobbledegook to me.

org 33000
ld hl,16384
ld b,255
loppyld c,21
ld (hl),c
inc hl
djnz loppy
ret

Now, however, I know exactly what it does - heck, I could even tell you why '232 128' = 33000.

And it's this eureeka that's prompted me to delve further.

The resources helping me through this are the books: 'Mastering Machine Code on the ZX Spectrum' by Toni Baker, and '40 Best Machine Code Routines for the ZX Spectrum' by A.Hardman and A.Hewson; and the online articles: ZX Spectrum Machine Code Game in 30 Minutes, by Jon Kingsman; and How to Write Spectrum Games, by Jonathan Cauldwell (the last two of which are currently available from the Chuntey blog.


Hexadecimal

Note to self: Hexadecimal is a very effective way of counting to 255 using only two digits. As the table below shows, you count from 0 to 9 in the usual way, and then use the letters A-F for the double-digit numbers: 10,11,12,13,14 and 15. Once you need to get to 16, a 1 is carried over to the left-hand column and the right starts from 0 again.

123456789101112131415
123456789ABCDEF

01 = 1
0D = 13
10 = 16
20 = 32
2A = 42

In short:

In decimal, 16 = 16. (1x10)+(6x1)=16
In hexadecimal, 16 = 22. (1x16)+(6x1)=22


The Spectrum Screen

I'm guessing that if you're actually reading this, then you'll know the way in which the Spectrum configures its screen, i.e. banded layers rather than sequential, and groups of pixels first, followed by the attributes.

Here are the addresses that matter:

16384 - the start of the screen addresses
6144 - number of pixel addresses
22527 - last pixel address

22528 - start of attribute addresses
768 - number of attribute addresses
23295 - last attribute address

And here they are in practice:

Spectrum screen setup
255 poked to 16384 and 22527 (see the solid line top-left and bottom right right)255 poked to 22528 and 23295 (attribute square top-left and bottom-right)

The Registers (temporary variables, to you and me)

A  The Accumulator - numbers 0-255 (00-FF)
HL  Can use H or L individually (0-255) or HL as a pair 0-65535 (0000 - FFFF)
BC  B or C, or BC
DE  D or E, or DE

High-Byte, Low-Byte - eh?

Before I get too far, I need to get to grips with this high-byte, low-byte stuff.

If I understand it right, whenever a register pair hex number (or any big number) is stored in the Spectrum memory it's stored in reverse order. So if the following value is placed into the BC register pair...

LD BC,16386

...it takes up three bytes in memory. So say it was placed at address 33000, and we 'peek' the addresses 33000, 33001 and 33002, we should see 1,2 and 64 stored there.

1Machine code for LD BC,(number)
2Low-Byte
64High-Byte

Then (High-Byte x 256) + Low-Byte should give us the number we originally stored in the BC register (64x256)+2 = 16386.


Hex or Dec in Assembly - it matters

Note to self: in assembly language, 4000 = 4000 and 4000h = 16384. The 'h' means treat the number as hexadecimal.


Adding to registers

Apparently, you can only add to two of the registers: HL and A.

So - ADD A,## or ADD HL,##


Adding more than a register can handle

Single registers can only hold numbers from 0-255, and paired registers only numbers from 0-65535. In the following code, the Accumulator is initially loaded with the value 155 and then 200 is added to it...

Note: Whenever a Machine Code (herefater referred to as 'MC') program is run using PRINT USR #####, any value in the BC regsiter is printed to the screen when, and if, the MC returns to basic after coming to the command RET (201).

ld a,155
add a,200
ld b,0
ld c,a
ret

As 155 + 200 comes to 355, the Accumulator is unable to store it and it simply loops round from 255 and displays 99 as the result. It's 99 and not 100 (as might be expected) as 0-255 is actually 256 numbers, and 350-256=99. However, what it does do is set the Carry Flag to 1 ...and that's about to come in handy.


 

 

 

 

 


Table of Z80 Machine Code instructions - (filled in as I go)

00NOP
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255