Learn, Learn... Assembly
Do You Speak Assembler?
(Part Two)
Welcome back to everyone curious about the inner workings.
In today’s lecture, I’ll try to discuss the instructions for changing and reading the contents of RAM cells.
As we remember, in the previous article I wrote that the Z80 has seven eight-bit registers (values from 0 to 255): A, B, C, D, E, H, L. However, the address bus (something responsible for getting a given value to the appropriate cell—one of 65,535) is sixteen bits wide.
So, in order to make it possible to load a value into a memory cell using registers, it became necessary to combine registers into pairs. A pair of two registers together forms “one sixteen-bit register.” But this is not quite literally so, because a pair such as HL can be modified by changing just one register—that is, H or L.
Unfortunately, you cannot combine registers into pairs arbitrarily. There are three basic pairs that can be used to address memory: BC, DE, HL.
We’ll look at this problem in more depth in one of the next articles.
For now, we only need to understand how memory cells are addressed. Suppose we want the first byte of the screen attributes to take on a colour with a black background and green ink (see “Przewodnik po ZX SPECTRUM”, p. 74). This can be done in various ways:
LD (HL), A
or
LD A, (HL)
Which method is best depends on the program as a whole. However, we should also note that loading into a cell—whether using registers or an address (a number)—is done with parentheses. If we write anything in parentheses in a program, it means writing to the cell at the address held in the given register pair or specified by the given number. The same applies to reading from a cell.
To explain these topics more clearly and visually, let’s write a program that draws a bar of a given colour in the attribute memory.
ORG 40000 ; start the program at address 40000
LD HL, 22528 ; beginning of the bar
LD B, 32 ; length of the bar
LD A, 79 ; colour
PETLA: LD (HL), A ; draw the bar
INC HL ; next byte
DJNZ PETLA ; decrement B and, if not 0, jump to PETLA
RET ; return
The little program may be primitive in its operation, but it should explain what the instructions we’ve learned do, and the possibilities associated with them.
As we learn new instructions, the little routines presented in this series will become more and more interesting.
If my explanations and observations are too complicated, please get in touch.
SCALMAN
Share your thoughts about the article