Download Print this page

ZiLOG Z80 Handbook page 126

Hide thumbs Also See for Z80:

Advertisement

a program to add the numbers from one to ten in a slightly different
implementation. We will use the A register to hold the total as
before, but we will let the B register hold the current number to be
added. This will vary from 10 to 1 as we go in reverse, adding 10,
then 9, then 8, and so forth down to 1. At that point, we will detect
that the next number to be added is 0 and stop. The program in
Zilog mnemonics looks like this:
XOR A CLEAR A
LD B,10 SET COUNT TO 10
LOOP ADD A,B ADD NEXT NUMBER
DEC B PREPARE NEXT NUMBER
JP NZ,LOOP JUMP IF NUMBER-A0
HALT HALT
The A register is first cleared by the XOR A instruction. Next, the
B register is loaded with 10 by the LD B,10 instruction. The next
three instructions comprise a loop. As long as B holds a number
from 10 to 1, the contents of B will be added to A (ADD A,B), the
contents of B will then be decremented by one (DEC 13), and the
jump will be made to the first instruction of the loop which is labeled
"LOOP" as a point of reference of where to return. When the B reg-
ister is decremented, the Z flag is set if the result is zero and reset if
the result is nonzero. If the B register is nonzero (9 through 1) the
JP NZ,LOOP instruction will detect the nonzero (NZ) and jump
back to LOOP. If the B register holds a 0, the Z flag is set and the
conditional jump back to LOOP will not be made, causing the CPU
to execute the next instruction (HALT).
Manually assembling the machine code for this program is a little
more complicated than the preceding example. First of all, while
the previous program could be relocated or loaded anywhere in
memory, since the instructions contained no addresses, the second
program does contain addresses (JP NZ, LOOP must specify the
address of LOOP in bytes two and three of the instruction). A deci-
sion must therefore be made where in memory this program is to
execute. We will arbitrarily choose location 0100H as the start. The
next step in the assembly process is to calculate the length in bytes
of each instruction and write it opposite each mnemonic. After this
step, the program appears as shown below:
Location Length Instruction
0100H 1 XOR A
2 LD B,10
1 LOOP ADD A,B
1 DEC B
3 JP NZ,LOOP
1 HALT
135

Advertisement

loading
Need help?

Need help?

Do you have a question about the Z80 and is the answer not in the manual?

Questions and answers