Programming in Z80 Assembly Language
Programming in Z80 Assembly Language
Roger Hutty
M
MACMILLAN
© Roger Hutty 1984
Pre f a c e ix
5. 1 Th e f l ag r e g i st er 21
5 .2 Con di ti on al j ump i nstructions 22
5.3 The c o mpare in s t r u c t ion 23
5 .4 Con di t i onal loo p te r mi na tion 24
5. 5 Pro gram 25
v
vi Contents
8.1 Carry 40
8.2 The carry flag 40
8. 3 Over flow 41
8.4 The overflow flag 42
8 .5 Conditional CALLs and RETs 42
8.6 Program 43
Index 118
Preface
ACKNOWLEDGEMENTS
Roger Hutty
ix
1 The zao Architecture
-.
display
keyboard
/
Figure 1.1
* Zilog and Z80 are trademarks of Zilog, Inc., with whom the
publisher is not associated. *
B C
D E
H L
SP Stack pointer
PC Program counter
IX X index register
IY Y index register
I Interrupt vector
register
Figure 1.2
1.3 MEMORY
o 01010010
01110111
2 10110000
3 01111110
/
address contents locations
Figure 1.3
1.4 INSTRUCTIONS
Have a quick glance at Appendix C where you will see the complete
zao instruction set.
10010010
SUB D
LD r,n
Quant ities can be added to, and subtracted from, the accumulator
by means of the
LD A,15
ADD A,46
SUB 22
6
Ac cumulator and Register Instruction s 7
LD rl,r2
LD A,56
SUB 22
LD B,A
ADD A,B
ADD A,B
NEG
LD A,27 27 (lBH)
NEG -27 (ESH)
INC A -26 (E6H)
The three instructions LD r,n, ADD A,n and SUB n are using the
immediate addressing mode - so called because the value of the
operand is included in the instruction (in the second byte for
these particular instructions).
LD A,027AH)
LD (nn),A
2.8 LABELS
Program 2.1 shows the listing produced by the assembler after the
source program has been assembled. The first column of the
assembler listing gives the byte address of an instruction or
data byte and the second column is the object program, in
hexadecimal.
The listing shows how the assembler has assigned values - their
byte address number - to the labels. For example, NUMBER has the
value 0009H.
2.9 PROGRAM
RESULT = N1 - 3(N2 + 1) - 1
program >main
program
1
/ / / / JI
su broutine
Figure 3.1
11
12 Programming in Z80 Assembly Language
LD A,(N2)
CALL QUAD
LD (R2) ,A R2 N2 x 4
HALT
N1 : DEFB 31
N2: DEFB 25
Rl: DEFB 0
R2: DEFB 0
,
; Subroutine - multiplies the accumulator by 4
The main program then continues its execution with the LD A,(N2)
instruction which loads the accumulator with N2 before calling
the subroutine again with the second CALL QUAD instruction. The
subroutine's two ADD instructions will be executed, followed by
the RET instruction which causes a return to the main program
to the LD (R2),A instruction this time. Finally, the program
terminates with the HALT instruction.
SUMREG: LD A,a
ADD A,B
ADD A,C
ADD A,D
ADD A,E
ADD A,H
ADD A,L
RET
A main program may need to use more than one subroutine. In that
case, all the required subroutines are located at the end of the
main program, one after the other.
Program 3.2
The END pseudo operator has also now gone out of general use. Its
function is to inform the assembler that there is no more program
to assemble, that is, to indicate the end of a program . This is
necessary in some environments, such as the old paper-tape
systems, where it is not clear that the end of the program has
been reached. However, programs are now more normally held in
files, of one kind or another, and the assembler automatically
finds the end of a program by detecting the end of the file
containing the program. Purists would say that a program should
always finish with the END pseudo operator but since it is
usually obvious where the end of a program is, it is now rarely
used. Exceptionally, some assembler systems require programs to
be terminated with an END pseudo operator.
3.5 PROGRAM
JP nn
LOOP: *
* instructions to be repeated
JP LOOP
*jump to LOOP
16
Unconditional Jumps and Keyboard Input 17
LD A,'*'
NEXT: CALL COUT
JP NEXT
In the same way that you were given a subroutine to output to the
display you are now given a subroutine called CIN which accept's a
character from the keyboard. The subroutine is shown in Program
4.2.
Program 4.2
You s h ould check with y ou r computer reference man ual or comp uter
s u p p l i er that the input subroutine in Program 4 .2 is correct, in
every detail , for your particular compute r system. You will als o
need to check if an input character is automatically echoed, or
not . The subroutine given is the one used with Z80 microcomputer
syst~ms operating under the CP/M operating s ys tem .
LD A,CR
Another example of the EQU pseudo operator being put to good use
is the naming of the address in the input and output. The outline
of a program using the display and keyboard subroutines given in
Programs 3.2 and 4.2 would look like
CIN:
CALL CPM
COUT:
CALL CPM
4.5 PROGRAM
2+2=4
5+3=8
4+1=5
S Sign flag
Z Zero flag
H Half-carry flag
p/v - Parity/Overflow flag
N Add/Subtract flag
C Carry flag
X Unused bits
LD A,23
SUB 56
the accumulator will contain -33 and the sign flag will be set to
1.
The operation of the Sand Z flags will suffice for now - the H,
P/V, Nand C flags will be dealt with later in the book.
21
22 Programming in Z80 Assembly Language
Not all instructions affect all of the flags. For example, none
of the LD instructions affects any flags . You can discover which
instruction s affect which flags by looking in the third column of
the t a b l e s in Appendix C. Glancing through the tables you will
see that the sign and zero flags are mainly affected by
arithmetic, shift, rotate and bit ins tructions .
A S Z
LD A,120 120 ?
SUB 122 -2 1 0
LD B,A -2 1 0
SUB B 0 0 1
ADD A, 70 70 0 0
NEG -7 0 1 0
Program 5.1
LD A, (X)
SUB 10 compute X-I0
JP Z,EQUAL
LD A,1 X I- 10
JP CaNTIN
EQUAL: LD A,O X 10
CONTIN: -
X: DEFB 25
i ns t r uc t i on JP CONTIN.
LO A,B
ADD A,C B+ C
JP M,NEG
JP Z, ZERO
LO A, ' p' +ve
JP DONE
NEG: 10 A,' N' -ve
JP DONE
ZERO: LO A, ' z' 0
DONE : CALL COUT
CP 50
JR Z,FIFTY
LO A,(COUNT)
CP 100
JP M,LESS COUNT < 100
JP Z,EQUAL COUNT 100
JP GREAT COUNT > 100
The CP a instruction is useful for setting the flag register
after an instruction which does not affect the flags. For
example, the LO instruction in the sequence
LO A,(TEMP)
CP a
does not affect the flags and a CP a instruction is, therefore ,
necessary if the status of the acc umul a t o r is required.
; Program 5.2
,
NEXTCH: CALL CIN
CP
JP NZ,NEXTCH
BLANK:
5.5 PROGRAM
The maximum loop count that can be used by the loop construction
in Program 6.1 is 255.
26
Counting Loops and the Stack 27
LD A,initial-value
NEXT:
DEC A
JR NZ,NEXT
Program 6.2 does just that by adding in the loop counter, which
goes from n to 1, each time through the loop.
N: EQU 11
LD
LD
A,O
B,N
set sum to °
NEXADD: ADD A,B add n to sum
DJNZ NEXADD
28 Programming in Z80 Assembly Language
The loop counter is being used to count the number of times that
the sequence of instructions is executed. It is also being used
as the value to be added in each time round through the loop.
After looping is complete the accumulator will contain the sum.
Notice that the accumulator must be set to zero to start with.
If, for example, we wanted to input the number 123 it would have
to be input as three separate digits - the digit 1 followed by
the digit 2 followed by the digit 3. This would be done with
three CALLs to the CIN subroutine so the program would then have
the character codes for the three digits 1, 2 and 3. How then do
we convert these codes to form the 8-bit value 123 i n a register
or memory byte ?
1 x 100 + 2 x 10 + 3
would be performed.
A stack has a top and a bottom. The bottom of the stack is where
the first item was added to the stack and the top of the stack is
where the last item was added. (A ZaD stack item is two bytes.)
The stack pointer (SP) register always points to the top of the
stack. Initially, when the stack is empty, the stack pointer
points to just below the bottom of the stack.
low address
- _stack top
I -=======r-
stack pointer(SP)
register _stack bottom
high address
.....
Figure 6.1
You can see from Figure 6.1 that the stack bottom is a high
address and the top is a low address, so usually the stack bottom
is a memory byte with one of the highest addresses. The address
of the stack bottom can be set by program instructions and there
may be more than one stack. However, a system will usually have
only one stack and the address of that stack bottom will be set
by the operating system.
The stack is used mainly for the temporary storage of data and
addresses, an example of which we shall look at in the next
section and also in the subroutine mechanism which will be
discussed in the next chapter.
30 Programming in Z80 Assembly Language
A B C D E SP
SUBIN: PUSH BC
PUSH DE
POP DE
POP BC
RET
When using the stack in this way care must be taken to ensure
that registers placed on the stack are removed before the RET
instruction and also that the registers are POPped off in reverse
order.
We are now able to code the CRLF (carriage-return line-feed)
subroutine in a better way - using EQU and saving and restoring
the accumulator which would otherwise be corrupted by the
subroutine. The improved version is as follows
CR: EQU 13
LF: EQU 10
CRLF: PUSH AF
LD A,CR output carriage-return
CALL COUT
LD A,LF output line-feed
CALL COUT
POP AF
RET
6.6 PROGRAM
Use the MULT and NUMIN subroutines, and subroutines from previous
programs, to write a main program which repeatedly inputs (with
echo) a decimal number terminated by a space and outputs the
number of digits (I, 2 or 3) in the number, a space, the word OK
if the number is in the range 50 to 100, inclusive, or otherwise
the word NO, and a carriage-return and a line-feed.
Although Program 7.1 sh ows two standard count ing l oops, there may
be more than two loops nested a nd a ny one of the nested loops may
be any type of loop which is used in any way a nd terminated in
any way.
The Z80 microprocessor has ten addressing modes, that is, ten
different ways of speci fying the operand o f an instruct ion. We
have already looked a t two a ddres si ng mode s expl i ci t ly (immediate
and e xtended), but have us ed another three (re g ister, implied and
rela tive) without speci al menti on.
33
34 Programming in Z80 Assembly Language
Implied addressing mode has the operand implied, that is, not
explicitly stated in the instruction. The accumulator is the
implied operand in the instruction NEG.
§8
opcode DA opcode
~ operand 78 operand
address
56
SUB 73 LD A, (5678H)
Figure 7.1
LD HL,NUMBER
I LD I
~
D HL opcode A, (HL) opcode
34
6Ej
operand
12 H operand
address
L 2B
LD HL,1234H LD A, (HL)
Figure 7.2
Program 7.2 shows the use of the immediate extended and register
indirect addressing modes.
The program computes the sum and the difference of two numbers Nl
and N2 which are located in a data area at the end of the
program.
LD HL,N2 ; HL points to N2
LD A,(Nl)
SUB (HL) DIFF Nl - N2
LD (DIFF),A
LD A, (Nl)
ADD A, (HL) SUM Nl + N2
LD (SUM) ,A
HALT
;
Nl: DEFB 14H
N2: DEFB -23H
DIFF: DEFB 0
SUM: DEFB 0
FIRST LINE
SECOND LINE
to be dis played.
When only one, two or three characters are being output to the
display it is normal to output them with separate sets of
statements, each of which loads the accumulator with a character
and calls COUTo However, for three or more characters this would
be a very inefficient way to output them to the display.
Having used subroutines we are now going to see how they work,
that is, what happens when a call is made from a main program to
a subroutine and a return is made from that subroutine. The
following description refers to Figure 7.3.
38 Programming in Z80 Assembl y Language
,.
CALL SUBR
NEXT!
.... - ,.
.... ....
NEXTr
SUBR
,-
- ....
RET
,. -
program and subroutine stack
Figure 7.3
7.6 PROGRAM
******************************
******************************
******************************
your name
your address first line
second line
- third line
******************************
******************************
******************************
As an extra challenge you could output after the last line the
following pattern
****************************
**************************
************************
**********************
********************
******************
****************
**************
************
**********
********
******
****
**
You should use nested loops to provide this additional output,
not just fourteen calls to the TEXOUT subroutine with fourteen
different fixed-character strings.
8 Carry and Overflow
Carry and overflow are two conditions which can occur during
addition and subtraction. The conditions affect bits in the flag
register which can then be tested by conditional jump
instructions.
8.1 CARRY
00110011 (+51)
+ 00011100 (+28)
--------
01001111 (+79)
--------
does not produce a carry, but the sum
11111110 (-2)
+ 11111111 (-1)
--------
[ 1] 11111101 (-3)
--------
has produced a carry which would be ignored as far as the result
of the 8-bit addition is concerned.
JP C,label
JP NC,label
JR C,label
or JR NC,label
40
Carry and Overflow 41
SUB 8
JP NC,NCARRY
CARRY:
There are two instructions which may be used to change the value
of the carry flag. The instructions are SCF which Sets the Carry
Flag to I, and CCF which Complements the Carry Flag. These two
instructions are specified in Table C.6 in Appendix C.
For addition, two numbers with different signs will never cause
overflow. However, when adding two posit ive numbers or two
negative numbers overflow mayor may not occur. For example, the
sum
01100100 (+100)
+ 00110001 (+49)
10010101 (-107)
does not produce the correct arithmet ic result because the real
sum of the two numbers, +149, is greater than +127 and is
therefore outside the arithmetic range.
For subtraction , overflow can only occur if the two numbers have
different signs. For example, the subtraction
42 Programm ing in Z80 Assembly Language
01111110 (+126)
- 11000000 (-64)
0) 10111110 (-66)
does not produce the correct arithmetic result. The real result,
+190, is outside the range. Additionally, in this particular
example, carry has occurred.
Both carry and overflow can occur together when adding two
numbers. For example, the sum
10010101 (-107)
+ 10010101 ( -107)
JP PO,label
or JP PE,label
JP NZ,OVER
CALL SUBEX
OVER:
CALL Z,SUBEX
JP NC,EXIT
JP EXIT
JP Z,EXIT
EXIT: RET
RET NC
RET
RET Z
RET
A single exit subroutine provides a cleaner interface which
ensures that all that has to be done before returning is done and
extensions to the subroutines can easily be made without the
possibility of forgetting that exits were made other than at the
end of the subroutine.
The ' ex i t ' label is normally associated with the first of several
instructions ending with the RET i ns t r uc t i on . If, for example,
registers have to be restored then the end of the subroutine may
look like
GOBACK: POP BC
POP DE
RET
8.6 PROGRAM
SUM IS znorp
PRODUCED OVERFLOW
PRODUCED CARRY
The two numbers nl and n2 are signed 2-digit decimal numbers, for
example, 63 and -08.
ITIIIIIJJ
7 6 5 4 320
BIT 6,E
tests the bit numbered 6 of reg ister E and would set the Z flag
to 0 if register E contained OlOOOlOOB.
Look back at Program 3.2 where you will see a BIT instruction
used to test bit 0 of the accumulator.
SET 2,C
RES 5, (HL)
NUM: DEFS 1
BUFFER: DEFS 96
NAME: DEFM 'FRED'
The 280 microprocessor has two index registers, IX and IY, so-
called because they allow a program to access a particular byte
in a block of bytes, by an index which is relative to the start
of the block.
The following program skeleton shows how an index reg ister can be
used.
START: DEFS 10
The IY index register can be used in exactly the same way as the
IX index register.
However, in the main you will find the index registers being used
with a zero displacement, in which case they are used in much the
same way as the HL register pair.
9.5 EXPRESSIONS
LD A, (NUM+1)
would cause the contents of the byte following the byte labelled
NUM to be loaded into the accumulator.
48 Programming in Z80 Assembl y Language
ITEMS: EQU m
LENTH: EQU n
rp contains JPTAB + 3 x (N - 1)
JP ( rp)
Program 9.1 shows the use of a jump table in displaying the name
of a day whose number is input from the keyboard.
Bit Operations and the Index Registers 49
MON: LD HL,MONDAY
CALL TEXOUT
JP FINI
SUN: LD HL,SUNDAY
CALL TEXOUT
JP FINI
FINI: HALT
9.7 PROGRAM
o---OIIIIID-D
1 6 5 4 3 2 o carry
flag
register or memory byte
SRL m
51
52 Programming in Z80 Assembly Language
SRA m
B C Car r y
D- DIDITIJ-o
ca rry 765 4 3 2 o
flag
register or memory byte
SLA m
SLA A 2 x A
LD B,A
SLA A 2 x (2 x A) 4 x A
SLA A 2 x (4 x A) 8 x A
ADD A,B 8 x A + 2 x A 10 x A
00000 x a
00 111 x 10
00000 x 000
0011 1 x 1000
000 00 x 00000
LD A,O
LD D,l
NEXBIT: SRL C test next bit of multiplier
JP NC,NOADD
ADD A,B add in multiplicand
JR PO,OVEKFL
NOADD: DEC D
JR Z,DONE
SLA B shift multiplicand
JR NEXBIT
10.5 PROGRAM
Write a new MULT subroutine which uses the 'shift and add' method
of multiplication and additionally deals with signed numbers.
Using the NUMOUT, DIV and MULT subroutines and subroutines from
previous programs, write a main program which inputs two signed
numbers separated by either an * character or a / character and
followed by an = character. The program should then output
either the product of the number (if the * character was input),
or the quotient and remainder ( if the / character was input). A
typical dialogue would look like
-11*5=-55
125/10=12 REMAINDER 5
11 Logical Operations and Macros
Glance at Table C.5 in Appendix C where you will see the AND, OR
and XOR instructions specified. Notice that the Symbolic
Operation column of that table uses the Boolean Algebra symbols
of A for AND, v for OR and $ for XOR.
Contents of A OOOl1010B
Contents of B llOOllllB
Contents of A after AND B 00001010B
56
Logi cal Operations and Macro s 57
AND r
n
OR (HL)
(IX)
XOR (IY)
The following program segment shows the effect of the four Z80
logical instructions on the accumulator and the S, Z and C flags
A S Z C
LD A,10110101B 10110101B ? ? ?
LD C,l1110000B 10110101B ? ?
AND 00011111B 00010101B 0 0 0
OR C 1111 0101B 1 0 0
XOR 11001100B 00111001B 0 0 0
CPL 11000110B 0 0 0
11.3 MASKING
11.4 MACROS
Now, at any place in the program the opcode RSF can be used - or,
as is usually said, 'the macro RSF can be called'. For example,
the RSF macro is called in the following program segment
LD (POINT) ,A
RSF reset carry flag macro
ADD A,B
LD (POINT) ,A
SCF macro RSF
CCF expansion
ADD A,B
Two macros are defined - one for the start of the loop, LOOPST,
and one for the end of the loop, LOOPEN. Unlike the previous RSF
macro, both of these macros have parameters. Any parameters used
in a macro definition must be listed after the MACRO pseudo
operator. The parameters may then by used anywhere in the macro
body. When a call is made to a macro with parameters, during
expansion of the macro the parameters are replaced by the actual
parameters in the macro call. The last macro call in Program
11.1 would be expanded to
DEC E
JP NZ,ROUND
Logical Operation s and M acros 59
TIMER '200'
TIMER '50'
TIMER '76'
would be expanded as
LD B,200
TMOOOO: DJNZ TMOOOO
LD B,50
TMOOOI : DJNZ TMOOOI
LD B,76
TMOO02: DJNZ TMOO02
COND CODE IN
ENDC
The instructions between the COND and ENDC pseudo operators will
be included during assembly if the value of CODEIN is non-zero,
but ignored by the assembler if CODEIN is zero.
Logical Operations and Ma cros 61
COND DORP
aUTLIN: - display output subroutine
RET
ENDC
RET
ENDC
11.6 PROGRAM
Two other common logical operations are NOR and NAND, which are
short for NOT OR and NOT AND, respectively. The rules of these
two logical operations are
o NOR 0 1 o NAND 0 1
o NOR 1 o o NAND 1 1
1 NOR 0 o 1 NAND 0 1
1 NOR 1 o 1 NAND 1 o
from which it can be seen that the result of the NOR operation is
the complement, or NOT, of the OR result and, similarly, the
result of the NAND operation is the NOT of the AND result.
b lop b =b
where b is 0 or 1
and lop is one of OR (space to be input after the R)
AND
XOR
NOR
or NAN (short for NAND)
and outputs further along the same line, TRUE if the input is
correct and otherwise FALSE.
A tra ining session is ended by inpu~ting the letter E.
12 Rotate Instructions and Parity
Some of the rotate instructions include the carry flag within the
rotation, whil e others, referred to as rotate circular
instructions, do not.
The accumulator can be rotated left and right, and for each
direction the carry flag may, or may not, be included within the
rotate, giving the four instructions
co.
carry
flag
ITIIIIITJJ
7 6 5 4 321
accumulator
0
63
64 Programming in Z80 Assembly Language
The contents of the accumulator move to the left one bit place,
and in doing so, bit 7 of the accumulator moves into the carry
flag and the carry flag moves round into bit 0 of the
accumulator.
[ITIIJIIJJ-.Q]
165 4 3 2 o carry
accumulator flag
The contents of the accumulator move to the right one bit place,
and in doing so, bit 0 of the accumulator moves into the carry
flag and the carry flag moves round into bit 7 of the
accumulator.
carry 165 4 3 2 o
flag accumulator
The contents of the accumulator move to the left one bit place,
and in doing so, bit 7 of the accumulator is moved to the carry
flag and around into bit 0 of the accumulator. So after a RLCA
instruction the carry flag and bit 0 of the accumulator will
always be the same value - the value of bit 7 of the accumulator
prior to execution of the instruction .
LITIIJ;J;;-Lo
165 4 321
accumulator
0 carry
flag
The contents of the accumulator move to the right one bit place,
and in doing so, bit 0 of the accumulator is moved into the carry
flag and also around into bit 7 of the accumulator .
Rotate Instructions and Parity 65
A Carry
10101011B o
RLA 01010110B 1
RLCA 10101100B o
RRA 01010110B o
RRCA 00101011B o
memory byte could be packed with the sex and age of a person, as
follows
ffiEEEEEJ
7 6 5 4 321 0
sex age
Bit 7 of the memory byte indicates the sex of the person, say 0
for female and 1 for male. Bits 6 to 0 of the memory byte
contain the person's age, giving a range for the age of 0 to 127.
The example above specifies a male aged 100 years.
Assuming that the sex and age of one thousand people was held in
memory then there is a saving of one thousand bytes by including
the sex bit in the byte containing the age. If the sex and age
were contained in separate bytes, two thousand bytes would be
needed to hold the same information.
LD A,(SEXAGE)
AND 10000000B
RLCA
LD B,A
LD A,(SEXAGE)
AND OlllllllB
LD C,A
12.4 PARITY
A Parity
10111001B ?
AND 11111110B 10111000B 1
SLA A 01110000B o
RLA 11100001B 1
JP PE,label
JP PO,label
CALL PE,label
CALL PO,label
RET PE
and RET PO
68 Programmi ng in Z80 Assembly Language
The mnemonics PE and PO stand for Parity Even and Parity Odd,
respectively.
12.6 PROGRAM
When working with 8-bit quant ities we used the DEFB pseudo
operator when it was necessary to initialise memory bytes at the
end of ou r programs. When working with 16-bit quantities we will
need to initialise double bytes, or words as they are called. To
do this we use the DEFW (DEFine Word) pseudo operator. For
example,
LD HL, (LABEL)
69
70 Programming in Z80 Assembly Language
ADD HL,DE
ADD HL,ss
LD BC,2054
LD HL,1362
ADD HL,BC
Remember that the range of signed numbers that can be dealt with
by 16-bit arithmetic is -32708 to +32767.
LD HL,(NiLS)
LD DE,(N2LS)
ADD HL,DE add least significant 16-bits
LD (RESLS),HL
LD HL,(N1MS)
LD DE,(N2MS)
ADC HL ,DE add most significant 16-bits
LD (RESMS),HL
JP PO,OVER}' overflow occurred?
SBC HL,ss
One important point to note about the 16-bit ADD, ADC and SBC
instructions is the flag setting of these instructions. Look at
the specifications of these instructions in Table C.7 of Appendix
C and you will see that the 16-bit ADC and SBC instructions set
the carry, zero, overflow and sign flags as you would expect,
but the 16-bit ADD instruction causes only the carry flag to be
set. A 16-bit ADC instruction, preceded by an instruction to set
the carry flag to zero, can be used if the setting of the zero,
overflow and sign flags are required during a single 16-bit
addition.
ADD IX,pp
ADD IY,rr
where rr is anyone of the register pairs BC, DE, IY and SP. The
instructions cause the contents of the register pair pp and rr to
be added to IX and IY, respectively.
You will often find that the IX and IY index registers are
awkward to use and that the other register pairs, particularly
HL, are more convenient to use because there is a wider range of
more flexible instructions involving these register pairs.
74 Programming in Z80 Assembly Language
There are two instructions which can be used for multiple byte
arithmetic, in the same way that we saw that the two ADC HL,ss
and SBC HL,ss instructions could be used for multiple 16-bit
arithmetic.
ADC A,s
SBC A,s
Program 13.3 shows a program segment which adds mult iple byte
numbers.
13.5 PROGRAM
The numbers m. nand k are uns igned hexadecimal numbers which the
program inputs from the keyboard.
76
Block Transfer and Search Instruct ions 77
The register pairs HL and DE are set to point to the first byte
of the source and destination blocks of memory ~ytes,
respectively. The register B is to be used as the counter and is
initialised to ten.
DEC BC
LD A,B
CP 0
JP NZ,NEXBYT
LD A,C
CP 0
JP NZ,NEXBYT
The following program segment has the same effect as Program 14.2
but uses the LDDR instruction instead
Two other block transfer instructions LDI and LDD are similar to
the LDIR and LDDR instructions except that they do not
automatically go on to transfer the next byte.
LD HL,SOURCE
LD DE,DESTIN
LD BC,10
NEXBYT : LDI transfer byte
JP PE,NEXBYT last byte? no, next byte
HALT yes
The LDIR and LDDR instructions can be used only when the number
of bytes to be transferred is known in advance. When the
criteria for the numbers of bytes to be transferred are not known
in advance, the LDI or LDD instructions must be used and the
program must write the instructions to transfer all the bytes.
Care must be taken during block transfers when the source and
destination blocks overlap. Take, for example, a situation where
it is necessary to move a block of bytes so many bytes through
memory as is done by the following program segment
LD HL,START
LD DE,START+100
LD BC,500
LDIR
The first one hundred bytes of the source block of bytes will be
copied into the first one hundred bytes of the destinat ion block
of bytes which also happens to be the second one hundred bytes of
the source block of bytes. So the last four hundred bytes of the
source block of bytes are overwritten before they can be
transferred to the destination block of bytes.
To make the block of bytes move down through memory correctly the
above program segment would have to be changed to
LD HL, START+4 99
LD DE,START+599
LD BC,500
LDDR
START: DEFB 1
DEFB 2
DEFB 3
DEFB 0
DEFB 4
DEFB 5
DEFB 0
DEFB 6
DEFB 0
DEFB 7
In the same way that the P/V flag is used to indicate that BC
contains zero during execution of the LDI and LDD block transfer
instructions, so the P/V flag is used to indicate the contents of
BC on termination of the block search instructions.
The CPDR instruction can be used to search a block from the end
Block Transfer and Search Instructions 81
The CPI - ComPare and Increment and CPD - ComPare and Decrement
instructions are similar to the CPIR and CPDR except that they do
not automatically go on to the next byte. Extra instructions
have to be used to test for a match between the accumulator and
whether the byte has been found - zero flag is set to 1 - and to
detect if the whole of the block has been searched - BC is zero
and the P/V flag is set to O. These two instructions are used in
place of the CPIR and CPDR instructions when intermediate
processing is required and, for example, when more than one
occurrence of the value in the accumulator needs to be detected.
To make Program 14.3 output the value of the counter for every
occurrence of a zero byte the instructions CPIR to HALT would
have to be replaced by the following instructions
NEXBYT : CPI
JP PO,FINI end of block?
JR NZ,NEXBYT
LD A,C output counter
ADD A,30H
CALL COUT
LD A,O restore A
JR NEXBYT
,
FINI: HALT
14.3 PROGRAM
The file should be defined at the end of the main program using
82 Programming in Z80 Assembly Language
7 6 5 4 3 2 o
Numbers are normally input from the keyboard and output to the
display as decimal digits. To enable arithmetic to be performed
on the numbers they have to be converted to binary, and then the
results converted from binary to decimal before being output. If
a computer has instructions which allow arithmetic to be
performed with numbers in their decimal digit form then the
conversion from decimal to binary, and vice versa, would be
unnecessary.
00101000 BCD 28
+ 00111001 BCD 39
0110 BCD 6
+ 0111 BCD 7
1000 BCD 8
+ 1001 BCD 9
00010111 BCD 17
+ 01101001 BCD 69
--------
10000000 BCD 80 - incorrect BCD result
+ 0110
--------
10000110 BCD 86 - correct BCD result
--------
A carry occurring from the left nibble of a 2-digit BCD addition
would indicate overflow, that is, a value greater than 99.
10000010 BCD 82
- 01010110 BCD 56
Although the half-carry and subtract flags are used only by the
DAA instruction they are set accordingly after every arithmetic
instruction. Neither of these flags may be used by a programmer
because there are no instructions to set or test them directly.
Using the setting of the half-carry and subtract flags, the DAA
instruction corrects the contents of the accumulator, if
necessary, to give a result in the accumulator as if the previous
arithmetic instruction had been a BCD one. For example, the
program segment
~D A,43H
10 B,28H
ADD A,B
DAA
loads the accumulator and B register with BCD 43 and BCD 28,
respectively, sums them using a binary ADD instruction, and then
adjusts the result to BCD representation using the DAA
instruction. Notice that hexadecimal representation, being a 4-
bit representation, is very useful for writing BCD constants in
programs.
instructions ADD A, SUB, INC A, DEC A, CP, NEG, ADC A, SBC and
the four block search instructions. Notice that the DAA
instructions operate on the accumulator only.
The two flags of most import ance to the programmer, which are set
by the DAA instruction, are the carry flag indicating BCD
arithmetic overflow and the zero flag indicating a zero BCD
value. The carry flag setting can also be used in multi-byte BCD
arithmetic, as we shall see later.
The rotation involves the right nibble of the accumulator and the
two nibbles of a memory byte pointed to by the HL register pair.
The Rotate Left Digit instruction, RLD, operates as follows
D_
t ---JI LJ
The RLD and RRD instructions are very useful for manipulating BCD
numbers. For example, look at Program 15.1 which inputs two 2-
digit BCD numbers, adds them, and outputs their sum.
88 Programming in Z80 Assembly Language
LD HL,NUM
CALL CIN input 1st digit of first number
AND OFH
LD (HL) ,A
CALL CIN input 2nd digit of first number
AND OFH
RLD store first number in NUM
INC HL
CALL CIN input 1st digit of second number
SUB 30H
LD (HL),A
CALL CIN input 2nd digit of second number
SUB 30H
RLD store second number in NUM+1
LD HL,NUM
10 A,(HL)
INC HL
ADD A,(HL) add the two numbers
DAA decimal adjust for BCD
LD HL,SUM save result
LD (HL),A
LD A,O output result
RLD
ADD A,30H
CALL COUT - first digit
LD A,O
RLD
ADD A,30H
CALL COUT - second digit
HALT
,
NUM: DEFS 2
SUM: DEFS 1
Working through the program, the first number is input, digit by
digit, and stored in NUM using an RLD instruction, followed by
the input of the second number into NUM+1 using another RLD
instruction. TIle HL register pair must be used to point to the
memory bytes because the RLD instruction assumes that it does.
The two numbers are then added and immediately adjusted for BCD
arithmetic using the DAA instruction. The result is stored
before being output, digit by digit, using an RLD i ns t r uc t i on to
move each nibble in turn from SUM to the accumulator ready for
output. Notice that the accumulator has to be set to zero before
the last two RLD instructions are executed - this is to ensure
that the left nibble of the accumulator is zero. Although the
left nibble is not involved in the decimal rotate instruction,
and partly because it is not, you must ensure that the contents
of the left nibble of the accumulator are what you want them to
be.
Decimal Arithmetic 89
15.5 PROGRAM
There are several instructions which have not yet been considered
because they are rarely used or because they are beyond the scope
of this book . However, for completeness, they are discussed
briefly in this final chapter.
90
Miscellaneous Instructions 91
The remaining input and output instructions allow the input and
output of blocks of data. These instructions, and their
variations, are similar to the block search instructions except
that, instead of comparing a data byte, a data byte is input or
output. (Also, only the single B register is used as a counter -
not the BC register pair.)
453 400 + 59 + 3 1)
(4 x 10 ) + (5 x 10 + (3 x 10°)
Looking at the three numbers above you can see that de cimal
numbers are expressed in terms of the powers of tens, hexadecimal
numbers are expressed in terms of the powers of sixteens and
binary numbers are expressed in terms of the powers of two . The
93
94 Programming in Z80 Assembl y Language
ten, sixteen and two are said to be the base or radix, of the
numbers. Decimal numbers have a base of ten, hexadecimal numbers
a base of sixteen and binary numbers a base of two. Any number
can be used as a base, but in computing, and particularly for
microprocessors, the most common bases are sixteen and two.
Exercise 1
By working out the expressions above, what are 974H and 101B
equivalent to as decimal numbers?
You know already that decimal numbers use the digits a to 9, that
is, zero through to one less than the base value.
Exercise 2
Which digits do binary numbers use?
Look now at Figure A.l which shows the equivalent hexadecimal and
binary numbers of the decimal numbers a to 15.
a a 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 1000
9 9 1001
10 A 1010
11 B 1011
12 C 1100
13 D 1101
14 E 1110
15 F 1111
Figure A.l
Exercise 3
What is the decimal equivalent of E8A5H?
4B3AH 01011100B
Exercise 4
Do the following arithmetic
C7BAH OllOllOlB
9FF8H + 01011110B
I'::
conversion of 745 to hexadecimal looks like
16
16 r-emat nder 9
16 2 remainder E
o remainder 2
the equivalent hexadecimal number being 2E9H.
Exercise 5
Convert the decimal number 1582 to hexadecimal.
the hexadecimal number in powers of 16, and then add the terms.
For example, the conversion of 3AB2H to its equivalent decimal
3AB2H
15026
Exercise 6
Using the converion tables in Appendix B, convert FBH and A3B2H
to decimal and 142 and 9467 to hexadecimal.
with the leading zero removed and the two sets of binary digits
joined together.
11 1110 0111
3 E 7
Exercise 7
Convert 9AB3H to binary and 110011101111B to he xadec imal.
Appendi x A: Binary and Hexadecimal Numb er Systems 97
Exercise 8
Convert 1290 to binary and 101110111101B to decimal using both
suggested methods.
A.7 BYTES
a character,
a number (unsigned),
or a signed number.
Exercise 9
What range of unsigned numbers can be represented in two bytes
(that is, 16 bits)?
+5 is 00000101B
so -5 is 11111010B
+ 1
--------
11111011B
--------
Hence, -5 is held as 11111011B in a register or memory byte. The
mechanism for producing a negative number in 2's complement form
is equivalent to subtracting the equivalent positive value from
2.
Exercise 10
Calculate the binary equivalent of -1, -2 and -126 and the
decimal equivalent of 10000000B and 10000001B, assuming an 8-bit
2's complement system.
00000101B +5
+ 11111011B + -5
[I] OOOOOOOOB 0
The one carry out of the addition of the eight two bits is
ignored and the result is contained in the 8 bits - that is,
zero, which you would expect to obtain when adding -5 to +5.
Exercise 11
Calculate -60 + 70, - 23 + -46, 85 - 96 and 5 - -121, in binary
using an 8-bit 2's complement system.
-128 10000000
-127 10000001
-2 11111110
-1 11111111
0 00000000
+1 00000001
+2 00000010
+127 01111111
Append ix A: Binary and Hexadecimal Number Systems 99
Exercise answers
2 0 and 1.
4 C7BAH OllOllOlB
- 9FF8H + 01011110B
27C2H 1l001011B
5 62EH
100 Programming in Z80 Assembly Language
10 -1 is equivalent to 11111111B
-2 is equivalent to 11111110B
-126 is equivalent to 10000010B
10000000B is equivalent to -128
10000001B is equivalent to -127
11 11000100 -60
+ 01000110 + +70
11101001 -23
+ 11010010 + -46
[1) 10111011 -69
01010101 +85
- 01100000 - +96
--------
[ 1) 11110101 -11
--------
00000101 +5
- --------
10000111 --121
[ 1) 01111110 +126
--------
Appendix B: Hexadecimal-Decimal
Conversion Tables
o 234 5 6 7 8 9 ABC D E F
00 000 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015
10 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031
20 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047
30 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063
40 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079
50 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095
60 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 III
70 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
80 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
90 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
AO 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
BO 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
CO 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
DO 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
EO 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
FO 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
101
102 Programming in Z80 Assembly Language
Hexadecimal Decimal
100 256
200 512
300 768
400 1024
500 1280
600 1536
700 1792
800 2048
900 2304
AOO 2560
BOO 2816
COO 3072
DOO 3328
EOO 3584
FOO 3840
1000 4096
2000 8192
3000 12288
4000 16384
5000 20480
6000 24576
7000 28672
8000 32768
9000 36864
AOOO 40960
BOOO 45056
COOO 49152
DOOO 53248
EOOO 57344
FOOO 61440
Appendix C: Summary of zao
Instructions
103
104 Programm ing in Z80 Assembly Language
.
sult into packed nco format following addition or subt raction using operands with packed BCD form at.
I The flag is affected according to the result of the operation.
The flag is unch anged by the operation.
o The flag is reset by the operatio n.
1 Th e flag is set by the operation.
X The flag is a " do n' t care."
V PfV flag affected accordi ng to the overflow result of the operation.
P P/V flag affected according to the parity result of the operation.
r Anyone of the CPU registers A, B, C, D. E, H. L.
S Any a·b it location for all the addressing mode s allowed for the particular instru ction. ,
"IRI Any 16-bit locati on for all the addressing modes allowed for that instruction.
Any one of the two index registers IX or IV.
Refresh count er,
n 8-bitvalue in range <0, 255>
no 16-bitvalue in range<D. 65535>
m Any 8- bit location for all the addressing modes allowed for the partic ular instructio n.
Flap OP-Code No . No . No .
Sy mbolic or or M err
Mn emonic Ope ratio n C Z P/V S N H 76 54 3 210 By tes Cycl es Cycles Com ments
LO r. r'
LD r.n
r _ r'
r -n .0 0
0
0
0
0
0
0
0
0 01 r
00 r
·-
r'
110
I
2
I
2
4
7
r,
000
~ R"l!.
B
-
.·
n 001 C
LD r, (HL) r-( HL) 0 0 0 0 0 0 01 r 110 I 2 7 010 D
LD r, (IX+d) r- (IX+d) 0 0 0 0 II 0 11 10 1 3 5 19 011 E
-
01 r 110 100 H
LD r, ( IY+d ) r - (IY+d ) 0 0 0 0
d
0 0
-
II III 101 3 5 19
10 1
III
L
A
-
0 1 r 1 10
LD (HL), r (HL)- r 0 0 0 0 0 0
- d
0 1 110 r 1 2 7
LD (IX+d), r (IX+d )- r 0 0 0 0 0 0 II 0 11 101 3 5 19
-
01 110 r
LD (IY+d), r (IY+d) -r 0 0 0 0 0 0
d
I I II I 10 1
- 3 5 19
-
0 1 110 r
- d
-
LD (HL), n (HL)- n 0 0 0 0 0 0 00 110 110 2 3 10
LD (IX+d), n (IX+d ) - n 0 0 0 0 0 0
- n
I I 0 11 101 4 5 19
00 110 110
-- -
d
LD (IY+d). n (IY+d) - n 0 0 0 0 0 0
n
II I I I
-
10 1 4 5 19
--
00 110
d
n
110
-
-
LD A, (DC) A-( BC) 0 0 0 0 0 0 00 00 1 010 I 2 7
LD A, (DE) A - (DE) 0 0 0 0 0 0 00 0 11 0 10 I 2 7
-- --
LD A. (nn ) A -(nn ) 0 0 0 0 0 0 00 III 0 10 3 4 13
n
n
LD (BC), A (BCl - A 0 0 0 0 0 0 00 000 0 10 I 2 7
LD (DE), A (DEj-A 0 0 0 00 0 10 010
0 0 0 I 2 7
LD Inn), A l nn) -A 0 0 0 0 0 0 001 100 10 3 4 13
n - -
LD A, I A- I 0
n
I IFf' I 0 0 II 101 101
- - 2 2 9
LD A,R A -R 0
I 01 010 III
I IFF I 0 0 II 10 1 10 1 2 2 9
·
01 0 11 II I
.
LDI ,A I -A 0 0 0 0 0 II 101 101 2 2 9
01 000 III
LD R, A R- A 0 0 0 0 0 II 101 101 2 2 9
01 00 1 III
fla B Notation : • = nag no t affec ted , 0 = flag reset, I = n ag set, X = nag is unknown,
· ·· ··· --
C .Pt" S N H 76 54) 110
...
MnftMH&ic Opcraltc. 1)'leli ",<I.. Sbl. Commeatl
--
t Odd.,nn dd ~nn 00 ddO 00 1 3 3 10 PIliI
n 00 Be
· ·· ··· -
n 01 DE
to IX,nn IX - nn II 011 10 1 • • " '0 HL
--
00 100 001 11 SP
······ --
n
n
LO IY,n n I Y-nn 11 III 101 • • "
--
00 100 001
·· ··· · --
n
n
--
LOU L ,tnn) H - (n n+ l ) 00 101 010 3 S I.
· · ·· ·· --
l-(nn) n
n
t o dd, tnn ) dd H"" (nn +l )
dd ....(nn )
I I 101 101
0 1 ddl 01 1
• • '0
-
L
···· ·· -- -
n
n
to IX. ( fi n ) IX - (nn+l)
H
IX - ( n n )
II 01 1 101
00 101 010
• • '0
-
L
-
· ·· ··· -
n
n
to IV, (n n ) IV - (nn+ 1)
H II I I I tO I • • '0
-- --
IYl - (n n ) 00 101 0 10
n
··· ··· -- -
n
lO (nn ) , HL (nn+ l) .... H 00 100 010 3 S I.
-
{nnl - l n
·· ····
n
t o (nn),d d IM+I)-dd
{nn ) .... dd
H
11 101 10 1
01 ddO 0 11
• • 20
- -
L
- -
n
·· ··· ·
n
LO Inn), IX lnn+ l ) - I X
H
11 011 101 • • '0
- -
(nn) -IX 00 t OO 010
L
· · ···· -
n
-
·
n
lD(n n}, IY (M +D -IV
H
II I II 101 6 '0
- --
I MI -IY 00 100 0 10
-
L
n
······
n
, ,
······
t o SP. HL SP - HL 11 III 001 I I s
i o SP, IX SP-IX 11 011 ) 01 10
, ,
··· · ··
11 111 00 1
t o SP, IV SP- IV II III 101 10
······
11 111 00 1 qq Poi,
PUSH qq (SP- 2) - qql It qqO 101 I 3 11 00 Be
,
······
(SP-I ) - qqH 0' DE
PUSH IX (5P- 2) - IX II 01 1 101 4 IS 10 HL
,
L
······
(5P-1) - I X II 100 10 1 II AF
H
PUSH IY (SP- 21 -I Y II III 10 1 4 IS
L
······
151-I )-I Y II 100 10 1
H
POP qq q qH - (SP+ I) II qqO 001 I J 10
,
······
qq l - (SP)
pop IX IX -~SP + 11 II 011 ' 0\ 4 14
H
,
·· · · ··
IX - (SP)
l
II 100 OOL
POPIY IY -(S'+! )
H
IY -(SP)
II 111 101
11 100001
• "
L
Ftaa No ta nce: • c flaa nat aff ect ed , 0 c fll a reset , I "fla, set . X • flaa is unk nown ,
t flaa is af fec ted acco rd ina t o the rescn o r the o pe ratio n.
Fla gs Op.code
No. No. No .
Symb oli c ~ 01 ol M 01T
·· ·· ·· ·· ··· ···
····
Mnem o n ic Ope ration C Z V S N II 76 543 21 0 B y t~$ Cycles Sta tes Commen ts
EX DE . ilL DE · · II L II 10 1 0 1 1 I I 4
EX AF . A F' AF · · AF' 00 00 1 0 00 I I 4
EXX
(Rv{}iD I I 0 11 0 0 1 I I 4 Regis te r bank an d
au x iliary regist er
·· ····
ban k ex ch ange
EX ISP), II L 11- (SP + I) II 100 0 11 I 5 19
· · ·· · ·
1 - (SPI
EX (S P), IX IX - (SP+ II II 0 1 1 101 2 6 23
II
· · · ···
IX ... (Sll) II 100 Oil
L
I· X ,S PI, I" IY Il ~ · ( SP+ II II I I I 10 1 2 6 23
IY - (S I') II 1110 01 1
L
·· ·
Q)
W I ( I.)F)- I I Il.1 I II 0 I I 10 1 10 1 2 4 16 Load ( H L) in to
(DE), incremen t the
UE - DE. I III 100 000
pointer s.and
Ill ..· II L+ I decrement the by te
Be ..· IH.·· 1 counter UK )
He - HC· l
Rq ' l'i1t unl it
Il l" '" U
·· ·
Q)
I DO IUI-I ..- tl lL l I II 0 I I ttl I 10 1 2 4 16
Ill · · 1"' · 1 III 101 000
II l · · II I.·1
Bl ' " HC- )
HC - 8 (' - 1
·
C) I
CPH< ,, - UIl ) I I I I I I I 10 1 10 1 2 5 21 If BC -j:. 0 and A ,. (H L)
In - B lot' 10 110 00 1 2 4 16 If 8 (' = 0 or A =(il L)
Il l " 0 - U<,'·I
RI,.'p ,·.)1 un nl
,\ := (l ll . l m
He = II
·
C)IQ;
(' I' D A - (111., I I 1 I I I I 10 1 10 1 ! 4 16
II I -· 11\ · 1 10 10 1 0 01
B(' - 0 (' · 1
I~
·
I
CPOH A - (111) I I I I I II 101 10 1 2 5 21 IfBC " 0 and A .. (II L )
II l .. II L· 1 10 III 00 1 2 4 16 If 8 C = 0 or A =(ilL)
B(' - B(' · I
Rep eat 11111,1
A =lULl o r
0(' =0
No tes: CD p." fla g i ~ U if the resu lt of H<.'-1 =O. o the rwise P/V = I
Q) I fl a ~ I .. 1 if A =lllll . o the rwise Z =(I .
H iI~ No ta tion : _ = l1 a ~ ne t af fec ted . (l = 11:lg re set . I : flag \CI, X '= tlag 1\ u nkn ow n,
J ;:. n at! i.. a fft= ~'h: d acco rdin g to th e rc..u lt o f the ope ra tio n.
r lags O p-Cod e
No . No . No.
Sy mbo lic ~ of of M ofT
Mnemo nic Op era lio n C Z V S N H 76 543 2 10 Byt es Cy cles Sta les Com me nts
- IQQQJ -
ADD A, n A - A +n I I V I 0 I II IQQQ] 110 1 1 7
00 1 C
n 0 10 D
ADD A,(HL) A · A+ (HL) I I V I 0 I 10 010 1 2 7 0 11 E
ADD A,(IX+d) A-A + (lX+d) II 0 11 101 3 5 19 100 1/
I I V I 0 I
101 L
IQQQ]
I
10 110
-
II I A
d -
ADD A,(IY+d) A -A +(JY+ d ) I I V I 0 I 11 I I I 10 1 3 5 19
10IQQQ] 1 10
·
CIlL) - (HL)+ I I V I 0 1 3 II
I NC (I X+d J n X +d ) ... I V I 0 I J 1 0 1 1 101 J b 2.1
(I X + d )+l
UU 11n[@ill
·
d
I NC HY +d) HY+d ) - I V I 0 I II III WI ] b 23
lIY "d ) + I
on ' I I U lTIE!l
- '.
·
d
DEC m m.. m-l ; V I I I lTIill m is any of r, (HL ),
f lX+d) .lIY +dIJ \
sno wn for INC
Same f orm al .1l1lJ
\ I;l t~·\ a\ I N C
K,' p l.I" " 100 wi th
111 1 III UP dKh:
NOles: Th e V sy mbol in urc P/ V O••g c o lumn indrca t c-, th at th e It V na~ co n t arn \ th e ove r flow ot thc rv...ul t 01 th.;
opera tion Simil ar ly t he P sym b o l ind u-atc x pan rv. V =: J mcunv ove rflow . V =0 mean ...llll l l l\' l ' r ll i llol. P =:I
mean s pa rit y of th e re..u lt is eve n, p ..: 0 rncauv p ant y o t th c rcvul t ,...odd ,
FlaS' Op-{:o<Ie
No. No. No.
Symbolic ~ of ofM ofT
Comments
Mn emonic Operation C Z V S N H 76 543 210 Byte s Cycl es Sta tes
· · ··
BCD operands
CPL A-A I I 00 101 III I 1 4 Complement
accumulator
(one 's complemen t)
NEG A-O -A I I V I I I I I WI 10 1 2 2 8 Negate ace. <two 's
comple me nt)
···
0 1 000 100
CCF Cy - a I 0 X 00 III I II I I 4 Complement carry
·· ····
flag
4 Set carry flag
· ··
CY - I I 0 0 00 110 III I I
··
SCF
4
· · ····
NOP No ope ration 00 000 00 0 I 1
······
HALT CPU ha lted
4
· ······
DI IFF-O II 110 OIl I I
·· · ···
mode 0 01 000 110
IMI Set interrupt II 101 10 1 2 2 8
· ·· · ··
mode 1 01 010 110
1M2 Set interrupt II 101 101 2 2 8
mode2 0 1 OI l 110
Flll Notation: • = flag no t affected. 0 = flag reset . 1 = flag set, X = flag is unkno wn.
*= flag is affec ted according to the result of the operation .
···
Z I" S
ADDH l , " HL - HL+ss I 0 X 00 ..1 001 I 3 II II Rei ·
00 DC
01 DE
ADCH l , .. HL-HL+ $I +CY t I V I 0 X II 101 101 2 4 15
10 Hl
01 ..1 010 II SP
s ac Hl , ,, Hl-H l· .. -cv I I V I I X 11 101 101 2 4 15
· ··
01 ..0 010
AD D IX, PP IX -IX + PP I 0 X 11 0 11 101 2 4 15 PP Reg.
00 pp l 001 00 DC
01 DE
10 IX
·· ·
II SP
AD D I Y.n I Y-IY+rr I 0 X I I III 101 2 4 15 rr Reg.
00 rr1 001 00 IlC
01 IJE
10 IY
II SP
INC .. SS ... IS + I
·· · · · · 00 ssO 0 1 I I I b
INC IX IX - IX + I
······ II 0 11 10 1 2 2 10
·· · ···
0010001 1
INC IY IV -IY + 1 II III 101 2 2 10
······
0010001 1
DEC ss 55'" 5S- ) 00 ..1 0 11 , I I 6
IJEC IX IX - IX · I
··· ··· I I 011 10 1 2 2 10
······
00 101 011
DECIY IY- IY ·I II III 101 2 2 10
00 101 011
folll No tatio n: . :: l1a~ net ..tf ccted , 0:: I1lg reset , 1 :: nag set , X = flag 1'\ unknown .
t = fhg i.. aff ect ed acco rding to the resul t of the opera tion.
Flags Op-Code
No. No. No.
Symbolic ~ 01 aIM olT
···
Mnemonic Operation C Z V S N H 76 543 210 Bytes Cycles States Comments
RLCA I 0 0 00 000 III 1 I 4 Rotate left circular
~ accumulator
RLA
~ I
·· · 0 0 00 010 III I I 4 Rotate left
accumulator
RRCA
~ I
··· 0 0 00 00 1 I II I I 4 Rotate right circular
accumulator
RRA
~ I
·· · 0 0 0001 1 III 1 1 4 Ro tate right
accum ulator
- -
•. \ HU . lI l1;....' . fIY••l!
11 001 0 11 011 E
d 100 H
00 [QQQ] '10 101 L
111 A
RLC (IY+d) I I P I 0 0 11 111 101 4 6 23
- -
11 001 011
d
00 ~ 1l 0
RL m
~ m '! • • IHLl . IIX"" I. lI YOdl
I I P I 0 0 [ill]] Instruction format an
states are as shown
for RLC,m. To form
§ OP-code replace
RRC m ~m < r,(H l I.lI X. c1I, Il Yodl
I I P I 0 0 lQill of RLC,m with
shown co de
RR m ~ m ~ r. f H LI . (I X . 4 1 . I I Y " 1
I I P I 0 0 (QITJ
SLA m ~o I I P I 0 0 [iN]
. " I. l HLI.I IX"<l'. llY · c1,
SRLm ~
" ",. I HLI, f IX· c1 1. / IY. "
I I P I 0 0 lIITl
.
01 101 III right between the
accumulator
and location (HL).
RRD
'~'"U I p I 0 0 11 101 101
0 1 100 III
2 5 18 The conten t of the
upper half of the
accumulator is
unaffected
All Notation : -,. tlag not affected, 0 = Oag reset, 1 = Oagset, X = flag is unknown.
; = nag is affected according to the result of the operation.
Flap Op{"ode
1"0. No. No.
Symbolic: ~ oC
Bytes
orM
Cycles
oCT
·
Mnemonic Opention C Z V S N H 76 543 210 States Comments
·
01 b r 000 B
BITb,lHLl Z-IHL\. I X X 0 I 11 001 011 2 3 12 001 C
010 D
·
01 b 110 011 E
BITb, (IX+d) Z-(iX+djb I X X 0 I 11 011 101 • 5 20 100 H
- -
11 001 011 101 L
111 A
d
·
01 b 110 b Bi.Teatecl
BIT b, (IY+d1 Z -IIY+dlb I X X 0 I 11 111 101 4 5 20 000 0
- -
11 001 011 001 I
010 2
d 011 J
01 b 110 100 4
IOJ S
110 6
·· · ·· ·
111 7
SETb" 11 001 011 2 2 8
" -I
···· ··
IITIb r
SETb, (HLI IHLlb -I 11 001 011 2 4 15
· ·. ·· ·
(j]b 110
SETb,IIX+dl (IX+d)b - I II 011 101 4 6 23
- -
II 001 011
d
·· ··· ·
(j]b 110
SETb. (IY+dl IIY+dl b - I 11 III 101 4 6 23
- -
11 001 011
d
IIiJb 110
FIaa Notation : • = n., not affected. 0 • flag reset . I ... flag ~ 1. X • flag is unknown.
* = flag i' affected according to the result or Ihe operation .
Flags Op-Code
No. No. No.
Symbo lic: ~ of ofM ofT
Mnemonic Operation C Z VS N H 76 543 210 Bytes Cycles Stales Comments
If nn PC-nn
······ - - II 000 OIl
n
3 3 10
- -
· ··· · · .- -
n cc Condi tion
If cc, nn If conditio n cc I I cc 0 10 3 3 10 000 N Z non zero
is true PC -enn , 001 Z zero
-
n
o therwise
continue - n
010
Oil
100
Nen oo carry
C carry
PO par it y odd
101 PE parity even
····· · -
110 P sign pc suwe
JR e PC -PC+e 00 01 1 000 2 3 12 III M sign negative
-
······ -
<-2
JR C, ' If C " O. 00 III 000 2 2 7 If co ndition n ot met
continue
IfC: I ,
.-2 - 2 3 12 If condit ion is met
···· · · -
PC ..... PC+e
JR NC•• IfC: I. 00 110 000 2 2 7 If c o nd itio n not mel
cont inue
If C: 0,
.-2 - 2 3 12 If cc nduion j.. met
· · ·· ·· -
PC -PC+ e
JR Z.< If Z: 0 00 101 000 2 2 7 I f condit io n not met
con tinue
IfZ = I.
<-2 - 2 3 12 If co ndition h me t
· · ·· ·· -
PC ....PC+e
JR NZ, < If Z = I. 00 100 000 2 2 7 If co nditi o n n OI m,
cont inue
IfZ: O.
<-2 - 2 3 12 If con di tio n me l
PC ....P('+e
l P (HL) ('( '-HL
······ II 101 00 1 I I 4
JPOX) ('('-IX
······ II 0 11 101 2 2 8
·· · · · ·
II 10 1 00 1
JPO Yl PC-IY II III 101 2 2 8
II 101 001
DlNZ.e 0-0-1
If 0 =0,
continue
······ - 00 010 000
<-2 -
2 2 8 Ir B =0
If 0 "0, 2 3 13 IF B _ 0
PC-PC+e
Notes: e represen ts the exten sion in the relative add ressing mode .
e is a signed two's complement number in the range <-126. 129>
e-2 in the op-code provides an effective address of pc +e as PC is
increment ed by 2 prior to the additio n of e.
Flag Notation : - = flag not affe cted, 0 = flag reset , t :: flag set, X = flag is unknown .
S = flag is affec ted acco rding to the result of the operation.
Jump group
Table C.l O Co urtesy Zilog . Inc.
114 Programming in zao Assembly Language
Flap OP<:OO.
No. No. No.
Symbolic: ~ ol olM ofT
Mnemonic Opencion C Z V S N H 76 543 210
···· · · - -
Byt.. Cycles States Comments
CAll nn (SP.IH'C 11 001 101 3 5 17
H
-" -
(SP.2)-PC n
l
PC-nn
-" -
cc is false
continue .
otherwise 3 S 17 If cc is true
sameas
· ·· ·· ·
CAll nn
RET PC -(SP) 11 001 001 I 3 10
l
PCH-{SP+I)
RETee If condition
cc is false
continue ,
otherwise
·· ·· ·· 11 ee 000 1
1 3
1 5
11
If cc is false
If cc is true
sameas ee Condition
RET 000 NZ non zero
····· ·
001 Z zero
010 NC non carry
RETI Return from 11 101 101 2 4 14 011 C carry
interrupt
······
01 001 101 100 PO parity odd
RETN Return from 101 PE parity even
11 101 101 2 4 14 110 P
non maskable sign positive
· ··· · ·
interrupt 01 000 101 111 M sign negative
RSTp (SP.I)-PC 11 t 111 1 3 11
H
(SP-2)-PC l
PCH-O
PCl -P
t P
000 OOH
001 08H
010 IOH
011 18H
100 20H
101 28H
110 30H
111 38H
fla. Notation: • = flag not affected, 0 =flag reset. I = nag set, X = nag is unknown
S = flag is affected according to the result of the operation.
FollgS Op-Code
No. No. No.
Sy mbo lic: ~ 01 ol M ofT
·· ·· · · - -
Mnemonic Opera tio n C Z V S N H 76 543 210 Byt es Cycles Stales Co m me nts
·
n Ace to AS - A 1S
IN , , (e) , - ( C) I P I 0 I I I 101 101 2 3 12 Cto A - A
ifr = 110 only , 01 000
O 7
BtoA - A I S
S
the flags will
be affected
'\'
·
'-"
INI (HL) -(C) I X X 1 X I I 101 101 2 4 16 C to A - A7
O
8 - B· 1 10 100 0 10 BtoAS- A
IS
·
HL-HL+ 1
INIR (HL)- (C) 1 X X 1 X 11 101 101 2 5 21 Ct OAO - ~
B-B ·1 10 110 010 (118"'0)
8 '0 AS - Al 5
HL - HL + 1 2 4 16
Repeat until (118 = 0)
8 =0
G::
IND (HL) -(C)
B- B -1 · I X X I X I I 101 10 1
10 101 010
2 4 16 C to A - A
O 7
B to AS - A I S
·
HL-HL - 1
INDR (HL) -(C) I X X I X 11 101 101 2 5 21 Ctu A - A
O 7
8 - 8-1 10 II I 010 (lfB "'0) 8 to AS - A I S
HL -HL -I
2 4 16
Repeal until (lfB = 0)
8=0
Otf T (n), A (n) _ A
· · ···· 11 0100 11 2 3 II n to AU - A
7
· · · ···
. - n -+ Ace to AS - A l 5
OUT( C),r (C)-, 11 101 10 1 2 3 12 C ta A .... A,
O
0 1 r 001 B '0 AS - A 1S
CD
OUTI (C)-( HL)
8 -B-1 · I X X I X 11 101 101
10 100 OI l
2 4 16 (' to AU "" A,
B to As .... A
1S
·
HL - HL + I
OTIR (C) -( HL) I X X 1 X 11 101 101 2 5 21 ( '1 0 AU .... A
7
8 - B- 1 10 110 0 11 (If 8'" 0) 8 t o As "" A
1S
HL - HL + I
2 4 16
Repeat until (If 8 = 0)
8- 0
CD
OUTD (C) - IHL)
8 - 8 -1 · I X X I X I I 101 101
10 10 1 Oil
2 4 16 Chi AO- A,
8 (0 AS - A
1S
·
HL - HL - I
OTDR (C) - IHL) I X X I X 11 101 10 1 2 5 21 c ro AU - A7
8-8 -1 10 111 0 11 (If 8 '" 0) 8 t o A g .... A
15
HL-H L -I
2 4 16
Repeat until (l f B = 0)
8=0
No tes: CD If the result o f B-1 is zero the Z flag is set . o therwise it is reset .
Flag Not ation: • = nag not affe cted , 0 = flag reset, 1 "" flag set , X = nag is unknown .
t "" nag is affe cted according to the result of the operation.
b7 --+ 0 0 0 0 I I I I
b6 --+ 0 0 I I 0 0 I I
b5 --+ 0 I 0 I 0 I 0 I
b b b
4 3 2 I b~+ 0 I 2 3 4 5 6 7
,
0 0 00 0 NUL OLE SP 0 @ P p
0 0 01 I SOH DCI I I A Q a q
II
0 0 10 2 STX DC2 2 B R b r
0 0 1I 3 ETX DC3 ~ 3 C S C 5
0 10 0 4 EOT DC4 $ 4 0 T d t
0 I I 0 6 ACK SYN a 6 F V f v
I
0 I 1 I 7 BEL ETB 7 G W 9 w
10 0 0 8 BS CAN ( 8 H X h x
10 01 9 HT EM ) 9 I Y i y
··
10 10 10
10 1 I II
LF SUB
VT ESC *+ ·,
J
K
Z
[
j
k
z
{
I 10 0 12 FF FS , < L <, I I
I
I 101 13 CR GS - - M ] m }
I I I 0 14 so RS . > N " n rv
I I I I 15 Sl us / ? 0 - 0 DEL
116
Appendix E: Expression Operators
OPERATOR FUNCTION
+ Unary plus
Unary minus
.NOT. or \ Logical NOT
.RES. Result
** Exponentiation
*/ Multiplication
Division
.MOD. Modulo
.SHR. Logical shift right
.SHL. Logical shift left
+ Addition
Subtraction
.AND. or & Logical AND
.OR. or A Logical OR
.XOR. Logical XOR
.EQ. or Equals
.GT. or > Greater than (signed)
.LT. or < Less than (signed)
.UGT. Unsigned greater than
.ULT. Unsigned less than
.MOD.B = A-B*(A/B)
where the A/B is an integer division.
The five comparison operators (.EQ., . GT. , .LT., .UGT. and . ULT. )
evaluate to logical TRUE (all ones) if the comparison is true,
and a logical FALSE (zero) otherwise.
117
Index
118
Index 119