Z80 Assembly Language Programming 1979 Leventhal PDF
Z80 Assembly Language Programming 1979 Leventhal PDF
ASSEmBLY lAnGUAGE
PROGRAmminG
Z80
ASSEmBLY LAnGUAGE
PROGRAmminG
Lance A. Leventhal
Osborne/McGraw-Hili
Berkeley, California
Published by
OSBORNE/McGraw-Hili
630 Bancroft Way
Berkeley, California 94710
U.S.A
For information on translations and book distributors outside of the U. S. A. ,
please contact the publisher at the above address.
56 789
DODO
8 76 54 3 2
All rights reserved. Printed in the United States of America. No part of this
publication may be reproduced. stored in any retrieval system, or transmitted
in any form or by any means, electronic, mechanical, photocopying, recording
or otherwise, without the prior written permission of the publishers.
Cover design by K. L. T. van Genderen.
This book is dedicated to my colleagues at the Society for Computer Simulation - Romeo Favreau. Natalie Fowler. Alexander McKenna. John McLeod.
Stanley Rogers. and Chip Stockton.
ACKNOWLEDGMENTS
The author would like to acknowledge the following people:
Mr. Curt Ingraham, Ms. Mary Borchers, and Ms. Janice Enger of Osborne/
McGraw-Hili, who made many corrections and suggestions; Mr. Winthrop
Saville of Sorrento Valley Associates, who provided assistance and examples; Mr. Tom Littlefield of Littlefield/Smith Associates, who provided
reference material; Ms. Marielle Carter of Sorrento Valley Associates, who
typed some of the material; Mr. Stanley Rogers of the Society for Computer
Simulation, who has continued to suggest improvements in the author's writing style; and his wife Donna. for her patience and understanding throughout
the writing of this book.
Others who provided assistance and suggestions were Mr. Colin Walsh, Mr.
Gary Hankins, Mr. Romeo Favreau, Mr. David Bulman, Ms. Kati Bulman. Mr.
Robert Turner, Mr. Irv Stafford, Mr. John Burgar, Mr. Ferenc Montvai-Lako,
and Mr. Warren McKenna. Other students and colleagues also helped to keep
the author on the right track.
The author, of course, bears responsibility for any remaining errors, misconceptions, and misinterpretations.
Contents
Chapter
Page
Introduction to Assembly Language Programming
How This Book Has Been Printed
The Meaning of Instructions
A Computer Program
The Programming Problem
USing Octal or Hexadecimal
Instruction Code Mnemonics
The Assembler Program
Additional Features of Assemblers
Disadvantages of Assembly Language
High-level Languages
Advantages of High-level Languages
Disadvantages of High-level Languages
High-level Languages for Microprocessors
Which Level Should You Use 7
How About the Future?
Why This Book?
References
1-1
1-1
1-1
1-1
1-2
1-3
1-4
1-5
1-6
1-6
1-7
1-7
1-8
1-9
1-10
1-11
1-11
1-12
Assemblers
Features of Assemblers
Assembler Instructions
Labels
Assembler Operation Codes (Mnemonics)
Pseudo-operations
The Data Pseudo-operation
The Equate (or Define) Pseudo-operation
The Origin Pseudo-operation
The Reserve Pseudo-operation
linking Pseudo-operations
Housekeeping Pseudo-operations
Labels with Pseudo-operations
Addresses and the Operand Field
Conditional Assembly
Macros
Comments
Types of Assemblers
Errors
Loaders
References
2-1
vii
2-1
2-1
2-2
2-4
2-4
2-5
2-6
2-7
2-7
2-8
2-8
2-9
2-9
2-11
2-11
2-13
2-14
2-14
2-15
2-15
Contents (Continued)
Page
Chapter
3-1
3-2
3-4
3-5
3-7
3-8
3-10
3-11
3-12
3-13
3-14
3-15
3-18
3-21
3-21
3-21
3-21
3-43
3-164
3-170
3-170
3-170
3-170
3-170
3-171
3-172
3-172
3-174
3-174
Contents (Continued)
Chapter
Page
Simple Programs
General Format of Examples
GUidelines for Problems
Program Examples
Ones Complement
8-Bit Add iliOn
Shift Left One Bit
Mask Off Most Significant Four Bits
Clear a Memory Location
Word Disassembly
Find Larger of Two Numbers
16-Bit .Addition
Table of Squares
16-Bit Ones Complement
Problems
Twos Complement
8-Bit Su btractlon
Sh ift Left Two Bits
Mask Off Least Significant Four Bits
Set a Memory Location to All Ones
Word Assembly
Find Smaller of Two Numbers
24-Bit .Addition
Sum of Squares
16-Bit Twos Complement
4-1
4-1
4-2
5-1
IX
4-3
4-3
4-4
4-6
4-6
4-7
4-7
4-9
4-11
4-12
4-14
4-15
4-15
4-15
4-16
4-16
4-16
4-16
4-16
4-16
4-17
4-18
5-3
5-3
5-6
5-9
5-11
5-14
5-17
5-17
5-17
5-18
5-18
5-18
Contents (Continued)
Page
Chapter
Character-coded Data
Examples
Length of a String of Characters
Find First Non-blank Character
Replace Leading Zeros with Blanks
Add Even Parity to ASCII Characters
Pattern Match
Problems
Length of a TeletypeWriter Message
Find Last Non-blank Character
Truncate Decimal String to Integer Form
Check Even Parity In ASCII Characters
String Comparison
6-1
6-2
6-2
6-8
6-11
6-13
6-16
6-19
6-19
6-19
6-20
6-20
Code Conversion
Examples
Hex to ASCII
Decimal to Seven-Segment
ASCII to Decimal
BCD to Binary
Convert Binary Number to ASCII String
Problems
ASCII to Hex
Seven-Segment to Decimal
Decimal to ASCII
Binary to BCD
ASCII String to Binary Number
References
7-1
7-1
7-1
7-3
7-8
7-10
7-11
7-13
7-13
7-13
7-14
7-14
7-14
7-15
Arithmetic Problems
Examples
MUltiple-Precision Addition
Block Move
Decimal Addition
8-Bit Binary Multiplication
8-Bit Binary Division
Self-Checking Numbers Double and
Double MOD 10
Problems
Multiple-PrecIsion Subtraction
Decimal Subtraction
8-Bit by 16-Bit Binary MultiplicatIOn
Signed Binary Division
Self-Checking Numbers Aligned 1,3,7 MOD 10
References
8-1
8-1
8-1
8-4
8-5
8-8
8-12
6-21
8-17
8-25
8-25
8-25
8-26
8-26
8-27
8-28
Contents (Continued)
Chapter
Page
9-1
9-1
9-1
9-5
9-8
9-10
9-14
9-16
9-16
9-17
9-17
9-18
9-18
9-19
10
Subroutines
Subroutine Documentation
Examples
Hex to ASCII
Length of a String of Characters
Add Even Panty to ASCII Characters
Pattern Match
Multiple-PrecIsion Addition
Problems
ASCII to Hex
Length of an ASCII Message
Check Even Panty In ASCII Characters
Stnng Companson
Decimal Subtraction
References
10-1
10-2
10-2
10-3
10-6
10-9
10-12
10-16
10-19
10-19
10-19
10-19
10-20
10-20
10-22
11
Input/Output
Timing Intervals (Delays)
Delay Routines
Example
Delay Program USing Accumulators
Simple I/O Devices
The Z80 Parallel Input/Output CirCUit (PIO)
PIO Mode Control
Configunng the PIO
Z80 Input/Output InstructIOns
Examples
A Pushbutton SWitch
A Toggle SWitch
A Multiple-Position (Rotary. Selector. or
Thumbwheel) SWitch
A Single LED
Seven-Segment LED Display
11-1
11-8
11-8
11-9
11-9
11-11
11-11
11-15
11-17
11-18
11-22
11-22
11-28
xi
11-33
11-40
11-43
Contents (Continued)
Page
Chapter
11 (Cont.)
12
Problems
An on-off Pushbutton
Debouncing a Switch In Software
Control for a Rotary Switch
Record Switch Positions on lights
Count on a Seven-Segment Display
More Complex I/O Devices
Examples
An Unencoded Kevboard
An Encoded Keyboard
A Digital-to-Analog Converter
Analog-to-Digital Converter
A Teletypewriter (TIY)
The ZSO Serial Input/Output Device (SIO)
Examples
Teletypewriter I/O via a USART
Standard Interfaces
Problems
Separating Closures from an Unencoded
Keyboard
Read a Sentence from an Encoded Keyboard
A Variable Amplitude Square Wave Generator
Averaging Analog Readings
A 30 Character-per-Second Terminal
References
Interrupts
ZSO Interrupt Svstem
Non-Maskable Interrupt
ZSO Interrupt Modes
ZSO/SOSO Interrupt Compatibility
PIO Interrupts
Examples
SIO Interrupts
Interrupt Examples
A Startup Interrupt
A Keyboard Interrupt
A Printer Interrupt
A Real-Time Clock Interrupt
A TeletypeWriter Interrupt
More General Service Routines
Problems
A Test Interrupt
A Keyboard Interrupt
A Printer Interrupt
A Real-Time Clock Interrupt
A TeletypeWriter Interrupt
References
xii
11-55
11-55
11-55
11-55
11-56
11-56
11-57
11-60
11-60
11-69
11-72
11-76
11-Sl
11-S9
11-9S
11-9S
11-103
11-103
11-103
11-103
11-104
11-104
11-104
11-105
12-1
12-2
12-3
12-4
12-5
12-6
12-S
12-10
12-12
12-12
12-14
12-17
12-20
12-26
12-30
12-31
12-31
12-31
12-31
12-31
12-31
12-32
Contents (Continued)
Chapter
13
Page
xiii
13-1
13-1
13-3
13-3
13-4
13-4
13-5
13-5
13-6
13-6
13-6
13-8
13-11
13-15
13-16
13-17
13-19
13-19
13-20
13-22
13-26
13-28
13-28
13-28
13-28
13-30
13-30
13-36
13-36
13-36
13-38
13-43
13-44
13-45
13-45
13-46
13-47
13-49
13-49
13-50
Contents (Continued)
Page
Chapter
14
15
16
14-1
14-1
14-8
14-10
14-11
15-1
15-1
15-2
Sample Projects
Project :#:1: A Digital Stopwatch
Project :#:2: A Digital Thermometer
References
16-1
16-1
16-15
16-29
14-16
14-21
14-27
14-28
14-29
14-29
14-29
14-30
14-31
15-4
15-5
15-7
15-7
15-7
15-8
15-10
15-10
15-10
15-11
15-12
15-13
15-14
15-15
15-16
15-18
xv
xvii
xiv
Chapter 1
INTRODUCTION TO ASSEMBLY
LANGUAGE PROGRAMMING
This book describes assembly language programming. It assumes that you are
familiar with An Introduction To Microcom~uters: Volume 1 - Basic Conce~ts
(particularly Chapters 6 and 71. This book does not discuss the general features of
computers, microcomputers, addressing methods, or instruction sets; you should
refer to An Introduction To Microcomputers: Volume 1 for that information.
A COMPUTER PROGRAM
A program is a series of instructions that cause a computer to perform a particular
task.
Actually, a computer program Includes more than instructions; It
also contains the data and memory addresses that the
microprocessor needs to accomplish the task defined by the in1-1
Ultimately every program becomes translated into a set of binary numbers. For
example. this is the
program that adds the contents of memory locations
6016 and 6116 and places the result in memory location 6216:
zao
00111010
01100000
00000000
01000111
00111010
01100001
00000000
10000000
00110010
01100010
00000000
This is a machine language. or object. program. If this program
were entered into the memory of a ZBO-based microcomputer. the
microcomputer would be able to execute it directly.
OBJECT
PROGRAM
MACHINE
LANGUAGE
PROGRAM
1)
The programs are difficult to understand or debug (binary numbers all look the
same. particularly after YOU have looked at them for a few hours).
2)
The programs are slow to enter since you must enter each bit indiVidually.
3)
4)
The programs do not describe the task which YOU want the computer to perform in
anything resembling a human readable format.
The programs are long and tiresome to write.
6)
The programmer often makes careless errors that are very difficult to find.
For example. the following version of the addition object program contains a single
bit error. Try to find it:
00111010
01100000
00000000
01000111
01110010
01100001
00000000
10000000
00110010
01100010
00000000
1-2
Although the computer handles binary numbers with ease. people do not. People find
binary programs long. tiresome. confusing. and meaningless. Eventually. a programmer
may start remembering some of the binary codes. but such effort should be spent more
productively.
60
00
47
3A
61
00
80
32
62
00
At the very least. the hexadeCimal version IS shorter to write and not qUite so tiring to
examine.
Errors are somewhat easier to find in a sequence of hexadecimal digits. The erroneous version of the addition program. in hexadecimal form. becomes:
3A
60
00
47
72
61
00
80
32
62
00
The mistake is easier to spot.
What do we do with this hexadecimal program? The microprocessor understands
only binary instruction codes. The answer is that we must convert the hexadeCimal
numbers to binary numbers. ThiS conversion is a repetitive. tiresome task. People who
attempt It make all sorts of petty mistakes. such as looking at the wrong line. dropping a
bit. or transposing a bit or a digit.
This repetitive. grueling task is. however, a perfect Job for a comHEXADECIMAL
puter. The computer never gets tired or bored and never makes
LOADER
silly mistakes. The idea then is to write a program which takes
hexadecimal numbers and converts them into binary numbers. This is a standard
program provided with many microprocessors; it is called a "hexadecimal loader."
Is a hexadecimal loader worth having? If you are willing to write a program uSing binary
numbers, and you are prepared to enter the program In ItS binary form into the computer. then you will not need the hexadeCimal loader.
1-3
If you choose the hexadecimal loader. you will have to pay a price for It. The hexadeclmalloader is itself a program which you must load Into memory. Furthermore. the
hexadecimal loader will occupy memory - memory that you may want to use in some
other way.
The basIc tradeoff. therefore. IS the cost and memory requirements of the hexadecimal
loader versus the savings In programmer time.
A hexadeCimal loader is well worth its small cost.
A hexadecimal loader certainly does not solve every programmIng problem. The hexadecimal version of the program IS still difficult to read or understand; for example. it
does not distinguish Instructions from data or addresses. nor does the program listing
provide any suggestion as to what the program does. What does 32 or 47 or 3A mean?
Memorizing a card full of codes is hardly an appetizing proposition. Furthermore. the
codes will be entirely different for a different microprocessor. and the program will reqUire a large amount of documentation.
Table 1-1. Hexadecimal Conversion Table
Hexadecimal
Digit
Binary
Equivalent
Decimal
Equivalent
0
1
2
3
4
5
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
0
1
2
3
4
5
6
7
8
9
A
B
C
0
E
F
6
7
8
9
10
11
12
13
14
15
1-4
Along with the instruction mnemonics. the manufacturer will usually assign names to
the CPU registers. As with the instruction names. some register names are obvious (e.g..
A for Accumulator) while others may have only historical significance. Again. we will
use the manufacturer's suggestions simply to promote standardization.
If we use standard
defined by Zilog. our
LD
LD
LD
ADD
LD
A.(60H)
B.A
A.(61H)
A.B
(62Hl.A
r----..,
ASSEMBLY
LANGUAGE
PROGRAM
The program is still far from obvIous. but at least some parts are comprehensible.
ADD A.B is a considerable Improvement over 80: LD does suggest loading data into a
register or memory location. Such a program is an assembly language program.
A.(NNl
B.A
A.B
(NNl.A
Hexadecimal Equivalent
3A
47
80
32
As in the case of hexadecimal to binary conversion. hand assembly IS a rote task which
is uninteresting. repetitive. and sublect to numerous minor errors. Picking the wrong
line. transposing digits. omitting instructions. and misreading the codes are only a few
of the mistakes that you may make. Most microprocessors complicate the task even
further by haVing instructions With different word lengths. Some instructions are one
word long while others are two or three words long. Some instructions require data in
the second and third words: others require memory addresses. register numbers. or
who knows what?
ASSEMBLER
SOURCE
PROGRAM
OBJECT
PROGRAM
program.
The tradeoffs we discussed in connection with the hexadecimal loader are magnified in the case of the assembler. Assemblers are more expensive. occupy more
memory. and require more peripherals and execution time than do hexadecimal
loaders. While users may (and often dol write their own loaders. few care to write their
own assemblers.
1-5
Assemblers have their own rules that you must learn to abide by. These include the
use of certain markers (such as spaces. commas. semicolons. or colons) In appropriate
places. correct spelling. the proper control information. and perhaps even the correct
placement of names and numbers. These rules typically are a minor hindrance that can
be quickly overcome.
3)
4)
5)
6)
7)
AllOWing the user to assign names to memory locations. Input and output deVices.
and even sequences of Instructions.
Converting data or addresses from various number systems (e.g.. decimal or hexadecimal) to binary and converting characters into their ASCII or EBCDIC binary
codes.
Performing some arithmetic as part.of the assembly process.
Telling the loader program where in memory parts of the program or data should be
placed.
Allowing the user to assign areas of memory as temporary data storage and to
place fixed data in areas of program memOry.
Providing the Information reqUired to include standard programs from program libraries. or programs written at some other time. in the current program.
Allowing the user to control the format of the program listing and the Input and
output devices employed.
zao
1-6
not run on the Motorola 6800. the Fairchild F8. or the National Semiconductor PACE.
For example. the addition program written for the Motorola 6800 would be:
LDAA
ADDA
STAA
$60
$61
$62
The lack of portability not only means that you won't be able to use your assembly
language program on another microcomputer. but It also means that you won't be able
to use any programs that weren't specifically written for the microcomputer you are
uSing. This is a particular drawback for microcomputers. since these devices are new
and few assembly language programs exist for them. The result. too frequently. is that
you are on your own. If you need a program to perform a particular task. you are not
likely to find it In the small program libraries that most manufacturers provide. Nor are
you likely to find it in an archive. journal article. or someone's old program file. You will
probably have to write It yourself.
HIGH-LEVEL LANGUAGES
That is a lot simpler (and a lot shorted than either the equivalent machine language program or the equivalent assembly language program. Other high-level languages include COBOL (for bUSiness applications). PASCAL (another algebraic language). PL!l (a
combination. of FORTRAN. ALGOL. and COBOL). and APL and BASIC (languages that
are popular for time-sharing systems).
1-7
Programs written in a high-level language are portableat least. in theory. They will run on any computer or
microcomputer that has a standard compiler for that language.
PORTABILITY
OF HIGH-LEVEL
LANGUAGES
,..;;..-----
1-8
tlon time or reduce memory usage. A few compilers (known as optimizing compilers)
can also do this. but such compilers are much larger and slower than regular compilers.
The general advantages and disadvantages of high-level languages are:
Advantages:
ADVANTAGES
OF
HIGH-LEVEL
LANGUAGES
Disadvantages:
Special rules
DISADVANTAGES
OF
Extensive hardware and software support required
HIGH-LEVEL
Orientation of common languages to algebraic or business
LANGUAGES
problems
Inefficient programs
Difficulty of optimizing code to meet time and memory requirements
Inability to use special features of a computer conveniently
1-9
bear the expense of the larger computer but must also physically transfer the program
from the larger computer to the micro.
A few self-compilers are available. These compilers run on the microcomputer for
which they produce object code. Unfortunately, they require large amounts of memory
(16K or more). plus special supporting hardware and software.
UNSUITABILITY
High-level languages also are not generally well-suited to
OF HIGH-LEVEL
microprocessor applications. Most of the common languages
LANGUAGES
were devised either to help solve scientific problems or to handle large-scale business data processing. Few microprocessor
applications fall in either of these areas. Most microprocessor applications Involve sending data and control information to output deVices and receiving data and status information from Input deVices. Often the control and status information consists of a few
binary digits with very precise hardware-related meanings. If you try to write a tYPical
control program In a high-level language, you often feel like someone who is trying to
eat soup with chopsticks. For tasks in such areas as test equipment. terminals. navigation systems, signal processing, and bUSiness equipment. the high-level languages
work much better than they do In instrumentation, commUniCations, peripherals, and
automotive applications.
APPLICATION
Applications better suited to high-level languages are those which
AREAS FOR
require large memories. If. as In a valve controller, electronic game,
LANGUAGE
appliance controller. or small Instrument. the cost of a single
LEVELS
memory chip IS important. then the Inefficiency of high-level
languages IS Intolerable. If. on the other hand, as In a terminal or
test equipment, the system has many thousands of bytes of memory anyway. the inefficiency of high-level languages IS not as important. Clearly the size of the program and
the volume of the product are important factors as well. A large program will greatly increase the advantages of high-level languages. On the other hand, a high-volume application will mean that fixed software development costs are not as important as
memory costs that are part of each system.
APPLICATIONS
FOR MACHINE
LANGUAGE
Assembly Language:
Short to moderate sized programs
Applications where memory cost is a factor
Real-time control applications
APPLICATIONS
FOR ASSEMBLY
LANGUAGE
1-10
APPLICATIONS
FOR HIGH-LEVEL
LANGUAGE
FUTURE TRENDS
IN LANGUAGE
LEVELS
Most current microcomputer users program in assembly language (almost twothirds. according to one recent survey).
Many microcomputer users will continue to program in assembly language since
they need the detailed control that It provides.
No suitable high-level language has yet become widely available or standardized.
Many applications require the efficiency of assembly language.
An understanding of assembly language can help In evaluating high-level
languages.
The rest of this book will deal exclusively with assemblers and assembly language programming. However. we do want readers to know that assembly language is not the
only alternative. You should watch for new developments that may significantly reduce
programming costs if such costs are a major factor in your application.
1-11
REfERENCES
Some overall comparisons of the time required to write various types of programs at
different language levels are in M.H. Halstead. Elements of Software SCience. American
Elsevier. New York. 1977 and In V. Schneider. "Prediction of Software Effort and Project
Duration - Four New Formulas" SIGPLAN Notices. June 1978. pp. 49-55.
1-12
Chapter 2
ASSEMBLERS
This chapter discusses the functions performed by assemblers, beginning with features
common to most assemblers, and proceeding through more elaborate capabilities such
as macros and conditional assemblY, You may wish to skim this chapter for the present
and return to it when you feel more comfortable With the material.
FEATURES OF ASSEMBLERS
As we mentioned previously, today's assemblers do much more than translate assembly language mnemonics into binary codes. But we will first describe how an
assembler handles the translation of mnemonics before describing additional assembler features. Finally, we will explain how assemblers are used.
ASSEMBLER INSTRUCTIONS
Assembly language instructions (or "statements") are divided
into a number of fields, as shown in Table 2-1.
ASSEMBLY
LANGUAGE
FIELDS
The operation code field is the only field which can never be
empty; it always contains either an instruction mnemonic or a
directive to the assembler, called a pseudo-instruction, pseudo-operation, or
pseudo-op.
The address field may contain an address or data, or it may be blank.
Table 2-1. The Fields of an Assembly Language Instruction
Label
Field
Operation
Code or
Mnemonic
Field
Operand
or
Address
Field
START
LD
LD
LD
ADD
LD
A,(VALll
B,A
A,(VAL2l
A,B
(SUM),A
NEXT
VALl'
VAL2:
SUM:
DEFS
DEFS
DEFS
1
1
1
Comment Field
The comment and label fields are optional. A programmer will assign a label to a
statement or add a comment as a personal convenience, e.g., to make the program
easier to code and read.
2-1
If the assembler cannot use the position In the line to tell the fields
DELIMITERS
apart. it must use something else. Most assemblers use a
special symbol or delimiter at the beginning or end of each field. The most obVIOUS
delimiter IS the space character. Commas. periods. semicolons. colons. slashes. question marks and other characters that would not otherwise be used In assembly
language programs also may serve as delimiters. Table 2-2 lists standard Zilog
assembler delimiters.
zao
after a label
'space' between operation code and address
between operands in the address field
before a comment
You will have to exercise a little care with delimiters. Some assemblers are fussy
about extra spaces or the appearance of delimiters in comments or labels. A wellwritten assembler will handle these minor problems, but many assemblers are not
well-written. Our recommendation is simple: avoid potential problems if you can.
The following rules will help:
1)
Do not use extra spaces. particularly after commas that separate operands.
2)
3)
Include standard delimiters even if your assembler does not reqUire them. Your programs will then be assembled by any assembler.
LABELS
The label field is the first field in an assembly language instruction: It may be blank. If a label IS present. the assembler
assigns to the label the value of the address for the memory location Into which the first object program byte for that instruction IS loaded. You may
subsequently use the label as data or as an address in another instruction's operand
field. The assembler will replace the label With the assigned value When creating an oblect program.
Labels are most frequently used in Jump, Call or Branch inLABELS
structions. These instructions place a new value In the Program
IN JUMP
Counter and so alter the normal sequential execution of instrucINSTRUCTIONS
tions. JUMP 15016 means "place the value 15016 into the Program Counter" The next instruction to be executed will be the one in memory location
15016 The instruction JUMP START means "place the value assigned to 'the label
START Into the Program Counter" The next Instruction to be executed will be the one
In the memory location to which the label START has been assigned. Table 2-3 contains
an example.
2-2
(MAIN PROGRAM)
JUMP START
When the machine language version of this program is executed. the instruction JUMP
START causes the address of the instruction labeled START to be placed Into the Program Counter. The instruction with the label START will be executed next.
Why use a label? Here are some reasons:
1)
2)
The label can be moved to change or correct a program. You do not have to change
any subsequent Instructions that use the label; the assembler will make all the
necessary changes.
The assembler or loader can relocate the whole program by
RELOCATION
adding a constant (a relocation constant) to each address in
CONSTANT
which a label was used. Thus w~ove the program to
allow for the insertion of other programs or simply to rearrange memory.
The program is easier to use as a library program. i.e.. It IS easier for someone else to
take your program and add it to some totally different program.
You do not have to figure out memory addresses. Figuring out memory addresses is
particularly difficult with microprocessors which have Instructions that vary In
length.
3)
4)
5)
It makes sense to assign a label to any instruction that you might want to use as a
destination or otherwise identify.
The next question is what label to use. The assembler often
places some restrictions on the number of characters (usuallv 5
or 6). the leading character (often must be a letter). and the trailing
characters (often must be letters. numbers. or one of a few special characters). Beyond
these restrictions. the choice is up to you.
Our own preference is to use labels that suggest their purpose. I.e.. mnemonic labels.
Typical examples are ADDW in a routine that adds one word Into a sum. SRETX in a
routine that searches for the ASCII character ETX. or NKEYS for a location In data
memory that contains the number of key entries. Meaningful labels are easier to
remember and contribute to program documentation. Some programmers prefer to use
a standard format for labels. such as starting with LOOOO. These labels are self-sequencing (you can skip a few numbers to permit Insertions). but they do not help document
the program.
Some label selection rules will keep you out of trouble. We
recommend the following:
1)
2-3
2)
3)
Do not use labels that are longer than the assembler permits. Assemblers have
various truncation rules.
Avoid special characters (non-alphabetic and non-numeric) and lower-case letters.
Some assemblers will not permit them: others allow only certain ones. The simplest
practice IS to stick to capital letters and numbers.
4)
Start each label with a letter. Such labels are always acceptable.
5)
Do not use labels that could be confused with each other. Avoid the letters 1.0 and
Z and the numbers O. 1 and 2. Also avoid things like XXXX and XXXXX. There's no
sense tempting fate and Murphy's laws.
6)
When you are not sure if a label IS legal. do not use It. You will not get any real
benefit from discovering exactly what the assembler will accept.
These are recommendations. not rules. You do not have to follow them. but don't blame
us if you waste time on silly problems.
PSEUDO-OPERATIONS
Some a,ssembly language instructions are not directly translated into machine language instructions. These instructions
are directives to the assembler; they assign the program to certain areas In memory. define symbols. designate areas of RAM for
temporary data storage. place tables or other fixed data in memory. allow references to other programs. and perform minor housekeeping fu nctions.
PSEUDOOPERATIONS
ASSEMBLER
DIRECTIVE
2-4
Different assemblers use different names for these operations. but the purposes are the
same. Housekeepmg pseudo-operations mclude:
END
LIST
NAME
PAGE
SPACE
TITLE
We will discuss these pseudo-operations bnefly. although their functions are usually
obvious.
DATA
12
will place the number 12 in the next available memory location and assign that
location the name DZeON. Usually every DATA pseudo-operation has a label. unless It
IS one of a series of DATA pseudo-operations. The data and label may take any form
that the assembler permits.
Most assemblers allow more elaborate DATA instructions that handle a large amount of
data at one time. e.g..
EMESS
SQRS
DATA
DATA
2-5
'ERROR'
1,4.9.16.25
A single Instruction may fill many words of program memory. limited only by the length
of a line. Note that if you cannot get all the data on one line. you can always follow one
DATA instruction with another. e.g..
MESSG
DATA
DATA
DATA
DATA
DATA
DATA
'NOW IS THE'
'TIME FOR ALL'
'GOOD MEN'
'TO COME TO THE'
'AID OF THEIR'
'COUNTRY'
TTY
LAST
EQU
EQU
5
5000
Most assemblers will allow you to define one label In terms of another. e.g..
LAST
ST1
EQU
EQU
FINAL
START+l
The label in the operand field must. of course, have been previously defined. Often. the
operand field may contain more complex expressions, as we shall see later. Double
name assignments (two names for the same data or address) may be useful In patching
together programs which use different names for the same variable (or different spellIngs of what was supposed to be the same name).
Note that an EQU pseudo-operation does not cause the assembler to place anything into memory. The assembler simply
enters an additional name into a table (called a symbol table)
which the assembler maintains. This table. unlike the mnemonic table, must be In
RAM since It vanes with each program. The assembler program will always need some
RAM to hold the symbol table; the more RAM it has. the more symbols it can accept.
This RAM is In addition to any which the assembler needs as temporary storage.
When do you use a name? The answer IS: whenever you have a
parameter that has some meaning besides ItS ordinary numenc
value. or the numeric value of the parameter might be changed.
We typically assign names to time constants. device addresses. masking patterns. conversion factors, and the like. A name like DELAY. TTY, KBD. NROW. or OPEN not only
makes the parameter easier to change, but It also adds to program documentation. We
also assign names to memory locations that have special purposes; they may hold data.
mark the start of the program. or be available for intermediate storage.
What name do you use? The best rules are much the same as
in the case of labels, except that here meaningful names really
count. Why not call the teletypewnter TTY instead of X15. a bit
time delay BTIME or BTDLY rather than WW. the number of the
2-6
CHOICE
OF
NAMES
"GO" key on a keyboard GOKEY rather than HORSE? This advice seems straightforward. but a surprising number of programmers do not follow It.
Main program
Subroutines
Memory addresses for
Input/output deVices
or speCial functions
Still other ORIGIN statements may allow room for later Insertions. place tables or data in
memory. or assign vacant RAM space for data buffers. Program and data memory In
microcomputers may occupy widely scattered addresses to simplify the hardware.
RESET
1000
INT3
Some assemblers assume an Origin of zero if the programmer does not put an ORG
statement at the start of the program. The convenience is slight: we recommend the inclUSion of an ORG statement to avoid confusion.
2-7
ALLOCATING
RAM
Using the RESERVE pseudo-operation, you assign a name to the memory area and
declare the number of locations to be assigned. Here are some examples:
NOKEY
TEMP
VOLTG
BUFR
RESERVE
RESERVE
RESERVE
RESERVE
1
50
80
100
You can use the RESERVE pseudo-operation to reserve memory locations in program
memory or in data memory: however the nature of the RESERVE pseudo-operation is
more meaningful when applied to data memory.
In reality, all the RESERVE pseudo-operation does is Increase the assembler's Location
Counter by the amount declared In the operand field. The assembler does not actually
produce any oblect code.
The label of the RESERVE pseudo-operation IS assigned the value of the first address reserved. For example, the sequence:
BUFl
BUF2
VOLTS
2)
3)
ORG
RESERVE
RESERVE
RESERVE
3000
100
50
assigns to the label BUFl the value 3000, to BUF2 3100, and to VOLTS 3150.
You must specify the number of locations to be reserved. There IS no default case.
No data IS placed into the reserved locations. Any data that by chance, may be In
these locations will be left there.
LINKING PSEUDO-OPERATIONS
We often want statements in one program or subroutine to
EXTERNAL
use names that are defined elsewhere. Such names are called
REFERENCES
external references; a special linker program IS necessary to actually fill in the external values and determine if any names are undefined or doubly
defined.
The pseudo-operation EXTERNAL, usually abbreviated EXT, signifies that the
name is defined elsewhere.
The pseudo-operation ENTRY, usually abbreviated ENT, signifies that the name is
available for use elsewhere, I.e., It is defined In thiS program.
The precise way in which linking pseudo-operations are Implemented vanes greatly
from assembler to assembler. We will not refer to such pseudo-operations again, but
they are very useful in actual applications.
HOUSEKEEPING PSEUDO-OPERATIONS
There are various housekeeping pseudo-operations, which affect the operation of
2-8
the assembler and its program listing rather than the output program itself. Common housekeeping pseudo-operations include:
1)
END. which marks the end of the assembly language source program.
2)
LIST. which tells the assembler to print the source program. Some assemblers allow
such variations as NO LIST or LIST SYMBOL TABLE to avoid long. repetitive listIngs.
3)
NAME or TITLE. which prints a name at the top of each page of the listing.
4)
P.AGE or SPACE. which skips to the next page or next line. respectively. and Improves the appearance of the listing. making It easier to read.
5)
PUNCH. which transfers subsequent object code to the paper tape punch. This
pseudo-operation may In some cases be the default option and therefore unnecessary.
All EQUATE pseudo-operations must have labels: they do not make any sense
otherwise. since their purpose IS to define the meaning of the labels.
2)
DATA and RESERVE pseudo-operations usually have labels. The label identifies the
first memory location used or assigned.
3)
Other pseudo-operations should not have labels. Some assemblers allow other
pseudo-operations to have labels. but the meaning of the labels vanes. We recommend that you aVOid thiS practice.
DECIMAL
1)
DATA OR
ADDRESSES
Decimal numbers
100
ADD
means "add the contents of memory location 100 deCimal to the contents of the Accumulator"
2)
Most assemblers will also accept binary. octal. or hexadeCimal entries. But you must identify these number systems In some way.
e.g.. by preceding or follOWing the number with an Identifying
character or letter. Here are some common Identifiers:
OTHER
NUMBER
SYSTEMS
B or % for binary
O. Q. C or @ for octal (we aVOid 0 because of the confusion with zero).
H or $ for hexadeCimal
2-9
Assemblers generally require hexadecimal numbers to start with a decimal digit (e.g..
OA36 Instead of A36) In order to distinguish between numbers and names or labels. It is
good practice to enter numbers in the base in which their meaning is the clearestI.e.. decimal constants in deCimal: addresses and BCD numbers In hexadecimal: maskIng patterns or bit outputs In binary if they are short and in hexadecimal if they are long.
3)
Symbolic names
Names can appear in the operand field: they will be treated as the data that they represent. But remember. there is a difference between data and addresses. The sequence:
FIVE
EQU
ADD
5
FIVE
will add the contents of memory location 5 (not necessarily the number 5) to the contents of the Accumulator.
4)
$+6
causes a Jump to the memory localion six words beyond the word that contains the
first byte of the JUMP instruction:
Memory
t----il}
JUMP
+6
6 locations
Jump here
Most microprocessors have many two and three-word Instructions. Thus. you will have
difficulty determining exactly how far apart two assembly language statements are.
Therefore. using offsets from the Location Counter frequently results in errors that you
can avoid if you use labels.
5)
Character codes
ASCII
CHARACTERS
We recommend that you use character strings for all text. It Improves the clarity and
readability of the program.
6)
2-10
may not be able to use multiplication In the logic of your own program write a subroutine for that specific purpose.
unless YOU
Assemblers vary In what expressions they accept and how they Interpret them. Complex expressions make a program difficult to read and understand.
We have made some recommendations dUring this section but will repeat them and
add others here. In general, the user should emphasize clarity and simplicity. There
IS no payoff for being an expert in the intricacies of assemblers or in having the most
complex expression on the block. We suggest the following approach:
1)
2)
3)
4)
Use the clearest number system or character code for data. Masks and BCD numbers in decimal. ASCII characters in octal. or ordinary numerical constants In hexadecimal serve no purpose and therefore should not be used.
Remember to distinguish data and addresses.
Don't use offsets from the Location Counter.
Keep expressions simple and obvious. Don't rely on obscure features of the assembler.
CONDITIONAL ASSEMBLY
Some assemblers allow you to include or exclude parts of the source program. depending on conditions existing at assembly time. This is called conditional assembly; It gives the assembler some of the flexibility of a compiler. Most microcomputer
assemblers have limited capabilities for conditional assembly. A usual form is:
IF COND
.CONDITIONAL PROGRAM
ENDIF
If the expression COND IS true at assembly time, the instructions between IF and ENDIF
(two pseudo-operations) are Included in the program.
Typical uses of conditional assembly are:
1)
2)
3)
4)
To
To
To
To
Unfortunately, conditional assembly tends to clutter programs and make them difficult
to read. Use conditional assembly only if It is necessary.
MACROS
You will often find that particular sequences of instructions ocDEFINING A
cur many times in a source program. Repeated instruction seSEQUENCE OF
INSTRUCTIONS
quences may reflect the needs of your program logic, or they
may be compensating for deficiencies in your microprocessor's
instruction set. You can avoid repeatedly writing out the same instruction sequence by
uSing a macro.
Macros allow you to assign a name to an instruction sequence. You then use the
macro name in your source program instead of the repeated instruction sequence.
2-11
The assembler will replace the macro name with the, appropriate sequence of instructions. This may be illustrated as follows:
Object Program
Source Program
MAC'
(macro definitloni
MACRO
instruction M 1 }
instruction M2
---------------.
instruction M3
ENDM
instructIon P 1
(ma,n programl }
-+
...
instructIon P2
Instruction
P3
instruction Pl
Instruction P2
Instruction P3
Instruction
------------------t'''----"I
MAC'
instruction P4
instruction
--------------+---t~
P7
instruction
instruction P9
MAC1 - - - - - - - - - - - - - - - - - - '......
instruction P11
... {
l
i
...
instruction
P7
Instruction
M1
instruction M2
Instruction M3
-{
.. {
--1_
instruction PB
P5
Instruction P6
-----------------f""---t....
instruction P10
M1
Instruction M2
instruction M3
instruction P4
tnstrucliOn P5
instruction P6
MAC'
Instruction
pa
P9
Instruction
M1
instruction
instruction M2
instruction
M3
instruction P10
instruction Pl1
Macros are not the same as subroutines. A subroutine occurs once in a program. and
program execution branches to the subroutine. A macro is expanded to an actual instruction sequence each time the macro occurs: thus a macro does not cause any
branching.
ADVANTAGES
OF MACROS
2)
3)
Use of debugged instruction sequences - once the macro has been debugged.
you are sure of an error-free Instruction sequence every time you use the macro.
EaSier changes. Change the macro definition and the assembler makes the change
for you every time the macro is used.
4)
5)
InclUSion of commands. keywords. or other computer instructions In the basic instruction set. You use the macro as an extension of your Instruction set.
DISADVANTAGES
2-12
OF MACROS
2)
3)
4)
----..,
LOCAL OR
GLOBAL
VARIABLES
COMMENTS
All assemblers allow you to place comments in a source program. Comments have
no effect on the object code. but they help you to read. understand. and document
the program. Good commenting is an essential part of writing assembly language
programs; without comments. programs are very difficult to understand.
We will discuss commenting along with documentation in a
later chapter. but here are some guidelines:
1)
COMMENTING
TECHNIQUES
Use comments to tell what the program IS doing, not what instructions do,
Comments should say things like "IS TEMPERATURE ABOVE L1MIT?", "LINE FEED
TO TTY". or "EXAMINE LOAD SWITCH"
Comments should not say things like "ADD 1 TO ACCUMULATOR" "JUMP TO
START", or "LOOK AT CARRY" You should describe how the program IS affecting
the system: internal effects on the CPU are seldom of any Interest.
2)
Keep comments brief and to the POint. Details should be available elsewhere in the
documentation,
3)
4)
5)
6)
7)
Comment all definitions, describing their purposes. Also mark all tables and data
storage areas.
8)
9)
Be consistent in your terminology, You can (should) be repetitive: you do not need
to consult a thesaurus,
10)
Leave yourself notes at points which you find confusing, e.g.. "REMEMBER CARRY WAS SET BY LAST INSTRUCTION" You may drop these In the final documentation.
A well-commented program is easy to work with. You will recover the time spent In
commenting many times over. We will try to show good commenting style In the programming examples, although we often over-comment for instructional purposes.
2-13
TYPES Of ASSEMBLERS
Although all assemblers perform the same tasks, their implementations vary
greatly. We will not try to describe all the existing types of assemblers; we will
merely define the terms and indicate some of the choices.
A cross-assembler is an assembler that runs on a computer
other than the one for which it assembles object programs.
The computer on which the cross-assembler runs IS typically a
large computer with extensive software support and fast peripherals - such as an IBM
360 or 370. a Univac 1108. or a Burroughs 6700. The computer for which the cross-assembler assembles programs is typically a microcomputer like the Z80 or MC6800.
Most cross-assemblers are written in FORTRAN so that they are portarb..;le;...
.,
A self-assembler or resident assembler is an assembler that runs
on the computer for which It assembles programs. The self-assembler will require some memory and peripherals. and it may run
qUite slowly.
MACROASSEMBLER
MICROASSEMBLER
ERRORS
Assemblers normally provide error messages, often consisting of a single coded
letter. Some typical errors are:
1)
2)
2-14
3)
4)
5)
6)
7)
8)
9)
10)
In interpreting assembler errors. you must remember that the assembler may get off on
the wrong track if It finds a stray letter. an extra space. or Incorrect punctuation. Many
assemblers will then proceed to misinterpret the succeeding instructions and produce
meaningless error messages. Always look at the first error very carefully: subsequent
ones may depend on it. Caution and consistent adherence to standard formats will
eliminate many annoying mistakes.
LOADERS
The loader IS the program which actually takes the output (oblect codel from the assembler and places it in memory. Loaders range from the very Simple to the very complex. We will describe a few different types.
A bootstrap loader is a program that uses its own first few instructions to load the rest of itself or another loader program
into memory. The bootstrap loader may be In ROM. or you may
have to enter it into the computer memory using front panel switches.
may place a bootstrap loader at the start of the obJect program that It
BOOTSTRAP
LOADER
The assembler
produces.
2-15
REFERENCES
A complete monograph on macros IS M. Campbell-Kelly. An Introduction to Macros.
American Elsevier. New York. 1973.
Microprogramming IS described conceptually in An Introduction to Microcomputers: Volume 1 - Basic Concepts. Chapter 4. A more technical description IS in A.K.
Agrawala and T.G. Rauscher. Foundations of Microprogramming. AcademiC Press. New
York. 1976.
You can find more detailed desCriptions of assemblers and loaders In D.W. Barron. "Assemblers and Loaders". American Elsevier. New York. 1972 and In C.W. Gear. Computer Organization and Programming. McGraw-HilI. New York. 1974.
2-16
Chapter 3
THE
We are now ready to start writing assembly language programs. We begin in this
chapter by defining the individual instructions of the zao assembly language instruction set, plus the syntax rules of the Zilog assembler.
We do not discuss any aspects of microcomputer hardware, signals. interfaces. or
CPU architecture in this book. This information IS described in detail In An Introduction
to Microcomputers: Volume 2 -Some Real Microprocessors and Volume 3 -Some
Real Support Devices. while
Programming for Logic Design discusses assembly
language as an extension of digital logic. In this book. we look at programming techniques from the assembly language programmer's viewpoint, where pins and signals are irrelevant and there are no important differences between a minicomputer and a microcomputer.
zao
zao
Interrupts. direct memory access. and the Stack architecture for the
will be described In later chapters of this book. In conjunction With assembly language programming discussions of the same subjects.
This chapter contains a detailed definition of each assemblv language instruction.
Programming for
These definitions are Identical to those found In Chapter 6 of
Logic Design.
zao
zao
3-'
I
s
Iz
II
,
lAd
IPlol N
Ic
Accumulators
s', Z',
Alternate {
Secondary
Accumulators
Flags
Accumulator
secondarv{
Subtract
Carry
lAd
Alternate Flags
A'
Alternate Accumulator
B'
C'
0'
E'
Alternate Secondary
} Data Counters
L'
H'
Stack Pointer
SP
PC
Program Counter
IX
Index Register
Index Register
IY
Refresh Register
The Accumulator is the primary source and destination for one-operand and twooperand instructions. For example, the shortest and fastest data transfers between the
CPU and I/O devices are performed through the Accumulator, In addition, more Memory Reference instructions move data between the Accumulator and memory than between any other register and memory, All 8-blt arithmetic and Boolean instructions take
one of the operands from the Accumulator and return the result to the Accumulator. An
instruction must therefore load the Accumulator before the l80 can perform any 8bit arithmetic or Boolean operations.
The B. C. D. E. H. and L registers are all secondary registers. Data stored in anv of
these SIX registers may be accessed with equal ease; such data can be moved to any
other register or can be used as the second operand In two-operand instructions,
There are, however, some important differences
H, and L.
In
Registers Hand L are the primary Data Pointer for the l80. That IS to say, you will
normally use these two registers to hold the 16-blt memory address of data being accessed, Data may be transferred between any registers and the memory location addressed by Hand L. Since HL IS the pnmary Data Pointer, it often takes fewer bytes of
oblect code and less instruction cycles to perform operations with it. The Z80 programmer should try to address data memory via Registers Hand L whenever possible,
Within your program logic. always reserve Registers Hand L to hold a data memoryaddress.
3-2
In your program logic you should normally use Registers B, C, 0, and E as temporary storage for data or addresses.
Registers IX and IV are index registers. They proyide a limited Indexing capability of
the type described in An Introduction to Microcomputers: Volume 1 for short instructions.
The alternate registers f', A', B', C', 0', E', H', and L' provide a duplicate set of
general purpose registers. Just two single-byte Exchange instructions select and
deselect all alternate registers; one Instruction exchanges AF and the alternate AF'
as a register pair. and one instruction exchanges BC. DE. and HL With the alternate BC',
DE'. and HL' Once selected. all subsequent register operations are performed on the active set until the next exchange selects the inactive set. The alternate registers can be
reserved for use when a fast interrupt response is required. Or. they may be used in
any desired way by the programmer.
There are a number of instructions that handle 16 bits of data at a time. These instructions refer to pairs of CPU registers as follows:
F
B
D
H
F'
B'
D'
H'
and
and
and
and
and
and
and
and
A
C
E
L
A'
C'
E'
L'
'-v-'
'-v-'
Highorder
byte
Loworder
byte
The combination of the Accumulator and flags. treated as a 16-blt unit. is used only for
Stack operations and alternate register switches. ArithmetiC operations access Band C.
D and E. or Hand L as 16-bit data units.
The Carry status flag holds carries out of the most significant bit in any arithmetic
operation. The Carry flag IS also Included in Shift instructions: It IS reset by Boolean instructions.
The Subtract flag is designed for internal use during deCimal adjust operations. This
flag is set to 1 for all Subtract instructions and reset to 0 for all Add Instructions.
The Parity/Overflow flag is a multiple use flag, depending on the operation being
performed. for arithmetic operations, it is an overflow flag. for input, rotate, and
Boolean operations, it is a parity flag, with 1 = even parity and 0 = odd parity. During block transfer and search operations. It remains set until the byte counter decrements to zero: then it is reset to zero. It IS also set to the current state of the Interrupt
enable flip-flop (JFF2) when a LD A.l or LD A.R instruction IS executed.
The Zero flag is set to 1 when any arithmetic or Boolean operation generates a
zero result. The Zero status is set to 0 when such an operation generates a nonzero result.
3-3
The Sign status flag acquires the value of the most significant bit of the result
following the execution of any arithmetic or Boolean instruction.
The Auxiliary Carry status flag holds any carry from bit 3 to 4 resulting from the
execution of an arithmetic instruction. The purpose of this status flag IS to simplify
Binary-Coded-Decimal (BCD) operations; this IS the standard use of an Auxiliary Carry
status flag as described In An Introduction to Microcomputers; Volume 1. Chapter 3.
All of the above status flags keep their current value until an instruction that modifies
them IS executed. Merely changing the value of the Accumulator will not necessarily
change the value of the status flags. For example, if the Zero flag IS set. and a load Immediate to the Accumulator is executed, that causes the Accumulator to acquire a nonzero value; the value of the Zero flag remains unchanged.
The 16-bit Stack Pointer allows you to implement a Stack anywhere in addressable memory. The size of the Stack IS limited only by the amount of addressable memory
present. In reality you will rarely use more than 256 bytes of memory for your Stack.
You should use the Stack for accessing subroutines and processing interrupts. Do not
use the Stack to pass parameters to subroutines. This IS not very efficient Within the
limitations of the
instruction set. The
Stack IS started at ItS highest address. A
Push decrements the Stack POinter contents; a Pop Increments the Stack POinter contents.
zao
zao
The Interrupt Vector register and the Refresh register are special-purpose
registers not normally used by the programmer.
The Interrupt Vector register IS used to store the page address of an Interrupt response
routine; the location on the page IS prOVided by the Interrupting device. This scheme
allows the address of the Interrupt response routine to be changed while still providing
a very fast response time for the interrupting device.
The Refresh register contains a memory refresh counter In the low-order seven bits.
This counter IS Incremented automatically after each Instruction fetch and prOVides the
next refresh address for dynamic memories. The high-order bit of the Refresh register
will remain set or reset. depending on how it was loaded at the last LD R,A instruction.
Implied
3-4
Implied
In implied memory addressing. the Hand L registers hold the address of the
memory location being accessed. Data may be moved between the Identified memory location and anyone of the seven CPU registers A. B. C. D, E. H, or L. For example, the
instruction
LD
C,(HL)
loads the C register with the contents of the memory location currently pOinted to by
HL. This IS illustrated as follows:
S ZACP/ON
FD
I I I
Data
Cl
Memory
B.C
D,E
H.L
,.
PP
SP
PC
mmmm
~mmmm+'
.......
IX
IY
I
R
I
I
LD
~
76543210
Load Implied
Via
C RegIster
3-5
HL
P q
Program
Memory
~
4E
C. (HLI
L[T
yy
mmm
m
mm mm+l
mmmm+2
mmmm+3
(BCl.A
stores the contents of A Into the memory location currently addressed by Register Pair
Be. This is illustrated as follows:
SZACPONC
Data
Memory
I I ! I I f I
A
ppqq
yy
qql
pp
S.C
II
D.E
H.L
SP
PC
... .r
~mmmm+l
mmmm
1
Program
~
Memory
IX
IY
I
LD
02
(BCIA
A ..._
r.,
54
...._
2
...
0-----
"
-./
store Implied from A via
3-6
Be
mm mm
mm mm+l
mmmm+2
mmmm+3
illustrated as follows:
AcPYON
FO:m{(]IJ
A
B.C
tt
O.E
rr
ss
.L
pp
uu
SP
PC
IX
IV
Program
mmmm
mory
~
ED
80
mmmm
mmmm+ 1
mmmm+2
mmmrri+3
3-7
Implied Stack
Since the Stack is part of ReadlWrite memory, we must consider Stack Instructions as
Memory Reference Instructions. Push and Pop instructions move two bytes of data
between a register pair and the addressed Stack Pointer location, i.e.. current topof-stack. The ZSO Stack address IS decremented with each Push and Incremented with
each Pop. The instruction
PUSH
DE
is illustrated as follows:
S
ZAcP/ON C
no
Data
Memory
A
B.C
D.E
pp
H.L
SP
SSSS
PC
mmmm
IX
IV
I
ssss
Program
Memory
~
05
PUSH
DE
LZ_
76543210
~~I'IOlqOlqOI1_!_ _
-
PUSH Instruction
Register Pair DE
3-8
ssss - 2
ssss - 1
ssss-2 : )
f# ... - ,
~---:I.. ..,:mmm + 1
qq
pp
mmmm
mmmm+l
mmmm+2
mmmm+3
The zao also has instructions that exchange the two top-of-stack bytes with a
16-bit register - HL or one of the two Index registers. The instruction
EX
IS
(SPl.HL
illustrated as follows:
S
FI
Z AC Pia N C
Data
Memory
! I ! ! I I
B.C
D.E
pp
H.L
II'
xx
yy
SP
ssss
PC
IX
mmmm
IY
I
ssss
ssss + 1
ssss + 2
__
-~mmm+:V
Program
Memory
~
E3
3-9
mmmm
mmmm+ 1
mmmm+2
mmmm+3
Indexed
The zao has two 16-bit index registers, called IX and IV. They may be used Interchangeably. All memory reference operations for which (HL) can be specified can
alternatively be specified as an indexed operation. The difference between implied addressing uSing HL and indexed addressing uSing IX and IY is that the index operand
includes a displacement value that is added to the index address. In the instruction
ADD
A.(IX+40H)
the memory address IS the sum of the contents of the IX register and 4016' This may be
illustrated as follows:
S
ZAc;P/ON C
Data
F~
A
xx
-, xx+yy
B.C
D.E
t:=:=:J
ppqq
...
VY
ippqqi
H.L
SP
PC
mmmm
IX
IY
I
R
ppqq
~mmm+.D
I
ADO
-- -
'-
.J-
A.UX + 40)
':T__.._
~,
16543210
1 0
1 1
0 1 } Add to A Indexed by IX Instruction
0000110
Program
Memory
00
~pqq+~
Displacement
3-10
40
8~
40
mmm m
mmmm+l
mmm m+2
mmm m+3
Direct
Direct addressing can be used to load the Accumulator with any 8-bit value from
memory. load Be. DE. HL. SP.IX. or IV with any 16-bitmemory value. and jump or
call subroutines direct at any memory location. The 16-blt direct address is stored in
the last two bytes of the instruction, In low-byte high-byte order (this IS the reverse of
the standard high-low scheme),
The instruction
LD
A,(NETX)
loads the A register with the contents of the memory location addressed by the label
NETX, The instruction
LD
HL.(1 FFH)
loads the L register With the contents of memory location 01FF 16 and the H register
with the contents of memory location 0200 16, ThiS may be illustrated as follows:
S
F
Z AcP/ON C
Data
Memory
I I I
A
S,C
xx
D,E
H,L
yy
SP
"--~mmm+~
mmmm
PC
IX
IV
I
I
I
R
LD
yy
01FF
xx
0200
Program
Memory
mmmm
mmmm+ 1
1
01
mmmm+2
1-"";';"---1 mmmm + 3
2A
FF
HL,( 1FFH)
543
The direct Jump instructions provide jumps and jumps-to-subroutines. both unconditional and conditional. These are all 3-byte instructions, with the direct address
stored in the second and third bytes of the Instruction, as shown above for Load Direct.
There are three additional addreSSing modes used by Z80 8ranch instructions: program relative, base page, and register Indirect. In general, they are shorter
and/or faster than direct Jumps but may have more limited addreSSing capabilities.
3-11
Program Relative
Jump Relative instructions provide program relative addressing in the range -126,
+129 bytes from the first byte of the Program Relative instruction. These instructions
are all 2-byte instructions, with the signed displacement value stored In the second
byte of the instruction. There are unconditional and conditional relative jumps, as
well as a Decrement and Jump If Not Zero instruction (DJNZ) that facilitates loop
control.
SRCH
ZACP/ON
Data
FI I ! ! I I I
A
s.c
D,E
H.l
SP
mmmm
PC
IX
~mmmm+
:::;;;a:
5A
IY
I
I
I
R
JR
-'SRCH
1I-.l-..I.....l....I._I-.l--'-O.... Displacement
3-12
~
Program
Memory
18
5A
mmmm
mmmm+l
mmmm+2
mmmm+3
Base Page
The Z80 has a modified base page addressing mode for the Restart instruction. This IS
a special Call instruction that allows a single-byte instruction to jump to one of
eight subroutines located at specific points in lower core. The effective address IS
calculated from a 3-bit code stored in the instruction. as follows:
Lower Core Address
3-Bit Code
OOH
000
001
010
011
100
101
110
111
08H
10H
18H
20H
28H
30H
38H
The decoded address value is loaded into the low-order bvte of the Program Counter;
the high-order byte of the Program Counter IS set to zero. For example. the instruction
RST
IS
OOH
illustrated as follows:
Data
Memory
I I I
S.D.EC
-+
r ::::_ 2)
.>..
--I
~~-----_Il"'c
~~......
.......,
H.l
SP
5555
__ - / : .
~
+y-
pcl------~m".:m:;;;m~m~-----r~mmm
'r
:
RST
ssss-2
t-----I ssSS
Program
Memory
l.( :: ~r.....
---'......-...:C;.;.7--t : : : : + 1
t - - - - I mmmm + 2
t - - - - I mmmm + 3
OOH
~
76543210
4T
mm+1
It-_m_m_.... 5555 - 1
Restart instruction
Address code
3-13
Register Indirect
In standard indirect addressing. a memory location contains the effective address. and
the instruction specifies the address of the memory location containing the effective
address. In register indirect addressing. a register contains the effective address. and
the instruction specifies which of the registers contains the effective address. Note that
for a Load. for Instance. this IS Just another way of describing Implied addressing.
However. the ZSO has Jump instructions that allow a jump to the memory location
whose address is contained in the specified register. This is a form of indirect addressing. and is described separately because. while most microcomputers have Implied addressing. very few have register Indirect jumps.
The instruction
JP
(HL)
directs that a jump IS to be taken to the memory location whose address IS contained In
HL. This may be illustrated as follows:
S Z ACP/0 N C
F
Data
c:r::::o::I:D
B.C
D.E
H.l
SP
PC
pp
mmmm
IX
IY
I
R
I
I
~
Program
mory
~
E9
JP IHLl
"":;('"'"
r.";-S-l4L.-II
;;-S- 4 "'3""--1112-.,......
~JUmpVlaHL
3-14
mmmm
mmmm+l
mmmm+2
mmmm+3
Immediate
Some texts Identify Immediate instructions as Memory Reference instructions. An Immediate instruction IS a 2-. 3-, or 4-byte instruction In which the last one or two bytes
hold fixed data that is loaded Into a register or memory location. The Z80 provides Immediate instructions to:
loads the immediate data value BC16 Into Register Pair BC. This may be illustrated as
follows:
S
F'
ZAcP/ON C
Data
I I I I I
B.C
D.E
, -
H.L
SP
Program
Memorv
::;;0::-::1 mmmm + 3
mmmm
PC
IX
IY
01
I
I
......
BC
00
mmmm
mmmm+l
mmmm+2
mmmm+3
LD
BC.OBCH
76543210
. ,. ,.,
4I~.~_"
'--------Regls'er Pair BC
76543210
1
3-15
zao
Instruction Code
Meaning
ADC
ADD
AND
CALL
CALL
CP
DEC
DJNZ
IN
INC
JR
JR
LD
LD
LD
LD
LD
LD
OUT
POP
PUSH
RET
RET
RLA
RRA
SLA
SRL
SUB
addr
cond,addr
cond,addr
reg,(HL)
A,(addr)
data
(HL),reg
(addrl.A
dst.src
cond
316
Instruction Code
BIT
CPD.CPDR
CPI. CPIR
CPL
DAA
01
EI
EX
HALT
IND,INDR
INI. INIR
JP
JP
LD
LD
LD
LD
LD
LD
LD
LD
LD
LD
LD
LD
LDD, LDDR
LDI. LDIR
NEG
NOP
OR
OUTD.OTDR
OUTI. OTIR
RES
RETI
RL
RLC
RLCA
RR
RRC
RRCA
SET
SRA
XOR
addr
cond,addr
A, (BC) or (DE)
HL,(addr)
reg. (xy+d isp)
rp,(addr)
xy.(addr)
(BC) or (DEl.A
(addr).HL
(xy+displ.reg
(addrl.rp
(addrl.xy
(HU,data
(xy+displ.data
zao
Meaning
Test Bit
Compare. Decrement. (Repeat)
Compare, Increment. (Repeat)
Complement Accumulator
Decimal Adjust Accumulator
Disable Interrupts
Enable Interrupts
Exchange
Halt
Input. Decrement. (Repeat)
Input. Increment. (Repeat)
Jump
Jump Conditional
Load Accumulator Secondary
Load HL Direct
Load Register Indexed
Load Register Pair Direct
Load Index Register Direct
Store Accumulator Secondary
Store HL Direct
Store Register Indexed
Store Register Pair Direct
Store Index Register Direct
Store Immediate to Memory
Store Immediate to Memory Indexed
Load, Decrement. (Repeat)
Load, Increment. (Repeat)
Negate (Twos Complement) Accumulator
No Operation
Logical OR
Output. Decrement. (Repeat)
Output. Increment. (Repeat)
Reset Bit
Retu rn from Interru pt
Rotate Left Through Carry
Rotate Left Circular
Rotate Accumulator Left Circular
Rotate Right Through Carry
Rotate Right Circular
Rotate Accumulator Right Circular
Set Bit
Shift Right Arithmetic
Logical Exclusive OR
3-17
Meaning
Instruction Code
ADC
CCF
EXX
1M
RETN
RLD
RRD
RST
SBC
SCF
LD
LD
LD
LD
LD
LD
HL.rp
A.I
A.R
I.A
R.A
SP.HL
SP.xy
ABBREVIATIONS
These are the abbreviations used in this chapter:
A.F.B.C.D.E.H.L
AF'.BC'.DE'.HL'
addr
x(b)
cond
data
data16
disp
xx(HU
IX IY
label
xx(LO)
LSB
MSB
PC
port
3-18
pr
reg
C
D
E
H
L
rp
SP
xy
Object Code
bbb
ccc
Condition Code
ddd
Destination register -
000 = non-zero
001 = zero
010 = no carry
all = carry
100 = panty odd
101 = parity even
110 = positive sign
111 = negative sign
same coding as rrr
Register
111
000
001
010
011
100
101
= A
= B
=C
= D
= E
=H
= L
sss
Source register -
Index register
xx
Register pair
0 = IX
1 = IY
xxx
00 = BC
01 = DE
10 = HL
11 = SP (rp) or AF (prl
Restart code (000 to 111)
yy
YYYY
3-19
Statuses
The
C
Z
S
PIO
AC
N
- Carry status
- Zero status
- Sign status
- Parity/Overflow status
- Auxiliary Carry status
- Subtract status
[[J]
[J
[[HL]]
[HL]
Logical AND
Logical OR
--
Logical ExclUSive-OR
Data IS transferred In the direction' of the arrow
Data IS exchanged between the two locations deSignated on either
side of the arrows,
3-20
INSTRUCTiON MNEMONICS
zao
are:
LD
rp.(addrl
xy,(addr)
LD
LD
BC,(DAT21
IX,(MEM)
STATUS
The
SIX
' - - t -....- i f - + - t - - -
3-21
AC Pia
1--+_+1-
1-
Unchanged
Unknown
L....
3-22
STATUS
CHANGES
WITH
INSTRUCTION
EXECUTION
Type
Mnemonic
IN
A,(portl
Object Code
DB yy
Bytes
Clock
Cycle.
Status
Operation Performed:
PIO
AC
IA)-Iport)
10
IN
INIR
reg.(CI
EO 01dddOOO
ED B2
11
20/15"
to)
N
to)
INOR
ED BA
20/15"
Repeat until [ B) = 0:
[[HLII-[[CII
IB)-IB] -1
[ HLJ - I HL) - 1
Transfer a block of data from 110 port addressed by contents of C
to memory location addressed by contents of HL, going from high
addresses to low Contents of B serve-as a count of bytes remaining
to be transferred
INI
ED A2
15
[[ HLI - I[ CII
IB)-IB].l
I HLJ -
[ HLI
+ 1
Type
Mnemonic
INO
Object Code
ED AA
Bytes
Clock
Cycles
15
Status
Operation Performed
PIO AC
?
N
1
[[HLl!-[[Cl!
IBI-IBI -1
[ HLI - I HLI - 1
Transfer a byte of data from 1/0 port addressed by contents of C to
memory location addressed by contents of HL Decrement both
byte count and destination address ,.
OUT
(portl,A
03 yy
11
Iport]-[Al
Output from Accumulator to directly addressed 110 port
OUT
(C),reg
ED 01 sssOOl
12
20/15
OliR
(.0)
ED B3
'il
<I>
"Sc"
Repeat until I Bl = 0:
[[Cl!-[[HLl!
IBI-IBI -1
I HL] - [HLI + 1
Transfer a block of data from memory location addressed by con~
OTDR
ED BB
20/15
Repeat until I BI = 0:
[[Cl!-[[HLl!
IBI-IBI-l
[ HLI - [HLI - 1
Transfer a block of data from memory location addressed by con~
tents of HL to 110 port addressed by contents of C. going from high
memory to low, Contents of B serve as a count of bytes remaining
to be transferred, **
Mnemonic
Operand
Object Code
Bytes
zao
Status
Clocl(
Cycles
Pia AC
OUTI
ED A3
15
[[ C))
[[ HLI)
IB) -[B) - 1
[ HL)
I HLI + 1
Transfer a byte of data from memory location addressed by eontents of HL to JlO port addressed by contents of C Decrement byte
aUTO
ED AB
15
, , ,
[[ CIl - [[ HLI)
[ B) 1
I B)
[ HL) - [ HLI - 1
Transfer a byte of data from memory location addressed by contents of HL to 110 port addressed by contents of C Decrement both
byte count and source address
3A ppqq
13
'ij
-g"
~
c
0
LD
eN
I
A.laddrl
[A)
[ addrl
N
<.71
LD
HLladdri
2A ppqq
16
LD
rp.[addr)
xy.laddrl
ED 01xxl011 ppqq
l1xll101 2A ppqq
4
4
20
20
"uc
"
a:
LD
[addrl.A
32 ppqq
13
LD
laddrl.HL
22 ppqq
16
LD
laddrl.rp
laddrl.xy
ED 01xx0011 ppqq
l1xl1101 22 ppqq
4
4
20
20
LD
A,IBCI
A.IDEI
OA
lA
1
1
:;"
>-
I;;
.5
a:
ry
[addrl- [AI
Store Accumulator contents in directly addressed memory location
[ addr + 11 - [ HI. I addrJ - [ L)
Store contents of HL to directly addressed memory location
[ addr + 1] - [rpIHIIJ. [addrJ - I rplLO)] or
I addr + 1) - [ xylHIIJ. I addr] - [xyILO)]
Store contents of register pair or Index register to directly addressed memory
[A] - J[ BC]] or I AI - J[ DE]]
Load Accumulator from memory locati9n addressed by the contents of the specified register pair
""c
E
.:!
,,_
"'..,
~ .~
E
" c
::!;g
Mnemonic
Operand
Object Code
Bytes
Clock
Cycles
PIO AC
LD
reg,(HLI
01ddd110
I reg] - IIHL]]
LD
(BCIA
(DEI. A
02
12
1
1
7
7
II BC]] - I A] or II DEll - I A]
(HLI.reg
01110sss
II HL]] - I reg]
LD
..5<:-
Ii:
LD
LD
19
19
tents
I reg] Load
II xy] +
of HL
II xy] + disp]
register from memory location using base relative addressing
dlsp] - I reg]
Index register
CAl
LDIR
ED BO
20/16""
Ol
J:
":E
~
....
"a
iii
= 0:
'"..,"c
~c
LDDR
ED B8
20/16""
Type
Mnemonic
LDI
Operand
Object Code
ED AO
Bytes
Clock
Cycles
Pia AC
X
16
N
0
I HLI - I HLI + 1
[ BC] - [ Bcl - 1
Transfer onB byte of data from the memory location addressed by
the contents of HL to the memory location addressed by the con-
tents of DE Increment source and destination addresses and decrement byte count
..
LDD
ED A8
16
CPIR
ED Bl
20/16"
'i:i
:l
~c
0
Co)
-.,j
'"..~
.
".
Ul
.!
"c
~
"""
iii
CPDR
ED 69
20/16"
Mnemonic
Operand
CPI
Object Code
ED A1
Bytes
Clock
Cycles
16
P/O
AC
"g"i5
= 1:
.!"o ':;::;CC
C
E.2
... J:
::<. u
ED A9
CPO
16
u "
-o =
=
lQ
(J)
ADD
Co)
I
A,(HLl
A.(xy +disp)
86
11x11101 86 disp
19
[AJ -
dressing
[AJ - [AI + [I HLIJ + C or [ AJ - [Al + [I xyl + dispJ + C
iog
[AI - [AJ - [I HLIJ or [AI - [ AI - [I xyl + displ
co
ADC
A,(HU
A. (xy +displ
=
C
.
.!.
a::
SUB
>
l;
S8C
8E
11x111018Edisp
19
.
.
::;
1::
AND
"u
C
0
(J)
OR
(HLI
(xy + displ
96
11x1110196disp
A.(HU
A. (xy + disp)
9E
11x111019Edisp
(HLI
(xy + disp)
A6
11x11101 A6disp
19
(fill
B6
11x11101 B6 disp
19
(xy + disp)
19
19
tive addressing
[Al - [AI - [[ HLJI - C or [AJ -
[AI -
[ AI V [[ xyl + displ
Mnemonic
Oparand
(HU
o .:
"
E
CP
,; g
8;
.
INC
>g
Ul8!.
W
I
N
Clock
Cycles
Status
Operation Performed
C
AE
l1xlll0l AE disp
1
3
7
19
(HU
(XV + displ
BE
llxlll0l BE disp
1
3
7
19
IHU
Ixv + disp)
(HU
(XV + displ
34
l1xl1101 34 disp
35
l1xlll0l 35 disp
1
3
1
3
11
23
11
23
(HU
CB 06
15
Ixv + disp)
l1xlll0l CB disp
06
23
PIO AC
P
N
0
+ disp]
relative addressing
:E g
Bytes
Ixv + disp)
XOR
~'i
Object Code
DEC
RLC
CD
+ disp]
+1
[[ HLJJ - [[ HLJJ - 1 or [[
&y7~OtJ
[[ HLJJ or [[
xvI + disp]
a:
"c
::'"
CB 16
15
l1xlll0l CBdisp
16
23
(HU
CB OE
15
Ixv + disp)
l1xl1101 CBdisp
OE
23
IHU
RL
(XV
+ disp)
:c
~7 .-oiJ
[[ HLJJ or [[ xvI
.
0
:E
RRC
+ disp]
Ul
l{7
~0J=LEJ
[[ HLJJ or [[ XV]
+ displ
Mnemonic
RR
Operand
IHLI
Ixy + disp)
Object Code
CB IE
llxlll0l CBdisp
Bytes
2
4
Clock
Cycles
15
23
Status
Operation Performed
Pia
AC
IE
'ii
SLA
"
'S"
IHLI
Ixy + disp)
CB 26
llxlll0l CBdisp
2
4
15
23
2
4
15
23
g
W
I
!
0
a:
c
SRA
."
IHLI
Ixy + disp)
::'"
CB 2E
llxlll0l CB disp
EJ.--17
<OIIliI
r--
26
~7--'O~
7 --'0
2E
:E
f/l
>
j Shift
:;
::<'"
SRL
IHLI
(xy + disp)
CB 3E
llxlll0l CB disp
3E
2
4
15
23
0-..j7---"'0r---E]
[[ HLJJ or [[ xy] + disp]
Shift contents of memory location right and clear MSB (logical Shift)
Type
Mnemonic
Operand
LD
reg, data
LD
E
E
LD
rpdata16
xV.data16
(HLI.data
(xv'+ disp).
data
..,.!!.
Object Code
00ddd110
vv
Bytes
Clock
Cycles
3
4
2
4
10
14
10
19
Status
Operation Performed
C
P/O
AC
N
[reg] -data
Load immediate into register
OOxxOOO 1 vvvv
11x11101 21 vvvv
36 VV
l1xl110136disPVY
+ dispi - data
addressing
JP
label
C3 ppqq
10
[PCI -label
JR
disp
18 (disp-2)
12
[PCI - I PCI
"-
..,:>
JP
c..>
I
c..>
CALL
(HLI
(XV)
E9
11x11101 E9
1
2
4
8
label
CD ppqq
17
..
..,
.
1i
cond.label
11ccc100 ppqq
10117
RET
C9
10
11 cccOOO
5/11
'iii
.,
(,)
:S:>
e
:>
(/J
[[ SP] - 1I - 1PC[HII]
[[ SPI - 21 - [ PC(LO))
CALL
.c
1SPI - 1SPI - 2
[PCI -label
a::
+ 2 + (disp-Z)
sequence
1PC(LO)) - [[ SPJI
[PC(HIII - [[ SPI + 11
1SPI - 1SPI + 2
Return from subroutine
RET
cond
Mnemonic
ADD
Operand
Adata
Object Code
C6
yv
Bytes
Status
Clock
Cycles
PIO
AC
Operation Performed
I Al - 1Al
+ data
ADC
A.data
CE
vv
1Al - 1Al
+ data + C
.
...
.
Co
SUB
data
06
vv
SBC
A.data
DE
vv
[ Al -
'6
E
E
w
(.,
[ Al - data - C
AND
data
E6
vv
OR
data
F6
vv
[ Al -
[ Al V data
XOR
data
EE
vv
[AI- [AI-\l-data
CP
data
FE
vv
IAl - data
Compare immediate data with Accumulator contents; only the
flags are affected
JP
cond.label
10
11 cccOl 0 ppqq
is true
JR
C.disp
38 (disp-21
7/12
'6
c
JR
NC.disp
30 (disp-2)
7/12
::l
1PCI - I PCI
+ 2 + (disp - 2)
If C
= O. then
[ PCl - [PCl
+ 2 + (disp -21
JR
Z,disp
28 (disp-2)
7/12
If Z
7/12
If Z
0
Co
..,E
= 1. then
()
If C
= 1. then
1 PCl - [PCl
+ 2 + (disp -21
JR
NZ.disp
20 (disp-21
= O. Ihen
[ PCl - I PC]
+ 2 + (disp -21
DJNZ
disp
10 (disp-2)
8/13
181-181-1
If [81 ,pO. then 1PCl
+2 +
(disp ~'2)
Mnemonic
LD
Operand
dst.src
Object Code
01dddsss
Bytes
Clock
Cycles
Status
Operation Performed
PIa
Ac
I dstl - [ src!
Move contents of source register to destination register Register
designations src and dst may each be A, B. C, D, E, H or L
LD
A,I
ED 57
IA!-Ill
LD
A,R
ED 5F
[A!-[R)
LD
I,A
ED 47
[II-[A!
LD
RA
ED 4F
[R!-[A!
LD
SP,HL
F9
[SP!-[HLI
::;;
LD
SP,xy
11x11101 F9
10
[SP) - [ xy)
EX
DE,HL
EB
[DE) - - [HL)
'Z'"
EX
AF,AF'
OB
IAFI - - IAF')
09
w,
w
w
'0"
>
"til
'c,
a:'"
EXX
CBC)
[DE!
[HL)
- - CBC')
IDE'!
[HL')
Mnemonic
ADD
Operand
A,reg
Object Code
10000",
Bytes
Clock
Cycles
Status
Operation Performed
P/O AC
0
I AI - [AI + [regl
Add contents of register to Accumulator
[ AI - [AI + [regl + C
[ AI -
ADC
A,reg
10001",
SUB
reg
10010",
[ AI - [AI - [regl - C
I AI - I AI A [regl
[ AI -
[ AI - I regl
SBC
A,reg
10011",
AND
reg
10000",
OR
reg
10110",
[ AI V [ regl
W
.j>.
"a.
XOR
reg
10101rrr
[Aj-[AI>J.[ regl
IAI -Iregl
CP
reg
10111",
[ HLI - [ Hli
';'
"
ADD
Hl,rp
00 1001
11
'63
ADC
Hl,rp
ED 01 1010
15
"
+ [rpl + C
0:
SBC
Hl,rp
ED 01 0010
15
Hl
[ IXI - [ IXI
ADD
IX,pp
DO 00 1001
15
+ [ppl
ADD
IY,rr
FD 00 1001
15
Type
Mnemonic
Operand
Object Code
Bytes
Clock
Cycles
DAA
27
CPL
2F
..
NEG
ED 44
INC
reg
00rrrl00
~
.s,
a::
INC
rp
XV
reg
00xx0011
l1xl1101 23
00",101
1
2
1
6
10
4
rp
xv
00xxl0ll
l1xl110128
1
2
6
10
07
P/O
AC
N
Decimal adjust Accumulator, assuming that Accumulator contents are
the sum or difference of BCD operands
[AI-[AI
Complement Accumulator (ones complement)
[AI-IAI + 1
I reg] - [regl + 1
c-
..
DEC
[ reg] - [ reg] - 1
Decrement register contents
DEC
W
I
<.n
RLCA
i
c
RLA
17
.:::
:<:
rJ)
..
[A]
[AI
's,
a::
lIIII
..,a::
..
of:l
~7 .-of:l
~7
RRCA
OF
li7---'0lJ.8
[A]
Rotate Accumulator right with branch Carry
RRA
Operand
Object Code
Byte.
Clock
Cycle.
1F
Operation Performed
L:f7---'0~
I OlD I
(A!
RLC
reg
CB OODODm
~7~OP
..
;;
( regl
Rotate contents of register left with branch Carry
"cc
;;
~7~DP
to)
W
0)
I a..
reg
Rl
CB 00010rrr
( reg!
IX:
'"..
::
:E
til
0>
IX:
I
I
reg
RRC
C80DODlm
D
( regl
Rotate contents of register right with branch Carry
RR
reg
CB 00011rrr
L:f7 - - - . 0
( regl
Rotate contents of register right through Carry
SLA
reg
CB 00100m
I I IIIIIII
2
&--i7~0~0
[ regl
Type
Mnemonic
SRA
Operand
reg
Object Code
CB 00101rrr
Bytes
Clock
Cycles
S 'P/O
AC
6~0~
I reg]
SRL
reg
CB00111m
""c
.....
-...J
~a
~O~
I regl
Shift contents of register right and clear MSB (Logical Shift)
'i!i
CAl
I
CAl
RLD
ED 6F
18
11
;";
I3
[AI
II:
I
01
'tl
C
::"
I3 oI
I ~
11
Rotate one BCD digit left between the Accumulator and memory loca-
:c
tion (implied addressing) Contents of the upper half of the Accumulator are not affected
'"
i
a:
RRD
ED 61
18
11
I3
[AI
r
o
t Il
11 4 I 3 o 1
[[HLJI
Rotate one BCD digit right between the Accumulator and memory
location (implied addressing) Contents of the upper half of the Accumulator are not affected.
Mnemonic
Operend
Object Code
Bytes
Clock
Cycles
Status
Operation Performed
P/O AC
BIT
b,reg
CB 01bbbrrr
Z -reglbl
BIT
b,lHLI
b,(xy -l displ
CB 01bbb110
11x11101 CB disp
01bbb110
CB 11 bbbrrr
2
4
12
20
Z-
reg(bl-1
CB 11bbb110
l1x11101 CB disp
11bbb110
CB 10bbbrrr
2
4
15
23
I[ HLIJ(bl -
reg(b) - 0
C810bbb110
11x11101 CBdisp
10bbb110
2
4
15
23
11xx0101
11x11101 E5
1
2
r:
0
"
C
..
:;;
0.
iii
SET
b,reg
SET
b,lHLI
b,!xV + displ
Zero flag contains complement of selected bit of the memory laeation (implied addressing or base relative addressing)
RES
b,reg
RES
b,lHLI
b. (XV + displ
1 or [[ xyl + dispJ(bl -
CAl
I
CAl
co
PUSH
pr
xy
11
15
[[ SPI-11 - [pr(HI}]
[[ SPI-2] - [pr(LOI]
I SP] - [ SPI-2
Put contents of register pair or Index register on top of Stack and
decrement Stack Pointer
POP
'"
~"
pr
XV
11xxOO01
11x11101 E1
1
2
10
14
[ pr(LO}] - [[ SPJI
Ipr(HII]-[[SP! + 11
I SPI - [SPI + 2
Put contents of top of Stack in register pair or Index register and
increment Stack Pointer
EX
ISPI,HL
(SPl,xv
E3
11x11101 E3
1
2
19
23
[H]--[[SPI+1!
[LJ-- [[SPIJ
Exchange contents of HL or Index register and top of Stack
Type
Mnemonic
01
EI
RST
Operand
Object Code
F3
FB
llxxxl11
Bytes
Clock
Cycles
1
1
1
4
4
11
2
2
2
2
2
14
14
8
8
8
Status
Operation Performed
PIa AC
N
Disable interrupts
Enable interrupts
1I SPI-1 I - [PCIHIII
1I SPI-21 - [ PCllO)]
[ SPI - [ SPI-2
[PCI -18.n)16
11
Co)
w
co
RETI
RETN
1M
0
1
2
ED
ED
ED
ED
ED
40
45
46
56
5E
..
SCF
37
l!!
CCF
3F
Nap
HALT
00
76
1
1
4
4
a
f/)
C-l
Set Carry flag
C-C
Complement Carry flag
No operation -
OBJECT CODE
INSTRUCTION
INSTRUCTION
39
ADD
HL,SP
00
Nap
01 yyyy
LD
BC,data16
3A ppqq
LD
A,laddrl
02
03
LD
INC
IBCl.A
BC
3B
DEC
SP
04
INC
3C
3D
INC
DEC
A
A
05
06 yy
DEC
3E yy
LD
Adeta
LD
B.data
3F
CCF
07
RLCA
40sss
LD
B.reg
08
EX
AF.AF
46
LD
B.lHL)
09
ADD
HL,BC
4 lsss
LD
C,rag
OA
LD
A,IBCl
4E
LD
C.IHLl
OB
DEC
BC
50sss
LD
D.reg
OC
INC
56
LD
D,IHU
00
DEC
5 lsss
LD
E,reg
OE yy
LD
C,data
5E
LD
E,IHL)
OF
RRCA
6 Osss
LD
H.reg
10 disp-2
DJNZ
disp
66
LD
H.IHU
11 yyyy
LD
DE.data16
6 lsss
LD
L.reg
12
LD
IDElA
6E
LD
L,IHLl
13
INC
DE
10sss
LD
IHLl.reg
14
INC
76
HALT
15
DEC
7 1555
LD
A,reg
16 yy
LD
D.data
7E
LD
A.lHU
17
RLA
80m
ADD
A.reg
18 disp-2
JR
disp
86
ADD
A,IHL)
19
ADD
ADC
ADC
lB
LD
DEC
HL.DE
A,IDE)
81m
lA
DE
90m
SUB
A,IHU
reg
lC
INC
96
SUB
IHL)
10
IE yy
DEC
91m
SBC
A,reg
LD
E,data
9E
SBC
IF
RRA
AOrrr
AND
A.IHU
reg
20 disp-2
JR
NZ,disp
A6
AND
21 yyyy
LD
HL,datalf
Alm
XOR
IHU
reg
22 ppqq
LD
laddrl.HL
AE
XOR
IHL)
23
INC
HL
BOrn
OR
reg
24
INC
B6
OR
25
26 yy
DEC
B 1m
CP
IHLI
reg
LD
H.data
BE
CP
IHU
27
DAA
CO
RET
NZ
28 disp-2
JR
Cl
POP
BC
29
ADD
Z.disp
HL,HL
C2 ppqq
JP
NZ.addr
2A ppqq
LD
HL,(addrl
C3 ppqq
JP
addr
NZ,addr
8E
A,reg
2B
DEC
HL
C4 ppqq
CALL
2C
INC
C5
PUSH
BC
20
DEC
C6 yy
ADD
A,data
L,data
C7
RST
OOH
C8
RET
2E
LD
2F
CPL
30 disp-2
31 yyyy
32 ppqq
JR
NC,disp
C9
RET
LD
LD
SP.data16
laddrl.A
CA ppqq
CBOOm
JP
RLC
reg
IHL)
Z,addr
33
34
INC
SP
CB 06
RLC
INC
IHLl
CBO 1m
RRC
reg
35
DEC
IHU
CB OE
RRC
IHL),data
36 yy
LD
37
SCF
38
JR
C.disp
3-40
CB 1 Om
RL
IHU
reg
CB 16
RL
IHL)
CB 11m
RR
reg
CB 2 Orrr
SLA
CB 26
CB 2 lrrr
SLA
SRA
CB 2E
CB 3 lrrr
SRA
CB 01bbbrrr
CB 01bbbll0
CB 10bbbrrr
CB 10bbbll0
CB llbbbrrr
CB llbbbll0
CC ppqq
CD ppqq
CE yy
INSTRUCTION
CB IE
CB 3E
In
SRL
SRL
BIT
BIT
RES
DO CB disp l1bbbl10
IHLI
reg
IHL)
DO El
DO E3
SET
POP
DO E5
EX
PUSH
reg
DO E9
JP
IHLI
DO F9
DE yy
LD
SBC
OF
EO
RST
RET
POP
JP
b,reg
b.IHL)
b.IHLI
SET
b,reg
SET
CALL
b.IHLI
CF
DO
01
RET
POP
02 ppqq
03 yy
JP
OUT
04 ppqq
05
06 yy
CALL
PUSH
INSTRUCTION
DO CB disp 10bbbl1O
RES
RES
CALL
ADC
RST
OBJECT CODE
IHLI
reg
b,reg
El
E2 ppqq
E3
E4 ppqq
E5
E6 yy
Z.addr
addr
A.data
OBH
NC
E7
E8
E9
DE
NC.addr
IportlA
NC.addr
DE
EX
CALL
PUSH
AND
RST
RET
EA ppqq
EB
JP
JP
EX
EC ppqq
ED 01dddOOO
CALL
IN
ED 01sss001
ED 01xx 2
OUT
SBC
LO
NEG
b,(lX+displ
b,(lX+disp)
IX
ISPI.IX
IX
(IX)
SP,IX
A,data
18H
PO
HL
PO,addr
ISP),HL
PO,addr
HL
data
20H
PE
(HL)
PE.addr
DE,HL
PE,addr
reg.lCI
ICl.reg
HL,rp
data
07
SUB
RST
DB
09
RET
EXX
EO 01xx 3 ppqq
ED 44
DA ppqq
DB yy
JP
IN
C.addr
A.lpert)
ED 45
RETN
CALL
C.addr
ED 010nnl1O
ED 47
1M
DC ppqq
LO
I.A
DO OOxx 9
DO 21 yyyy
DO 22 ppqq
ADD
IX.pp
IX.data16
laddrl.IX
ED 01xx A
ED 01xx B ppqq
ED 40
AOC
LD
RETI
HL,rp
rp,laddri
IX
IX.laddri
ED 4F
ED 57
LD
LD
IX
(IX + displ
(IX + displ
ED 5F
ED 67
LD
RRD
RLO
RA
A,I
A,R
DO 23
DO 2A ppqq
LD
LD
INC
LD
DO 2B
DO 34 disp
DEC
INC
DO 35 disp
DEC
LD
DO 36 disp yy
DO 01dddll0 diso
10H
ED 6F
ED AO
(IX + displ.data
reg.(lX + disp)
DO 86 disp
DO 8E disp
ADD
ADC
DO 96 disp
DO 9E disp
SUB
SBC
(IX + displ
ED A8
ED A9
A,lIX + disp)
ED AA
CPO
iNO
DO A6 disp
DO AE disp
AND
XOR
(lX+displ
(lX+displ
ED AB
ED 80
LOIR
DO B6 disp
OR
DO BE disp
CP
(IX + displ
(IX + displ
ED Bl
ED B2
CPIR
INIR
DO CB disp 06
RLC
OTIR
ED B8
LOOR
DO CB disp 16
RRC
RL
(IX + diSPI
(IX + displ
ED B3
DO CB disp OE
(lX+displ
ED B9
CPDR
DO CB disp IE
RR
(IX +dispi
ED BA
INOR
DO CB disp 26
SLA
(lX+dispi
DO CB disp 2E
DO CB disp 3E
DO CB disp 01bbbl10
SRA
SRL
BIT
(IX + displ
(lX+displ
ED BB
EE yy
EF
OTOR
XOR
RST
(IX + dispI,reg
A,(lX+ disp)
A,(lX+disp)
b,(lX+displ
3-41
LDI
CPI
INI
LD
LD
ED Al
ED A2
ED A3
laddrl.rp
oun
LOO
OUTO
data
28H
INSTRUCTION
OBJECT CODE
INSTRUCTION
FO
RET
FD 8E disp
ADC
A,liY+disp)
Fl
pop
AF
FD 96 disp
SUB
liY+disp)
F2 ppqq
JP
P.addr
FD 9E disp
SBC
A,lIY+ disp)
F3
01
FD A6 disp
AND
lIY+disp)
lIY+ displ
F4 ppqq
CALL
P.addr
FD AE disp
XOR
F5
PUSH
AF
FD B6 disp
OR
liY+displ
F6 yv
OR
data
FD BE disp
CP
lIY+ displ
liY+ displ
F7
RST
30H
FD CB disp 06
RLC
F8
RET
FD CB disp OE
RRC
lIY+ disp)
F9
LD
SP.HL
FD CB disp 16
RL
liY+dispJ
FA ppqq
JP
M.addr
FD CB disp lE
RR
IIY+disp)
Fa
Fe ppqq
EI
FD CB disp 26
SLA
lIY+ disp)
FD CB disp 2E
SRA
IIY+displ
CALL
M.addr
FD ooxx 9
ADD
IY.IT
FD CB disp 3E
SRL
lIY+dispi
FD 21 yvyv
LD
IY.data16
FD CB disp 01bbbll0
BIT
b,liY+ dispi
FD 22 ppqq
LD
laddrJ.IY
FD CB disp lObbbl10
RES
b,liY+ disp)
FD 23
FD 2A ppqq
INC
IY
IY,(addr)
SET
POP
IY
FD CB disp llbbbll0
FD El
FD E3
b,liY+disp)
IY
(SPI,IV
FD 2B
LD
DEC
FD 34 disp
INC
lIY+ displ
FD E5
PUSH
IY
FD 35 disp
DEC
lIY+ displ
FD E9
JP
lIY)
FD 36 disp yv
LD
lIY + dispi,data
FD F9
LD
SP.lY
LD
rag.lIY + displ
FE yv
CP
data
FD 7 Osss disp
LD
lIY + dispi,reg
FF
RST
38H
FD 86 disp
ADD
A,lIY+disp)
3-42
EX
Data
~c+xx+~
xx
B.C
D.E
H.L
SP
~-:i""'mmmm+D
mmmm
PC
IX
~
Program
Memory
IV
I
R
I
I
CE
yy
mmmm
mmmm+ 1
mmmm+2
mmmm+3
--
ADC A.
data
CE
vv
-.-
Add the contents of the next program memory byte and the Carry status to the Accumulator.
Suppose xx=3A16. VV=7C16. and Carry=O. After the Instruction
ADC
A.7CH
~"Sto
0011
01 11
1010
1 100
0
0110
fJ
1011
1
No carry. set C to 0
LNoo-"ro
""'t ~t Z to 0
Carry. set AC tb 1
Addition instructIOn. set N to 0
The ADC Instruction is frequentlv used in multibyte addition for the second and subsequent bytes.
3-43
ADC A,reg -
ZACP/ON!
r,
tillIillI2IEl
Data
xx+yy+C
"-
} ".10'
xx
,.
B.C
O.E
-..A,B,C,D,E,H
orLisyy
H.L
SP
PC
__---::I mmmm +
mmmm
IX
IY
I
R
~
Program
Memory
10001 xxx
mmmm
t-_ _-i\mmmm + 1
1-_ _--1mmmm + 2
1-_ _--1mmmm + 3
ADCA
reg
'-v-'
10001
xxx
000
for
for
for
for
for
for
for
001
010
011
100
101
111
reg=B
reg=C
reg=D
reg=E
reg=H
reg=L
reg=A
Add the contents of Register A, B, C. D. E. H or L and the Carry status to the Accumulator.
Suppose xx=E316. Register E contains A016' and Carry= 1. After the instruction
ADC
A.E
1 1 10
1010
1000
1 sets S to 1
Carry, set C to 1
001 1
0000
1
0100
fU
'
10 0
No carry, set AC to 0
The ADC instruction is most frequently used in multibyte addition for the second and
subsequent bytes.
3-44
Data
Memorv
XIO XI
....l..
'::x.+yy+~
~x
yy
ppqq
B.C
D.E
H.L
PP
SP
PC
mmmm
..l~mmm+V
IX
IY
I
R
Program
Memory
~
BE
mmm
mmm m+1
mmmm+2
mmmm+3
8E
Add the contents of memory location (specified by the contents of the HL register pair)
and the Carry status to the Accumulator.
Suppose xx=E316. yy=A016. and Carry=l. After the instruction
ADC
A.(HL)
1 1 10
1010
001 1
0000
1
0100
fJ
1000
, 00" S 10 ,
Carry. set C to 1
N,"_""
,,,"Ie '"
Z 10 0
No carry. set AC to 0
8E
Add the contents of memory location (specified by the sum of the contents of the IX
register and the displacement digit dl and the Carry to the Accumulator.
ADC A(lY+disp)
--..--.-
FD
8E
This instruction is Identical to ADC A.(IX+displ. except that It uses the IY register instead of the IX register.
The ADC instruction is most frequently used In multibyte addition for the second and
su bseq uent bytes.
3-45
ADC Hl,rp S
ZACP/ON
Data
B.C
D.E
H.L
SP
PC
xx
~xxx+yyyy
xx
......
mmmm
IX
IY
I
+c
Program
Memorv
~mmm+2
mmmm
mmmm + 1
mmmm+2
t - - -...... mmmm + 3
ED
01xxl0l0
l2t
ED 01
X X
1010
00
01
10
11
for
for
for
for
-.-
rp IS register pair Be
rp is register pair DE
rp is register pair HL
rp IS Stack Pointer
Add the 16-blt value from either the BC. DE. HL register pair or the Stack POinter. and
the Carry status. to the HL register pair.
Suppose HL contains A53616. BC contains 104416. and Carry=l. After execution of
ADC
HL.BC
1010010100110110
0001000001000100
1
1011010101111011
The ADC Instruction is most frequently used In multibyte addition for the second and
subsequent bytes.
3-46
Z AcP/o N C
:mmrmm
=--:'"~>-
xx
B.C
D.E
H.L
SP
PC
.;&~mmm+!)
mmmm
IX
IY
I
R
I~~" I
Data
I
I
Program
Memory
C6
yy
--...-.
-.-
C6
yy
mmmm
mmmm+l
mmmm+2
mmmm+3
ADD A. data
Add the contents of the next program memory byte to the Accumulator.
Suppose xx=3A 16. yy= 7C 16. and Carry=O. After the instruction
ADD
A.7CH
101 1
1 " " S to 1
No carry. set C to 0
1010
1100
01 10
3-47
ADD A,reg S
Ac P/O
Data
I x I x I x I x I 0 Ixl
f.
xx
yy
' " x::- )
} ~Io'
B.C
--'A.B.C.D,E.
D,E
HorL,syy
- +v
H,L
SP
PC
mmmm
IX
IV
I
- ---:;;'mmm
Program
Memory
10000xxx
mmmm
mmmm+1
1---""'""1 mmmm + 2
I
I
~
t - - - - - t mmmm + 3
ADD
-..--'
reg
-.-
10000 xxx
000
001
010
011
100
101
111
for
for
for
for
for
for
for
reg=B
reg=C
reg=D
reg=E
reg=H
reg=L
reg=A
Carry, set C to 1
1 1 10
10 10
1000
001 1
0000
0011
r
-
IS
Z 10
No carry. set AC to 0
Addition Instruction. set N to 0
3-48
Z AC Pia N C
Data
x X X X a X
F~
A
_/ r-
~......
xx
B.C
xx+yy
o.E
H.L
SP
PC
mmmm
IX
ppqq
L-."'""':I'mmmm +
~
......
Memory
YY
ppqq + d
Program
Memorv
IY
I
R
I
L.0pqq +
d)"=
00
86
d
mmm m
mmm m+ 1
mmm m+2
mmm m+3
~-,.-
DO
86
Add the contents of memory location (specified by the sum of the contents of the IX
register and the displacement digit d) to the contents of the Accumulator.
Suppose ppqq=400016. xx= 1A16, and memory location 400F16 contains 5016. After
the instruction
ADD A.(lX+OFH)
has executed, the Accumulator will contain 6A16.
lA
0001
50 = 0 1 0 1
01 10
1010
0000
1010
fJ
0"" S ,,0
No carry. set C to 0
No carry, set AC to 0
~+~
FD
86
This Instruction IS Identical to ADD A. (lX+displ. except that It uses the IY register In
stead of the IX register.
ADD A,(HU
~
86
This version of the instruction adds the contents of memory location. specified by the
contents of the HL register pair. to the Accumulator.
The ADD Instruction IS a routine data manipulation instruction.
3-49
ADD HL,rp -
S Z ACP/D N C
Data
FI ! Ix I 10 Ix I
BC. DE. HL or SP
contain VYVY
A
B.C
D.E
H.L
xx
SP
PC
"....,
xx
......
mmmm
IX
IV
I
I.
Program
Memory
~mmm+1
00xx1001 mmmm
mmmm
1)t
00 xx 1001
OOfor
01 for
10 for
11 for
rp IS register pair BC
rp is register pair DE
rp is register pair HL
rp IS Stack POinter
Add the 16-blt value from either the BC. DE. HL register pair or the Stack Pointer to the
HL register pair.
Suppose HL contains 034A16 and BC contains 214C16. After the Instruction
ADD HL.BC
has executed. the HL register pair will contain 249616.
034A
0000001101001010
214C = 0010000101001100
0010010010010110
No carry. set C to
1----; mmmm 2
....- - - 1 mmmm 3
0.J
tJ....
"'lI,..-
NO carry. set AC to 0
Addition instruction. set N to 0
IS
3-50
Z AC PIO N C
Data
FI \ Ixl \o\xl
A
S.C
D.E
H.l
rr
55
SP
PC
mmmm
IX
ppqq
IY
I
R
~ :...;;,mmm
.........
........
+')
" ~pqq+rr5~
...... -
Program
Memorv
l1vlll0l
OOxxl00l
mmmm
mmmm + 1
t-_ _-Immmm + 2
I -_ _-Immmm + 3
/R
11..4- 11 01 00J- 1001
---J
00
01
10
11
for
for
for
for
rp IS register pair BC
rp is register pair DE
rp IS specified Index register
rp is Stack POinter
Add the contents of the specified register pair to the contents of the specified Index
register.
Suppose IY contains 4FF016 and BC contains 000F16 After the instruction
ADD IY.BC
has executed. Index Register IY will contain 4FFF16
3-51
Z AC PIO N
Data
A
B,C
D,E
H,L
SP
PC
_-~mmm+v
mmmm
IX
IY
I
R
z XX.VY.r
-
I-.
Program
Memorv
E6
yy
mmmm
mmmm+ 1
mmmm+2
I - - - - - - i mmmm + 3
AND
-.-
data
E6
yy
-.-
AND the contents of the next program memory byte to the Accumulator.
Suppose xx=3A 16. After the Instruction
AND 7CH
has executed, the Accumulator will contain 3816.
3A
7C
sets S to
001 1
01 1 1
1010
1 100
0011
1000
o..-J
This IS a routine logical instructIOn: It IS often used to turn bits "off" For example, the
Instruction
AND 7FH
will unconditionally set the high order Accumulator bit to O.
3-52
"r,
} ,~Io'
Z ACP/O N C
xxyy
0 xl 1 X lOIoi
J
xx
B.C
--liIooA,B,C,D.E,
HorLlsyy
D.E
H.L
SP
PC
IX
IY
I
mmmm
:;;&-::1. mmmm + 1
......
I
I
Data
~
Program
Memorv
10100xxx
mmmm
I-_ _~mmmm + 1
I-_ _-Immmm + 2
I-_ _-Immmm + 3
AND
-..10100
reg
xxx
000
for reg=B
for reg=C
forreg=D
for reg=E
for reg=H
for reg=L
for reg=A
001
010
011
100
101
111
AND the Accumulator with the contents of ReglsterA. B, C. D. E. H or L. Save the resu It
in the Accumulator.
Suppose xx=E316' and Register E contains A016. After the instruction
AND E
has executed. the Accumulator will contain A016.
E3
AO =
1 1 10
10 10
1010
001 1
0000
0000
sets S to 1..-J
3-53
Data
Memory
Z AC PIC N C
'XIXI11xloioi
__--f xx vv ) . . . . -
xx
A
B,C
D,E
H,L
~::J.mmmm+3
mmmm
--
ppqq
IY
I
R
ppqq +d
1\
SP
PC
IX
VV
I
I
ppqq+dJ-
Program
Memory
FD
A6
d
t----'==------f
mmm m
mmmm+l
mmmm+2
mmmm + 3
~~
FD
A6
AND the contents of memory location (specified by the sum of the contents of the IY
register and.the displacement digit d) with the Accumulator.
Suppose xx=E316, ppqq=400016, and memory locatIOn 400F16 contains A016' After
the Instruction
AND (IY+OFH)
has executed, the Accumulator will contain A016'
E3
1 1 10
AO =
_~...:.~_~...:~_-,--~..:..~_~_~
1 sets S to
01 1 1
1~
~-.-
DO
A6
This instruction IS Identical to AND (IY+disp), except that It uses the IX register Instead
of the IY register,
AND (HL)
~
A6
AND the contents of the memory location (specified by the contents of the HL register
pair) with the Accumulator.
AND is a frequently used logical Instruction,
3-54
BIT b,reg -
s
F
Z ACP/O N C
lulblllulol I
"
~
b
Data
A
B.C
yyy'Dyyyy
D.E
--...... --
H.L
SP
PC
mmmm
IX
IY
I
R
__~:::l mmmm+2
Program
Memory
CB
01bbbxxx
mmmm
mmmm + 1
mmmm+2
t - - - - I mmmm + 3
BIT
CB 01
b.
-.-
reg
~ ~
Bit Tested
a
1
2
3
4
5
6
7
Register
000
001
010
011
100
101
110
111
000
001
010
011
100
101
111
--BC
0
E
H
L
3-55
ZACP/ON
Data
Memory
Flu!SlllulOI I
B.C
D.E
pp
../
yyy'Dyyyy
Program
p.q
H.L
-,.
SP
PC
IX
mmmm
I -~:I mmmm + 2
Memory
IY
I
mm mm
CB
01bbb110 mm mm+1
mmmm+2
11----1 mmmm + 3
The illustration shows execution of BIT 4.(HL). Bit 0 IS the least significant bit.
BIT
-.-
b,
(HL)
CB 01
bbb
-.-
110
~-
3-56
Suppose Index Reglste( IX contains 4000H and bit 4 of memory location 4004H IS 0,
The instruction
BIT 4, (IX +4H)
will then set the Z flag to 1, while bit 4 of memory location 4004H remains 0,
JK
FD CB d 01 bbb 110
bbb IS the same as In BIT b, (HL)
This instruction is identical to BIT b,(lX+displ. except that It uses the IY register instead
of the IX register,
CAll label S
Z AC Pia N C
! ! !
Data
I I I
Memorv
A
B,C
mm+3
mm
..I::xxxx-Z
D,E
H,L
SP
PC
../y
rAI"
xxxx
mmmm
IX
IY
Ii
R
b-~mmm+D-
xxxx-Z
xxxx-1
xxxx
Program
Memorv
mmmm
mmmm+ 1
{
qq
mmmm+Z
t--'-'--I mmmm + 3
CO
pp
CALL
label
"-v-'
"-v-'
CD
ppqq
Store the address of the instruction following the CALL on the top of the stack: the top
of the stack is a data memory byte addressed by the Stack POinter, Then subtract 2
from the Stack POinter in order to address the new top of stack, Move the 16-bit address
contained in the second and third CALL instruction object program bytes to the Program Counter. The second byte of the CALL instruction IS the low-order half of the address, and the third byte is the high-order byte,
ConSider the Instruction sequence:
CALL
AND
SUBR
7CH
SUBR
After the instruction has executed, the address of the AND instruction is saved at the
top of the stack. The Stack POinter IS decremented by 2, The instruction labeled SUBR
will be executed next.
3-57
CALL
condition.
label
T~I
T
--11 xxx 100
000
001
010
011
100
101
110
111
pp qq
Condition
NZ Non-Zero
Z
Zero
NC Non-Carry
C Carry
PO Panty Odd
PE Parity Even
P Sign Positive
M Sign Negative
Relevant Flag
Z
Z
C
C
P/O
P/O
S
S
This instruction IS identical to the CALL instructIOn. except that the Identified
subroutine will be called onlv if the condition is satisfied: otherwise. the Instruction sequentially following the CALL condition instruction will be executed.
Consider the instruction sequence:
CALL
: COND.SUBR
7CH
condition
satisfied
SUBR
If the condition IS not satisfied. the AND instruction will be executed after the CALL
COND.SUBR instruction has executed. If the condition IS satisfied. the address of the
AND instruction is saved at the top of the stack. and the Stack POinter IS decremented
by 2. The instruction labeled SUBR will be executed next.
3-58
ZACP/ON C
[I::Ii:::II::I::J::J12X~!t:;;.i
. i=======~"'~~
A
B.C
D,E
H.L
SP
PC
mmmrn
-/ :v
_--l. mmmm+ 1
IX
IY
I
R
Data
~
Program
emorv
~
3F'
CCF
3F
Complement the Carry flag. No other status or register contents are affected,
3-59
mmmm
mmmm+l
mmmm+2
mmmm+3
S Z AC P/Q N C
Data
(XW]JxDW
r---<
xx
A
S,C
D,E
H,L
SP
PC
___
~mmm+.D
mmmm
IX
IY
I
R
xx-yv:r-
~
Program
Memorv
FE
mmmm
vy
mmmm+l
mmmm+2
mmmm+3
CP
data
FE
yy
Subtract the contents of the second oblect code byte from the contents of the Accumulator. treating both numbers as simple binary data. Discard the result i.e.. leave
the Accumulator alone, but modify the status flags to reflect the result of the subtraction.
Suppose xx=E316 and the second byte of the CP Instruction object code contains
AO 16. After the Instruction
CP OAOH
has executed, the Accumulator will still contain E316, but statuses will be modified as
follows:
1 1 10
1010
0100
E3
AO
o sets
001 1
0000
0011
fJ
S to 0
No borrow. set C to 0
re' Z" 0
LN,o'M' rewlt.
complemented,
3-60
s
F
,,-
Z AC P/O N C "-
XX-YY " )
IxlxlxlxlJixl
1
xx
}-...- A,B,C,D,E,H
eoo.m,,'
B,C
D,E
H,l
SP
PC
or Lis yy
mmmm
~--::I
mmmm + 1
......
IX
Data
~
Program
Memory
10111xxx
..-----1
1-_ _--1
..-_ _--1
IY
CP
mmmm
mmmm + 1
mmmm + 2
mmmm + 3
reg
-.-
-.-
10111
xxx
(j'Q'Q
001
010
011
100
101
111
for
for
for
for
for
for
for
reg=B
reg=C
reg=D
reg=E
reg=H
reg=L
reg=A
Subtract the contents of Register A, B, C, D, E, H or L from the contents of the Accumulator. treating both numbers as simple binary data, Discard the result: i.e" leave
the Accumulator alone, but modify status flags to reflect the result of the subtraction,
Suppose xx=E316 and Register B contains A016' After the Instruction
CP B
has executed, the Accumulator will still contain E316, but statuses will be modified as
follows:
E3 =
1 1 10
0011
0000
AO = 1 0 1 0
fJ
101 00
0"" S 10 0
No borrow, set C to 0
001 1
Noo .""
"""'t. '"
Z 10 0
No borrow. set AC to 0
Subtract instruction, set N to 1
IS
complemented,
3-61
~
Ixlxlxlxlllx'
Data
Memory
---< -
"-
xx
XX-yy.r-
B.C
D.E
H.L
SP
PC
PP
~mmm+~
mmmm
IX
IY
P qq
Program
Memory
~
BE
yy
mmm
mmmm+l
mmmm+2
mmmm+3
BE
Subtract the contents of memory location (specified by the contents of the HL register
pair) from the contents of the Accumulator. treating both numbers as simple binary
data. Discard the result: I.e.. leave the Accumulator alone. but modify status flags to
reflect the result of the subtraction.
Suppose xx=E316 and yy=A016. After executIOn of
CP (HU
the Accumulator will still contain E316. but statuses will be modified as follows:
E3
AO =
0"" S ,,0
No borrow. set C to 0
1 1 10
0 1 10
001 1
0000
0100
0011
fJ
complemented.
CP (IX+disp)
-...--,-
DO BE
3-62
Subtract the contents of memory location (specified by the sum of the contents of the
IX register and the displacement value d) from. the contents of the Accumulator. treatIng both numbers as simple binary data. Discard the result I.e., leave the Accumulator
alone. but modify status flags to reflect the result of the subtraction.
CP (lY+disp)
--...-.FD BE
This instruction IS Identical to CP (IX +disp), except that It uses the IY register Instead of
the IX register.
t
[iJx Ix r{!1
5
...J..
' - xx-yy
Z ACP/ON C'
B.C
D.E
H.L
SP
PC
IX
xx
uu
tt
pp
mmmm
Data
Memory
lluu-1 ' )
"/
"I
"'*"
VV
.-rrf' ppqq-1
t-....
IV
mmmm+v
I
I
Program
Memory
ED
A9
pp qq
mmmm
m mmm+l
I -_ _~ mmmm + 2
1 - - - - 1 mmmm + 3
CPO
'-v-"
ED A9
Compare the contents of the Accumulator with the contents of memory location
(specified by the HL register pair), If A IS equal to memory. set Z flag. Decrement the HL
and BC register pairs. (BC IS used as the Byte Counter.)
3-63
Suppose xx=E316. ppqq=400016. BC contains 000116. and yy=A016 After the Instruction
CPO
has executed. the Accumulator will still contain E316. but statuses will be modified as
follows:
E3
AO
1 1 10
1010
0100
001 1
0000
0011
LN,"~"" ""It.
001 Z "
- - - - N o borrow. set AC to 0
l,-_
ED B9
ThiS instruction IS Identical to CPO. except that it IS repeated until a match is found or
the byte counter is zero. After each data transfer. Interrupts will be recognized and two
refresh cycles will be executed.
Suppose the HL register pair contains 500016. the BC register pair contains 00FF16.
the Accumulator contains F916. and memory has contents as follows:
Location
Contents
500016
4FFF16
4FFE16
4FF016
4FFC16
4FFB16
AA16
BC16
1916
7A16
F916
0016
After execution of
CPDR
the P/O flag will be 1. the Z flag will be 1. the HL register pair will contain 4FFB16. and
the BC register pair will contain 00FA16.
3-64
A
B,C
D,E
H,L
.J.
xx-yy
Z ACP/ON C
eX IXI x I \ : ! J ereset
t if BC-l ,. 0,
otherwise
L -
pp
Data
Memorv
tluu-l
xx
uu
tl
"/
yv
I.tIII"
r;..a--
ppq q
Ppqq'+l)
SP
PC
IX
IY
I
R
mmmm
Program
Memorv
"'-'ltiI..
~mmm+v
I
I
m mmm
m mmm+l
mmmm+2
t - - - - I mmmm + 3
ED
Al
CPI
-..-ED A1
Compare the contents of the Accumulator with the contents of memory location
(specified by the HL register pair!, If A IS equal to memory, set the Z flag, Increment the
HL register pair and decrement the Be register pair (BC IS used as Byte Counter!,
Suppose xx=E316, ppqq=400016, BC contains 003216, and yy=E316' After the instruction
CPI
has executed, the Accumulator will still contain E316' but statuses will be modified as
follows:
E3
-E3
0"" S"
1 111
0000
001 1
1 10 1
0000
0000
o---l
~,----No
borrow, set AC to 0
3-65
-..-..ED B1
This instruction IS Identical to CPI. except that it IS repeated until a match IS found or
the byte counter is zero. After each data transfer interrupts will be recognized and two
refresh cycles will be executed.
Suppose the HL register pair contains 450016. the BC register pair contains 00FF16.
the Accumulator contains F916. and memory has contents as follows:
Location
-450016
4501 16
450216
Contents
AA16
15 16
F916
---
After execution of
CPIR
the P/0 flag will be 1. and the Z flag will be 1. The HL register pair will contain 450316.
and the BC register pair will contain 00FC16.
3-66
ZACP/ON
Data
FOJI[]:[O
-~
....... xx
xx
::::""""1
B,C
D,E
H,L
SP
PC
IX
IY
I
R
~"""'mmmm+)
mmmm
CPL
~
Program
emorv
~
2F
mmmm
mmmm+ 1
mmmm+2
mmmm+3
2F
Complement the contents of the Accumulator. No other register's contents are
affected.
Suppose the Accumulator contains 3A16 After the instruction
CPL
has executed, the Accumulator will contain C516
3A = 001 1
Complement = 1 1 00
1010
0101
This IS a routine logical Instruction. You need not use it for binary subtraction: there are
special subtract instructions (SUB, SBC).
3-67
DAA S
Data
nXlxlxlxl Ixl
xx
t:)
I.-..
.!"Cpnvert
_deCimal
I.-..
~mmm+~
B.C
D.E
H.L
SP
PC
mmmm
IX
IY
I
Program
emorY
~
27
DAA
mmmm
mmmm+1
mmmm+2
mmmm+3
27
Convert the contents of the Accumulator to binary-coded decimal form. This instruction should only be used after adding or subtracting two BCD numbers: i.e.. look upon
ADD DAA or ADC DAA or INC DAA or SUB DAA or SBC DAA or DEC DAA or NEG DAA
as compound. decimal arithmetic Instructions which operate on BCD sources to generate BCD answers.
Suppose the Accumulator contains 3916 and the B register contains 4716. After the instructions
ADD B
DAA
have executed. the Accumulator will contain 8616. not 8016.
l80 CPU logic uses the values In the Carry and Auxiliary Carry. as well as the Accumulator contents. In the Decimal Adjust operation.
3-68
DEC reg 5
Z ACP/O N C
Data
F[[IXIX I x 11 LI
}L
A
B.C
D.E
H.l
SP
PC
IX
mmmm
:.:-
C,,""U of A
B. C. D. E. H.
orL,syy
:I~mmm ~
+
~
Program
Memorv
IY
I
R
OOxxxl0l
i
i
mmmm
I-_ _-Immmm + 1
I-_ _-Immmm + 2
1-_ _-1 mmmm + 3
R
00
xxx
101
000
001
010
011
100
101
111
for reg=B
for reg=C
forreg=D
for reg=E
for reg=H
forreg=L
for reg=A
-,-
3-69
DEC rp DEC IX
DECIY
S
Z AC PIO N C
I ! !
Data
! I
S.C
D.E
~COO,,""
DE. HL or"SP"
H.L
IS YYYY
SP
PC
IX
mmmm
_--~mmm+v
~
Program
Memorv
IY
OOxxl0ll
mmmm
mmmm+l
t - - - - ; mmmm + 2
t-----I mmmm + 3
The illustratIOn shows execution of DEC rp:
1l
00 xx 1011
-.00
01
10
11
for
for
for
for
rp
rp
rp
rp
IS
IS
IS
IS
register pair BC
register pair DE
register pair HL
Stack POinter
Subtract 1 from the 16-blt value contained In the specified register pair. No status flags
are affected.
Suppose the Hand L registers contain 2F0016. After the instruction
DEC HL
has executed. the Hand L registers will contain 2EFF16.
DEC IX
--..-
DD 2B
Subtract 1 from the 16-blt value contained In the IX register.
DEC IY
--..FD 2B
3-70
IS
LD
DE. DATA
DEC
LD
OR
DE
A.D
JP
NZ.LOOP
:DECREMENT COUNTER
TO TEST FOR ZERO. MOVE D TO A
:THEN OR A WITH E
:RETURN IF NOT ZERO
LOOP
Data
F([lxjxJXJ1!
yy-l
P;
VV
S.C
D.E
pp
H.L
SP
PC
mmmm
IX
IV
I
;;;&'""::;;'mmm +
Program
Memory
~
35
mmm
m mmm+l
mmmm+2
mmmm+3
DEC (HL)
'-v-"
35
Subtract 1 from the contents of memory location (specified by the contents of the HL
register paid.
Suppose ppqq=450016. yy=5F16' After execution of
DEC (HL)
memory location 450016 will contain 5E16.
__
5F = 0 1 0 1
-01 = 1 111
1111
1 111
10101
1 1 10
0 "" S to O.JJ
r
-
to 0
No borrow, set AC to 0
Subtract instruction, set N to 1
3-71
DEC (IX+disp)
~-.-
DO 35
Subtract 1 from the contents of memory location (specified by the sum of the contents
of the IX register and the displacement value dl.
DEC (IY+disp)
~-,-
FD 35
This instruction is Identical to DEC (IX+displ. except that it uses the IY register instead
of the IX register.
01 -
DISABLE INTERRUPTS
S
Z AC Pia N C
Data
FI I i I I I I
A
S.C
D.E
H.L
SP
PC
IX
IY
I
R
mmmm
,. -
t-.~
......mmmm+l
Program
emorv
~
F3
01
mmmm
mmmm+l
mmmm+2
mmmm+3
F3
When this instruction is executed. the maskable interrupt request IS disabled and the
INT input to the CPU will be Ignored. Remember that when an interrupt is
acknowledged. the maskable interrupt IS automatically disabled.
The maskable interrupt request remainS disabled until It IS subsequently enabled by an
EI instruction.
No registers or flags are affected by this instruction.
3-72
DJNZ disp -
ZACP/ON
I I I I I I
A
S.C
D.E
H.L
xx-1
Data
T..
xx
SP
PC
IX
IY
I
........mmmm+
I-.~:::l {dd-2l + 2
mmmm
---DJNZ
disp
10
dd-2
~
Program
Memorv
10
dd-2
mmmm
mmmm+ 1
mmmm+2
mmmm+3
Decrement Register B. If remaining contents are not zero. add the contents of the DJNZ
Instruction object code second byte and 2 to the Program Counter. The lump is
measured from the address of the Instruction operation code. and has a range of -126 to
+ 129 bytes. The Assembler automatically adjusts for the tWice-Incremented PC.
If the contents of B are zero after decrementing. the next sequential Instruction IS executed.
The DJNZ instruction IS extremely useful for any program loop operation. since the one
Instruction replaces the typical "decrement-then-branch on condition" instruction sequence.
Z AC PIO N C
Data
diLl! []
A
B.C
D.E
H.L
.-mmmm+ 1
SP
PC
IX
IY
mmmm
~~::I
"-
Program
emorv
~
FB
3-73
mmmm
mmmm+ 1
mmmm+2
mmmm+3
EI
FB
Execution of this instruction causes interrupts to be enabled. but not until one more instruction executes.
Most Interrupt service routines end with the two instructions:
EI
RET
:ENABLE INTERRUPTS
:RETURN TO INTERRUPTED PROGRAM
If Interrupts are processed serially. then for the entire duration of the interrupt service
routine all maskable interrupts are disabled - which means that In a multi-interrupt
application there IS a Significant possibility for one or more Interrupts to be pending
when any Interrupt service routine completes execution.
If Interrupts were acknowledged as soon as the EI instructions had executed, then the
Return Instruction would not be executed. Under these circumstances. returns would
stack up one on top of the other - and unnecessarily consume stack memory space.
This may be illustrated as follows:
Interrupt
By inhibiting Interrupts for one more instruction following execution of EI. the Z80 CPU
ensures that the RET instruction gets executed in the sequence:
EI
RET
:ENABLE INTERRUPTS
;RETURN FROM INTERRUPT
It IS not uncommon for interrupts to be kept disabled while an interrupt service routine
is executing. Interrupts are processed serlallv:
3-74
Z ACP/ON C
Alternate
Register Set
D.E
H.L
SP
PC
mmmm
IX
IY
I
R
F'
A'
B',C'
A
B.C
_~-t'mmmm + ~
EX AF.AF
~
H',L'
Program
~
emorY
08
D',E'
mmmm
mmmm+ 1
mmmm+2
mmmm+3
08
The two-byte contents of register pairs AF and AT are exchanged.
Suppose AF contains 4F9916 and AT contains lOAA 16 After execution of
EX AF,AF
AF will contain lOAA16 and AF will contain 4F9916.
3-75
EX DE,HL -
SZACP/ONC
FO:I
Data
10
S,C
D,E
H,L
SP
PC
IX
IY
I
pp
;..:
xx
VV
mmmm
"--~mmm+~
~
Program
emorv
~
ES
EX DE,HL
~
mmmm
mmmm+l
mmmm+2
mmmm+3
EB
The D and E registers' contents are swapped with the Hand L registers' contents,
Suppose pp=0316, qq=2A 16, xx=4116 and yv=FC 16, After the instructIOn
EX DE,HL
has executed, H will contain 0316, L will contain 2A 16, 0 will contain 4116 and E will
contain FC 16,
The two Instructions:
EX DE,HL
LD A,(HL)
but if you want to load data addressed by the D and E register into the B register,
EX DE,HL
LD B,(HL)
3-76
FI
ZACP/ON C
Data
Memory
I I I I I I
B.C
pp
D.E
H.L
xx
yy
SP
ssss
mmmm
PC
IX
_~
IY
I
I
I
R
mmmm+l
Program
Memory
~
E3
ssss
ssss+ 1
ssss + 2
mmmm
mmmm+l
mmmm+2
mmmm+3
EX (SP).HL
~
E3
Exchange the contents of the L register with the top stack byte. Exchange the contents
of the H register with the byte below the stack top.
Suppose xx=2116. yv=FA16. pp=3A16. qq=E216. After the Instruction
EX (SP).HL
has executed. H will contain 3A 16. L will contain E216 and the two top stack bytes will
contain FA16 and 2116 respectively.
The EX (SPl.HL Instruction is used to access and manipulate data at the top of the stack.
EX (SPl.IX
~
DD E3
Exchange the contents of the IX register's low-order byte with the top stack bvte. Exchange the IX register's high-order byte with the byte below the stack top.
EX (SPl.IY
~
FD E3
This Instruction
register.
IS
3-77
EXX -
FI I I
Altemate
Regtster Set
I I I
.. ffi ~"'.C'
F'
}.
B,C
D,E
H,L
SP
PC
mmmm
__~fmmmm+v
IX
IY
I
D',E'
H',L'
Program
emorv
~
.
EXX
D9
mmmm
mmmm+l
mmmm+2
mmmm+3
09
The contents of register pairs BC, DE and HL are swapped with the contents of register
pairs B'C', D'E', and H'L'
Suppose register pairs BC, DE and HL contain 490116, 5F0016 and 725116 respectively, and register pairs B'C', D'E', H'L' contain 000016, lOFF16 and 333316 respectively. After the execution of
EXX
the registers will have the following contents:
3-78
HALT
S
FI I
ZACP/ONC
Data
B,C
D,E
H,L
SP
PC
mmmm
~mmm+~
.....
IX
IY
I
R
Program
emorv
~
76
HALT
mmmm
mmmm+ 1
mmmm+2
mmmm+3
76
When the HALT instruction IS executed, program execution ceases, The CPU requires
an interrupt or a reset to restart execution, No registers or statuses are affected:
however, memory refresh logic continues to operate,
3-79
1M 0 -INTERRUPT MODE 0
5
F
A
S.C
D.E
H.L
SP
PC
IX
Z ACP/0N
Data
I I I I I I I
mmmm
IY
I
-- ~
Program
Memory
ED
46
mmmm
mmmm+ 1
I - - - - - i mmmm + 2
1------lI mmmm + 3
--.--IMO
ED 46
This instruction places the CPU In interrupt mode O. In this mode. the interrupting
deVice will place an instructIOn on the Data Bus and the CPU will then execute that instruction. No registers or statuses are affected.
1M 1 -INTERRUPT MODE 1
IM1
--.--ED 56
This instruction places the CPU In Interr.upt mode 1. In this mode. the CPU responds to
an Interrupt by executing a restart (RST) to location 003816.
1M 2 -INTERRUPT MODE 2
1M 2
--.,ED 5E
This instruction places the CPU In interrupt mode 2. In this mode. the CPU performs an
indirect call to any specified location In memory. A 16-bit address IS formed uSing the
contents of the Interrupt Vector (I) register for the upper eight bits. while the lower
eight bits are supplied by the interrupting device. Refer to Chapter 12 for a full descnption of interrupt modes. No registers or statuses are affected by this instruction.
3-80
Z AC P/O N C
I I I I I I I
Data
14-
A
B.C
D.E
H.l
SP
PC
I/O port yy
mmmm
I-.
IX
-=<:mmm +,V
~
Program
Memory
IV
I
DB
mmmm
YV
mmmm+ 1
mmmm+2
mmmm+3
--IN A.
DB
(port)
'-v-'
yy
Load a byte of data Into the Accumulator from the I/O port (identified by the second IN
instruction object code byte).
Suppose 3616 is held In the buffer of I/O port 1A 16. After the instruCtion
IN A.(lAH)
has executed. the Accumulator will contain 3616.
The IN Instruction does not affect any statuses.
Use of the IN Instruction IS very hardware dependent. Valid I/O port addresses are
determined by the way in which I/O logic has been Implemented. It IS also possible to
deSign a microcomputer system that accesses external logiC uSing memory reference
instructions with specific memory addresses.
3-81
Z AC
plo
N C
Flxlxlxlxlol I
~e-"o..or
B.C
B. C. D. E. H
DE
LIs YV
_/ - V
H.l
SP
PC
mmmm
_.--3.. mmmm +
IX
IY
I
R
Program
Memorv
1----1 mmmm + 3
it
00 xxx 100
000
001
010
all
100
101
111
for
for
for
for
for
for
for
reg=B
reg=C
reg=D
reg=E
reg =H
reg=L
reg=A
3-82
F[!
Data
n ] []
A
B.C
D,E
H,L
SP
PC
Co~mOC
DE. HL or SP
--'15 YYYY
"
......
:;;a:-:I mmmm + 1
mmmm
IX
IY
I
R
I
I
~
Program
Memory
OOxxOO11
mmmm
f-_ _-Immmm + 1
mmmm+2
f - - - - I mmmm + 3
The illustration shows execution of INC rp:
~
00 xx 0011
00
01
10
11
Add 1 to the 16-bit value contained
affected.
In
for
for
for
for
rp
rp
rp
rp
IS
IS
IS
IS
register pair BC
register pair DE
register pair HL
Stack POinter
Suppose the D and E registers contain 2F7 A 16. After the instruction
INC DE
---
DD 23
Add 1 to the 16-blt value contained
In
the IX register.
INC IY
FD 23
3-83
Z AC Pia N
'xixlxlxioi I
A
B.C
D.E
H.L
PC
mmmm
IX
IV
ppqq
I
R
I:!.-
yy+l
.. /"
SP
Data
Memory
~ -::l mmmm + 3
~pqq+d~
yy
./
Program
Memory
DD
34
d
pp qq
+d
t
m mmm
m mmm+ 1
m mmm+2
m mmm+3
-.-
DO 34
Add 1 to the contents of memory location (specified by the sum of the contents of
Register IX and the displacement value d).
Suppose ppqq=400016 and memory location 400F16 contains 3616. After execution
of the instruction
INC (IX+OFH)
memory location 400F16 will contain 3716.
36 = 001 1
0110
01011
o sets
...
S to 0
01 1 1
INC (IY+disp)
"-v-'
-.-
FD 34
This Instruction IS Identical to INC (IX+disp). except that it uses the IY register Instead
of the IX register.
INC (HL)
~
34
Add 1 to the contents of memory location (specified by the contents of the HL register
pair).
3-84
Z ACP/O N C
lulxlulull!
,C
xx-l
I/O port yy
Memory
A
S.C
D.E
xx
H.l
pp
yy
I..-
mmmm
mmmm+2
IX
IY
I
ppqq
.....I'" ppqq-l
SP
PC
Data
Program
Memory
mmm m
mmm m+ 1
mmmm+2
i - - - - - 1 mmmm + 3
ED
AA
INo
-.--
ED AA
Input from I/O port (addressed by Register C) to memory location (specified by HU.
Decrement Registers Band HL.
Suppose xx=0516. yy=1516. ppqq=240016. and 1916 IS held in the buffer of I/O port
1516. After the instruction
INo
has executed. memory location 240016 will contain 1916. The B register will contain
0416 and the HL register pair 23FF16
-.--
ED BA
Contents
17 16
59 16
AE16
This instruction is extremely useful for loading blocks of data from an Input device Into
memory.
3-85
INI -
Z AC Pia N C
I I I I I I I
I/O port yy
D.E
H.L
SP
PC
xx
Data
Memory
IL__.....I===~
B.C
VV
t----::7'"---+---=.::----i-~~~
ppqq + 1
pp
qq
ppqq
~-
~mmm+v
mmmm
IX
IV
I
R
Program
Memory
ED
A2
mmmm
mmmm+ 1
mmmm+2
I - - - - - f mmmm + 3
INI
-..--
ED A2
Input from I/O port (addressed by Register CI to memory location (specified by HL).
Decrement Register B: Increment register pair HL
Suppose xx=0516. yy= 1516. ppqq=240016. and 1916 IS held In the buffer of I/O port
15 16.
After the instruction
INI
has executed. memory location 240016 will contain 1916. The B register will contain
0416 and the HL register pair 240116.
Contents
17 16
59 16
AE16
ThiS instruction IS extremely useful for loading blocks of data from a deVice Into memory.
3-86
Z ACP/O N C
Ixlxlolxlol I
.1
Register
S.C
A. B. C. D. E.
D.E
H or L
H.L
SP
PC
Data
I/O port yy I
mmmm
"........
__--::I mmmm + 2
IX
IY
I
Program
Memorv
ED
mmmm
mmmm + 1
mmmm+2
.....- - - - I mmmm + 3
01xxxOOO
for reg=B
for reg=C
for reg=D
for reg=E
for reg=H
forreg=L
for reg=A
for setting of status flags without
changing registers
Load a byte of data Into the specified register (reg) from the I/O port (identified by the
contents of the C register).
Suppose 4216 is held In the buffer of I/O port 3616. and Register C contains 3616.
After the Instruction
IN D.(C)
3-87
JP label S
Z AC PIO N C
Data
""1_....."--'-_....1_'
B.C
D.E
H.L
SP
PC
mmmm
IX
IY
..-( ppqq)
Program
Memory
I
I
C3
qq
pp
mmmm
mmmm+ 1
mmmm+2
mmmm+3
JP label
-.'-.,,-'
C3
ppqq
Load the contents of the Jump instruction obJect code second and third bytes Into the
Program Counter: thiS becomes the memory address for the next instruction to be executed. The prevIous Program Counter contents are lost
In the follOWing sequence:
JP
AND
NEXT
NEXT
7FH
CPL
The CPL instruction will be executed after the JP instruction. The AND instruction will
never be executed. unless a Jump instruction somewhere else In the instruction sequence lumps to thiS instruction.
3-88
HI
11 cc 010 ppqq
000
001
010
NZ
Z
NC
C
PO
PE
P
M
all
100
101
110
111
Condition
Non-Zero
Zero
No Carry
Carry
Parrty Odd
Parrty Even
Sign Positive
Sign Negative
Relevant Flag
Z
Z
C
C
Pia
Pia
S
S
This Instruction IS Identical to the JP Instruction. except that the lump will be performed only if the condition IS satisfied: otherwise. the Instruction sequentially followIng the JP condition Instruction will be executed.
Consider the Instruction sequence
JP
condition
AND
satisfied
LABEL
OR
COND.LABEL
I condition not satisfied
7CH
After the JP cond.label Instruction has executed. if the condition IS satisfied then the
OR Instruction will be executed. If the condition IS not satisfied. the AND Instruction.
being the next sequential Instruction. IS executed.
3-89
JP (Hll JP (IX)
JP (IV)
S
Z ACPfON C
Data
FCIIIIIJ
A
B.C
D.E
H.L
pp
SP
mmmm
PC
IX
IY
I
R
Program
emorv
~
E9
mmmm
mmmm+1
mmmm+2
mmmm+3
JP (HU
'-v-"
E9
The contents of the HL register pair are moved to the Program Counter: therefore. an
Implied addressing lump IS performed.
The Instruction sequence
LD
JP
H.ADDR
(HU
AD DR
Both specify that the instruction with label ADDR IS to be executed next.
The JP (HU instruction IS usefu I when you want to Increment a return address for a
subroutine that has multiple returns.
Consider the following call to subroutine SUB:
CALL
JP
SUB
ERR
;CALL SUBROUTINE
;ERROR RETURN
:GOOD RETURN
Using RET to return from SUB would return execution of JP ERR: therefore. if SUB executes without detecting error conditions. return as follows;
POP
HL
INC
INC
INC
JP
HL
HL
HL
(HU
:RETURN
JP (IX)
-.--'
DD9
This instruction IS identical to the JP (HU instruction. except that It uses the IX register
3-90
--.,FD E9
This instruction IS Identical to the JP (HU instruction, except that It uses the IY register
instead of the HL register pair,
38
dd-2
This instruction IS identical to the JR disp instruction, except that the lump IS only executed if the Carry status equals 1, otherwise, the next instruction IS executed,
In the following instruction sequence:
I
4000
JR
C,$+8
, . . - - - - - - - - - - 1 c=o
4002
AND
C=l
'----111.... 4008
OR
7FH
After the JR C,$+8 instruction, the OR instruction IS executed if the Carry status equals
1, The AND instruction IS executed if the Carry status equals 0,
3-91
JR disp S
Z ACP/0N
I ! I
Data
I I I
B.C
D.E
H.L
SP
PC
IX
rmmmm+
~ (dd-21+2
mmmm
IY
I
R
I
I
~
Program
Memory
18
dd-2
mmmm
mmmm+ 1
mmmm+2
mmmm+3
JR disp
-.--.18 dd-2
Add the contents of the JR instruction object code second byte. the contents of the Program Counter, and 2. Load the sum Into the Program Counter. The lump IS measured
from the address of the instructIOn operation code, and has a range of -126 to + 129
bytes. The Assembler automatically adiusts for the tWice-incremented PC.
The following assembly language statement
dress 400016.
IS
JR $+4
Result of this instruction is shown below:
Location
4000
4001
4002
4003
4004
Instruction
18
02
......
111--- new PC value
3-92
-,.--.30
dd-2
r-_~4000
c=o
4001
4002
4003
4005
After the JR NC.$-3 instruction. the OR instruction IS executed if the Carry status equals
1. The ADD instruction IS executed if the Carry status equals O.
-,.--.20
dd-2
ThiS instruction is Identical to the JR disp instruction. except that the jump IS only executed if the Zero status equals 0: otherwise. the next Instruclion IS executed.
In the follOWing Instruclion sequence:
I
_ _ _...;4..;;.0.;;..00,,--_.;;..JR""--1'
z=o
:~~~
AND
NZ.$+6
+z=~FH
4005
'---"'4006
OR
After the JR NZ.$+6 instruction. the OR instruction IS executed if the Zero status equals
3-93
JR Z,disp -
dd-2
This instruction IS Identical to the JR disp instruction. except that the lump IS only executed if the Zero status equals 1 otherwise. the next Instruction IS executed.
In the following Instruction sequence:
Z=l
I
l
4000
JR
4002
AND:
Z.$+6
7FH
4004
4005
4006
OR
t z=o
After the JR Z.$+6 instruction, the OR Instruction IS executed if the Zero status equals
1. The AND Instruction IS executed if the Zero status equals O.
Z AC PIO N
Data
F'xlxlolxlol
xx
B.C
D,E
H.l
SP
PC
IX
IY
I
__,...(""mmmm + )
mmmm
Program
Memory
xx
ED
57
mmmm
mmmm+ 1
mmmm+2
t------1 mmmm + 3
ED 57
Move the contents of the Interrupt Vector register to the Accumulator. and reflect Interrupt enable status in Panty/Overflow flag.
Suppose the Interrupt Vector register contains 7F16. and Interrupts are disabled. After
execu tlon of
LD A,I
Register A will contain 7F16. and P/O will be O.
LD A.R
~
ED 5F
Move the contents of the Refresh register to the Accumulator. The value of the Interrupt
flip-flop will appear in the Parity/Overflow flag.
3-94
LD A,(addr) S
F(
Z AC Pia N C
I I I
Data
Memory
I I
YV
YV
ppqq
S,C
D,E
H,L
SP
PC
mmmm
IX
--
~:::r""mmm
IY
Program
Memory
I
I
I
R
+3
3A
qq
mmmm
mmmm+ 1
'I-"""::P;'::P--4
mmmm + 2
1 - - - - 1 mmmm + 3
LD A, (addrl
-.---.-3A
ppqq
Load the contents of the memorv bvte (addressed directlv bv the second and third
bvtes of the LD A, (addrllnstructlon object code) into the Accumulator. Suppose memorv byte 084A16 contains 2016, After the instruction
label
EQU
LD
A,(Iabef)
084AH
has executed, the Accumulator will contain 2016Remember that EQU is an assembler directive rather than an instruction: It tells the Assembler to use the 16-bit value 084A16 wherever the label appears,
The instruction
LD
A,(Iabef)
HUabel
A(HL)
When vou are loading a single value from memory, the LD A'(Iabef) instruction IS preferred: It uses one instruction and three object program bytes to do what the LD HUabel.
LD A,(HL) combination does In two instructions and four object program bytes, Also,
the LD HUabeL LD A,(HL) combination uses the Hand L registers, which LD A,(Iabef)
does not.
3-95
FI
Z ACP/0N C
I I
I !
A
S.C
yy
D.E
H.l
SP
PC
mmmm
-.
..(;,mmm+V
IX
Data
Memorv
yy
ppqq
_t
Program
Memorv
IY
I
R
OOOxl0l0 mrnmm
mmmm+l
mmmm+2
1 - - -...... mmmm + 3
LDA,(rp)
IIw
........
o if register
palr=BC
1 if register palr=DE
Load the contents of the memory byte (addressed by the BC or DE register palrllnto the
Accumulator.
Suppose the B register contains OB16, the C register contains 4A16' and memory byte
084A16 contains 3A16. After the instruction
LD A,(BC)
has executed, the Accumulator will contain 3A16.
Normally, the LD A,(rp) and LD rp,data will be used together. since the LD rp,data instruction loads a 16-bit address Into the BC or DE registers as follows:
LD
LD
BC,084AH
A.(BC)
3-96
LD dst,src S
FI
Z AC Pia N
Data
I I I I I. I
B.C
D.E
H.L
B. C.
Hir L
D. E. H. L
mmmm
IX
IY
D. E.
A.
~""~'A'C
SP
PC
Register
:;;;O:-::J.~mmm + ~
~
Program
Memorv
01dddsss
I
I
mmmm
I--_ _--tmmmm + 1
~---fmmmm + 2
~---fmmmm + 3
ill
01 ddd sss
~
000
001
010
011
100
101
111
for
for
for
for
for
for
for
dst
dst
dst
dst
dst
dst
dst
or
or
or
or
or
or
or
src=B
src=C
src=D
src=E
src=H
src=L
src=A
The contents of any designated register are loaded into any other register.
For example:
LD A.B
loads the contents of Register B into Register A.
LD L.D
loads the contents of Register 0 into Register L.
LD C.C
does nothing. since the C register has been specified as both the source and the
destination.
3-97
LD
LD
LD
LD
Z AC Pia N C
Data
I ! I ! ! ! I
B.C
D.E
H.L
SP
PC
Memorv
xx
ppqt
xx
yy
, mmmm+3
I-..~::I..
mmmm
IX
IY
I
ppqq
yy
Program
Memorv
2.A
qq
pp
mmm m
mmm m+ 1
mmm m+2.
mmm m+3
~
ED 01 dd 1011 ppqq
00
01
10
11
for
for
for
for
rp is register pair Be
rp is register pair DE
rp IS register pair HL
rp IS Stack Pointer
------LD IX.(addr)
DO 2A ppqq
3-98
Suppose memory location 011116 contains FF16 and memory location 011216 contains 5616. After the instruction
LO IX.(0111 H)
has executed. the IX register will contain 56FF16
LO IY.(addrl
--.,---.,FD 2A ppqq
Load IY register from directly addressed memory.
Affects IY register instead of IX. Otherwise Identical to LO IX(addr).
lD I,A lD R,A
S
F
Z AC Pia N
Data
I I I I I I I
xx
B.C
D.E
H.L
SP
PC
mmmm
IX
IY
IV
~~mmm+!)
Program
Memory
ED
4F
mmmm
mmmm+!
.....- - - , 1 mmmm + 2
.....- - - , 1 mmmm + 3
ED 4F
ED 47
Load Interrupt Vector register from Accumulator.
3-99
LD reg,data S
Z AC P/O N C
Data
I I I I I I I
}-""'''"''''" ,.
A
B.C
D.E
H.L
SP
PC
Register A. B. C.
D. E, H or L
mmmm
IX
IY
I
R
.- :l~mmm+~
Program
Memory
OOxxxll0
YY
mmmm
mmmm+ 1
mmmm+2
Il-----;lmmmm + 3
00 xxx 110 VV
000
001
010
011
100
101
111
for
for
for
for
for
for
for
reg=B
reg=C
reg=D
reg=E
reg=H
reg=L
reg=A
Load the contents of the second object code byte into one of the registers.
When the instruction
LD A,2AH
has executed, 2A16 is loaded into the Accumulator.
3-100
Z ACP/ON
r:
H I I I I I I
A
B.C
D.E
H.L
SP
IX
IY
HL or
SO..,
'"ppqq
" into
SP. Load
sele::::estina tion
I
I
~
emorv
--~:I~mmm+v
mmmm
PC
Dala
Program
Memorv
OOXX0001
qq
pp
mmmm
mmmm+l
mmmm+2
mmmm+3
~
0 0 xx 0001 ppqq
-,.-
00
01
10
11
for
for
for
for
rp IS register pair Be
rp is register pair DE
rp is register pair HL
rp is Stack POinter
Load the contents of the second and third object code bytes Into the selected register
pair. After the Instruction
LD SP,217AH
has executed, the Stack POinter will contain 217A 16
LD IX. data
'-v-" -,.-
DD 21 ppqq
Load the contents of the second and third object code bytes into the Index register IX.
LD IY.
--.-
data
-,.-
FD 21 ppqq
Load the contents of the second and third object code bytes Into the Index Register IY
Notice that the LD rp,data Instruction
IS
For example:
LD
HL.032AH
is equivalent to
LD
LD
H,03H
L,2AH
3-101
Z AC Pia N C
Data
FI I I I I 0
A
}--''';'W
A. ,. C.
D. E. H or L
8.C
D.E
.......
H.L
SP
PC
mmmm
IX
ppqq
..r
---:I mmmm + 3
I-
~
yy
+d
ppqt
Program
Memory
IY
I
R
I
I
~pqq
-
d)::;:
DD
mmm m
01xxxll0 mmm m+ 1
mmm m+2
d
mmm m+3
~~.
DDOlxxxll0d
-.-
000
001
010
011
100
101
111
for
for
for
for
for
for
for
reg=B
reg=C
reg=D
reg=E
reg=H
reg=L
reg=A
Load specified register from memory location (specified by the sum of the contents of
the IX register and the displacement digit tiL
Suppose ppqq=400416 and memory location 401016 contains FF16. After the instruction
LD B(lX+OCH)
has executed. Register B will contain FF16.
~J
FD 01 xxx 110 d
T...
ThiS instruction IS Identical to LD reg.(IX+displ. except that It uses the IY register instead of the IX register.
3-102
m
0 1xxxll0
-.-
Load specified register from memory location (specified by the contents of the HL
register paid.
Z AC P/O N C
Data
FI I I I I I I
A
S.C
D.E
H.L
SP
PC
pp
l:;)"
_ --,. mmmm + 1
mmmm
IY
I
Program
emorv
IX
I
I
~
F9
F9
Load contents of HL Into Stack POinter.
Suppose pp=0816 and qq=3F16. After the Instruction
LD SP,HL
has executed, the Stack POinter will contain 083F16
LD SP.IX
'-v-"
DD F9
Load contents of Index Register IX into Stack POinter.
LD SP,IY
~
FD F9
Load contents of Index Register IY Into Stack Pointer.
3-103
mmmm
mmmm+ 1
mmmm+2
mmmm+3
LD (addr),A S
Z AC Pia N C
Data
Memorv
o::or::n
yy
yy
ppqq
B,C
D,E
H,L
SP
...r
I
I
Program
Memorv
..... -
IX
IY
I
~mmmm+3
mmmm
PC
32
qq
pp
1----;
mmmm
mmmm+1
mmmm+2
mmmm + 3
kr
32 ppqq
Store the Accumulator contents In the memory byte addressed directly by the second
and third bytes of the LD (addrl,A Instruction object code,
Suppose the Accumulator contains 3A16. After the instruction
label
EQU
084AH
LD
Oabell.A
3-104
Z AC Pia N C
I I I 0
Data
Memorv
yy
ppqq
B,C
xx
~ ppqq
D.E
H.L
SP
PC
/'
yy
xx
.-
mmmm
Program
Memory
t-.--:I mmmm + 4
......
IX
IY
I
ED
01010011
qq
pp
+1
rnmmm
mmmm+1
mmmm+2
mmmm+3
.......
00
01
10
11
for
for
for
for
rp
rp
rp
rp
is register pair BC
is register pair DE
IS register pair HL
is Stack Pointer
Store the contents of the specified register pair in memory. The third and fourth object
code bytes give the address of the memory location where the low-order byte is to be
written. The high-order byte is written into the next sequential memory location.
Suppose the BC register pair contains 3C2A 16. After the Instruction
label
LD
EQU
084AH
(Iabell.BC
has executed. memory byte 084A16 will contain 2A16. Memory byte 084B16 will contain 3C16.
Remember that EQU is an assembler directive rather than an instruction: it tells the Assembler to use the 16-bit value 084A16 whenever the word "label" appears.
~
22 ppqq
3-105
~
DO 22 ppqq
Store the contents of Index register IX In memory. The third and fourth object code
bytes give the address of the memory location where the low-order bvte is to be Written. The high-order byte is written into the next sequential memory location.
rr
FD 22 ppqq
This instruction is identical to the LD (addrl.lX instruction. except that it uses the IY
register instead of the IX register.
3-106
Z ACP/ON C
..
D:IIDJ
S.C
D.E
,. -
H.L
SP
~mmmm+4
mmmrn
ppqq
PC
IX
~pqq+drl......
IV
I
R
'--
Data
Memory
xx
+d
~l
Program
Memory
DO
36
d
xx
mmm m
mmm m+ 1
mmm m+2
mmm m+3
--
LD (IX +disp),data
--..-
-.-
DO 36
xx
Load Immediate Into the Memory location designated by base relative addressing.
Suppose ppqq=540016. After the instruction
LD (lX+91.FAH
has executed. memory location 540916 will contain FA16.
-.---
LD (lY+disp),data
--..FD 36
This instruction
IX register.
IS
xx
-.-
36
xx
Load Immediate Into the Memory location (specified by the contents of the HL register
pair),
The Load Immediate Into Memory instructions are used much less than the Load Immediate Into Register instructions.
3-107
Data
FcrJ I I n
Contents of A, B,
C. 0, E, H or L
>----is Vy
B.C
D.E
H,L
pp
SP
PC
IX
YV
mmmm
~mmmm+~
Program
Memorv
IY
01110xxx
mm mm
mm mm+l
mmmm+2
1----; mmmm + 3
The illustration shows execution of LD (HU.reg:
T1
01110xxx
000
001
010
011
100
101
111
for
for
for
for
for
for
for
reg=B
reg=C
reg=D
reg=E
reg =H
reg=L
reg=A
Load memory location (specified by the contents of the HL register pair) from specified
register.
Suppose ppqq=450016 and Register C contains F916. After the instruction
LD (HU,C
has executed, memory location 450016 will contain F916.
J:Y
DD 01110 xxx
Load memory location (specified by the sum of the contents of the IX register and the
3-108
:LI
FD 0111 0 xxx
This instruction IS identical to LD (IX+disp).reg. except that It uses the IY register instead of the IX register.
Z ACP/Q N C
I I I I I I I
Data
Memory
yy
B.C
D.E
H.L
contain ppqq
--.,- 0
SP
PC
IX
....BC mOE
mmmm
~-:J..mmmm+
yy
Program
Memorv
ppqq
IV
I
R
OOOXOO10
mmmm
mmmm+ 1
mmmm+2
1----1 mmmm + 3
LD (rp).A
il1
o if register palr=BC
1 if register pair=DE
Store the Accumulator in the memory byte addressed by the BC or DE register pair.
Suppose the BC register pair contains 084A16 and the Accumulator contains 3A16.
After the instruction
LD (BC).A
has executed. memory byte 084A 16 will contain 3A 16.
The LD (rp).A and LD rp.data will normally be used together. since the LD rp.data instruction loads a 16-blt address into the BC or DE registers as follows:
LD BC.084AH
LD (BC).A
3-109
LDD -
Z ACP/O N C
Data
FO 101 IIJ
ppqq-l
ppqq
B.C----t-t--.....----uu---"f"':jl{'
rrss-l
D.E 1I-__~r::::r---4---~ss~--_t:::_:_-~~--1
rrss
...
IX
IY
--1
11.,..
-;
ED
II---;:A~8-'1I
t---,
1---,
mmmm
mmmm + 1
mmmm + 2
mmmm + 3
LDD
--.,-ED A8
Transfer a byte of data from memory location addressed by the HL register pair to
memory location addressed by the DE register pair. Decrement contents of register
pairs BC., DE. and HL.
Suppose register pair BC contains 004F16. DE contains 454516. HL contains 201216.
and memory location 201216 contains 1816. After the instruction
LDD
has executed. memory location 454516 will contain 1816. register pair BC will contain
004E16. DE will contain 454416. and HL will contain 201116.
3-110
ED B8
This instruction IS Identical to LDD, except that It IS repeated until the BC register pair
contains zero, After each data transfer, Interrupts will be recognized and two refresh Cycles will be executed,
Suppose we have the following contents In memory and register pairs:
Register/Contents
HL
DE
BC
Location/Contents
201216
454516
000316
201216 1816
201116 AA16
2010162516
After execution of
LDDR
register pairs and memory locations will have the following contents:
Register/Contents
HL
DE
BC
200916
454216
000016
Location/Contents
Location/Contents
201216
201116
201016
454516
454416
454316
1816
AA16
2516
1816
AA16
2516
This instruction is extremely useful for transferring blocks of data from one area of
memory to another,
3-111
S Z ACP'ON C
FI
101
liD
Ar----:'~-~t--~~----ir:;
......,f-__..;:u:;;,u_ _--4~/'"
1.==='1
II------- --------f..
---1
IX 11-
IY ....
..,..
--I
ED
mmmm
t--;A:;:.O-.... mmmm + 1
1-----4 mmmm + 2
t - - -.... mmmm + 3
LDI
-...ED AO
Transfer a byte of data from memory location addressed by the HL register pair to
memory location addressed by the DE register pair. Increment contents of register pairs
HL and DE. Decrement contents of the BC register pair.
Suppose register pair BC contains 004F16. DE contains 454516. HL contains 201216.
and memory location 201216 contains 1816. After the instruction
LDI
has executed. memory location 454516 will contain 1816. register pair BC will contain
004E16. DE will contain 454616. and HL will contain 201316.
3-112
LDIR -
ED BO
This Instruction is Identical to LOL except that It IS repeated until the BC register pair
contains zero. After each data transfer. Interrupts will be recognized and two refresh cycles will be executed.
Suppose we have the following contents In memory and register pairs:
Register/Contents
HL
DE
BC
Location/Contents
201216
454516
000316
2012161816
201316 C016
201416 F016
After execution of
LOIR
register pairs and memory will have the following contents:
Register/Contents
HL
DE
BC
Location/Contents
201516
454816
000016
201216
201316
201416
Location/Contents
1816
C016
F016
454516
454616
454716
1816
C016
F016
This instruction is extremely useful for transferring blocks of data from one area of
memory to another.
NEG S
ZACP/ON
F~IJ
A
- , Xx'+
1 )
=--J.
xx
B.C
D.E
H.L
SP
PC
IX
IY
I
R
Data
mmmm
~mmmm+2
........
Program
Memory
.ED
44
mmmm
mmmm+ 1
I----~ mmmm + 2
I--_ _~ mmmm + 3
Negate contents of Accumulator. This is the same as subtracting contents of the Accumulator from zero. The result is the two's complement. 80H will be left unchanged.
Suppose xx=5A 16. After the instruction
NEG
has executed. the Accumulator will contain A616
5A
0101
Two's complement = 1 01 0
1010
01 1 0
3-113
NOP 5
NO OPERATION
Z AC Pia N C
Data
Fcr::IIIIJ
A
B.C
D.E
H.L
SP
PC
mmmm
-"
IX
IY
~
Program
~
emorv
00
~~~mmm+l
NOP
--.--
mmmm
mmmm+l
mmmm+2
mmmm+3
00
This IS a one-byte instruction which performs no operation, except that the Program
Counter IS Incremented and memory refresh continues. This instruction is present for
several reasons:
1)
A program error that fetches an object code from non-existent memory will fetch
00. It is a good idea to ensure that the most common program error will do nothing.
2)
The NOP Instruction allows you to give a label to an object program byte:
HERE NOP
3)
To fine-tune delay times. Each NOP instruction adds four clock cycles to a delay.
3-114
OR data S
ZACP!ON C
Data
F~
A
:::;;o::-=C.xx OR YY~
xx
B,C
D,E
H,L
SP
PC
mmmm
......
IX
IY
I
mmmm+2
F6
yy
Program
Memory
OR
data
F6
yy
mmmm
mmmm+ 1
mmmm+2
mmmm+3
OR the Accumulator with the contents of the second instruction object code byte.
Suppose xx=3A 16 After the instruction
OR 7CH
has executed. the Accumulator will contain 7E16'
3A
7C
sets S to
0011
01 1 1
1010
1 100
0111
1110
o.....J
IS
OR SOH
will unconditionally set the high-order Accumulator bit to 1.
3-115
OR reg S
ZACP/DN
xx
.......xxOR Y~
.
ff
'XIXI11xloioi
} ,"",.1.
A B.
-IIIo-C, 0, E, H or L
IS yy
B,C
D,E
H,L
SP
PC
IX
Data
'-
".
~mmmm+\
mmmm
IY
I
R
OR
-..--
Program
Memory
10110xxx mmmm
1l-_ _--4mmmm + 1
11-_ _-1 mmmm + 2
11-_ _-1 mmmm + 3
reg
10110 xxx
for
for
for
for
for
for
reg=C
reg=D
reg=E
reg=H
reg=L
reg=A
1 1 10
1010
1 110
001 1
1000
101 1
1 sets S to 1.-J
3-116
Data
Memory
IxlxllIxlolo'
xx
1.....oL.....
B.C
D.E
H.L
SP
PC
:I~XORyy~
P qq
PP
mmmm
.........
IX
IY
I
R
yy
Program
Memory
~
B6
mmm
m
m mmm+l
mmmm+2
mmmm+3
86
OR contents of memory location (specified by the contents of the HL register pair) with
the Accumulator.
Suppose xx=E316. ppqq=400016. and memory locatIOn 400016 contains AS16. After
the instruction
OR
(HU
1 1 10
001 1
_1.:..0..;.1..;.0_..;.1",,-0.:..0..:.,0
1 1 10
101 1
LSiX 1 bits. set PIO to 1
1 sets S to 1......J
---
OR (lX+disp)
DD 86
-,-
OR contents of memory location (specified by the sum of the contents of the IX register
and the displacement value d) with the Accumulator.
OR (lY+disp)
-.,~
FD 86
This instructIOn IS Identical to OR (lX+displ. except that It uses the IY register instead of
the IX register.
3-117
Z AC PIO N C
I I I
IJ 0
A
I
I
yy
B.C
D.E
t
}__,,,,,J~ ",
I/O port yy I
D. E, H or L
H.L
SP
mmmm
PC
IX
IY
I
~mmm+.D
~
Program
Memorv
ED
I
I
Data
01xxx001
r------1
l--i
ED 01 xxx 001
000
001
010
all
100
101
111
for reg=B
for reg=C
for reg=D
for reg=E
for reg=H
forreg=L
for reg=A
Suppose yy= 1F16 and the contents of Hare AA16. After the execution of
OUT (Cl.H
AA16 will be
In
3-118
mmmm
mmmm + ,
mmmm+2
mmmm + 3
OUTD -
s
F
Z AC
Pia N C
ITIEEI u I 11 ]
xx-l
Lf<!0IW
Ij
B.C
D.E
xx
H.L
PP
SP
PC
yy
--
--
~mmm+2
IX
I
I
I
ppq q
~ Ppqq-l)
mmmm
IY
I
Data
Memorv
Program
Memorv
ED
AB
1----1
mmmm
mmmm+l
mmmm+2
mmmm + 3
aUTO
-.,-..'
ED AB
Output from memory location specified by HL to I/O port addressed by Register C.
Registers Band HL are decremented.
Suppose xx=OA16' yy=FF16. ppqq=500016. and memory location 500016 contains
7716. After the instruction
aUTO
has executed. 7716 will be held In the buffer of I/O port FF16. The B register will contain 0916, and the HL register pair 4FFF16.
OTDR -
ED BB
OTOR is identical to aUTO, but is repeated until Register B contains 0.
Suppose Register B contains 0316, Register C contains FF16' and HL contains 500016.
Memory locations 4FFE16 through 500016 contain:
Location/Contents
4FFE16
4FFF16
500016
CA16
1B16
F116
After execution of
OTOR
register pair HL will contain 4FF016. Register B will contain zero, and the sequence
Fl16' 1B16, CA 16 will have been written to I/O port FF16.
ThiS instruction IS very useful for transferring blocks of data from memory to output
devices.
3-119
Z ACP/O N C
xx-1
+
+
lulxlulul1l I
A
S,C
I/O port vv I
yy
xx
D.E
H,L
Data
Memorv
pp qq
..-r..... -
ppqq+l
pp
SP
PC
mmmm
io!J!Y
--....
Program
""'~mmm+2
IX
IV
I
Memorv
~
ED
A3
OUTI
-.,-'
mmm
m
mmmm+l
mmmm+2
mmmm+3
ED A3
Output from memory location specified by HL to I/O port addressed by Register C.
Register 8 IS decremented and the HL register pair IS Incremented.
Suppose xx=OA16, yy=FF16' ppqq=500016, and memory location 500016 contains
7716. After the instruction
OUTI
has executed, 7716 will be held in the buffer of I/O port FF16. The 8 register will contain 0916 and the HL register pair will contain 500116,
OTIR -
-.,ED 83
OTIR is Identical to OUTI. except that It IS repeated until Register 8 contains O.
Suppose Register 8 contains 0416, Register C contains FF16' and HL contains 500016.
Memory locations 500016 through 500316 contain:
Location/Contents
5000 16 CA16
500116 18 16
5002 16 81 16
5003 16 AD16
After execution of
OTIR
register pair HL will contain 500416, Register 8 will contain zero and the sequence
CA16. 1816, 8116 and AD16 will have been written to I/O port FF16.
This instruction IS very useful for transferring blocks of data from memory to an output
device,
3-120
OUT (port),A -
s
F
Z AC P/O N C
0 .. 1 I CO
A
S.C
D.E
~mmm+!)
H.L
SP
PC
mmmm
IX
IY
I
I/O port yy ~
Data
~
Program
Memory
D3
yy
OUT
mmmm
mmmm+l
mmmm+2
mmmm+3
(portl.A
1/1
03
yy
Output the contents of the Accumulator to the I/O port Identified by the second OUT instruction oblect code byte.
Suppose 3616 IS held in the Accumulator. After the instruction
OUT (lAHl.A
has executed. 3616 will be in the buffer of I/O port 1A16.
The OUT instruction does not affect any statuses. Use of the OUT Instruction is very
hardware-dependent. Valid 1/0 port addresses are determined by the way In which 1/0
logiC has been Implemented. It is also possible to deSign a microcomputer system that
accesses external logiC uSing memory reference instructions with specific memory addresses. OUT instructions are frequently used In special ways to control microcomputer
logic external to the CPU.
3-121
POP rp -
POP IX
POPIY
S
Z ACP/ON
Data
I I I I I ! I
Memory
qq
S.C
D.E
H.L
SP
SSSS
PC
IX
IV
I
mmmm
pp
~A
5555
5555 + 1
5555 + 2
5555+2)
~-=
Program
"'-~:J. mmmm + 1
Memory
11000001
mmmm
mmmm+1
mmmm+2
11----1 mmmm + 3
1Z11 xx 0001
00
01
10
11
for
for
for
for
rp
rp
rp
rp
s
s
s
s
reg
reg
reg
reg
ster
ster
ster
ster
pa
pa
pa
pa
r
r
r
r
BC
DE
HL
A and F
POP the two top stack bytes in10 the designated register pair.
Suppose qq=0116 and pp=2A16 Execution of
POP HL
loads 0116 into the L register and 2A 16 Into the H register. Execution of the instruction
paPAF
loads 01 Into the status flags and 2A 16 Into the Accumu lator. Thus. the Carry status
will be set to 1 and other statuses will be cleared.
POP IX
~
DD El
POP the two top stack bytes Into the IX register.
POP IY
~
FD E1
POP the two top stack bytes Into the IY register.
The POP Instruction IS most frequently used to restore register and status contents
which have been saved on the stack; for example. while servicing an Interrupt.
3-122
I I I
A
B,C
D,E
H,L
SP
SSSS
PC
mmmm
IX
IV
I
R
Data
Memory
CTJ
qq
pp
ssss-2
ssss-l
ssss
SSSS-2)
- ')
//
~-,.
_,......; mmmm+2
Program
ppqq
Memory
FD
E5
rnmmm
mmmm+ 1
1----1 mmmm + 2
11----1 mmmm + 3
DD E5
PUSH the contents of the IX register onto the top of the stack,
JK
11
xx
00
01
10
11
0101
for
for
for
for
rp
rp
rp
rp
is
IS
is
IS
register
register
register
register
pair Be
pair DE
pair HL
pair A and F
PUSH contents of deSignated register pair onto the top of the stack,
Execution of the instruction
PUSH AF
loads the Accumulator and then the status flags onto the top of the stack,
The PUSH Instruction is most frequently used to save register and status contents: for
example, before servicing an Interrupt
3-123
RES b,reg -
S Z AC PIa N C
FD_J I I I ,
A
YYYYYYYY
B.C
D.E
&
H.L
SP
PC
~mmm+:D
mmmm
IX
IY
I
R
Program
Memory
CB
I
I
10bbbxxx
mmmm
mmmm + 1
mmmm+2
1----1 mmmm + 3
-ll\
CB
10 bbb xxx
Bit
-.- -.bbb ~
1
2
3
4
5
6
000
001
010
011
100
101
110
111
000
001
010
011
100
101
111
Register
B
C
o
E
H
L
3-124
IS
F'
Data
I I I I I I
0
B.C
D.E
H.L
SP
PC
IX
IY
I
R
mmmm
~mmmm+4
ppqq
CPpqq+d~
YvvvYYYV
ppqq+ d
Program
Memorv
mmm m
mmm m+l
mmm m+2
d
10bbbll0 mmm m+3
DO
CB
mmm m+4
~
D DCB d 10 bbb 110
bbb
000
001
010
011
100
101
110
111
Bit Reset
0
1
2
3
4
5
6
7
Reset Indicated bit within memory location Indicated by the sum of Index Register IX
and d.
Suppose IX contains 411016. After the instruction
RES 0.(IX+7)
has executed. bit 0 In memory location 411716 will be O.
~
FDCB d 10 bbb 110
-.-
3-125
of the IX register.
RES b.(HU
III
CB 10 bbb 110
RET 5
Fern
Data
I I
emorv
PP
B.C
D.E
H.L
SP
PC
xxxx
mmmm
IX
IY
I
r~
qq
- + 2)
_---i xxxx
~~).
RET
xxxx
xxxx+ 1
xxxx + 2
Program
emorv
~
C9.
mmmm
mmmm+l
mmmm+2
mmmm+3
C9
Move the contents of the top two stack bytes to the Program Counter: these two bytes
provide the address of the next Instruction to be executed. Previous Program Counter
contents are lost. Increment the Stack POinter by 2. to address the new top of stack.
Every subroutine must contain at least one Return (or conditional Return) instruction:
thiS IS the last instruction executed within the subroutine. and causes execution to
return to the calling program.
3-126
RET cond -
~
11 xxx 000
000
001
010
011
100
101
110
111
NZ
Z
NC
C
PO
PE
P
M
Condition
Non-Zero
Zero
Non-Carry
Carry
PantyOdd
Parity Even
Sign Positive
Sign Negative
Relevant Flag
Z
Z
C
C
Pia
PIO
S
S
This instruction IS Identical to the RET instruction. except that the return IS not executed unless the condition IS satisfied: otherwise. the instruction sequentially followIng the RET cond instruction will be executed.
Consider the instruction sequence:
CALL
AND
SUBR
7CH'-'
1
I
I
R T
cond
----------'
condition not
satisfied
80H
After the RET cond is executed. if the condition IS satisfied then execution returns to the
AND instruction which follows the CALL. If the condition IS not satisfied. the OR instruction. being the next sequential instruction. IS executed.
3-127
Data
I I I I I I I
Memorv
qq
pp
B.C
D.E
H.L
SP
PC
xxxx
mmmm
-...
IX
IV
I
XXXX
XXXX + 1
XXXX + 2
l:::XXX+2)
r
-
ppqq
I
RETI
Program
Memory
~
ED
4D
mmmm
mmmm+1
mmmm+2
mmmm+3
'-.,-'
ED 4D
Move the contents of the top two stack bytes to the Program Counter: these two bytes
provide the address of the next instruction to be executed. PrevIous Program Counter
contents are lost. Increment the Stack POinter by 2. and address the new top of stack.
This Instruction IS used at the end of an Interrupt service routine. and. in addition to
returning control to the Interrupted program. it is used to signal an I/O deVice that the
Interrupt (outine has been completed. The I/O deVice must provide the logic necessary
to sense the Instruction operation code: refer to An Introduction to Microcomputers: Volume 2 for a deSCription of how the RETI instruction operates With the Z80
familv of deVices.
3-128
RETN S
ZACP/ON
Data
Memory
qq
pp
FCIIII:D
A
e.C
D.E
H.L
SP
PC
IX
IY
I
R
xxxx
mmmm
.r
-.:..1
mmmm
mmmm+l
mmmm+2
xxxx +2
rL( - r
ppqq
Program
Memory
ED
mmmm
45
mmmm+ 1
mmmm+2
mmmm+3
--RETN
ED 45
Move the contents of the top two stack bytes to the Program Counter: these two bytes
provide the address of the next instruction to be executed. PrevIous Program Counter
contents are lost. Increment the Stack Pointer by 2 to address the new top of stack.
Restore the Interrupt enable logiC to the state It had prior to the occurrence of the nonmaskable Interrupt.
ThiS instruction IS used at the end of a service routine for a non-maskable interrupt. and
causes execution to return to the program that was interrupted.
3-129
RL reg -
Z AC P/O N
Data
FIXIXIOIXIO,I
A
u.u
D.E
H.L
SP
PC
__ ~'::mmm0
mmmm
Program
Memorv
IX
IY
I
CB
00010001
mmmm
mmmm + 1
mmmm+2
11-----11 mmmm + 3
-t\
CB 00010 xxx
000
001
010
011
100
101
111
for
for
for
for
for
for
for
reg=B
reg=C
reg=D
reg=E
reg=H
reg=L
reg=A
After
Register D
Carry
Register D
Carry
1101010011
[ill
ill
o sets S to 0......J
3 ones. set P/0 to 0
3-130
A_----~f_
____I
11-------;-------;
11-------;-------;
H.L
SP 1 1 - - - - - - - - ' - - - - - - - ;
B.C
D.E
PC
IX
IY
I
II------m-m-m-=mr--------i-....r
t------j)pi)ipqi!cql------r--.........._ _""
------'"""'Ir--------;
aJ
Memory
DD
mmmm
CB
mmmm+ 1
d
mmmm+2
........_ _,..rr'lIII""il---;;,6;,.....--1 mmmm + 3
Rotate contents of memory location (specified by the sum of the contents of Index
Register IX and displacement Integer d) left one bit through Carry.
Suppose the IX register contains 400016. memory location 400716 contains 2F, 6. and
Carry is set to 1. After execution of the Instruction
RL (lX+7)
memory location 400716 will contain 5F16. and Carry is 0:
Before
After
Memory
Carry
Memory
Carry
1001011111
OJ
[QJ
0.J
o sets S to
6 ones. set P10 to 1
FD CB d 16
This Instruction
register.
IS
3-131
RL {HU
~
CB 16
Rotate contents of memory location (specified by the contents of the HL register pair)
left one bit through Carry.
RLA -
S ZACPION~
FI I 101 lo~
Data
B.C
D.E
H.L
SP
PC
IX
IY
I
,.
mmmm
~"""'"":l
mmmm+l
.......
I
I
~
Program
emorY
~
17
RLA
mmmm
mmmm+1
mmmm+2
mmmm+3
17
Rotate Accumulator contents left one bit through Carry status.
Suppose the Accumulator contains 2A16 and the Carry status is set to 1. After the instruction
RL.A
has executed. the Accumulator will contain F516 and the Carry status will be reset to 0:
Before
After
Accumulator
10 1 1 1 1 0 1 0
Carry
Accumulator
Carry
OJ
11111 01011
@]
3 -132
RLC reg S
Z AC P/O N
Data
FlxlxlOlxlOI ..........
A
B.C
H.L
SP
PC
IX
IY
I.
R
mmmm
Program
Memory
mmmm
mmmm + 1
mmmm+2
I - - - - i mmmm + 3
CB
00000011
Jl
CB 000 00 xxx
000
001
010
011
100
101
111
for
for
for
for
for
for
for
reg=B
reg=C
reg=D
reg=E
reg=H
reg=L
reg=A
Rotate contents of specified register left one bit. copying bit 7 Into Carry.
Suppose Register D contains A916 and Carry is 1. After execution of
RLC D
Register D will contain 5316 and Carry will be 1.
Before
After
Register D
Carry
11010 10011
IJJ
sets S to 0
4 ones. set P/0 to 1
3-133
Register D
Carry
Data
~]]TI[]rr2I:J.------------~L_J:i::Qiiiw::d._.J
Memory
A
B.C
D.E
H.L
:.~mmm+V
-
pp
SP
PC
mmmm
IX
IY
I
R
ppqq
t
Program
Memory
mmm m
mmm m+1
mmmm+2
1------1 mmmm + 3
CB
06
CB 06
Rotate contents of memory location (specified by the contents of the HL register paid
left one bit. copying bit 7 Into Carry.
Suppose register pair HL contains 54FF16 Memory location 54FF16 contains A516.
and Carry IS O. After execution of
RLC (HL)
memory location 54FF16 will contain 4B16. and Carry will be 1
Before
After
Memory
Carry
1101001011
@]
Memory
Carry
OJ
sets S to 0
4 ones, set P/0 to 1
RLC (IX+disp)
'-v-"-
Rotate memory location (specified by the sum of the contents of Index register IX and
displacement Integer d) left one bit. cOPYing bit 7 into Carry.
Suppose the IX register contains 400016. Carry is 1. and memory location 400716 contains 2F16. After the instruction
RLC (IX+7)
3-134
has executed. memory location 400716 will contain 5E16. and Carry will be 0:
Before
After
Memory
Carry
Memory
Carry
1001011111
III
@]
o.-J
sets S to
5 ones. set P/0 to 0
RLC (lY+disp)
'-.r-"-'-
This instruction
register.
RLCA S
FI
IS
I 101 101
Data
B.C
D.E
H.L
SP
PC
IX
IY
I
--
__--:r mmmm + 1
.......
mmmm
Program ::
emory
~
07
I
I
RLCA
--..-
mmmm
mmmm+ 1
mmmm+2
mmmm+3
07
Rotate Accumulator contents left one bit. copying bit 7 Into Carry.
Suppose the Accumulator contains 7A16 and the Carry status
struction
IS
RLCA
has executed. the Accumulator will contain F416 and the Carry status will be reset to 0:
Before
After
Accumulator
Carry
Accumulator
Carry
1011110101
OJ
1111101001
@]
3-135
RLD S
Data
F'xIXlolxlol I
x
B.C
D.E
H.L
SP
PC
PP
_~~mmm+:D
mmmm
IX
IY
I
Program
Memory
ED
6F
PP1
.----1
mmm m
mmm m+ 1
mmmm + 2
t -_ _-lImmmm + 3
RLD
--.".-'
ED 6F
The four low-order bits of a memory locatIOn (specified by the contents of register pair
HL) are copied Into the four high-order bits of the same memory location. The prevIous
contents of the four high-order bits of that memory location are copied into the four
low-order bits of the Accumulator. The prevIous four low-order bits of the Accumulator
are copied Into the four low-order bits of the specified memory location.
Suppose the Accumulator contains 7F16. HL register pair contains 400016. and memory location 400016 contains 1216. After execution of the instruction
RLD
the Accumulator will contain 7116 and memory location 400016 will contain 2F16:
Before
Accumulator
7
After
Memorv
Accumulator
Memory
o::DJ~l ffi]
\.....F J\t_.,/ll~
...'
high-order
blt=O~::t-~~~-O
3-136
RR reg -
'I
Z ACP/D N
Data
FlxlxlOlxlOI
D.E
H.L
SP
PC
IX
IY
I
-.
mmmm
::l. mmmm + 2
Program
Memory
CB
00011001
mmmm
mmmm + 1
mmmm+2
!----I,I mmmm + 3
L\
CB 00011 xxx
000
001
010
011
100
101
111
for reg=B
for reg=C
for reg=D
for reg=E
for reg=H
forreg=L
for reg=A
IS
RR H
has executed. Register H will contain 8716. and Carry will be 1
Before
-
Register H
After
-Carry
Register H
Carry
100 00 111 11
1 sets S to 1
4 ones. set P/0 to 1
3-137
RR (HL) -
Data
Memory
IXIXIOIXIOIVI
ppq q+d
B.C
D.E
H.L
SP
PC
~--.{"mmmm
mmmm
+l:)
IX
IY
Program
Memory
ppqq
FD
CB
d
lE
(Ppqq+d~
mmm m
mmm m+l
mmm m+2
mmm m+3
mmm m+4
-.,,-'
J;;h
Rotate contents of memory location (specified by the sum of the contents of the IY
register and the displacement value dl right one bit through Carry.
Suppose the IY register contains 450016. memory locatIOn 450F16 containS 1D16. and
Carry IS set to O. After execution of the instruction
RR (lY+OFHl
memory location 450F16 will contain OE16. and Carry will be 1
Before
After
Memory
Carry
1000111011
@]
Memory
Carry
OJ
sets S to 0
3 ones. set P/0 to 0
RR (lX+disp)
-.,,-'
-.-
ThiS instruction is Identical to RR (lY+displ. but uses the IX register Instead of the IY
register.
3-138
RR (HU
~
CB lE
Rotate contents of memory location (specified by the contents of the HL register pair)
fight one bit through Carry.
s
FI
Z AC P/O N
101
Data
101
B.C
D.E
H.L
SP
PC
IX
IY
I'
~...:mmm+)
mmmm
~
Program
~
emorY
1F
RRA
mmmm
mmmm+ 1
mmmm+2
mmmm+3
IF
Rotate Accumulator contents fight one bit through Carry status.
Suppose the Accumulator contains 7A16 and the Carry status IS set to 1. After the instruction
RRA
has executed. the Accumulator will contain B016 and the Carry status will be reset to
0:
After
Before
Accumulator
1011110101
Accumulator
Carry
11011 11011
@]
Carry
3-139
RRC reg -
s Z AC PIO N C
FLXIXIOIXIOl
Data
A
B.C
D.E
SP
PC
~mmm+!)
mmmm
IX
IY
I
Program
Memory
CB
00001101
I
I
1----1
mmmm
mmmm + 1
mmmm+2
mmmm + 3
l~
CB 00001 xxx
000
001
010
011
100
101
111
for
for
for
for
for
for
for
reg=B
reg=C
reg=O
reg=E
reg=H
reg=L
reg=A
Rotate contents of specified register right one bit clrcularry. copying bit 0 Into the Carry
status.
Suppose Register 0 contains A916 and Carry is O. After execution of
RRC 0
Register 0 will contain 0416. and Carry will be 1
Before
After
Register 0
Carry
Register 0
Carry
1101010011
[Q]
1110101001
IT1
, "" S "
,..re.,,,"
3-140
"'"It. '"
Z 100
ZACP/ON
Data
Memory
FXXOXO
p Pqq
B.C
D.E
H.L
pp
--
SP
PC
IX
mmmm
IY
1\
R
+
:I~mmm+:D
Program
Memory
~
CB
OE
mmm
mmm m+ 1
mmmm+2
mmmm+3
RRC (HU
~
CB OE
Rotate contents of memory location (specified by the contents of the HL register pair)
right one bit circularly. copYing bit 0 into the Carry status.
Suppose the HL register pair contains 450016. memory location 450016 contains
3416. and Carry is set to 1. After execution of
RRC (HL)
memory location 450016 will contain 1A16. and Carry will be 0:
Before
After
Memory
Carry
1001101001
OJ
Memory
o sets S to 0
3 ones. set PIO to 0
Carry
RRC (IX+disp)
'Xh
Rotate contents of memory location (specified by the sum of the contents of the IX
3-141
register and the displacement value d) right one bit circularly, copying bit 0 into the Carry status.
RRC (IY+disp)
This instruction IS identical to the RRC (IX+disp) instruction, but uses the IY register instead of the IX register.
RRCA -
s
FL I
101
Data
101
B.C
D.E
H.L
SP
PC
-,
mmmm
~~~mmm+l
IX
IY
1
R
~
Program
emorY
~
OF
I
I
--RRCA
mmmm
mmmm+l
mmmm+2
mmmm+3
OF
Rotate Accumulator contents fight one bit circularly, cOPYing bit 0 into the Carry status.
Suppose the Accumulator contains 7A 16 and the Carry status is set to 1. After the instruction
RRCA
has executed, the Accumulator will contain 3016 and the Carry status will be reset to
0:
Before
After
Accumulator
Carry
Accumulator
Carry
1011110101
OJ
1001111011
[Q]
3-142
RRD S
Ixlxlolxlol I
M~ry
r
I
I~~
B.C
D.E
H.L
SP
PC
PP
"
Program
Memory
__~:.l mmmm + 2
mmmm
IX
P q
IY
I
R
ED
67
mm mm
mm mm+1
mmmm+2
.----I mmmm + 3
-RRD
ED 67
The four high-order bits of a memory location (specified by the contents of register pair
HL) are copied Into the four low-order bits of the same memory location. The previous
contents of the four low-order bits are copied into the four low-order bits of the Accumulator. The prevIous four low-order bits of the Accumulator are copied Into the four
high-order bits of the specified memory location.
Suppose the Accumulator contains 7F16' HL register pair contains 400016, and memory location 400016 contains 1216. After execution of the instruction
RRD
the Accumulator will contain 7216 and memory location 400016 will contain F116:
Before
Accumulator
7
\
SE2L
After
Memory
F J
~ rpj}2
" '<, /'
\
3-143
Accumulator
Memory
lIDJ
Non-zero result,
set Z to
RST n -
RESTART
S Z ACP/O N
Data
Memory
I I I I:J
B.C
D.E
H.L
SP
ppqq
PC
mmmm
IX
--
mm+1
mm
ppqq-2
ppqq-l
ppqq
{:" ppqq-2 )
.......
~~mmm+y.
IY
1-'
Cooooooooooxxxooo
Program
Memorv
-6
- xxx
11
111
mmmm
mmmm+1
mmmm+2
mmmm+3
3-144
F X
Z AC P/O N [
1X I X 1X 11 'I X I
(.
Data
xx-yy-C ")---
xx
B.C
D.E
H.l
SP
PC
"
mmmm
IX
IV
Memory
DE
I
I
I
R
Program
~mmmm+2
'--
YV
mmmm
mmmm+ 1
mmmm+2
mmmm+3
--
SBC A.
data
DE
-.yy
Subtract the contents of the second object code byte and the Carry status from the Accumulator.
Suppose xx=3A 16 and Carry= 1. After the instruction
SBC A.7CH
has executed, the Accumulator will contain B016
3A
Twos comp of 7C
Twos comp of Carry
1 "" 5" 1
Borrow. set C to 1
0011
1 000
1111
101 1
1010
0 1 00
1111
1 101
fJ
-
Borrow. set AC to 1
IS
3-145
IS
no borrow.
SBC A,reg -
.t
Z ACPIQ N C
Ixlxlxlxlqxl
A
B.C
D.E
"
xx
"~c,~I~,
C, D. E, H or L
is yy
H.L
_/'
SP
PC
Data
xx-yy-C
- :i)
~~.J..mmmm+
mmmm
Program
Memorv
IX
IY
I
l00llxxx
mmmm
mmmm+ 1
t---""'1 mmmm + 2
t-----t mmmm + 3
SBC A.
reg
10011
xxx
000
001
010
011
100
101
111
for
for
for
for
for
for
for
reg=B
reg=C
reg=D
reg=E
reg=H
reg=L
reg=A
Subtract the contents of the specified register and the Carry status from the Accumulator.
Suppose xx=E316' Register E contains A016. and Carry=l. After the instruction
SBC A.E
has executed. the Accumu lator will contain 4216.
E3
Two's comp of AO
Two's comp of 1
No borrow. set C to 0
1110
0110
1111
0100
0011
0000
1111
0010
3-146
IS
no borrow.
ZACP/ON
Data
Memorv
F IXIXIXIXlllX
.:;" .-=C
xx
.1
xx-yy-C) . -
B.C
D.E
H.l
SP
PC
IX
IY
I
R
PP
yy
PP;
mmmm
.-.---::I~mmm + 1
Program
Memorv
~
9E
mmm
mmm m+ 1
mmmm+2
mmmm+3
SBC A,(HL)
~
9E
Subtract the contents of memory location (specified by the contents of the HL register
paid and the Carry from the Accumulator.
Suppose Carry=O. ppqq=400016' xx=3A16, and memory location 400016 contains
7C 16. After execution of the instruction
SBC A,(HL)
the Accumulator will contain BE16.
3A
Two's comp of 7C
Two's comp of Carry
1 sets S to 1
Borrow, set C to 1
0011
1 000
1010
0100
101 1
1 1 10
f.J
Noo-""
""It '"
Z to
'-------Borrow, set AC to 1
The Carry flag is set to 1 for a borrow and reset to 0 if there IS no borrow.
SBC A,{IX+disp)
"-v-"
-,-
DD 9E
Subtract the contents of memory location (specified by the sum of the contents of the
IX register and the displacement value d) and the Carry from the Accumulator.
SBC A, (IY+disp)
~-,-
FD 9E
This instruction IS identical to the SBC A,{IX+disp) instruction, except that It uses the IY
register Instead of the IX register.
3-147
SBC Hl,rp -
FROM HAND l
SZACPIONC
Data
: I[~[IIx[I1~xI!ExIII1I!ZX}~--------II"'II>1E?
Be. DE. HL or SP
contains ~yyy
B.C
D.E
H.L
SP
I
I
xx
xx
mmmm
PC
-- -
IV
I
R
Program
:1. mmmm + 2
IX
Memorv
ED
mmmm
mmmm + 1
mmmm+2
....---11 mmmm + 3
01xx0010
lX
01
xx 0010
00
01
10
11
for
for
for
for
rp is register pair BC
rp IS register pair DE
rp IS register pair HL
rp IS Stack POinter
Subtract the contents of the designated register pair and the Carry status from the HL
register pair.
Suppose HL contains F4A216. BC contains A03416, and Carry=O. After the instruction
SBC HL,BC
has executed, the HL register pair will contain 546E16:
Two's camp of F4A2
Two's camp of A034
Two's camp of Carry
o sets S to 0
No borrow, set C to 0
1111
0101
0101
tJ
1....
IS
3-148
IS
no borrow.
SCF -
F'
S ZACP/ON
I I I I I
Data
B.C
D.E
H.L
SP
PC
mmmm
""""'-
IX
IY
I
R
Program
emorY
~
37
SCF
mmmm
mmmm+l
mmmm+2
mmmm+3
37
When the SCF instruction IS executed. the Carry status IS set to 1 regardless of ItS preVIOUS value. No other statuses or register contents are affected.
3-149
SET b,reg S
Z AC
PIO N C
Data
I I I I
S,C
D,E
H,L
VVVV VVVY
SP
PC
".......
I'
mmmm
IX
IY
~:I~mmm+v
Program
Memory
CS
11bbbxxx
mmmm
mmmm + 1
mmmm+2
1----1 mmmm + 3
11\
CB 11 bbb
xxx
-.-
Bit bbb
o 000
1 001
2 010
3 011
4 100
5 101
6 110
7 111
xxx
000
001
010
011
100
101
111
-,-
Register
B
C
D
E
H
L
SET 2.L
has executed. bit 2
In
3-150
IS
Z AC Pia N C
I I I I I I I
yvyy yyyy
P;
B.C
D.E
H,L
qq'
PP
--
SP
PC
mmmm
:>
~~mmm+2
IX
IY
I
Program
Memory
CB
l1bbbllO
mm mm
mm mm+l
I-----f mmmm + 2
I--_ _-fmmmm + 3
The illustration shows execution of SET b,(HU. Bit 0 is the least significant bit
lr~
CB 11 bbb 110
Bit Set
o
1
2
3
4
5
6
7
bbb
000
001
010
011
100
101
110
111
DD CB
d 11
bbb 110
3-151
In
FD CB d 11
bbb
110
In
SET b. (HU
This instruction IS identical to SET b.(lX+displ. except that It uses the IY register instead
of the IX register.
SLA reg S
ZAcP/ON
9-
Data
FIX'X'O'X'O' '.
A
/'
",
D.E
H.L
SP
PC
IX
IV
mmmm
")
1-&:""::1 mmmm + 2
......
Program
Memory
CB
001oo1
mmmm
mmmm + 1
1-----1
1-_ _--1
mmmm + 2
mmmm + 3
r~
CB 00100 xxx
-.000
001
010
011
100
101
111
for
for
for
for
for
for
for
reg=B
reg=C
reg=D
reg=E
reg=H
reg=L
reg=A
Shift contents of specified register left one bit. resetting the least significant bit to O.
Suppose Register B contains 1F16. and Carry= 1. After execution of
SLA B
Register B will contain 3E16 and Carry will be zero.
3-152
Before
After
Register B
Carry
Register B
Carry
[Q]
10001 1 1 111
o sets S to 0
5 ones. set P/O to 0
S
F
Z AC P/O N C
Data
Memory
0X1i1XI I 1411I
0
S.C
D.E
H.L
SP
PC
pp qq
pp
~~mmm+v
mmmm
IX
IY
I
I
I
Program
Memory
mmm m
mmm m+l
mmmm+2
1 - - - - 1 mmmm + 3
CB
26
CB 26
Shift contents of memory location (specified by the contents of the HL register pairlleft
one bit. resetting the least significant bit to O.
Suppose the HL register pair contains 450016. memory location 450016 contains
8416. and Carry=O. After execution of
SLA (HU
memory location 450016 will contain 0816. and Carry will be 1.
Before
After
Memory
Carry
1100001001
ill
Memory
Carry
ill
o sets S to 0
lone. set P/O to 0
3-153
n
n
SLA (IX+disp)
Shift contents of memory location (specified by the sum of the contents of the IX
register and the displacement value d) left one bit arithmetically. resetting least significant bit to O.
SLA OY+disp)
This instruction
register.
SRA reg S
IS
Z AC P/O N C
Data
IXIXIOIXI.OI
i41
S.C
D.E
'-
H.L
SP
PC
IX
IY
I
R
.",.
mmmm
.....
:::;;;::"""'" mmmm+2
~
Program
Memory
CS
00101111
mmmm
mmmm + 1
1I-----lI mmmm + 2
I - - - - i mmmm + 3
The illustration shows execution of SRA A:
--t\
CB 00101 xxx
-.000
001
010
011
100
101
111
for
for
for
for
for
for
for
reg=B
reg=C
reg=D
reg=E
reg=H
reg=L
reg=A
Shift specified register right one bit. Most significant bit is unchanged.
Suppose Register H contains 5916. and Carry=O. After the instruction
SRA H
has executed. Register H will contain 2C16 and Carry will be 1.
3-154
Before
After
Register H
10 1 0 1 1 00 1
III
@]
o sets S to 0
3 ones. set P/0 to 0
SRA (HL)SRA (lX+disp)
SRA (lY+disp)
Z ACP!ON
Register H
Data
Memory
IX\X\O\X\O\lI
A
B.C
D.E
L
_/ - V
H.L
SP
PC
IX
IY
I
-."'":1 mmmm + 4
mmmm
ppqq
I
I
+d
ppqt
Program
Memory
DO
CB
d
2E
~pqq+d~
mmm m
mmm m+l
mmm m+2
mmm m+3
mmm m+4
J;:hr
Shift contents of memory location (specified by the sum of the contents of Register IX
and the displacement value d) right. Most significant bit is unchanged.
Suppose Register IX contains 340016. memory location 34AA16 contains 2716. and
Carry=1. After execution of
SRA (IX+OAAH)
memory location 34AA16 will contain 1316. and Carry will be 1.
Before
After
Memory
Carry
1001001111
ill
Memory
Carry
OJ
sets S to 0
3 ones. set P10 to 0
3-155
SRA (IY+disp)
This instruction
register.
IS
J:h
CB 2E
Shift contents of memory location (specified by the contents of the HL register pair)
fight one bit. Most significant bit IS unchanged.
SRL reg -
r
s
Z AC P/O N
'-
Data
FIOIXIOIXIOI
A
S.C
~.~
H.L
SP
PC
IX
IY
I
...r
-. -:::l mmmm + 2
mmmm
Program
Memorv
mmmm
mmmm + 1
mmmm+2
1----1 mmmm + 3
CS
00111011
l\
CB 00111 xxx
000
001
010
011
100
101
111
for
for
for
for
for
for
for
reg=B
reg=C
reg=D
reg=E
reg=H
reg=L
reg=A
Shift contents of specified register fight one bit. Most significant bit
Suppose Register D contains 1F16. and Carrv=O. After execution of
SRL D
Register D will contain OF16. and Carry will be 1.
3-156
IS
reset to O.
Before
After
Register D
Carry
Register D
Carry
1000111111
@]
10000 11111
IJJ
Z AC Pia N C
: m:EJ 0 I x ro:~~
B,C
D,E
H,L
sp
PC
pp
l..OiL.~mmm+V
mmmm
IX
IY
I
Program
Memory
mmm m
mmmm+l
I -_ _-immmm + 2
I -_ _-immmm + 3
CB
3E
CB 3E
Shift contents of memory location (specified by the contents of the HL register paid
right one bit Most significant bit is reset to 0,
Suppose the HL register pair contains 200016. memory location 200016 containS 8F16.
and Carry=O, After execution of
SRL (HL)
memory location 200016 will contain 4716. and Carry will be 1.
Before
After
Memory
Carry
Memory
Carry
1100011111
[Q]
1010001111
OJ
L.
SRL (IX+disp)
Shift contents of memory location (specified by the sum of the contents of the IX
register and the displacement value d) right one bit Most Significant bit IS reset to 0,
3-157
SRL (IY+disp)
J:h,
This instruction is identical to SRL (IX+disp), but uses the IY register Instead of the IX
register.
ZACP/ON
[x Lx JxIX 11 I xI
B.C
Data
xx
~~:x:yyr
D.E
H.L
SP
~~mmm+)
mmmm
PC
IX
IY
I
R
Program
Memory
yy
D6
I-
mmmm
mmmm+l
mmmm+2
mmmm+3
SUB
data
D6
VV
Subtract the contents of the second oblect code byte from the Accumulator.
Suppose xx=3A16 .After the instruction
SUB 7CH
has executed. the Accumulator will contain BE16.
3A
001 1
Two's comp of 7C = 1 000
1 01 1
1 sets S to 1
Borrow, set C to 1
o OdO,
1010
01 00
1 1 10
U
t L
set P10 to 0
3-158
SUB reg -
S Z ACP/0N C
~
A
xx
B.C
D.E
r.
-,
.......
}-coo"IA'
c,
D. E. H or L IS YY
H.L
SP
PC
IX
IY
Data
xx-yy
mrnmm
::::;;0:--::1 mmmm + 1
......
~
Program
Memory
mmmm
_ _-Immmm + 1
~_ _-Immmm + 2
~_ _-Immmm + 3
10010xxx
SUB
reg
'-".-'
10010
XXX
000
001
010
011
100
101
111
for
for
for
for
for
for
for
reg=B
reg=C
reg=D
reg=E
reg=H
reg=L
reg=A
o sets S to 0
No borrow, set C to 0
001 1
0000
0011
3-159
'xlXIXlxlllxl
- ).-
"
xx-yy
'-.--:1
xx
Data
Memorv
yy
ppqq +d
B.C
D,E
H.L
SP
PC
IX
--
,.... ~mmm+v
mmmm
ppqq
IV
I
R
~pqq+d~
Program
Memory
DD
96
d
mmm m
mmm m+ 1
mmmm+2
mmm m+3
~-.-
DD 96
Subtract contents of memory location (specified by the sum of the contents of the IX
register and the displacement value d) from the Accumulator.
Suppose ppqq=400016. xx=FF16. and memory location 40FF16 contains 5016. After
execution of
SUB (lX+OFFH)
the Accumulator will contain AF16
FF
Two's comp of 50 =
1 sets S to 1
1 111
10 11
1 01 0
1111
0000
1111
fJ
No borrow. set C to 0
IS
complemented,
SUB (lY+disp)
---...-..
-.FD 96
This Instruction IS identical to SUB (IX+displ. except that It uses the IY register Instead
of the IX register.
SUB {HU
---...-..
96
Subtract contents of memory location (specified by the contents of the HL register pair)
from the Accumulator,
3-160
XOR data S
Z ACP/O N C
Data
F~
A
;,,;,,&
XX
B.C
D.E
~~mmm+~
H.L
SP
PC
IX
~x-;;'yyr
mmmrn
~
Program
Memorv
IY
I
EE
'-
XOR
data
EE
Vy
Vy
mmmm
mmmm+ 1
mmmm+2
mmmm+3
Exclusive-OR the contents of the second oblect code byte with the Accumulator.
Suppose xx=3A16. After the instruction
XOR 7CH
has executed. the Accumulator will contain 4616.
3A
001 1
7C = 0 1 1 1
0100
o sets S to
1010
1 1 00
0110
0..J
IS
3-161
In
bit status.
XOR reg S
Z ACP/O N C
f,
IXIXI1 iXIOIOI
A
B,C
D,E
H,L
-.
~Coo.~ol~'
D, E, H or L
C,
IS yy
SP
PC
/"
...--,. mmmm + 1
mmmm
IX
IY
I
R
Data
~
Program
Memory
I
I
10101xxx
mmmm
mmmm+l
I I - - - - I mmmm + 2
II-_ _-Immmm + 3
XOR
reg
10101
xxx
for
for
for
for
for
for
reg=C
reg=D
reg=E
reg=H
reg=L
reg=A
1 1 10
10 10
0100
001 1
0000
0011
o sets S to 0....J
3-162
In
bit status.
Z AC PIO N C
Ixlxl 1 1XIOIOI
-..---:t'" xx yy ~
xx
S.C
Data
Memory
yy
ppqq +d
o.E
......-:.J., mmmm+ 3
H.L
SP
PC
mmmm
IX
ppqq
IY
I
"
10-
le..(ppqq+~
Program
Memory
00
AE
d
mmm m
mmm m+l
mm mm+2
mm mm+3
DD AE
Exclusive-OR contents of memory location (specified by the sum of the contents of the
IX register and the displacement value d) with the Accumulator.
Suppose xx=E316. ppqq=450016. and memory location 45FF16 contains A016. After
the Instruction
XOR (IX+OFFH)
has executed. the Accumulator will contain 4316.
E3
AO =
1 1 10
001 1
10 10
0000
0100
0011
o..~ "o..J
FD AE
This instruction is identical to XOR (IX+displ. except that It uses the IY register Instead
of the IX register.
XOR (HU
--..-'
AE
Exclusive-OR contents of memory location (specified by the contents of the HL register
paid with the Accumulator.
3-163
8080A/Z80 COMPATIBILITY
Although the zao microprocessor can certainly be used on
its own merits. one of its important characteristics is its
compatibility with the aOaOA microprocessor. This compatibility has the following features:
1)
2)
3)
4)
8080A/Z80
COMPATIBILITY
FEATURES
All 8080A machine language Instructions are also Z80 machine language instructions.
All 8080A registers are also Z80 registers (see Table 3-6).
Almost all 8080A programs will run on a Z80. with some minor differences to be
noted later.
The Z80 has instructions, registers, and other features not present on the 8080A.
so Z80 programs will not generally run on 8080A processors.
8080AlZ80
ASSEMBLY
LEVEL
CONVERSION
Readers should note the binary coding limitations that thiS compatibility places on the extra featu res of the Z80 microprocessor.
The 8080A has some unused operation codes (see Table 3-9) that
are used for some of the Z80's extra Instructions. But there are
simply not enough such codes to cover the large number of
features in a simple form.
8080A
UNUSED
OPERATION
CODES
Thus, many of the added Z80 instructions require a 2-byte operation code. The first byte IS CB, DO, ED. or FD. Note the follOWing
meanings of these codes from Table 3-9:
2-BYTE
OPERATION
CODES
CB DO ED FD -
The second byte of the operation code describes the actual operation to be performed.
FASTER AND
The end result is that these multi-byte instructions execute rather
slowly (and use more memory) because an additional memory
SLOWER
access is required. The reader should be aware of this vanatlon In
EXECUTING
execution times and try to use faster executing instructions when
INSTRUCTIONS
possible. This warning particularly applies to the extra shift
instructions (RLC, RRC, RL. RR. SRA, SRL) and to instructions Involving the Index
registers IX and IY.
There are a few minor incompatibilities between the
8080A and the Z80. These are:
1)
2)
3)
8080A/Z80
INCOMPATIBILITIES
The Z80 uses the P (or P/O) flag to Indicate twos complement overflow after anthmetlc operations. The 8080A always uses thiS flag for
panty.
The Z80 and 8080A execute the DAA Instruction differently. On the Z80, thiS instruction will correct decimal subtraction as well as decimal addition. On the
8080A. It will correct only decimal addition.
The Z80 rotate instructions clear the AC flag. The 8080A rotate Instructions do
not affect the AC flag.
3-164
SOaOA Register
A
A'
None
B
None
B'
C
C'
C
None
0
0'
None
E
E'
None
None
H'
None
None
IX
None
IY
None
L'
None
None
PC
PC
SP
SP
BC
DE
HL
AF
PSW
zao Flags
aOaOA Flags
C (Carry)
C (Carryi
H (Half-Carry)
AC (Auxiliary Carryi
N (Subtract)
None
Pia (Paritv/Overflowl
S(Sign)
P (Paritvi
S(Sign)
Z (Zero)
Z (Zeroi
8085/Z80
INCOMPATIBILITIES
TIMING
INCOMPATIBILITIES
The N flag on the Z80 occupies bit 2 of the F register; the corresponding bit in the
Processor Status Word of the 8080A is always a logic '1'.
3-165
aoaOA
zao Mnemonic
aoaOA Mnemonic
zao Mnemonic
Mnemonic
HL,(addr)
ADC
A,data
LHLD
addr
LD
ADC
A,rag or (HLI
LXI
rp,datale
LD
rp,datale
reg or M
ADD
A,reg or (HU
MOV
reg,reg or
LD
reg,rag or (HU
ADD
A,data
MOV
reg or M,reg
LD
rag or (HLI,reg
AND
reg or (HU
MVI
reg or M,data
LD
reg or (HU,data
ACI
data
ADC
reg or
ADD
ADI
data
ANA
reg or
NOP
ANI
dete
AND
data
NOP
CALL
addr
CALL
eddr
ORA
reg or
CC
addr
CALL
C.addr
ORI
data
OR
data
CM
addr
CALL
M,addr
OLrr
port
OUT
(portl.A
CMA
CPL
PCHL
CMC
CCF
POP
pr
pr
OR
reg or (HLI
JP
(HU
POP
pr
PUSH
pr
CMP
reg or M
CP
reg or (HU
PUSH
CNC
CNZ
addr
addr
CALL
CALL
NC,addr
NZ,addr
RAL
RAR
CP
addr
addr
CALL
P,addr
RC
RET
CPE
CALL
PE,addr
RET
RET
RLCA
RLA
RRA
C
CPI
data
CP
data
RLC
CPO
addr
CALL
PO,addr
RM
RET
CZ
addr
CALL
Z,addr
RNC
RET
NC
rp
ADD
HL,rp
DAA
DAD
DAA
RNZ
RET
NZ
RP
RET
OCR
reg or M
DEC
reg or (HLI
RPE
RET
PE
DCX
rp
DEC
rp
RPO
RET
PO
01
RRC
RRCA
EI
EI
RST
HLT
HALT
RZ
01
n
M
RST
RET
sac
IN
A,(portl
saa
reg or
INC
rag or (HU
SBI
data
SBC
A.reg or (HLI
A,data
rp
INC
rp
SHLD
addr
LD
(addrl.HL
JP
LD
JP
C.addr
M,addr
SPHL
JM
addr
addr
STA
LD
SP,HL
(addr).A
LD
(BCI or (DEI.A
IN
port
INR
reg or
INX
JC
addr
8 or 0
JMP
addr
JP
addr
STAX
JNC
addr
JP
NC,addr
STC
JP
addr
JP
P,addr
SUB
reg or M
SUB
JNZ
addr
JP
NZ,addr
SUI
data
SUB
data
JPE
JPO
JP
JP
PE,addr
PO.addr
XCHG
XRA
reg or M
EX
XOR
DE,HL
reg or (HLl
JZ
addr
addr
addr
JP
Z,addr
XRI
data
XOR
LOA
addr
LD
A,(addrl
XTHL
data
(SPI.HL
LDAX
a or 0
LD
A,(aC) or (DEI
3-166
SCF
EX
rag or (HU
zao
aOaOA Mnemonic
Mnemonic
Mnemonic
BOaOA
A,data
ACI
data
INC
rp
INX
ADC
A(HLI
ADC
INC
reg
INC
xv
(xv + disp)
Mhemonic
rp
ADC
Areg
ADC
ADC
A.lxv + displ
ADC
HL,rp
ADD
A,data
ADI
data
ADD
A,(HLI
ADD
INIR
ADD
A.reg
ADD
reg
JP
addr
JMP
addr
ADD
JP
C,addr
JC
addr
ADD
A(xv + displ
HL,rp
JP
(HLI
PCHL
ADD
IX,pp
JP
M.addr
JM
addr
ADD
IY.IT
JP
NC.addr
JNC
bAD
IND
INDR
INI
rp
AND
data
ANI
data
JP
NZ,addr
JNZ
addr
addr
AND
(HLI
ANA
JP
P,addr
JP
addr
AND
reg
ANA
reg
JP
PE.addr
addr
AND
(xv + disp)
JPE
JP
PO,addr
JPO
addr
BIT
b,(HLI
JP
Z.addr
JZ
addr
BIT
b,reg
JP
xv
BIT
b,{xy + displ
addr
JR
CALL
C.disp
disp
CALL
CALL
CALL
addr
JR
C.addr
CC
JR
NC,disp
M.addr
CM
addr
addr
JR
NZ.disp
CALL
NC,addr
CNC
addr
JR
Z,disp
CALL
NZ.addr
CNZ
addr
LD
A.laddrl
LDA
LDAX
CALL
P.addr
CP
addr
LD
A.lBC) or (DE)
CALL
PE,addr
CPE
addr
LD
AI
CALL
PO,addr
CPO
addr
LD
Z,addr
CZ
addr
LD
AR
(addr),A
CALL
CCF
CMC
CP
CPI
LD
(addr),BC or DE
data
STA
addr
8 or 0
addr
data
(HL)
LD
(addrl,HL
SHLD
CMP
LD
(addrl,SP
CP
reg
CMP
reg
LD
(addrl,xv
(xv + displ
CP
LD
(BCI or (DE),A
STAX
LD
BC or DE,(addr)
LD
HL,(addr)
LHLD
LD
(HLl,data
MVI
M,dats
LD
(HLI,reg
MOV
M,reg
CPL
CMA
LD
I,A
DAA
DAA
LD
R,A
CP
CPD
CPDR
CPI
CPIR
DEC
(HLI
DEC
reg
DEC
DEC
rp
DEC
{XV + dispj
xv
DI
DJNZ
disp
EI
DCR
LD
reg,data
DCR
reg
LD
reg.lHL)
DCX
rp
LD
LD
reg,reg
reg.(xv + dispi
reg,M
reg,reg
LD
rp,datat6
LXI
SP,(addri
LD
SP,HL
SPHL
EI
LD
SP,xv
EX
AF,AF
LD
xY,data16
DE,HL
XCHG
LD
xy.laddrl
EX
(SP),HL
XTHL
LD
(xv + dispi,dat8
EX
(SP),xv
LD
(xy
EXX
LDD
HALT
HLT
LODR
LOI
IN
A.lpOrtl
IN
IN
reg.lCI
INC
(HLI
INC
reg
rp,datat6
port
LOIR
INR
NEG
Nap
INR
reg
OR
3-167
addr
reg,data
LD
8 or 0
MVI
DI
+ disp),reg
addr
MOV
MOV
EX
1M
zao
ADC
Nap
data
ORI
data
8080A Mnemonic
Z80 Mnemonic
8080A Mnemonic
OR
IHLI
ORA
RR
IHLI
OR
reg
ORA
reg
RR
reg
OR
OTOR
Ixv + displ
Ixv + displ
RR
RRA
RRC
IHLI
RRC
reg
RRC
(xv + disp)
OTIR
OUT
ICl,reg
OUT
IportlA
OUT
port
OUTO
OUTI
RRCA
RRC
RRO
RST
RST
S8C
A,data
581
data
S8C
A,IHLI
SB8
SBC
A,reg
588
reg
SBC
A,(xv + displ
SBC
HL,rp
SCF
RET
SET
b,(HLI
b,reg
b,(xv + disp)
POP
pr
POP
POP
xv
PUSH
pr
PUSH
PUSH
RES
xv
b,lHLI
RES
b,reg
RES
b,lxv + displ
RET
RAR
pr
pr
STC
RET
RC
SET
RET
RM
SET
RlIT
NC
RNC
SLA
RET
NZ
RNZ
SLA
IHLI
reg
RET
RP
SLA
(XV
IHLI
+ displ
RET
PE
RPE
SRA
RET
PO
RPO
SRA
reg
RET
RZ
SRA
(xv + displ
SRL
SRL
SRL
IHL)
reg
RETI
RETN
RL
(HLI
RL
reg
RL
(XV
+ displ
RLA
RLC
IHLI
RLC
reg
RLC
(xv + disp)
RLCA
IS
SU8
data
SUI
data
SU8
IHLI
SU8
RAL
SU8
reg
SUB
reg
SUB
Ixv + displ
XOR
data
XRI
data
XOR
XRA
RLC
XOR
IHLI
reg
M
reg
XOR
(xv + displ
RLO
indicates that there
no corresponding instruction
3-168
(xv + dispi
XRA
Table 3-9. Unused 8080A Operation Codes and Their Z80 Meanings
8080A Operation Code
OB
Z80 Use
EX AF.AF'
10
DJN7 disp
18
JR disp
20 IRIM on 8085i
JR NZ.disp
28
JR Z.disp
30 ISIM on BOB5)
JR NC.disp
3B
JR C.disp
CB
BIT. RES. RL. RLC. RR. RRC. SET. SLA. SRA. SRL
OS
EXX
DO
ED
ADC
HL.rp
A.I
LD
A.R
OTDR
CPDR
LD
laddr).rp
OTIR
CPI
LD
I.A
OUT
CPIR
LD
R.A
aUTO
rp,(addr)
OUTI
1M
LD
IN
reg.IC)
LDD
RETI
IND
LDDR
RETN
INDR
LDI
RLD
INI
LDIR
3-169
ICl.res
RRD
SBC
INIR
FD
NEG
LD
CPO
HL.rp
ZILOG
The standard zao assembler is available from zao manufacturers and on the major
time-sharing networks; it is also part of most development systems. Cross assembler versions are available for most large computers and many minicomputers.
A colon after a label, except for the pseudo-operations EQU, DEFL, and
MACRO, which require a space.
A space after the operation code.
A comma between operands in the operand field. (Remember this oneil
A semicolon before a comment.
Parentheses around memory references.
TYPical
START
A.(1000l
HL.DE
:GET LENGTH
LABELS
The assembler allows six characters in labels; the first character must be a letter,
while subsequent characters must be letters, numbers, ?, or the underbar
character (_I. We will use only capital letters or numbers, although some versions
of the assembler allow lower-case letters and other symbols.
RESERVED NAMES
Some names are reserved as keywords and should not be used by the programmer. These are the register names (A, B, C. D. E, H, L, I, Rl. the double register
names (IX, IV, SPI. the register names (AF, BC. DE. HL, AF'. BC', DE', HL'I. and
the states of the four testable flags (C, NC, Z, NZ, M, P, PE. PO).
PSEUDO-OPERATIONS
The assembler has the following basic pseudo-operations:
DEFB
DEFINE BYTE
DEFL
DEFM
DEFS
DEFW
END
EQU
ORG
DEFINE LABEL
DEFINE STRING
DEFINE STORAGE
DEFINE WORD
END
EQUATE
ORIGIN
3-170
Note that DEFB and DEFW define the value of only a single byte or single word. respectively. Establishing a table of values requires a series of DEFB or DEFW pseudo-operations. one for each byte or word of data.
Examples:
ADDR:
DEFW
3165H
results in (ADDR) = 65. and (ADDR+1) = 31 (hexadecimal).
TCONV' DEFB
32
ThiS pseudo-operation places the number 32 In the next byte of ROM and assigns the
name TCONV to the address of that byte.
ERROR: DEFM
'ERROR'
ThiS pseudo-operation places the 7-bit ASCII characters E. R. R. O. and R In the next five
bytes of ROM and assigns the name ERROR to the address of the first byte.
OPERS:
DEFW
DEFW
DEFW
DEFW
FADD
FSUB
FMUL
FDIV
ThiS series of pseudo-operations places the addresses FADD. FSUB. FMUL. and FDIV In
the next eight bytes of memory and assigns the name OPERS to the address of the first
byte. Note that the first byte contains the least significant bits of address FADD.
DEFS IS the Reserve pseudo-operation used to assign locations
RAM: It allocates a specified number of bytes.
In
zao programs usually have several origins: the origins are used as
follows:
DEFS
PSEUDOOPERATION
EQU
PSEUDOOPERATION
DEFL
PSEUDOOPERATION
ORG
PSEUDOOPERATION
1)
2)
3)
4)
5)
6)
7)
To
To
To
To
To
3-171
Examples:
RESET
EOU
0
ORG
RESET
This sequence places the RESET instruction sequence in memory beginning at address
O.
INT1
EOU
ORG
38H
INT1
In
END
~~i~~~ioN
zao
pseudo-operations are as
EOU, DEFL, and MACRO require labels, since the function of these pseudo-operations IS to define the meaning of that label.
DEF8, DEFM. DEFW, and DEFS usually have labels.
ORG, CONDo EN DC. ENDM, and END should not have labels, since the meaning of
such labels IS unclear.
ADDRESSES
The Zilog Z80 assembler allows entries
of the following forms:
any
NUMBERS AND
CHARACTERS
IN ADDRESS
FIELD
ASSEMBLER
ARITHMETIC
AND LOGICAL
OPERATIONS
In
In
1)
2)
3)
4)
5)
6)
3-172
OPERATOR
.NOT. or \
.RES.
...
/
.MOD.
.SHR.
.SHL.
+
-
.AND. or &
.OR. or I
.XOR.
.EO. or =
.GT. or >
.LT. or <
.UGT.
.ULT.
FUNCTION
PRIORITY
UNARY PLUS
UNARY MINUS
LOGICAL NOT
RESULT
EXPONENTIATION
MULTIPLICATION
DIVISION
MODULO
LOGICAL SHIFT RIGHT
LOGICAL SHIFT LEFT
ADDITION
SUBTRACTION
LOGICAL AND
LOGICAL OR
LOGICAL XOR
EQUALS
GREATER THAN
LESS THAN
UNSIGNED GREATER THAN
UNSIGNED LESS THAN
1
1
1
1
2
3
3
3
3
3
4
4
5
6
6
7
7
7
7
7
In address expressions with more than one operator. the order of evaluation IS defined
by the priorities given in the list above. Operators having the same priorities are evaluated from left to right. Expressions In parentheses are evaluated first. Remember that
enclosing an expression entirely In parentheses Indicates a memory address.
The Result operator (.RES.) causes overflow to be suppressed; I.e.. a change in sign
caused by overflow into the sign bit does not result In an assembler error.
The shifts have the form:
.SHR.
.SHL.
op 1.0p2
op 1. op2
where op1 IS the number to be shifted and op2 IS the number of shifts. The shifts
are logical. I.e.. zeros are shifted Into the high-order or low-order bits. respectively.
3)
The comparison operators produce a result of either logical True (all ones) or logical
False (zero).
4)
The operators .GT. and .LT. assume signed twos complement numbers. whereas
.UGT. and .ULT. assume unsigned operands. This means that. for .GT. and .L T..
positive twos complement numbers are larger than negative twos complement
numbers. while the opposite IS the case for .UGT. and .ULT.
3-173
CONDITIONAL ASSEMBLY
The ZSO assembler has a simple conditional assembly
capability based on the pseudo-operations COND and ENDC.
COND IS followed by an expression. for example:
COND
COND
BASE - 1000H
or
BASE - OPER1
COND AND
ENDC
PSEUDOOPERATIONS
If the expression is not zero. the assembler Includes all of the instructions up to the
ENDC pseudo-operation In the program: if the expression IS zero. the assembler Ignores
all instructions between COND and ENDC.
We will not use conditional assemblies or refer to this capability again: It IS sometimes
handy for adding or eliminating debugging instructions. or configuflng unique versions
of a common program.
MACROS
The standard ZSO assembler has a macro capability that
assigns names to instruction sequences. Use the pseudo-operation MACRO to begin the definition and ENDM to end It. The
macro may have parameters and may Include any assembly
language Instructl0!1s except the definitions of other macros.
MACRO AND
ENDM
PSEUDO
OPERATIONS
The macro capability IS often a convenient programming shorthand. but we will not use
It.
Note that instruction sequences defined by macros are generally qUite short they
should not exceed ten or fifteen instructions. longer sequences Should be made Into
subroutines to conserve memory space.
Every MACRO pseudo-operation must have a label: the label is the name with which
you Identify the macro. For a diSCUSSion of thiS sublect. see Chapter 2.
3-174
Chapter 4
SIMPLE PROGRAMS
The only way to learn assembly language programming is to write assembly
language programs. That is whilt we will do for the next six chapters. which contain examples of typical microprocessor tasks. Problems at the end of each
chapter contain variations on the examples given in the text of the chapter. You
should try to run the examples on a ZSO-based microcomputer system to ensure
that you understand the material covered in the chapter.
In thiS chapter we begin with some very simple programs.
2)
3)
4)
5)
6)
7)
Explanatory notes that discuss the instructions and methods used in the program.
The problems at the end of the chapter are similar to the examples; problems
should be programmed on a ZSO-based microcomputer system using the examples
as guidelines.
The source programs in the examples have been constructed as follows:
...----....,
GUIDELINES
FOR
EXAMPLES
1)
2)
The forms In which data and addresses appear are selected for
clarity rather than for consistency. We use hexadecimal numbers for memory addresses. instruction codes. and 8CD data: decimal for numeric
constants: binary for logical masks: and ASCII for characters.
3)
4)
Examples illustrate tasks that microprocessors perform In communications. instrumentation. computer. business equipment. industrial. and military applications.
5)
6)
Simple and clear structures are emphaSized. but programs are as effiCient as possible within thiS gUideline. The notes often describe more efficient procedures.
7)
Programs use consistent memory allocations. Each program starts in memory location 0000 (the RESET location) and ends With the HALT instruction. If your
4-1
microcomputer has no monitor and no Interrupts. you may prefer to end programs
with an endless loop instruction. e.g.:
HERE:
JR
HERE
The hexadecimal version IS 18 followed by FE. You may replace the HALT or JR
HERE instruction with a RESTART or JP instruction that transfers control back to
the monitor in some Z80-based microcomputers.
Consult the user's manual for your microcomputer to determine the required memory
allocations and terminating instruction for your particular system.
PROGRAMMING
GUIDELINES
1)
2)
3)
Make programs reasonably general. Do not confuse parameters (such as the number of elements in an array) with fixed constants (such as 7T or ASCII C).
4)
Never assume fixed initial values for parameters. i.e.. use an instruction to load an
Initial value into a parameter.
5)
6)
Use hexadecimal notation for addresses. Use the clearest possible form for data.
7)
If your microcomputer allows it start all programs In memory location 0000 and
use memory locations starting with 0040 for data and temporary storage. Other16
wise. establish equivalent addresses for your microcomputer and use them consistently. Again. consult the user's manual.
8)
Use meaningful names for labels and vanables. e.g.. SUM or CHECK rather than X.
Y. or Z.
9)
We now summarize some useful information that you should keep in mind when
writing programs.
USING THE
Almost all processing instructions (e.g.. ADD. SUBTRACT.
ACCUMULATOR
AND, OR) use the Accumulator. In most cases you will load
data Into the Accumulator With LD. using either LD A, (addrl to
load data from any memory location or using LD A.(HU to load
data from the address specified in Registers Hand L. Remember that the parentheses
indicate a memory address rather than data.
The preferred method of accessing memory is using implied addreSSing via Registers Hand L. that IS. uSing (HL). This code causes
the Z80 to perform a memory access uSing the address stored in
Registers Hand L. You can use LD HL. data 16 to load a fixed num-
4-2
USING
REGISTER
PAIR HL
ber Into Registers Hand L or LD HL,(addrl to load the contents of two successive memory locations Into Hand L. You can use INC HL or DEC HL to increment or decrement (by
1) the address in Registers Hand L.
The 8-bit anthmetic and logical operations all use the data In the Accumulator as one of
their operands and place their result into the Accumulator.
Some of the 8-bit anthmetlc and logical operations have special
uses, for example:
SPECIAL
INSTRUCTIONS
PROGRAM EXAMPLES
Ones Complement
Purpose: Logically complement the contents of memory location 0040 and place thA
result into memory location 0041.
Sample Problem:
Result:
(0040)
6A
(0041)
95
Source Program:
LD
CPL
LD
HALT
A,(40H)
(41Hl.A
:GET DATA
;COMPLEMENT
:STORE RESULT
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
3A
40
00
2F
32
41
00
76
Instruction
(Mnemonic!
LD
A,(40H)
CPL
LD
(41Hl.A
HALT
The LD A, (addr) and LD (addrl,A instructions contain addresses to determine the source
or destination of the data. The addresses are 16 bits long, with the eight least Significant bits In the word Immediately following the instruction code and the eight most significant bits in the next word (this order IS contrary to normal computer practice). CPL IS
a one-word instruction that Inverts each bit of the Accumulator. It replaces each '0'
with a T and each T with a '0', Just like a set of Inverter gates.
HALT IS used to end all the examples.
Note that we could also place an address into Registers Hand L and then use that address throughout the program. This is shown in the following program.
4-3
Source Program:
LD
LD
CPL
INC
LD
HALT
HL,40H
A,(HL)
POINT TO OPERAND
GET DATA
COMPLEMENT
POINT TO DESTINATION
;STORE RESULT
HL
(HL),A
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
21
40
00
7E
2F
23
Which version do
77
76
YOU
Instruction
(Mnemonic)
LD
HLAOH
LD
CPL
INC
LD
HALT
A,(HL)
HL
(HL),A
think is better?
The two versions require the same number of bytes of memory even though the second
version is two instructions longer, This is because the second version uses fewer explicit addresses,
8-Bit Addition
Purpose: Add the contents of memory locations 0040 and 0041, and place the result
Into memory location 0042.
Sample Problem:
Result:
(0040)
(0041)
38
2B
(0042)
63
Source Program:
LD
LD
LD
ADD
LD
HALT
A,(40H)
B,A
A,(41H)
A,B
(42Hl.A
4-4
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
3A
40
00
47
3A
41
40
80
32
42
00
76
Instruction
(Mnemonic)
LD
A (40H)
LD
LD
B.A
A.(41H)
ADD
LD
AB
(42Hl.A
HALT
Here again. we could alternatively use Registers Hand L as the source for all addresses.
Source Program:
LD
LD
INC
ADD
INC
LD
HALT
HLAOH
A(HU
HL
A.(HU
HL
(HU.A
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
21
40
00
7E
23
86
23
77
76
Instruction
(Mnemonic)
LD
HL,40H
LD
INC
ADD
INC
LD
HALT
A.(HU
HL
A.(HU
HL
(HU.A
In thiS case. the program using Registers Hand L IS Shorter than the one using direct
addressing. Why?
LD HL,40H loads the contents of the following two words of program memory Into
Register Pair HL. The first word goes into Register L. the second Into Register H.
The code (HU means that data IS obtained from or sent to the memory location addressed by Registers Hand L. Thus. LD A.(HU loads the Accumulator with the contents
of the addressed memory location; LD (HU.A loads the addressed memory location with
the contents of the Accumulator. ADD A(HU adds the contents of the location addressed by HL to the contents of the Accumulator. Remember that Hand L contain a
16-blt address. but the memory location with that address contains eight bits of data.
Note the difference between ADD A.(HU and ADD A.H or ADD A.L.
INC HL performs a 16-bit Increment in one instruction cycle. The CPU doesn't use the
8-bit arithmetic Unit for the Increment; It uses the incrementer that It normally uses to
increment the 16-bit Program Counter.
4-5
LDA.(HU and LD (HU,A are preferable to LD A.(addr) and LD (addr).A whenever you
use the same memory location repeatedly or use adjacent locations. because LD A(HU
and LD (HU.A require less program memory and time. Note. however. that you must
load Registers Hand L before you can use (HL).
Result:
(0040)
6F
(0041)
DE
Source Program:
LD
ADD
LD
HALT
A.(40H)
A.A
(41H).A
:GET DATA
:SHIFT LEFT
:STORE RESULT
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
3A
40
00
87
32
41
00
76
Instruction
(Mnemonic)
LD
A.(40H)
ADD
LD
A.A
(41H).A
HALT
ADD A.A Simply adds the contents of the Accumulator to itself. The result. of course. is
twice the onginal data. which is the same result that a logical left shift would produce.
The least Significant bit of the result IS zero. since 0+0 = 1+1 = 0: 1+1 also produces a
Carry to the next bit.
Alternatively. we could replace ADD A.A with SLA A. certainly the more obvious
choice. However. SLA A requires two words of program memory and eight clock cycles.
while ADD A.A requires one word of program memory and four clock cycles. The
difference IS caused by the fact that SLA A is one of the extra instructions added to the
original 8080A set (re;"ember the companson presented earlier).
Resu It:
(0040)
3D
(0041)
OD
4-6
Source Program:
LD
AND
LD
HALT
A.(40H)
00001111B
(41H).A
:GET DATA
:MASK 4 LSB'S
:STORE RESULT
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
3A
40
00
E6
OF
32
41
00
76
Instruction
(Mnemonic)
LD
A.(40H)
AND
00001111B
LD
(41H),A
HALT
The mask (00001111) IS written In binary to make ItS function clearer to the reader. Binary notation for masks is generally much clearer than hexadeCimal notation. although
the resu Its are the same. Hexadecimal notation shou Id be used for masks longer than
four bits. The comments should explain the masking operation.
When the argument In the address field IS a number. AND logically ANDs the contents
of the Accumulator with the contents of the word of program memory immediately
following the instruction. AND may be used to clear bits that are not in use. The four
least significant bits could be an Input from a switch or an output to a numeric display.
SUB
LD
HALT
A
(40H),A
:CLEAR LOCATION 40
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
97
Instruction
(Mnemonic)
SUB
LD
32
40
00
76
A
(40H).A
HALT
SUB A subtracts the number in the Accumulator from Itself. The result IS to clear the
Accumulator. SUB A. XOR A. or LD A.O can all clear the Accumulator. LD A.O takes
more time and memory but doesn't affect the status flags.
Word Disassembly
Purpose: Divide the contents of memory location 0040 into two 4-blt sections and
store them In memory locations 0041 and 0042. Place the four most significant bits of memory location 0040 into the four least Significant bit positions
4-7
of memory location 0041, place the four least significant bits of memory
location 0040 into the fou r least significant bit positions of memory location
0042. Clear the four most significant bit positions of memory locations 0041
and 0042.
Sample Problem:
Result:
(0040)
3F
(0041)
(0042)
03
OF
Source Program:
LD
LD
LD
RRA
RRA
RRA
RRA
AND
INC
LD
LD
AND
INC
LD
HALT
HLAOH
A.(HU
B,A
:GET DATA
;SHIFT DATA RIGHT 4 TIMES
00001111B
HL
(HLl,A
A,B
00001111B
HL
(HL),A
Object Program:
Memory Address
(Hex)
Memorv Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
OOOB
0009
OOOA
OOOB
OOOC
0000
OOOE
OOOF
0010
0011
0012
21
40
00
7E
47
1F
1F
lF
1F
E6
OF
23
77
78
E6
OF
23
77
76
Instruction
(Mnemonic)
LD
HLAOH
LD
LD
RRA
RRA
RRA
RRA
AND
A.(HL)
B,A
00001111B
INC
LD
LD
AND
HL
(HLl,A
A,B
00001111B
INC
LD
HALT
HL
(HLl,A
Instructions usmg the address in Registers Hand L occupy only one word of program
memory. However, HL must be loaded before the address can be used. Thus, implied
memory addressing saves time and memory, as compared to direct memory addressing,
only when the program repeatedly uses the same address or consecutive addresses.
4-8
RRC shifts the Accumulator right one bit circular. with the least significant bit going to
the most significant bit positiOn and to the Carry. Shifting the Accumulator right four
times requires four RRCs. We could use SRL A to provide a logical shift directly (no final
AND would then be necessary). However. SRL A requires tWice as much time and
memory as RRC. Try substituting SRL A for RRC and see the difference. Another alternative would be to use the RLD instruction to replace both the mask and the store.
However. this solution IS not optimal In terms of either storage or execution speed due
to the constraint that the high-order nibble of each result must equal zero.
Many Z80 instructions affect a pair of 8-bit registers. The pairs are HL (H and L). DE (0
and El. and 8C (8 and Cl. Registers B. D. and H are the most significant eight bits of the
pairs; Registers C. E. and L are the least significant eight bits. The common instructions
that use pairs of registers are LD rp (Load Register Pair). INC rp (Increment Register
Pair). DEC rp (Decrement Register Pair). and .ADD HL.rp (Add Register Pair to Hand L).
a
Result:
b
Result:
(0040)
(0041)
3F
2B
(0042)
3F
(0040)
(0041)
75
A8
(0042)
A8
Source Program:
DONE.
LD
LD
INC
CP
JR
LD
INC
LD
HALT
HL.40H
A.(HL)
HL
(HL)
C.DONE
A.(HL)
HL
(HL).A
Object Program:
Memory Address
(Hex)
Memorv Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
21
40
00
7E
23
BE
30
01
7E
23
77
76
Instruction
(Mnemonlc)
DONE:
4-9
LD
HLAOH
LD
INC
CP
JR
A(HL)
HL
(HL)
NC.DONE
LD
INC
LD
HALT
A(HL)
HL
(HL).A
CP (HL) sets the flags as if the contents of the memory location addressed by Hand L
had been subtracted from the contents of the Accumulator. However, the contents of
the Accumulator are left unchanged for later comparisons or other processing.
If A is the contents of-the Accumulator and X is the second operand for a CP instruction, then the flags are set as follows:
1)
2)
Zero = 1 if A = X
Zero = 0 if A f:. X
Carry = 1 if A < X
Carry = 0 if A 2;; X
(A, X are unsigned binary numbers)
CP sets the Carry to 1 if a borrow would be necessary to actually perform the subtraction. I.e.. if the number being subtracted from the contents of the Accumulator is
greater than those contents. Thus, the sequence CPo JR NC.DONE causes a Jump to
DONE if the contents of the Accumulator are greater than or equal to the other number.
JR NC,DONE causes a jump to memory location DONE if the Carry flag = O. Otherwise
(if Carry = 1). the computer continues with the next sequential memory location after
the JR instruction.
DONE is a label. a name which you assign to a location In memory so that it IS easier to
remember. Note that labels are followed by a colon on the line where they are defined.
The label makes the destination of the branch clearer. particularly when relative addressing IS being used. The assembler calculates the required offset (caution: some zao
assemblers will not do thiS). Using a label is preferable to Just specifying the offset (j.e..
JR NC.$+3) since the zao's instructions vary in length. You could therefore easily make
an error In determining an offset.
If the branch conditions are not satisfied, the processor simply proceeds to the next sequential location In program memory (i.e.. it executes the instruction LD A,(HL}l.
The zao assemblers allow SIX characters in labels - the first must be a letter. while the
others may be letters or numbers (some special characters are allowed but we will not
use them).
The JR instruction uses relative addressing in which the second word of the Instruction
IS an 8-blt twos complement number that the CPU adds to the address of the next instruction to find the target address. In the example, the relative offset is 0009 (target
address) minus 0008 (address Immediately follOWing the branch) or 01.
We should note that some Z80 assemblers will not calculate the offset In the form
shown. These assemblers require an offset in the address field, rather than the label of
the target instruction. If you have such an assembler. use the form JR NC,DONE-$.
Remember that $ means "the address of the current instruction"
The Z80 has two sets of Jump Instructions, JP (Jump) and JR (Jump RelatiVe). The JP
Instructions require a complete memory address; they occupy three bytes of memory
and execute In ten clock cycles. The JR instructions require only a one-word offset:
they occupy two bytes of memory and execute in 12 cycles if a jUmp is actually performed and in 7 if not. So the JR instructions use less memory than JP instructions but
may require a little extra time if a jump IS performed hhe extra time IS used to execute
the required 16-blt addition of program counter and offset).
4-10
16-Bit Addition
Purpose: Add the 16-bit number in memory locations 0040 and 0041 to the 16-bit
number in memory locations 0042 and 0043. The most significant eight bits
are in memory locations 0041 and 0043. Store the result in memory locations 0044 and 0045. with the most significant bits in 0045.
Sample Problem:
(0040)
(0041)
(0042)
(0043)
2A
67
F8
14
LD
LD
ADD
LD
HALT
HL.(40H)
DE.(42H)
HL.DE
(44H),HL
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
2A
40
00
ED
58
42
00
19
22
Instruction
(Mnemonic)
LD
HL.(40H)
LD
DE.(42H)
ADD
LD
HL.DE
(44H).HL
44
00
76
HALT
LD HL,(addr) loads Registers Hand L from two memory locations. the one specified in
the instruction and the next consecutive one. The contents of the first addressed location go to Register L. The contents of the next location go to Register H. Thus. LD
HL,(40H) means L = (40), H = (41), The actual transfer proceeds one byte at a time and
takes 16 clock cycles. The advantage of the 16-bit Load Instruction over two 8-bit Load
Instructions is that the CPU has to fetch only one instruction from memory.
Note the difference betweell LD HL,(addr). which loads the contents of the two RAM
locatIOns at addr and addr+ 1 into Hand L. and LD HL.data16. which loads the contents
of the next two bytes pOinted to by the instruction counter into Hand L. Since these
two bytes immediately follow the op-code. loads of thiS type are referred to as load immediate instructions.
LD DE.(addr) is similar to LD HL,(addr) except that it takes one extra word of memory
and four more clock cycles. This is one of the Instructions that is present in the Z80 set
but not in the 8080/8085 sets. An alternative approach is:
EX
LD
DE.HL
HL,(42H)
EX DE.HL exchanges the contents of Registers D and E with Hand L. No numbers are
changed or destroyed. The advantage of EX DE.HL will become obvIous if you try to
replace it with a series of LD instructions.
ADD HL.DE adds the 16-blt number In Registers D and E to the 16-bit number in
Registers Hand L. The result IS placed into Registers Hand L. ADD HL.DE actually adds
one byte at a time. It executes in 11 clock cycles.
LD (addrl.HL stores the contents of Registers Hand L Into two memory locatIOns. the
one specified In the instruction and the next consecutive one. The contents of L go Into
the specified location and the contents of H go Into the next location. Thus. LD (44Hl.HL
means (44) = L. (45) = H.As with LD HL.(addrl. the actual transfer proceeds one byte at
a time and requires 16 clock cycles.
Although the zao IS an a-bit processor. It has instructions that handle 16-bit numbers.
These instructions are Intended pnmarily for handling addresses. but you can also use
them for 16-blt data. The most common ones and their uses are:
1)
2)
3)
4)
5)
6)
Table of Squares
Purpose: Calculate the square of the contents of memory location 0040 from a table
and place it Into memory location 0041. Assume that memory location 0040
contains a number between 0 and 7 inclusive (0 ~ (0041) ~ 7).
Memory Address
(Hex)
(Hex)
0050
0051
0052
0053
0054
0055
0056
0057
00
01
04
09
10
19
24
31
(Decimall
0
1
4
9
16
25
36
49
Sample Problems:
a.
(0041)
Result:
b.
Result:
03
(0042)
09
(0041)
06
(0042)
24
4-12
(0 2)
(12)
(22)
(3 2)
(4 2)
(52)
(6 2)
(7 2)
Source Program:
LD
LD
LD
LD
ADD
LD
LD
HALT
SQTAB:
ORG
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
A.(40H)
:GET DATA
;MAKE DATA INTO 16-BIT INDEX
L.A
H.O
DE,SOTAB
HL.DE
A,(HL)
(41H).A
50H
;SQUARE TABLE
o
1
4
9
16
25
36
49
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
3A
40
00
LD
A.(40H)
6F
LD
LD
L.A
LD
DE.SOTAB
ADD
LD
LD
HL.DE
A.(HL)
(41H).A
OOOA
OOOB
oooe
0000
OOOE
0050
0051
0052
0053
0054
0055
0056
0057
Instruction
(Mnemonic)
26
00
11
H.O
50
00
19
7E
32
41
00
76
00
01
04
09
10
SQTAB:
19
24
31
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
0
1
4
9
16
25
36
49
Note that you must also enter the table of squares into memory (the assembler pseudooperation DEFB will handle this!' The table of squares is constant data, not parameters
that may change; that is why you can initialize the table using the DEFB pseudo-operation, rather than by executing instructions to load values into the table. Remember that
the table is part of the program memory (ROM in most systems).
LD LA moves the data in the Accumulator to Register L. The data IS the eight least significant bits of the index. You cannot always assume that the data presented to your
4-13
program is in the proper range. It is always a good practice to range check all critical
values. Range checking consists of testing a value to ensure that It is within the proper
lower and upper limits. Any byte can have a value in the range 0 to 255. If the value
stored in the byte at location 0040H is greater than seven, the program will reference
an undefined byte beyond the end of the square table, causing the program to generate
erroneous results Range checking will prevent this error from occurring.
LO H,O clears Register H so that it does not interfere with the 16-bit addition of starting
address and index. Never assume that a register contains zero at the start of a program.
LO OE.SQTAB loads the starting address of the table Into Registers 0 and E. We use 0
and E for the starting address since the ADD HL Instruction does not change 0 and E.
Thus, the starting address of the table will still be in 0 and E after the addition, in the
event that we want another element from the table.
ADD HL,OE adds the starting address and the Index: the result in Hand L is thus the address of the correct entry. LO A.(HL) then moves that entry to the Accumulator.
Arithmetic that a microprocessor cannot do directly in a few instructions is often best
performed with lookup tables. Lookup tables Simply contain all the possible answers to
the problem: they are organized so that the answer to a particu lar problem can be
found easily, The arithmetic problem now becomes an accessing problem - how do
we get the correct answer from the table? We must know two things: the position of
the answer in the table (called the index) and the base. or starting, address of the table.
The address of the answer is then the base address plus the index,
The base address. of course. is a fixed number for a particular table, How can we determine the index? In simple cases. where a single piece of data is involved. we can organize the table so that the data is the Index. In the table of squares, the Oth entry in the table contains zero squared. the first entry one squared, etc. In more complex cases,
where the spread of input values is very large or there are several data items involved
(e.g.. roots of a quadratic or number of permutations), we must use more. complicated
methods to determine indexes.
The basic tradeoff in using a table is time vs. memory. Tables are faster. since no computations are required. and simpler. since no mathematical methods must be devised
and tested. However, tables can occupy a large amount of memory if the range of the
input data is large, We can often reduce the size of a table by limiting the accuracy of
the results, scaling the input data. or organizing the table cleverly. Tables are often
used to compute transcendental and trigonometric functions. linearize inputs. convert
codes, and perform other mathematical tasks.
Result:
(0040)
(0041)
67
E2
(0042)
(0043)
98
10
The ones complement inverts each bit of the original number: the sum of the original
number and its ones complement will always be all 1 bits.
4-14
Source Program:
LD
LD
CPL
LD
LD
CPL
HL,{40Hl
A.L
A.H
:COMPLEMENT 8 MSS'S
LD
H.A
(40H).HL
LD
HALT
:GET DATA
:COMPLEMENT 8 LSS'S
LA
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOS
OOOC
2A
40
00
7D
2F
6F
7C
2F
67
22
42
00
76
Instruction
(Mnemonic)
LD
HL,{40H)
LD
CPL
LD
A.L
LD
LA
A.H
CPL
LD
LD
H.A
(42H).HL
HALT
Despite the Z80' s 16-bit instructions. you must use 8-bit instructions to perform most
arithmetic and logical operations. The 16-bit instructions can. however. be used to load
and store data and occasionally to do a few 16-bit arithmetic operations. such as addition. subtraction. incrementing. and decrementing. You will soon learn that the 16-bit
instructions are far from a complete set and you may often run into awkward problems
if using them to manrpulate 16-blt data.
PROBLEMS
1) Twos Complement
Purpose: Place the twos complement of the contents of memory location 0040 into
memory location 0041. The twos complement is the ones complement plus
one.
Sample Problem:
Result:
(0040)
3E
(0041)
C2
The sum of the original number and its twos complement is zero (try the sample case).
2)
8-Bit Subtraction
Purpose: Subtract the contents of memory location 0041 from the contents of memory
location 0040. Place the result into memory location 0042.
Sample Problem:
Result:
(0040)
(0041)
77
39
(0042)
3E
4-15
~--~ ..
_---
3)
Purpose: Shift the contents of memory location 0040 left two bits and place the result
into memory location 0041. Clear the two least significant bit positions.
Sample Problem:
Result:
4)
(0040)
50
(0041)
74
Purpose: Place the four most significant bits of the contents of memory location 0040
into memory location 0041. Clear the four least significant bits of memory
location 0041.
Sample Problem:
Resu It:
5)
(0040)
C4
(0041)
CO
6)
Word Assembly
Purpose: Combine the four least significant bits of memory locations 0040 and 0041
Into a word and store them in memory location 0042. Place the four least significant bits of memory location 0040 Into the four most significant bit POSItions of memory location 0042; place the four least Significant bits of memory location 0041 Into the four least Significant bit positions of memory location 0042.
Sample Problem:
Result:
7)
(0040)
(0041)
6A
83
(0042)
A3
Purpose: Place the smaller of the contents of memory locations 0040 and 0041 Into
memory location 0042. Assume that 0040 and 0041 contain unsigned binary numbers.
Sample Problems:
a.
Result:
b.
Result:
8)
(0040)
(0041)
3F
2B
(0042)
28
(0040)
(0041)
75
A8
(0042)
75
24-Bit Addition
Purpose: Add the 24-blt number in memory locations 0040.0041. and 0042 to the 24bit number in memory locations 0043. 0044. and 0045. The most significant
eight bits are in memory locations 0042 and 0045: the least significant eight
bits are in memory locations 0040 and 0043. Store the result In memory
locations 0046. 0047. and 0048 with the most significant bits in 0048 and
the least significant bits in 0046.
4-16
Sample Problem:
(0040)
(0041)
(0042)
(0043)
(0044)
(0045)
2A
67
35
F8
A4
51
(0046)
(0047)
(0048)
22
OC
87
Result:
9)
Sum of Squares
Purpose: Calculate the squares of the contents of memory locations 0040 and 0041
and add them together, Place the result Into memory location 0042. Assume
that memory locations 0040 and 0041 both contain numbers between 0 and
7 inclusive (0 ~ (0040) ~ 7 and 0 ~ (0041) ~ 7), Use the table of squares
from the example entitled Table of Squares.
Sample Problem:
(0040)
(0041)
03
06
(0042)
2D
2
2
that IS, 3 +6 = 9 + 36 =45
2D (hex)
Result:
10)
Purpose: Place the twos complement of the 16-blt number In memory locations 0040
and 0041 (most Significant bits in 0041) Into memory locations 0042 and
0043 (most significant bits In 0043).
Sample Problems:
a.
Result:
b.
Result:
(0040)
00
(0041)
58
(0042)
(0043)
00
A8
(0040)
(0041)
72
00
(0042)
(0043)
8E
FF
4-17
Chapter 5
SIMPLE PROGRAM LOOPS
The program loop IS the basIc structure that forces the CPU to repeat a sequence of instructions. Loops have four sections:
1)
The initialization section. which establishes the starting values of counters. address
registers (pointers), and other variables.
2)
The processing section. where the actual data manipulation occurs. This is the section that does the work.
3)
The loop control section. which updates counters and pointers for the next Iteration.
4)
Note that the computer performs Sections 1 and 4 once. while it may perform Sections
2 and 3 many times. Thus. the execution time of the loop will mainly depend on the execution time of Sections 2 and 3. You will want Sections 2 and 3 to execute as quickly
as possible; do not worry about the execution time of Sections 1 and 4. A typical program loop can be flowcharted as shown in Figure 5-1. or the positions of the processing
and loop control sections may be reversed as shown in Figure 5-2. The processing section in Figure 5-1 IS always executed at least once. while the processing section In
Figure 5-2 may not be executed at all. Figure 5-1 seems more natural. but Figure 5-2 IS
often more efficient and avoids the problem of what to do when there IS no data (a
bugaboo for computers. and the frequent cause of silly situations like the computer
dunning someone for a bill of $0.00).
The loop structure can be used to process entire blocks of data. To accomplish this. the
program must increment an address register (usually register pair HL) after each iteration so that the address register points to the next element in the data block. The next
iteration will then perform the same operations on the data in the next memory location. The computer can handle blocks of any length with the same set of instructions.
Implied addressing through register pairs (particularly HL) is the key to processing a
block of data with the
since it allows you to vary the actual memory address by
changing the contents of registers. Indexed addressing. while longer and slower on the
than implied addressing, may be handy when processing more than one block of
data. Note that In the immediate and direct addressing modes. the addresses that are
used are completely determined by the instruction (and thus fixed if the program
memory IS read-only).
.
zao.
zao
5-1
Initialization
SectIon
Processing
SectIon
Loop Control
Section
Concluding
Section
5-2
Initialization
Section
Ves
Processing
Concluding
Section
Section
EXAMPLES
Sum of Data
Purpose: Calculate the sum of a series of numbers. The length of
the series IS In memory location 0041. and the series
begins In memory location 0042. Store the sum in
memory location 0040. Assume that the sum IS an 8-bit
number so that you can Ignore carnes.
Sample Problem:
Result:
(0041)
(0042)
(0043)
(0044)
03
28
55
26
(0040)
(0042) + (0043)
28+55+26
(0044)
A3
There are three entries
In
5-3
8BIT
SUMMATION
Flowchart:
POint.r
Count
=
=
41
(Point.rl
Sum = 0
POInter :; Pointer + 1
Sum = Sum
+ (Polnt.rl
Count
(401
Count - 1
Sum
Note: (Pointed is the contents of the memory location addressed by Pointer. Remember
that on the ZSO, Pointer IS a 16-bit address, while (Pointed is an S-blt byte of
data.
Source Program:
SUMO:
LD
LD
SUB
INC
ADD
DEC
JR
LD
HALT
HL.41 H
B,(HL)
HL
A,(HL)
:SUM = SUM
B
NZ,SUMD
(40H).A
:STORE SUM
5-4
+ DATA
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
0000
21
41
00
46
97
23
86
05
20
FB
32
40
00
76
Instruction
(Mnemonic)
SUMO:
LD
HL.41 H
LD
SUB
INC
ADD
DEC
JR
B.(HU
A
HL
A.(HU
B
NZ.SUMD
LD
(40H),A
HALT
The initialization section of the program IS the first three instructions which set the sum.
counter. and data pointer to their starting values.
Note that you can use LD to transfer data between memory and any of the primary
general purpose registers O.e.. A. B. C. D. E. H. U uSing the address in Registers Hand L.
However. the only transfers allowed uSing direct addreSSing are those that move data to
or from the Accumulator O.e.. LD A.(addrl and LD (addrl.A - there IS no instruction LD
E.(addr). for example),
The processing section of the program is the single Instruction ADD A.(HU which adds
the contents of the memory location being addressed by Registers Hand L to the contents of the Accumulator. and stores the result In the Accumulator. This instruction
does the real work of the program.
The loop control section of the program consists of the instructions INC HL and DEC B.
INC HL updates the pOinter so that the next Iteration adds the next number to the sum.
DEC B decrements the counter that keeps track of how many iterations are left.
The Instruction JR NZ causes a branch if the Zero flag is zero. The offset IS a twos complement number. and the count begins from the memory location Immediately followIng the JR instruction. In this case. the reqUired lump is from memory location OOOA to
memory location 0005. So the offset IS:
0005
-OOOA
05
+F6
FB
If the Zero flag IS one. the CPU executes the next instruction In sequence O.e.. LD
(40H),A), Since DEC B was the last instruction before JR to affect the Zero flag. JR
NZ.SUMD causes a Jump to SUMO if DEC B does not produce a zero result. I.e..
SUMO if B #=0
PC =
{ PC+2 if B =0
(The 2 IS caused by the two-word JR Instruction!'
5-5
The loop control sequence DEC followed by JR NZ IS so common that the Z80 has a
special instruction that both decrements the counter and performs the jump. This instruction IS DJNZ. Decrement and Jump on Not Zero, which decrements Register Band
then lumps by the specified relative offset if the remainder IS not zero. So we could
change the end of the example to:
DJNZ
LD
HALT
SUMO
(40Hl.A
07
08
09
OA
OB
OC
DJNZ
SUMO
LD
(40Hl.A
FC
32
40
00
76
HALT
This change saves one byte of memory and three clock cycles. Note, however, that you
must use Register B as the counter since this is the register that DJNZ decrements.
Since the offset in Z80 relative lumps IS only one byte long. such lumps can go no
further than 127 locations forward or 128 locations backward (actually 129 forward or
126 backward, since the count starts at the end of the 2-word instruction). Longer
jumps must use the JP instructions.
Most computer loops count down rather than up so that the Zero flag can serve as an
eXit condition. Remember that the Zero flag IS 1 if the result was zero and 0 if the result
was not zero. Try rewriting the program so that it counts up rather than down: which
method IS more effiCient?
The order of instructions is often very Important. DEC B must come right before JR
NZ,SUMD, since otherwise the Zero result set by DEC B could be changed by another
instruction. INC HL must come before ADD A. (HL) or else the first number added to the
sum will be the contents of memory location 0041 Instead of the contents of memory
location 0042.
(0042)
(0043)
(0044)
(0045)
03
C8
FA
96
Result: C8 + FA + 96 = 0258
(0040)
58
(0041) = 02
5-6
Flowchart:
POinter
Count
:=
42
(Pointer!
Suml = 0
Sumu = 0
Suml =Suml
+ (PolOterl
Sumu
Count
-=::
Sumu + 1
Count - 1
No
(40) = Suml
(411 ~ Sumu
Source Program:
LD
LD
SUS
LD
DSUMD: INC
ADD
JR
INC
CHCNT DJNZ
LD
LD
INC
LD
HALT
HLA2H
S,(HU
A
C,A
HL
A,(HU
NC,CHCNT
+ DATA
DSUMD
HLAOH
(HU,A
HL
(HU,C
5-7
Object Program:
Memorv Address
(Hex)
Memorv Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
OOOD
OOOE
OOOF
0010
0011
0012
0013
21
42
00
46
97
4F
23
86
30
01
OC
10
F9
21
40
00
Instruction
(Mnemonic)
LD
LD
SUB
LD
DSUMD: INC
ADD
JR
CHCNT'
77
23
71
76
HLA2H
B.(HL)
A
C.A
HL
A.(HL)
NC.CHCNT
INC
DJNZ
C
DSUMD
LD
HL40H
LD
INC
LD
HALT
(HL).A
HL
(HL).C
The structure of this program IS the same as the structure of the last one. The most significant bits of the sum now must be initialized and stored. The processing section consists of three instructions (ADD A.(HL); JR NC.CHCNT; and INC Cl. Including a Conditional Jump.
JR NC.CHCNT causes a lump to memory location CHCNT if the Carry = O. Thus. if there
IS no carry from the 8-blt addition. the program jumps around the statement that increments the most significant bits of the sum. The relative offset IS:
0008
-COOA
--0-1
The relative offset for DJNZ DSUMD
IS:
0006
06
-OOOD
+F3
F9
INC C adds 1 to the contents of Register C. Note that INC BC IS a 16-blt Increment that
adds 1 to Register C and adds the resulting carry to Register B; INC C IS an 8-bit increment that does not account for the carry.
5-8
(0041)
(0042)
(0043)
(0044)
(0045)
(0046)
(0047)
Result:
(0040)
06
68
F2
87
30
59
2A
02. since 0043 and 0044 contain
numbers with an MSB of 1.
Flowchart:
Pomter = 41
Count = (Pointerl
Nne9 = 0
Nne9 = Nne9 + 1
Count = Count - 1
No
(401
= Nne9
5-9
Source Program:
SRNEG:
CHCNT
LD
LD
LD
INC
LD
AND
JP
INC
DJNZ
LD
LD
HALT
HL,41H
B.(HL)
C.O
HL
A.(HU
A
P.CHCNT
SRNEG
A.C
(40H),A
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
OOOB
0009
OOOA
OOOB
OOOC
OOOD
OOOE
OOOF
0010
0011
0012
0013
21
41
00
46
OE
00
23
7E
A7
F2
OD
00
OC
10
F7
79
32
40
00
76
Instruction
(Mnemonic)
SRNEG:
CHCNT:
LD
HL,41H
LD
LD
B.(HU
C.O
INC
LD
AND
JP
HL
A.(HU
A
P.CHCNT
INC
DJNZ
C
SRNEG
LD
LD
A.C
(40H),A
HALT
AND A simply sets the flag bits according to the contents of the Accumulator without
affecting those contents: OR A has the same effect. This is necessary since merely loadIng the Accu mu latar does not affect the flags.
JP P.CHCNT requires a full 16-blt address. There
there IS on the Carry and Zero flags.
IS
Note that all we really want to do IS test the value of bit 7 of the memory location addressed by Registers Hand L. The zao has a special bit testing instruction. BIT. that IS
designed specifically for thiS purpose. BIT sets the Z flag to the complement of the indicated bit Within the Indicated register or memory location. For example. BIT 5.D will set
Z to 1 if bit 5 of Register D IS zero. and to 0 if bit 5 of Register D IS one. An implementation of this alternative IS as follows.
5-10
Source Program:
SRNEG:
CHCNT
LD
LD
LD
INC
BIT
JR
INC
DJNZ
LD
LD
HALT
HL,41 H
B.(HL)
C.O
HL
7.(HL)
Z.CHCNT
SRNEG
A.C
(40Hl.A
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
0000
OOOE
OOOF
0010
0011
0012
21
41
00
46
OE
00
23
CB
7E
28
01
OC
10
F8
79
32
40
00
76
Instruction
(Mnemonic)
SRNEG:
CHCNT
LD
HL,41 H
LD
LD
B.(HL)
C.O
INC
BIT
HL
7.(HL)
JR
Z.CHCNT
INC
DJNZ
C
SRNEG
LD
LD
A.C
(40Hl.A
HALT
BIT 7. (HU sets the Z bit if bit 7 of the memory location addressed by Registers Hand L IS
zero. and clears the Z bit if bit 7 of that location is one. BIT does not affect any registers
or memory locations.
This program uses JR Z.CHCNT since no incrementing
IS zero.
IS
Still another approach would be to use the instruction RLC (HL) to shift the sign bit of
the data In memory to the Carry. The required jump would then be JR NC.CHCNT
However. this approach uses extra time (RLC (HL) takes 15 cycles as compared to the
12 needed by BIT 7. (HL)) and also changes the data In memory which may be needed
for other purposes. Note that these disadvantages are related: the extra time IS needed
to return the result to the memory location.
Find Maximum
Purpose: Find the largest element in a block of data. The length of the block IS In
memory location 0041 and the block itself begins in memory location 0042.
Store the maximum In memory location 0040. Assume that the numbers in
the block are all 8-blt unsigned binary numbers.
5-11
Sample Problem:
(0041)
(0042)
(0043)
(0044)
(0045)
(0046)
Result:
(0040)
05
67
79
15
E3
72
Flowchart:
Pointer =
Count =
Max
Pointer
Max
41
(Pointer)
= 0
= POinter
+ 1
= (Pointer}
Count = Count - 1
No
1401 = Max
5-12
Source Program:
NEXTE:
DECNT:
LD
LD
SUB
INC
CP
JR
LD
DJNZ
LD
HALT
:POINT TO COUNT
:COUNT = NUMBER OF ELEMENTS
:MAXIMUM = MINIMUM POSSIBLE VALUE (ZERO)
HLA1H
B.(HU
A
HL
(HU
NC.DECNT
A.(HU
NEXTE
(40H).A
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
OOOD
OOOE
OOOF
21
41
00
46
97
23
BE
30
01
7E
10
F9
32
40
00
76
Instruction
(Mnemonic)
NEXTE:
DECNT'
LD
HLA1H
LD
SUB
INC
CP
JR
B.(HU
A
HL
(HU
NC.DECNT
LD
DJNZ
A.(HU
NEXTE
LD
(40H).A
HALT
05
+F4
F9
The first three instructions of this program form the initialization section.
This program takes advantage of the fact that zero IS the smallest 8-blt unsigned binary
number. When you set the register that contains the maximum value - I n this case the
Accumulator - to the minimum possible value before you enter the loop. then the program will set the Accumulator to a larger value unless all the elements In the array are
zeros.
5-13
The program works properly if there are two elements. but not if there are one or none
at aU Why? How could you solve this problem?
The instruction CP (HU sets the Carry flag as follows (ELEMENT IS the contents of the
address In Registers Hand L and MAX IS the contents of the Accumulatod:
CARRY = 1 if ELEMENT> MAX
CARRY = 0 if ELEMENT ~ MAX
If CARRY = O. the program proceeds to DECNT and does not change the maximum. If
CARRY = 1. the program replaces the old maximum with the current element by executing the instruction LD A.(HU.
The program does not work if the numbers are signed because negative numbers will
appear to be larger than positive numbers. The problem IS somewhat tncky because
overflow could make the result appear to have the wrong sign.
Remember that overflow occurs when the magnitude of a result affects its sign bit. The
Z80 has a Panty/Overflow flag that indicates when twos complement overflow has occurred. Anthmetic operations that result In overflow set this flag. You can then test ItS
value With the instructIOns JP PE.ADDR (Jump on Panty Even - or Jump on Overflow)
or JP PO.ADDR (Jump on Panty Odd - or Jump on No Overflow). One thing you may
have to watch IS that thiS Z80 usage IS inconSistent With the 8080A or 8085
microprocessors. which always use the P flag to indicate panty. The 8080A and 8085
microprocessors have no overflow Indicator.
a.
(0040)
22
Result:
(0041)
(0042)
88
02
(0040)
01
Result:
(0041)
(0042)
80
07
(0040)
CB
Result:
(0041)
(0042)
CB
00
(0040)
00
Result:
(0041)
(0042)
00
00
c.
d.
5-14
Flowchart:
Nshft
Numb
0
140)
Yes
Yes
Shift Numb
lelt 1 bit
Nshlt = Nsht + 1
1411
142)
Numb
Nshlt
Source Program:
CHKMS.
DONE:
LD
LD
LD
AND
JR
JP
INC
ADD
JP
INC
LD
INC
LD
HALT
B.O
HL.40H
A.(HU
A
Z.DONE
M.DONE
B
A.A
CHKMS
HL
(HU.A
HL
(HU,B
NUMBER OF SHIFTS=ZERO
:GET DATA
IS DATA ZEROt
YES. DONE
;DONE IF SIGN BIT IS ONE
;ADD 1 TO NUMBER OF SHIFTS
:SHIFT LEFT ONE BIT
5-15
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
OOOD
OOOE
OOOF
0010
0011
0012
0013
0014
0015
06
00
21
40
00
7E
A7
28
08
FA
11
00
04
87
C3
09
00
23
Instruction
(Mnemonic)
CHKMS.
DONE:
77
23
70
76
LD
B.O
LD
HLAOH
LD
AND
JR
A.(HU
A
Z.DONE
JP
M.DONE
INC
ADD
JP
B
A.A
CHKMS
INC
LD
INC
LD
HALT
HL
(HU.A
HL
(HU.B
JP M.DONE causes a Jump to location DONE if the Sign bit IS 1. This condition may
mean that the last result was a negative number or may Just mean that ItS most significant bit was 1 - the computer supplies only the results: the programmer must prOVide
the interpretation.
ADD A.A adds the number in the Accumulator to itself. The program uses this instructIOn. rather than RLA or RLCA. because ADD A affects the Sign bit while RLA and RLCA
do not.
We could reorganize this program so as to eliminate an extraneous JP and use relative
rather than absolute lumps. One reorganized version would be:
LD
LD
LD
AND
JR
DEC
CHKMS: INC
RLA
JR
RRA
INC
DONE:
LD
INC
LD
HALT
B.O
HL40H
A.(HU
A
Z.DONE
B
B
NC.CHKMS
HL
(HU.A
HL
(HU.D
Show that this version also works. What are its advantages and disadvantages as compared to the prevIous program?
5-16
PROBLEMS
1)
Checksum of Data
Purpose: Calculate the checksum of a series of numbers. The length of the series
IS in
memorv location 0041 and the series itself begins m memory location 0042.
Store the checksum in memory location 0040. The checksum is formed bv
Exclusive-ORing all the numbers in the series together.
Note: Such checksums are often used m paper tape and cassette svstems to ensure
that the data has been read correctly. The calculated checksum is compared to
the one stored with the data - if the two checksums do not agree. the system
will usually either indicate an error to the operator or automatically read the data
agam.
Sample Problem:
(0041)
(0042)
(0043)
(0044)
Result:
2)
(0040)
03
28
55
26
(0042) EB (0043) EB (0044)
28EB55EB26
00101000
EB01010101
011 11101
EB00100110
01011011
5B
Purpose: Calculate the sum of a series of 16-bit numbers. The length of the series
IS in
memory location 0042 and the series itself begms m memory location 0043.
Store the sum in memory locations 0040 and 0041 (eight most significant
bits in 0041). Each 16-bit number occupies two memory locations. with the
eight most significant bits in the higher address. Assume that the sum can
be contained in 16 bits.
Sample Problem:
(0042)
(0043)
(0044)
(0045)
(0046)
(0047)
(0048)
03
F1
28
1A
30
89
4B
5-17
3)
Purpose: Determine the number of zero. positive (most significant bit zero but entire
number not zerol. and negative (most significant bit 1) elements In a block.
The length of the block IS in memory location 0043 and the block Itself starts
In memory location 0044. Place the number of negative elements In memory
location 0040. the number of zero elements In memory location 0041. and
the number of positive elements in memory location 0042.
Sample Problem:
(0043)
(0044)
(0045)
(0046)
(0047)
(0048)
(0049)
Resu It: 2 negative.
(0040)
(0041)
(0042)
4)
06
68
F2
87
00
59
2A
1 zero. and 3 positive. so
02
01
03
Find Minimum
Purpose: Find the smallest element In a block of data. The length of the block is In
memory location 0041 and the block Itself begins In memory location 0042.
Store the minimum In memory location 0040. Assume that the numbers In
the block are 8-blt unsigned binary numbers.
Sample Problem:
Resu It:
5)
(0041)
(0042)
(0043)
(0044)
(0045)
(0046)
05
67
79
15
(0040)
E3
72
Count 1 Bits
Purpose: Determine how many bits in memory location 0040 are one and place the
result In memory location 0041.
Sample Problem:
Result:
(0040)
38 =00111011
(0041)
05
5-18
Chapter 6
CHARACTER-CODED DATA
Microprocessors often handle character-coded data. Not only do keyboards,
teletypewriters, communications devices, displays, and computer terminals expect or
provide character-coded data: many instruments, test systems, and controllers also reqUire data in this form. The most commonly used code IS ASCII. Baudot and EBCDIC are
found less frequently. We will assume all of our character-coded data to be 7-blt ASCII
with the most significant bit zero (see Table 6-1).
In handling ASCII-coded data are:
HANDLING
DATA IN
The codes for the numbers and letters form ordered sub-seASCII
quences. The codes for the decimal numbers are hex 30
through 39, so that you can convert between decimal and
ASCII with a simple additive factor. The codes for the upper-case letters are hex 41
through 5A. so that you can do alphabetic ordering by sorting the data In increasing numerical order.
2)
3)
An ASCII device will handle only ASCII data. To print a 7 on an ASCII printer. the
microprocessor must send hex 37 to the printer: hex 07 is the 'bell' character.
Similarly, the microprocessor will receive the character 9 from an ASCII keyboard
as hex 39: hex 09 IS the 'tab' character.
4)
Some ASCII devices do not use the full character set. For example, control characters and lower-case letters may be Ignored or printed as spaces or question marks.
5)
16
16
16
3F 16 - ? (question mark)
7F 16 - rubout or delete character
6)
Each ASCII character occupies seven bits. This allows a large character set but IS
wasteful when the data is limited to a small subset such as the decimal numbers.
An a-bit byte, for example. can hold only one ASCII-coded decimal digit. while I'
can hold two BCD-coded digits.
6-1
NUL
SOH
STX
ETX
EOT
ENQ
ACK
BEL
BS
HT
LF
VT
FF
CR
SO
SI
OLE
DCl
DC2
DC3
DC4
NAK
SYN
ETB
CAN
EM
SUB
ESC
FS
GS
RS
US
SP
0
1
2
3
4
5
6
P
Q
R
S
T
U
V
W
X
Y
a
b
c
d
e
f
g
h
Hex LSD
0
1
2
3
6
6
7
8
9
A
B
C
D
E
F
I
"
#
$
%
&
7
(
.
)
A
B
C
0
E
F
G
H
<
=
>
?
K
L
M
N
Z
[
1
A
p
q
r
s
t
u
v
w
x
I
k
I
m
n
0
DEL
EXAMPLES
Length of a String of Characters
Purpose: Determine the length of a string of ASCII characters (seven bils with most
significant bit zerol. The string starts In memory location 0041. the end of
the string IS marked by a carriage return character ('CR', hex 001. Place the
length of the string (excluding the carriage returnl Into memory location
0040.
Sample Problems:
a.
Result:
b
Result:
(0041)
00
(0040)
00
(00411
(0042)
(0043)
(00441
(0045)
(0046)
(0047)
52
41
54
48
45
52
00
'R'
'A'
(0040)
06
T
'H'
'E'
'R'
CR
6-2
Flowchart:
Pomter
41
Length
Yes
Length
POinter
= Length
+1
= Pointer
+1
(40)
Length
Source Program:
CHKCR:
LD
LD
LD
CP
HL.41 H
JR
Z.DONE
B
HL
CHKCR
INC
INC
JR
DONE:
LD
LD
HALT
B.O
A.ODH
(HL)
A.B
(40H).A
6-3
Object Program:
Memorv Address
(Hex)
Memorv Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
OOOD
OOOE
OOOF
0010
0011
0012
21
41
00
06
00
3E
aD
BE
28
04
04
23
18
F9
78
32
40
00
76
Instruction
(Mnemonic)
CHKCR:
DONE:
LD
HL.41 H
LD
B.O
LD
A.ODH
CP
JR
(HL)
Z.DONE
INC
INC
JR
B
HL
CHKCR
LD
A.B
LD
(40H).A
HALT
The camage return (CR) IS just another ASCII character (hex aD) as far as the computer
IS concerned. The fact that the output device treats the carnage return as a control
character rather than as a pnntlng character does not affect the computer.
The Compare Instruction. CPo sets the flags as if a subtraction had been performed. but
leaves the camage return character In the Accumulator for later comparisons. The Zero
(Z) flag IS affected as follows:
Z= 1
Z= a
The instruction INC B adds 1 to the string length counter in Register B. LD B.O Initializes
this counter to zero before the loop begins. Remember to initialize vanables before
uSing them in a loop.
This loop does not terminate because a counter is decremented to zero. The computer
will simply continue examining characters until it finds a camage return. You may have
to place a maximum count in a loop like this to avoid problems with erroneous strings
that do not contain a carnage return. What would happen if the example program were
used with such a stnng?
Note that. by rearranging the logiC and changing the initial conditions. you can shorten
the program and decrease ItS execution time. If we adjust the flowchart so that the program increments the counter and pOinter before it looks for the camage return. only one
Jump instruction is necessary instead of two. The new flowchart and program are as
follows:
6-4
Flowchart:
POinter
40
-1
Length
length = Length + 1
POInter = POinter + 1
(40)
Length
Source Program:
CHKCR:
LD
LD
LD
INC
INC
CP
HLAOH
B.OFFH
A.ODH
HL
B
JR
NZ.CHKCR
A.B
(40Hl.A
LD
LD
HALT
(HU
6-5
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
OOOD
OOOE
OOOF
0010
21
40
00
06
FF
3E
OD
23
04
BE
20
FB
78
32
40
00
76
Instruction
(Mnemonic)
CHKCR:
LD
HLAOH
LD
B.OFFH
LO
AOOH
INC
INC
CP
JR
HL
8
(HL)
NZ.CHKCR
LO
LD
A,B
(40H),A
HALT
The task of looking for a particular value In a list table, or stnng IS a common one. The
Z80 microprocessor has. in fact special instructions that simplify this task.
These special Instructions are called Block Search Instructions;
they operate as follows:
I""':""'~~-""""
BLOCK
SEARCH
CPI compares the contents of the memory location addressed by INSTRUCTIONS ..
HL with the contents of the Accumulator (just like CP (HUl. It then
Increments HL and decrements the byte counter (register pair BC). The Panty/Overflow
bit IS reset if the byte counter is decremented to zero and set otherwise. CPO IS the
same instruction except that It decrements HL Instead of incrementing It.
CPIR and CPDR are the repeated forms of the Block Search instructions. These instructions repeat the basic Search instruction until either BC IS decremented to zero or a true
comparison occurs (i.e.. A = (HUl. Remember that decrementing BC to zero resets the
Panty/Overflow bit while finding a match sets the Zero bit.
Note that BC contains a 16-bit counter. Thus. the Block Search Instructions can handle
stnngs of any length.
A version of the prevIous program uSing CPI IS shown below.
Source Program:
CHKCR:
LD
LD
LD
CPI
JR
LD
SUB
LO
HALT
HLAl H
BC.O
A,ODH
NZ.CHKCR
A.OFFH
C
(40H),A
6-6
Object Program:
Memory Address
(f~ex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
0000
OOOE
OOOF
0010
0011
0012
21
41
00
01
00
00
3E
00
ED
Al
20
FC
3E
FF
91
32
40
00
76
Instruction
(Mnemonic)
CHKCR:
LO
HL.41 H
LO
BC.O
LO
A.OOH
CPI
JR
NZ.CHKCR
LO
A.OFFH
SUB
LO
C
(40Hl.A
HALT
A little manipulation is necessary to calculate the string length. since CPI decrements
the byte counter (BC) Instead of incrementing It as we did with INC B In the earlier program. Also. the byte counter IS decremented one extra time when the camage return IS
found. How cou Id you adjust the initial conditions to handle this problem?
In fact. we can Imorove the program even further by using CPIR to remove the need for
the relative lump JR. CPIR does everything that CPI does. but it also automatically
repeats the comparison procedure unless A = (HL) or BC has been decremented to zero.
The program uSing CPIR is shown below.
Source Program:
LO
LO
LO
CPIR
LO
SUB
LD
HALT
HL,41 H
BC.O
A.OOH
A.OFFH
C
(40H).A
6-7
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
0000
OOOE
OOOF
0010
21
41
00
01
00
00
3E
00
LO
HL.41 H
LO
BC,O
LO
A,OOH
ED
CPIR
Bl
3E
FF
91
32
40
00
76
Instruction
(Mnemonic)
LO
A,OFFH
SUB
LO
C
(40H),A
HALT
The multiple operation Instructions like CPI and CPIR have the same effect as the sequences they replace. The savings in execution time and memory come about because
the processor needs fewer instructions for each pass through the loop. Thus, the real
savings IS in loop execulon.
All these programs assume that the string is less than 256 bytes long. How would you
change them to handle longer strings?
a.
Resu It:
b.
Result:
(0042)
37
(0040)
(0041)
(0042)
(0043)
(0044)
(0045)
(0046)
20
20
20
46
20
(0040)
(0041)
'7'
SP
SP
SP
F
SP
6-8
Flowchart:
POinter
42
No
POinter =
POinter + 1
= POinter
Source Program:
CHBlK:
DONE,
lD
lD
CP
JR
INC
JR
lD
Hl,42H
A,20H
(HU
NZ,DONE
Hl
CHBlK
(40Hl.Hl
HALT
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
OOOD
OOOE
21
42
00
3E
20
BE
20
03
23
Instruction
(Mnemonic)
CHBlK,
18
FA
22
40
00
76
DONE,
lD
HL.42H
lD
A,20H
CP
JR
(HU
NZ.DONE
INC
JR
Hl
CHBlK
LD
(40Hl.Hl
HALT
6-9
Looking for spaces in strings IS a common task. Spaces often are eliminated from
strings when they are used simply to increase readability or to fit partlcu lar formats. It is
obviously wasteful to store and transmit beginning. ending or extra spaces. particularly
if you are paying for the communications capability and memory required. Data and
program entry. however. are much simpler if extra spaces are tolerated. Microcomputers are often used In situations like this to convert data between forms that are easy
for humans to use and forms that are efficiently handled on computers and communications lines.
The Instruction LD (addrl.HL is convenient for stonng addresses in the Z80 format (least
significant byte first). LD (40Hl.HL stores the contents of Register L in memory location
0040 and the contents of Register H In memory location 0041.
Again. if we alter the initial conditions so that the loop control section precedes the processing section. we can reduce the number of bytes In the program and decrease the
loop's execution time. The rearranged flowchart is:
Pointer
POinter
f40
and
':=
41
Pointer + 1
41)
0:::
POinter
Source Program:
CH8LK:
LD
LD
INC
CP
JR
LD
HL,41 H
A,20H
HL
(HL)
Z.CHBLK
(40Hl.HL
HALT
6-10
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
21
41
00
3E
20
23
BE
28
FC
22
40
00
76
Instruction
(Mnemonic)
CHBlK:
lD
Hl.41H
lD
A,20H
INC
CP
JR
Hl
(HU
Z.CHBlK
lD
(40H).Hl
HALT
As In the previous example. we could replace the sequence INC Hl, CP (HU with the
single instruction CPI. However. since we do not need the byte counter In this program.
CPI takes Just as much memory (two bytes) and more time (16 clock cvcles Instead of
13) than the instructions It replaces. We could not use CPIR here since we want the program to terminate when the characters are not the same.
(0040)
(0041)
a.
=
=
02
36
'6'
The program leaves the string unchanged, since the leading digit is not zero.
b.
Result:
(0040)
(0041)
(0042)
(0043)
08
30
30
38
'0'
'0'
'8'
(0041)
(0042)
20
20
SP
SP
6-11
Flowchart:
Count
(40)
POinter
41
(Pointer) = ASCII SP
= 20 (Hexl
Pointer
Count
.==
POinter + 1
Count - 1
Source Program:
CHKZ:
DONE:
LD
LD
LD
INC
CP
JR
LD
DJNZ
HALT
HLAOH
B,(HL)
A:O'
HL
(HL)
NZ,DONE
(HL),20H
CHKZ
6-12
Object Program:
Memory Address
(Hexl
Memory Contents
(Hexl
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
000A
OOOB
OOOC
DODD
OOOE
21
40
00
46
3E
30
23
BE
20
04
36
20
10
F8
76
Instruction
(Mnemonic)
CHKZ:
DONE:
LD
HL.40H
LD
LD
B.(HU
A.-D'
INC
CP
JR
HL
(HU
NZ.DONE
LD
(HU.20H
DJNZ
CHKZ
HALT
You will frequently want to edit decimal strings before they are printed or displayed to
improve their appearance. Common editing tasks include eliminating leading zeros.
JustifYing numbers. adding signs or other identifYing markers. and rounding. Clearly.
printed numbers like 0006 or $27.34382 can be confUSing and annoying.
Here the loop has two exits - one if the processor finds a non-zero digit and the other if
it has examined the entire stnng.
The instruction LD (HU.20H places 20 (hex) Into the memory location addressed by
Registers Hand L. You could also initialize Register C to 20 hex (i.e.. LD C.20H) and use
LD (HU.C to replace the leading zero With a blank. Note the tradeoffs Involved In this example. LD (HU.C executes faster than LD (HU.20H and would thus decrease the Inner
loop's execution time. The overhead reqUired. however. IS an LD C.20H Instruction in
the initialization section of the routine. If this example were to be used in a cash register
application. which sequence would you choose and why?
All digits In the string are assumed to be ASCII: that IS. the digits are hex 30 through 39
rather than the ordinary decimal 0 to 9. The conversion from decimal to ASCII is Simply
a matter of adding hex 30 to the decimal digit.
You may have to be careful. when blanking leading zeros. to leave one zero In the event
that all the digits are zero. How would you do this?
Note that each ASCII digit requires eight bits. as compared to four for a BCD digit.
Therefore. ASCII IS an expensive format in which to store or transmit numerical data.
6-13
Sample Problem;
Result:
(0040)
(0041)
(0042)
(0043)
(0044)
(0045)
(0046)
06
3,1
32
33
34
35
36
(0041)
(0042)
(0043)
(0044)
(0045)
(0046)
81
82
33
84
35
36
Flowchart;
Pointer
41
Count
(40)
(Pointer! = (Pomter!
OR10000000B
(set parity bit!
POinter
= POinter
Count
= Count -
6-14
+1
1
Source Program:
SETPR:
CHCNT
LD
LD
LD
INC
LD
OR
JP
LD
DJNZ
HALT
HL.40H
B,(HU
:GET STRING LENGTH
C,10000000B :GET PARITY BIT OF 1
HL
A,(HU
:GET A CHARACTER
:SET PARITY BIT TO 1 AND TEST PARITY
C
PO,CHCNT
:IS PARITY NOW EVEN?
:YES. SAVE CHARACTER WITH EVEN PARITY
(HU.A
SETPR
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
OOOD
OOOE
OOOF
21
40
00
46
OE
80
23
7E
B1
E2
OD
00
Instruction
(Mnemonic)
SETPR:
77
10
F7
76
CHCNT
LD
HL.40H
LD
LD
B.(HU
C,10000000H
INC
LD
OR
JP
HL
A.(HU
LD
DJNZ
(HU.A
SETPR
"-
PO,CHCNT
HALT
Parity IS often added to ASCII characters before they are transmitted on nOIsy communication lines, to provide a simple error-checking facility. Parity detects all single-bit
errors but does not allow error correction (i.e.. you know that an error has occurred
when the received parity IS wrong, but you cannot tell which bit was changed).
LD C, 1OOOOOOOB saves a panty bit of 1 in Register C. (Note the use of the binary mask:
the purpose of the mask IS clearer when It is specified in thiS manner rather than as 80H
or 128 deCimal.)
The instruction OR C sets the panty (most significant) bit to 1 while retaining all the
other bits as they were. as well as selling the Z80 Panty flag.
The follOWing procedure is used to determine if the panty of the byte in memory is odd
or even. We OR a panty bit Into the byte loaded from memory and then test to see if the
panty is odd. If the panty IS odd. then the byte In memory has even panty. and we jump
down to decrement the count of remaining bytes. If the panty is even. then we know
that the byte in memory has odd panty, and therfore we store the byte in the Accumulator Into that memory location.
The conditional jumps JP PO (Jump on Parity Odd) and JP PE (Jump on Panty Even) are
seldom used except In panty generation and checking. Note that there are no relative
jumps conditional on the value of the Panty bit. just as there are none conditional on
the value of the Sign bit.
6-15
Do not confuse the Panty bit 1nluded In each character and the Z80's Parity flag,
which IS set to 1 if the last anthmetlc or 800lean result had even parity.
An alternative approach uses the Z80 SET instruction. This version takes a little longer
but does not require a temporary register for the parity bit.
Source Program:
SETPR:
CHCNT
LD
LD
INC
LD
OR
JP
SET
DJNZ
HALT
HL,40H
B,(HL)
HL
A,(HL)
A
PE,CHCNT
7,(HL)
SETPR
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
0000
OOOE
21
40
00
46
23
7E
B7
EA
OC
00
CB
FE
10
F6
76
Instruction
(Mnemonic)
SETPR:
CHCNT
LD
HL,40H
LD
INC
LD
OR
JP
8,(HL)
HL
A,(HL)
A
PE,CHCNT
SET
7,(HL)
OJNZ
SETPR
HALT
Pattern Match
Purpose: Compare two stnngs of ASCII characters to see if they are the same. The
length of the strings IS In memory location 0041, one string starts In memory
location 0042 and the other in memory location 0052. If the two stnngs
match, clear memory location 0040: otherwise, set memory location 0040 to
FF hex (all ones).
Sample Problems:
a.
Result:
(0041)
03
(0042)
(0043)
(0044)
43
41
54
(0052)
(0053)
(0054)
43
41
54
'C'
'A'
T
(0040)
00,
'C'
'A'
6-16
b.
Result:
(0041)
03
(0042)
(0043)
(0044)
52
41
54
(0052)
(0053)
(0054)
43
41
54
(0040)
'R'
'A'
T
'C'
'A'
T
In
the
Note: The matching process ends as soon as the CPU finds a difference the strings need not be examined.
Flowchart:
POinter 1 = 42
POinter 2 = 52
Count = (411
Mark = FF (he.1
Mark
(401
Mark
6-17
the rest of
Source Program:
CHCAR:
DONE:
LD
LD
INC
LD
LD
LD
CP
JR
INC
INC
DJNZ
LD
LD
LD
HALT
HL,41H
B.(HU
HL
DE.52H
C.OFFH
A.(DE)
(HL)
NZ.DONE
DE
HL
CHCAR
C.O
A.C
(40Hl.A
Object Program:
Memorv Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
DODD
OOOE
OOOF
0010
0011
0012
0013
0014
0015
0016
0017
0018
21
41
00
46
23
11
52
00
DE
FF
lA
BE
20
06
13
23
10
F8
DE
00
79
32
40
00
76
Instruction
(Mnemonic)
CHCAR:
DONE:
LD
HL.41 H
LD
INC
LD
HL
LD
C.OFFH
LD
CP
JR
A.(DE)
(HL)
NZ.DONE
INC
INC
DJNZ
DE
HL
CHCAR
LD
C.O
LD
LD
A.C
(40Hl.A
B.(HU
DE.52H
HALT
6-18
The order of operations is very important because of the small number of instructions
that use the address In Register Pair DE. You must move a character from the string
pOinted to by DE to the Accumulator and compare It to a character in the string pointed
to by HL. This order of operations IS necessary because the Z80 has no instruction
which allows a comparison to a character in a string pOinted to by DE.
For example. if you replaced LD A.{DE) With LD A,(HL). what would the next instruction
be? This asymmetry IS peculiar to the Z80 and can cause programming nightmares.
Note that each Iteration updates both pointers.
ThiS program could take advantage of the fact that a register IS known to contain zero
after a particular conditional Jump is executed. When the DJNZ CHCAR instruction is
executed. if the branch IS not performed. then we know that Register B contains zero.
Therefore. we can move Register B to Register C. our flag register. to indicate that a
match has been found.
We could also use the Z80's SET and RESET Instructions to handle the flag if we
needed to conserve bits for other purposes.
PROBLEMS
1) Length of a Teletypewriter Message
Purpose: Determine the length of an ASCII message. All characters are 7-bit f\SCII
With MSB = O. The stnng of characters In which the message IS embedded
starts In memory location 0041. The message Itself starts With an ASCII STX
character (hex 02) and ends with ETX (hex 03). Place the length of the
message (the number of characters between the STX and the ETX but including neither) Into memory location 0040.
Sample Problem:
Result:
2)
(0041)
(0042)
(0043)
(0044)
(0045)
40
02
47
4F
03
(0040)
STX
'G'
'0'
ETX
Purpose: Search a string of ASCII characters for the last non-blank character. The
string starts in memory location 0042 and ends With a carriage return
character (hex 00). Place the address of the last non-blank character into
memory locations 0040 and 0041 (most significant bits in 00411.
Sample Problems:
a.
Result:
(0042)
(0043)
37
00
(0040)
(0041)
'7'
CR
6-19
b.
Result:
3)
(0042)
(0043)
(0044)
(0045)
(0046)
(0047)
(0048)
(0049)
ob
(0040)
(0041)
46
00
41
20
48
41
54
20
20
'A'
SP
'H'
'A'
'T'
SP
SP
CR
Purpose: Edit a string of ASCII decimal characters by replacing all digits to the right of
the decimal POint with ASCII blanks (hex 20). The string starts In memory
location 0041 and is assumed to consist entirely of ASCII-coded decimal
digits and a possible decimal point (hex 2E). The length of the string is in
memory location 0040. If no decimal point appears in the string, assume that
the decimal point is implicitly at the far right.
Sample Problems:
a.
Result:
b.
(0040)
04
(0041)
(0042)
(0043)
(0044)
37 '7'
2E"
38 '8'
31 '1'
(0041)
(0042)
(0043)
(0044)
37'7'
2E"
20 SP
20 SP
(0040)
03
(0041)
(0042)
(0043)
26 '6'
37 '7'
31 '1'
Unchanged, as number is assumed to be 671.
Result:
4)
6-20
Sample Problems:
a.
Result:
b.
Result:
5)
(0041)
03
(0042)
(0043)
(0044)
B1
B2
33
(0040)
00,
(0041)
03
(0042)
(0043)
(0044)
B6
33
(0040)
B1
String Comparison
Purpose: Compare two strings of ASCII characters to see which is larger (i.e.. which
follows the other in 'alphabetical' ordering). The length of the strings is In
memory location 0041, one string starts in memory location 0042 and the
other in memory location 0052. If the string starting In memory location
0042 is greater than or equal to the other string, clear memory location
0040: otherwise, set memory location 0040 to FF hex (all ones).
Sample Problems:
a.
Result:
b.
Result:
c.
Result:
(0041)
03
(0042)
(0043)
(0044)
43
41
54
'C'
(0052)
(0053)
(0054)
42
41
54
'B'
'A'
T
(0040)
00,
since CAT
(0041)
03
(0042)
(0043)
(0044)
43
41
54
(0052)
(0053)
(0054)
43
41
54
(0040)
00,
(0041)
03
(0042)
(0043)
(0044)
43
41
54
'C'
'A'
(0052)
(0053)
(0054)
43
55
54
'C'
'U'
T
(0040)
FF.
since CUT
'A'
T
IS
'C'
'A'
T
'C'
'A'
6-21
IS
Chapter 7
CODE CONVERSION
Code conversion is a continual problem in most microcomputer applications. Peripherals provide data In ASCII. BCD. or various special codes. The system must convert the
data into some standard form for processing. Output devices may require data In ASCII.
BCD. seven-segment. or other codes. Therefore, the system must convert the results to
a sUitable form after the processing IS completed.
There are several ways to approach code conversion:
1)
Some conversions can easily be handled by algorithms Invo[ving arithmetic or [oglcal functions. The program may, however, have to handle some special cases separately.
2)
More complex conversions can be handled With lookup tables. The lookup table
method requires [ittle programming and IS easy to apply. However. the table may
occupy a large amount of memory if the range of input values IS large.
3)
Hardware IS readily available for some conversion tasks. Typlca[ examples are
decoders for BCD to seven-segment conversion and Universal Asynchronous
Receiver/Transmitters (UARTs) for conversion between para[lel (ASCII) and serial
(teletypeWriter) formats.
In most applicatIOns, the program should do as much as possible of the code conversion
work. ThiS results In a savings in parts and board space as we[1 as In increased
reliability. Furthermore, most code conversions are easy to program and require [ittle
execution lime.
EXAMPLES
Hex to ASCII
Purpose:
Sample Problems:
a.
Result:
b.
Result:
(0040)
OC
(0041)
43
(0040)
06
(0041)
36
'C'
'6'
7-1
Flowchart:
Data =(401
Ves
Data
= Data
+ ASCII A
-ASCII 9 - 1
Result =
Data + ASCII Zero
(411 = Result
Source Program:
ASCZ:
LD
CP
JR
ADD
ADD
LD
HALT
A,(40H)
10
C,ASCZ
A.'A'-'9'-1
A.'O'
(4IHl,A
:GET DATA
:IS DATA 10 OR MORE?
:YES,ADD OFFSET FOR LETTERS
:ADD OFFSET FOR ASCII
:STORE ASCII RESULT
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
0008
OOOC
DODD
OOOE
3A
40
00
FE
OA
38
02
C6
07
C6
30
32
41
00
76
Instruction
(Mnemonic)
ASCZ:
LD
A,(40H)
CP
10
JR
C,ASCZ
ADD
A.'A'-'9'-1
ADD
A.'O'
LD
(41Hl,A
HALT
7-2
In this program. the basIc Idea IS to add ASCII 0 to all the hexadecimal digits. This addition converts the decimal digits correctly; however. there IS a break between ASCII 9
(39 hex) and ASCII A (41 hex) which must be considered. This break must be added to
the nondecimal digits A, 8. C. D. E, and F This is accomplished by the ADD A instruction which adds the offset 'A'-'9'-1 to the contents of the Accumulator. Can you explain
why the offset IS .A' -'9'-17
Note that the addition terms are placed in the assembly language program in ASCII
form (apostrophes surround an ASCII character or string of characters). The offset for
the letters is left as an arithmetic expression. The effort IS to make the purpose of the
terms as clear as possible in the assembly language listing. The extra assembly time IS a
very small price to pay for a large Increase in clarity.
This routine could be used In a variety of programs; for example, monitor programs
must convert hexadecimal digits to ASCII In order to display the contents of memory
locations in hexadecimal on an ASCII printer or video display.
Another (quicker! conversion method that requires no conditional lumps at all is the
following program, described by Allison In Computer magazine. 1
LD
ADD
DAA
ADC
DAA
LD
HALT
A,(40HI
A,90H
AAOH
(4IHI,A
Try this program on some digits. Can you explain why It works?
Decimal to Seven-Segment
Purpose:
Seven-segment table; The following table can be used to convert decimal numbers to
seven-segment code. The seven-segment code is organized With the most significant
bit always zero followed by the code (1 = on, 0 = offl for segments g, f, e, d, c, b. and a
(see Figure 7-1).
Digit
Code
0
1
2
3
4
3F
06
5
6
7
8
9
58
4F
66
6D
7D
07
7F
6F
7-3
Note that the table uses 70 for 6 rather than the alternative 7C (top bar off) to avoid
confusion with lower case b. and 6F for 9 rather than 67 (bottom bar off), for no particular reason.
Sample Problems:
(0040)
a.
Result:
b.
Result:
03
(0042)
4F
(0040)
28
(0042)
00
Flowchart:
Data =(40)
Ves
Result
Result =0
(SSEG + Datal
(421
= Result
Note that the addition of base address SSEG and Index (DATA) produces the address
that contains the answer.
7-4
Source Program:
LD
LD
CP
JR
DONE.
SSEG:
LD
LD
LD
ADD
LD
LD
LD
HALT
ORG
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
B.O
A.(40H)
10
NC.DONE
L.A
H.O
DE.SSEG
HL.DE
B.(HL)
A,B
(42Hl.A
20H
3FH
06H
5BH
4FH
66H
6DH
7DH
07H
7FH
6FH
7-5
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
0000
OOOE
OOOF
0010
0011
0012
0013
0014
0015
06
00
3A
40
00
FE
OA
30
08
6F
26
00
11
20
00
19
46
78
32
42
00
76
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
3F
06
5B
4F
66
60
70
07
7F
6F
Instruction
(Mnemonic!
DONE:
LD
B,O
LD
A,(40H)
CP
10
JR
NC,DONE
LD
LD
L,A
H,O
LD
DE,SSEG
ADD
LD
LD
LD
HL,DE
B,(HU
A.B
(42Hl.A
HALT
SSEG:
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
3FH
06H
5BH
4FH
66H
6DH
7DH
07H
7FH
6FH
The program calculates the memory address of the desired code by adding the Index
(i.e.. the digit to be displayed) to the base address of the seven-segment code table.
This procedure IS known as a table lookup.
The assembly language pseudo-opera lion DEFB (Define Byte) places constant data into
program memory. Such data may Include tables, headings, error messages, priming
messages. format characters, thresholds, etc. The label attached to a DEFB pseudooperation IS assigned the value of the address Into which the byte of data is placed.
Tables are often used to perform code conversions that are more complex than the previous example. Such tables typically contain all the results organized according to the
input data, e.g.. the first entry IS the code corresponding to the number zero.
Seven-segment displays provide recognizable forms of the decimal digits and a few letters and other characters. Calculator-type seven-segment displays are inexpensive,
easy to combine, and use little power. However. the seven-segment coded digits are
somewhat difficult to read.
The assembler simply places the data for the table into memory. Note that one DEFB
pseudo-operation fills one byte of memory. We have left some memory space between
the program and the table to allow for later additions or corrections.
7-6
USE OF zao
INDEX
REGISTERS
1)
The fixed offset In program memory IS only eight bits long and
so cannot hold a complete memory address. It must be used either as a short displacement or to hold the eight least Significant bits of a memory address.
2)
The Index registers are 16 bits long. Either IX or IY can be loaded from memory Just
like a register pair - from two consecutive memory addresses with the least significant eight bits at the lower address.
3)
All operations involVing the Index registers take extra time and memorv because
one word of the operation code Simply declares that an Index register is to be used.
Source Program:
LD
LD
CP
JR
LD
LD
LD
LD
DONE.
LD
LD
HALT
B,O
A,(40H)
10
NC,DONE
HL,41 H
(HLI,O
IX,(40H)
B,(IX+SSEG)
A,B
(42Hl.A
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
0000
OOOE
OOOF
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
06
00
3A
40
00
FE
OA
30
OC
21
41
00
36
00
DO
2A
40
00
DO
46
20
78
32
42
00
76
Instruction
(Mnemonic)
DONE.
LD
B.O
LD
A.(40H)
CP
10
JR
NC.DONE
LD
HL,41 H
LD
(HLI,O
LD
IX,(40H)
LD
B,(IX+SSEG)
LD
LD
A.B
(42Hl.A
HALT
7-7
The indexed load mstruction LD B.OX + SSEG) adds the index (j.e.. the digit to be displayed) to the base of the seven-segment table to get the address of the desired code.
Note that the 16-bit index register contains the data as its eight least significant bits
and the most significant bits of the starting address of the table as Its eight most significant bits. This odd arrangement is necessary because the offset Included with the mdexed instruction is only eight bits long and can therefore hold only the eight least significant bits of the starting address of the table.
A more general program would allow the table to be placed anywhere in memory. If the
table startmg address is SSEGM (eight MSBs) and SSEGL (eight LSBs). the instruction
LD (HU.O must be replaced by LD (HU.SSEGM. Why is thiS change necessary?
Note that all operations involvmg Index Register IX have a 2-word operation code in
which the first word is DD.
Clearly thiS IS not a very efficient use of the mdex registers. These
MOVING DATA
WITHIN
registers really become useful when you must access several data
in a block. The block might contain the characteristics of a
A BLOCK
message. the parameters of an equation. the current state of a process or machine. or the data for a video display. You could, for example. take the COntents of the twelfth location m the block and move them to the twentieth location With
either of the followmg programs. assuming that the starting address of the block IS
stored in memory locations PTR and PTR+l.
1)
2)
DE.(PTR)
HL.12
HL.DE
A,(HU
HL.20
HL,DE
(HU.A
IX.(PTR)
A.OX+12)
OX+201.A
Usmg IX.
LD
LD
LD
The program using the mdex registers IS far shorter and clearer. Its only limitation is
that the offsets must be small enough to fit into an 8-blt byte.
ASCII to Decimal
Purpose: Convert the contents of memory location 0040 from an ASCII character to a
decimal digit and store the result in memory location 0041. If the contents of
memory location 0040 are not the ASCII representation of a decimal digit.
set the contents of memory location 0041 to FF (hex).
Sample Problems:
a.
(0040)
37
Result:
(0041)
07
(0040)
55
Result:
(0041)
FF
b.
'7'
7-8
Flowchart:
Data =(401
Yes
Yes
Result
=
Result = FF (Hexl
Data - ASCII 0
(411 = Result
Source Program:
LD
LD
SUB
JR
DONE:
B.OFFH
A, (40H)
'0'
C.DONE
CP
'9'+1
JR
NC.DONE
B.A
A.B
(41H).A
LD
LD
LD
HALT
7-9
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
OOOD
OOOE
OOOF
0010
0011
0012
06
FF
3A
40
00
D6
30
38
05
FE
3A
30
01
47
78
32
41
00
76
Instruction
(Mnemonic)
DONE:
LD
B,OFFH
LD
A,(40H)
SUB
'0'
JR
C,DONE
CP
'9'+1
JR
NC,DONE
LD
LD
LD
B,A
A.B
(41H),A
HALT
This program handles ASCII-coded characters lust like ordinary numbers. Note that the
decimal digits and the letters form groups of consecutive codes. Strings of letters (like
names) can be alphabetized by placing their ASCII representations In increasing
numerical order (ASCII B = ASCII A + 1 for example),
Subtracting ASCII zero (30 hex) from any ASCII decimal digit gives the BCD representation of that digit
ASCII to decimal conversion IS necessary when decimal numbers are being entered
from an ASCII device like a teletypewnter or video terminal.
The basIC idea of the program IS to determine if the character is between ASCII 0 and
ASCII 9, inclusive. If the character IS, It'S an ASCII decimal digit. since the digits form a
sequence. It may then be converted to decimal simply by subtracting hex 30 (ASCII 0),
e.g.. ASCII 7 - ASCII 0 = 37-30 = 7
Note that one comparison IS done with an actual subtraction (SUB '0') since the subraction is necessary to convert ASCII to decimaL The other companson is done with an Implied subtraction (CP '9'+1) since the final result IS now in the Accumulator if the onginal number was valid.
BCD to Binary
Purpose: Convert two BCD digits in memory locations 0040 and 0041 to a binary
number in memory location 0042. The most significant BCD digit is in
memory location 0040.
Sample Problems:
a.
(0040)
(0041)
Resu It
b.
Result:
02
09
= 29
(0042)
1D (hex)
(0040)
(0041)
07
01
(0042)
47 (hex) = 71 (deCimal)
(declmaJ)
7-10
Note: No flowchart is Included since the program multiplies the most significant digit
by 10 simply by using the formula lOx = 8x + 2x. Multiplying by 2 requires one
arithmetic left shift and multiplying by 8 requires three such shifts.
Source Program:
LD
LD
ADD
LD
ADD
ADD
ADD
INC
ADD
INC
LD
HALT
HLAOH
A(HU
A.A
B.A
AA
A.A
A.B
HL
A(HU
HL
(HU.A
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
OOOD
21
40
00
7E
87
47
87
87
80
23
86
23
77
76
Instruction
(Mnemonic)
LD
Hl,40H
LD
ADD
LD
ADD
ADD
ADD
INC
ADD
INC
LD
HALT
A(HL)
A.A
B.A
A.A
A.A
A.B
HL
A.(HL)
HL
(HU.A
BCD entries are converted to binary in order to save on storage and to simplify calculations. However. the conversion may offset some of the advantages of binary storage
and arithmetic.
This program multiplies the BCD digit in memory locatIOn 0040 by ten uSing repeated
additions 2 Note that ADD A.A multiplies the contents of the Accumulator by 2. This
allows you to multiply the contents of the Accumulator by small decimal numbers In a
few instructions. How would you use this procedure to multiply by 167 by 12? by 7?
BCD numbers reqUire about 20% more storage than do binary numbers. Representing 0
to 999 reqUires 12 bits In BCD form but only 10 bits In binary (since
2 10 = 1024::::: 1000).
7-11
Sample Problem:
Result:
(0041)
02
(0042)
(0043)
(0044)
(0045)
(0046)
(0047)
(0048)
(0049)
31
31
30
31
30
30
31
30
= 11010010
'1'
'1'
'0'
'1'
'0'
'0'
'1'
'0'
Flowchart:
(Pointer)
Counter
=
=
=
POinter
POinter H
Pointer
Data
(Pointer! =
41
8
ASCII 0
No
(Pointerl
Counter
= Counter -1
ASCII 1. i.e..
(Pointed + 1
Source Program:
CONV:
COUNT
LD
LD
LD
LD
INC
LD
RLA
JR
INC
DJNZ
HALT
HL.41 H
A.(HU
B,8
CO'
HL
(HU.C
NC.COUNT
(HU
CONV
:GET DATA
:COUNTER = NUMBER OF BITS IN WORD
:GET ASCII ZERO TO STORE IN STRING
:PUT ASCII ZERO IN STRING
:IS NEXT 81T OF DATA 1?
:YES. MAKE STRING ELEMENT ASCII ONE
7-12
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
OOOD
OOOE
OOOF
0010
21
41
00
7E
06
08
OE
30
23
71
17
30
01
34
10
F8
76
Instruction
(Mnemonic)
'CONV:
COUNT
LD
HL,41 H
LD
LD
A.(HU
B.8
LD
CO'
INC
LD
RLA
JR
HL
(HU.C
INC
DJNZ
(HL)
CONY
NC.COUNT
HALT
The ASCII digits form a sequence so ASCII 1 = ASCII 0+ 1. Remember that the Z80
registers have special uses. You should place the loop counter into Register B so that
you can use the DJNZ instruction.
Be careful of the difference between INC HL. which adds one to the 16-blt contents of
Register Pair HL. and INC (HL). which adds one to the 8-blt contents of the memory
location addressed by Register Pair HL.
Binary-to-ASCII conversion is necessary when numbers are printed in binary form on an
ASCII device.
The conversion to ASCII simply Involves adding ASCII 0 (hex 30).
PROBLEMS
1) ASCII to Hex
Purpose: Convert the contents of memory location 0040 to a hexadeCimal digit and
store the result in memory location 0041. Assume that memory location
0040 contains the ASCII representation of a hexadeCimal digit (7 bits with
MSB OJ.
Sample Problems:
a.
(0040)
Resu It:
b.
Result:
2)
43
(0041)
OC
(0040)
36
(0041)
06
'C'
'6'
Seven-Segment to Decimal
Purpose: Convert the contents of memory location OU40 from a seven-segment code
to a deCimal number In memory location 0041. If memory location 0040 does
not contain a valid seven-segment code. set memory location 0041 to FF
(hex). Use the seven-segment table given under the DeCimal to Seven-Segment example and try to match codes.
7-13
Sample Problems:
a.
Resu It:
b
Result:
3)
(0040)
4F
(0041)
03
(0040)
28
(0041)
FF
Decimal to ASCII
Purpose: Convert the contents of memory location 0040 from a decimal digit to an
ASCII character and store the resu It in memory location 0041. If the number
In memory location 0040 is not a deCimal digit. set the contents of memory
location 0041 to an ASCII blank character (20 hex!.
Sample Problems:
(0040)
07
(0041)
37
(0040)
55
(0041)
20
a.
Result:
b.
Result:
4)
'T
SP
Binary to BCD
Purpose: Convert the contents of memory location 0040 to two BCD digits in memory
locations 0041 and 0042 (most Significant digit In 0041). The number In
memory location 0040 IS unsigned and less than 100.
Sample Problems:
a.
(0040)
10
Result:
(0041)
(0042)
02
09
(0040)
47
Resu It:
(0041)
(0042)
07
01
b.
5)
(29 decimal)
(71 deCimal)
Purpose: Convert the eight ASCII characters in memory locations 0042 through 0049
to an B-bit binary number In memory location 0041 (the most Significant bit
IS in 0042). Clear memory location 0040 if all the ASCII characters are either
ASCII 1 or ASCII 0 and set it to FF otherwise.
Sample Problems:
(0042)
(0043)
(0044)
(0045)
(0046)
(0047)
(0048)
(0049)
31
31
30
31
30
30
31
30
(0041)
(0040)
02
00
37
(0040)
FF
a.
Result:
Result:
'1'
'1'
'0'
'1'
'0'
'0'
'1'
'0'
'7'
7-14
REFERENCES
1.
2.
7-15
Chapter 8
ARITHMETIC PROBLEMS
Most arithmetic in microprocessor applications consists of multiple-word binary or
decimal manipulations. A decimal correction (decimal adjust) or some other means for
performing decimal arithmetic is frequently the only arithmetic Instruction provided
besides basic addition and subtraction. You must Implement other arithmetic operations with sequences of instructions.
Multiple-precIsion binary arithmetic requires simple repetitions of the basIc single-word
Instructions. The Carry bit transfers Information between words. Add with Carry and
Subtract with Carry use the Information from the prevIous arithmetic operations. You
must be careful to clear the Carry before operating on the first words (obviously there IS
no carry Into or borrow from the least significant bits).
Decimal arithmetic is a common enough task for microprocessors that most have
special instructions for this purpose. These instructions may either perform decimal
operations directly or correct the results of binary operations to the proper decimal
form. Decimal arithmetic IS essential in such applications as point-of-sale terminals.
calculators. check processors. order entry systems. and banking terminals.
You can implement multiplication and divIsion as series of additions and subtracilons
respectively. much as they are done by hand. Double-word operations are necessary
since a multiplication produces a result twice as long as the operands. while a division
similarly contracts the length of the result. Multiplications and divisions are time-consuming when done In software because of the repeated arithmetiC and shift operations
that are necessary. Of course. multiplying or dividing by a power of 2 is simple because
such operalions can be implemented with an appropriate number of left or right
arithmetiC shifts.
EXAMPLES
Multiple-Precision Addition
Purpose: Add two multiple-word binary numbers. The length of the numbers (in bytes)
is In memory location 0040. the numbers themselves start (least significant
bits first) in memory locations 0041 and 0051. respectively. and the sum
replaces the number starting in memory location 0041.
8-1
Sample Problem:
Result:
that
(0040)
04
(0041)
(0042)
(0043)
(0044)
C3
A7
58
2F
(0051)
(0052)
(0053)
(0054)
88
35
DF
14
(0041)
(0042)
(0043)
(0044)
78
DD
3A
44
IS.
2F58A7C3
14DF3588
443ADD78
Flowchart:
CoUnt
Pointer 1
Pointer 2
Carry
(Pointer 1) =
(Pointer 1) t
(Pointer 2) +
ll-_-.;c;;arrv;.;._ _..1
Pointer 1= Pointer 1
+ 1
POInter 2 = Pointer 2
+ 1
Count
= Cnunt - 1
8-2
Source Program:
ADDW:
LD
LD
INC
LD
AND
LD
ADC
LD
INC
INC
DJNZ
HALT
HLAOH
B.(HU
HL
DE.51H
A
A.(DE)
A.(HU
(HU.A
DE
HL
ADDW
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
OOOD
OOOE
OOOF
0010
21
40
00
46
23
11
51
00
A7
lA
8E
77
13
23
10
F9
76
Instruction
(Mnemonic)
ADDW;
LD
HL.40H
LD
INC
LD
B.(HU
HL
DE.51H
AND
LD
ADC
LD
INC
INC
DJNZ
A
A.(DE)
A.(HL)
(HU.A
DE
HL
ADDW
HALT
09
+FO
f.'9
The Instruction AND A is used to clear the Carry bit. Any other logical operation would
have the same effect. The Carry must be cleared. since there is no carry Involved in the
addition of the least significant bytes.
The instruction ADC. Add with Carry. includes the Carry from the previous words In the
addition.ADC IS the only Instruction In the loop that affects the Carry Remember that
neither INC nor DJNZ does.
Both the pOinter In Register Pair DE and the one in HL must be updated during each
Iteration.
8-3
DECIMAL
ACCURACY
IN BINARY
."
10d'"
bitS) = 33 bits
(10 digits)
"x ( 3
Iglts
BLOCK
If we were only transferring the data from one place In memory
TRANSFER
to another and not also processing it. we could use the ZBO's
INSTRUCTIONS
powerfu I block transfer instruction LDIR. This single instruction
moves a byte of data from the address In HL to the addre"ss in
DE. increments the pOinters in HL and DE. and decrements the byte counter in Be. It
repeats the move operation until BC is decremented to zero. LDI is the same instruction
without the repetition factor: LDD and LDDR are non-repeated and repeated moves.
respectively. that decrement the pointers rather than incrementing them.
A program to transfer a fixed number of bytes (LENGTH) from one place in memory
(starting at PTR1) to another place in memory (starting at PTR2) IS the following.
Block Move
Purpose: Move a block of data BC characters long from the address
dress In DE.
In
HL to the ad-
Sample Problem:
Result:
(HL)
(DE)
(BC)
40
50
3
(0040)
(0041)
(0042)
31
32
33
(0050)
(0051)
(0052)
0
0
0
(0050)
(0051)
(0052)
31
32
33
Source Program:
LD
LD
LD
BC.LENGTH
HL.PTR1
DE.PTR2
LDIR
HALT
8-4
Object Program:
Memory Address
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
Memory Contents
(Hex)
Instruclion
(Mnemonic)
01
LD
BC.LENGTH
LENGTH
21
LD
HL.PTRl
PTRl
11
LD
DE.PTR2
PTR2
ED
BO
76
LDIR
HALT
Try to Implement the same program without the LDIR instruction. How many bytes of
memory and clock cycles does It require each way?
Decimal Addition
Purpose: Add two multiple-word decimal (BCD) numbers. The length of the numbers
is in memory location 0040. the numbers themselves start (least significant
bits first! In memory locations 0041 and 0051. respectively. and the sum
replaces the number starting in memory location 0041.
Sample Problem:
Result:
that is.
(0040)
04
(0041)
(0042)
(0043)
(0044)
85
19
70
36
(0051)
(0052)
(0053)
(0054)
59
34
66
12
(0041)
(0042)
(0043)
(0044)
44
54
36
49
36701985
+12663459
49365444
8-5
Flowchart:
Count
140)
Pointer 1
POInter 2
41
51
Carry
Pointer 1 =
(PoInter 11 +
(Pointer 2) +
Carry +
Decimal correction
Pointer 1
POinter 1 + 1
Pointer 2
Pointer 2 + 1
Count
Count - 1
Source Program:
DECAD
LD
LD
INC
LD
AND
LD
ADC
DAA
LD
INC
INC
DJNZ
HALT
HLAOH
B.(HL)
HL
DE.51H
A
A(DE)
A.(HL)
(HL).A
DE
HL
DECAD
8-6
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
OOOB
0009
OOOA
OOOB
OOOC
0000
OOOE
OOOF
0010
0011
21
40
00
46
23
11
51
00
A7
lA
SE
27
77
13
23
10
FS
76
Instruction
(Mnemonic)
DECAD:
LD
HLAOH
LD
INC
LD
B,(HL)
HL
DE.51H
AND
LD
ADC
DAA
LD
INC
INC
DJNZ
A
A,(DE)
A,(HL)
HALT
The Decimal Adjust instruction (DAA) uses the Carry (C) and Half
Carry (H) bits to correct the following situations:
1
(HL),A
DE
HL
DECAD
DECIMAL
ADJUST
(5)
+ 1000 (S)
1101 (D)
0110
0001 0011 (BCD 13, which IS correctl
2)
The sum of two digits IS 16 or more. In this case the result IS a proper BCD number
but SIX less than It should be, I.e.
1000 (S)
+ 1001 (9)
0001 0001 (BCD 11)
+ 0110
0001 0111 (BCD 17. which IS correctl
Six must be added in both situations. However, case 1 can be recognized by the fact
that the sum IS not a BCD digit. It IS between 10 and 15 (or A and F hexadecimal). Case
2 can be recognized only by the fact that the Carry (most Significant digltl or Half Carry
(least Significant digit) has been set to 1, since the result is a valid BCD number. DAA is
the only instruction that uses the Half Carry. Note that DAA operates only on the Accumulator.
zao
ADD/SUBTRACT
The
microprocessor also has a flag that distinguishes beFLAG
tween Add Instructions (ADD. ADC) and Subtract instructions
(SUB, SBC). This flag, called the Add/Subtract flag or N flag, IS
cleared by all Add instructions and set by all Subtract Instructions. The sole use of this
flag IS to allow the DAA instructIOn to correctly change binary addition Into BCD addition and binary subtraction into BCD subtraction. The SOSO and SOS5 microprocessors
do not have an N flag, and so their DAA Instructions operate properly only after addition.
8-7
DAA can be used only after instructions that place their result into the Accumulator
and that properly affect the Carry. Half-Carry. and Add/Subtract flags. Thus. you cannot
use DAA after INC (since INC does not affect the Carry!. DEC. or any of the double-word
instructions that place their results into the Index registers or Register Pair HL.
ThiS procedure can add deCimal (BCD) numbers of any length.
Here four binary bits are required for each decimal digit. so tendigit accuracy requires:
ACCURACY IN
BINARY AND
BCD
"OJ
10 x 4 = 40 bits
as opposed to 33 bits In the binary case. ThiS IS essentially fiye 8-bit words Instead of
four. The deCimal procedure also takes a little longer per word because of the extra
DAA instruction.
Result:
(0040)
(0041)
03
05
(0042)
(0043)
OF
00
or In deCimal 3 x 5 = 15
~
~04m
(0041)
Result:
6F
61
(0042)
OF
(0043)
2A
or 111 x 97 = 10.767
You can perform multiplication on a computer In the same way that you do long
multiplication by hand. Since the numbers are binary. the only problem IS whether to
multiply by 0 or 1; multiplying by zero obViously gives zero as a result. while multiplying
by one produces the same number that you started with (the multiplicand). So, each
step In a binary multiplication can be reduced to the following operation.
If the current bit in the multiplier IS 1. add the multiplicand
to the partial product.
-------.
MULTIPLICATION
ALGORITHM
The only remaining problem IS to ensure that you line everything up correctly each
lime. The following operations perform thiS task.
1)
2)
Shift multiplier left one bit so that the bit to be examined is placed Into the Carry.
Shift product left one bit so that the next addition is lined up correctly.
8-8
0000
61
6F
08
0000
C2
6F
07
0
006F
84
6F
06
1
0140
08
6F
05
1
029A
10
6F
04
0
0534
20
6F
03
0
OA68
40
6F
02
0
8-9
1400
80
6F
01
2AOF
00
6F
00
1
Flowchart:
Multiplicand
Multiplier
Product
Count
(40l
(41l
0
8
Product = 2 x Product
Product =
Product +
Multiplicand
Count == Count - 1
No
8-10
Source Program:
MULT
CHCNT
LD
LD
LD
INC
LD
LD
LD
ADD
RLA
JR
ADD
DJNZ
LD
HALT
HLAOH
E.(HL)
0.0
HL
A.(HL)
HL.O
B.8
HL.HL
:GET MULTIPLICAND
:EXTEND TO 16 BITS
NC.CHCNT
HL.DE
MULT
(42HLHL
:GET MULTIPLIER
:PRODUCT = ZERO
:COUNT = BIT LENGTH OF MULTIPLIER
:SHIFT PRODUCT LEFT 1 BIT
;SHIFT MULTIPLIER LEFT 1 BIT
:IS CARRY FROM MULTIPLIER 17
:YES. ADD MULTIPLICAND TO PRODUCT
;SAVE PRODUCT IN MEMORY
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
0000
OOOE
OOOF
0010
0011
0012
0013
0014
0015
0016
0017
21
40
00
5E
16
00
23
7E
21
00
00
06
08
29
17
30
01
19
10
F9
22
42
00
76
Instruction
(Mnemonic)
MULT
CHCNT
LD
HL.40H
LD
LD
E.(HL)
0.0
INC
LD
LD
HL
A.(HL)
HL.O
LD
B.8
ADD
RLA
JR
HL.HL
ADD
DJNZ
HL.DE
MULT
LD
(42HI.HL
NC.CHCNT
HALT
Note that the multiplicand must be extended to 16 bits by clearing Register 0 so that It
can be added to the product using the ADD HL.DE instruction.
The Instruction ADD HUHL acts as a 16-blt logical left shift for the 16-blt product.
In this program. the Z80 16-blt instructions handle data rather than addresses. LD HL.O
is used to initialize the product; ADD HL.HL to perform a 16-bit logical left shift: ADD
HL.DE to add the multiplicand to the partial product; and LD (42HI.HL to store the
result in memory. You must be careful to extend 8-blt quantities (like the multiplicand
In this example) to 16 bits. Note that you cannot use the 16-blt facililles simultaneously
for addreSSing and data manipulation. However. if you have no other'need for the alternate registers. you could save the old contents of the regular registers there and restore
8-11
them afterward using the EXX instruction. This instruction exchanges the contents of
Register Pairs BC. DE. and HL with the contents of their alternate counterparts in just
four clock cycles.
Besides its obvious use in calculators and point-of-sale terminals. multiplication is a key
part of almost all signal processing and control algorithms. The speed at which
multiplications can be performed determines the usefulness of a CPU in process control. signal detection. and signal analysis.
The algonthm takes between 390 and 440 clock cycles to multiply on a Z80
microprocessor. The precise time depends on the number of one bits in the mUltiplier.
Other algorithms may be able to reduce the average execution time somewhat but 400
clock cycles will still be a typical execution time for a software muItiplication 2
a.
b.
(0040)
(0041)
(0042)
40 (64 decimal)
00
08
Result
(0043) = 08
(0044) =00
I.e.. 64/8 = 8
(0040)
(0041)
(0042)
6D (12.909 decimal!
32
47 (71 decimal!
Result
You can perform division on the computer lust like you would perform diVISion with pen and paper. i.e.. using trial subtractions.
Since the numbers are binary. the only question IS whether the bit
In the quotient IS 0 or 1. I.e.. whether or not the divisor can be subtracted from what is
left of the dividend. Each step in a binary division can be reduced to the following
operation:
If the diVisor can be SUbtracted from the eight
most Significant bits of the diVidend Without
a borrow. the corresponding bit in the quotient IS 1. otherwise it IS O.
The only remaining problem is to line up the diVidend and quotient properly. You can
do this by shifting the dividend and quotient logically left one bit before each tnal
subtraction. The diVidend and quotient can share a 16-bit register. since the procedure
clears one bit of the diVidend at the same time as It determines one bit of the quotient.
8-12
3260
47
00
00
3068
47
05
05
Dividend
Divisor
Quotient
Counter
1900
47
OB
04
Dividend
Divisor
Quotient
Counter
33AO
47
16
03
8-13
2040
47
2D
02
Dividend
Divisor
Quotient
Counter
4080
47
5A
01
Dividend
Divisor
Quotient
Counter
3AOO
47
B5
00
In
8-14
Flowchart:
Dividend
Divisor
Count
8 MSBs of
Dividend = 8 MSBs
of Dividend - Divisor
Quotient =QuQtient + 1
Count
= Count - 1
No
(43l = Quotient
(44l = 8 MSBs of
Dividend
Source Program:
DIV:
CNT
LD
LD
LD
LD
ADD
LD
SUB
JR
LD
INC
DJNZ
LD
HALT
HL.(40Hl
A,(42Hl
C.A
B.8
HL.HL
A.H
;GET DIVIDEND
:GET DIVISOR
;COUNT = NUMBER OF BITS IN DIVISOR
;SHIFT DIVIDEND. QUOTIENT LEFT 1 BIT
;CAN DIVISOR BE SUBTRACTED?
C
C.CNT
H,A
L
DIV
(43Hl.HL
8-15
~~---~._---
Object Program:
Memorv Address
(Hex)
Memorv Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
0000
OOOE
OOOF
0010
0011
0012
0013
0014
0015
2A
40
00
3A
42
00
4F
06
08
29
7C
91
38
02
67
2C
10
F7
22
43
00
76
Instruction
(Mnemonic)
DIV:
CNT:
LD
HL.(40H)
LD
A,(42H)
LD
LD
C,A
B,8
ADD
LD
SUB
JR
HL,HL
A,H
C
C,CNT
LD
INC
DJNZ
H,A
L
DIV
LD
(43Hl,HL
HALT
Register Pair HL holds both the dividend and the quotient. The quotient simply replaces
the dividend in Register L as the dividend is shifted left logically.
For longer division problems, you could use the instruction SBC HL which subtracts the
contents of a register pair and the contents of the Carry from the contents of Register
Pair HL.
The instruction INC L sets the least significant bit of the quotient to 1. since ADD HL,HL
has previously cleared that bit.
Division is necessary In calculators, terminals, communications error checking, control
algorithms, and many other applications.
This algorithm takes between 400 and 430 clock cycles to divide on a Z80
microprocessor. The precise time depends on the number of one bits in the quotient.
Other algorithms may reduce the average execution time somewhat. but 400 clock cycles will still be typical for a software divIsion. Some of the references listed at the end
of this chapter discuss faster methods for implementing division.
8-16
Self-Checking Numbers
Double Add Double Mod 10
Purpose: Calculate a checksum digit from a string of BCD digits. The length of the
string of digits (number of words) is in memory location 0041, the string of
digits (2 BCD digits to a word) starts in memory location 0042. Calculate the
checksum digit by the Double Add Double Mod 10 technique 3 and store it in
memory location 0040.
SELF-CHECKING
NUMBERS
Clear the checksum to start.
Multiply the leading digit by two and add the result to the
checksum.
Add the next digit to the checksum.
Continue the alternating process until you have used all the digits.
The least significant digit of the checksum IS the self-checking digit.
Self-checking digits are commonlv added to Identification numbers on credit cards. inventory tags, luggage, parcels. etc.. when they are handled by computerized systems.
They may also be used in routing messages. identifying files, and other applications.
The purpose of the digits is to minimize entry errors such as transposing digits (69 instead of 961. shifting digits (7260 Instead of 37261. missing digits by one (65 instead of
641. etc. You can check the self-checking number automatically for correctness upon
entry and can eliminate many errors immediately.
The analysis of self-checking methods is qUite complex. For example, a plain checksum
will not find transposition errors (4 + 9 = 9 + 4). The Double Add Double algOrithm will
find Simple transpOSition errors (2 x 4 + 9 = 17 '" 2 x 9 + 4); but will miss some errors,
such as transpositions across even numbers of digits (367 Instead of 763). However.
thiS method will find many common errors! The value of a method depends on what errors it will detect and on the probability of particular errors in an application.
For example, if the string of digits IS
549321
the result will be:
Checksu m
Se!f-checklng digit
5 x 2 + 4 + 9 x 2 + 3 + 2 x 2 + 1 = 40
0 (least significant digit of a checksum)
Note that an erroneous entry like 543921 would produce a different self-checking digit
(41. but erroneous entries like 049321 or 945321 would not be detected.
Sample Problems:
a.
(0041)
(0042)
(0043)
(0044)
03
36
68
51
Result: Checksum = 3 x 2 + 6 + 6 x 2
(0040)
03
b.
(0041)
(0042)
(0043)
(0044)
(0045)
+8 +5 x 2 + 1 =
43
04
50
29
16
B3
Result: Checksum = 5 x 2 + 0 + 2 x 2
(0040) = 00
8-17
50
Flowchart:
Checksum
Count
Pointer
POinter
0
(41)
42
POinter + 1
Count = Count - 1
(40) = Checksum
AND 000011116
8-18
Source Program:
CHDIG.
LD
LD
LD
LD
LD
LD
RRA
RRA
RRA
RRA
AND
ADD
DAA
ADD
DAA
LD
LD
AND
ADD
DAA
LD
INC
DJNZ
AND
LD
HALT
A, (41 H)
B.A
C.O
HL,42H
A.(HU
D.A
00001111B
A.A
A.C
C,A
A.D
00001111B
A.C
C.A
HL
CHDIG
00001111B
(40Hl.A
;DOUBLE MSD
;MAKE DOUBLED MSD DECIMAL
;ADD DOUBLED MSD TO CHECKSUM
;KEEP CHECKSUM DECIMAL
;GET LEAST SIGNIFICANT DIGIT
;(MASK OUT MSD)
;ADD LSD TO CHECKSUM
;KEEP CHECKSUM DECIMAL
8-19
Object Program'
Memory Address
(Hex)
Memorv Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
0008
OOOC
0000
OOOE
OOOF
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
001A
0018
001C
0010
001E
001F
0020
0021
0022
0023
0024
3A
41
00
47
OE
00
21
42
00
7E
57
1F
1F
1F
1F
E6
OF
87
27
81
27
4F
7A
E6
OF
81
27
4F
23
10
EA
E6
OF
32
40
00
76
Instruction
(Mnemonic)
CHDIG:
LD
A.(41H)
LD
LD
8.A
C.O
LD
HL.42H
LD
LD
RRA
RRA
RRA
RRA
AND
A(HU
D.A
ADD
DAA
ADD
DAA
LD
LD
AND
000011118
A.A
AC
C.A
A.D
000011118
ADD
DAA
A.C
LD
C.A
INC
DJNZ
HL
CHDIG
AND
000011118
LD
(40H),A
HALT
The digits are removed by shifting and masking. Four right shifts are needed to separate
out the most significant digit.
A decimal adjust (DAA) must follow each addition to produce the proper decimal result.
A single DAA after a series of additions will not work (try ItO. Remember that DAA
works only on the Accumulator.
There IS no problem with carries from the deCimal sum. since the procedure uses only
the least Significant digit of the checksum anyway.
8-20
DECIMAL SHIFT
An alternative (and superior) approach IS to use the Z80
decimal shift instruction RLD. This instruction IS a 4-blt
INSTRUCTIONS
shift that moves the contents of the four least significant
bits of the memory location addressed by HL Into the four most significant bits of that
location. the previous contents of the four most significant bits of that location Into the
four least significant bits of the Accumulator. and the previous contents of the four
least significant bits of the Accumulator Into the four least significant bits of the memory location. Thus. RLD not only moves a single digit to the Accumulator. but it also
shifts the next digit so that it can be moved to the Accumulator with the next RLD.
Figure 8-1 shows an example of how RLD works; RRD is the same instruction except
that the shift is right instead of left.
The Double Add Double Mod 10 algorithm can be implemented as follows uSing RLD:
Source Program:
CHDIG;
LD
LD
LD
LD
SUB
RLD
ADD
DAA
ADD
DAA
LD
SUB
RLD
ADD
DAA
LD
INC
DJNZ
AND
LD
HALT
A.(41Hl
B.A
C.O
HL,42H
A
A.A
A.C
C.A
A
A.C
C.A
HL
CHDIG
00001111B
(40H),A
8-21
Object Program:
Memorv Address
(Hex)
Memorv Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB.
OOOC
OOOD
OOOE
OOOF
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
001A
001B
001C
0010
001E
001F
3A
41
00
47
OE
00
21
42
00
97
ED
6F
87
27
81
27
4F
97
ED
6F
81
27
4F
23
10
EF
E6
OF
32
40
00
76
Instruction
(Mnemonic)
CHDIG:
LD
A.(41H)
LD
LD
B.A
C.O
LD
HL42H
SUB
RLD
ADD
DAA
ADD
DAA
LD
A.A
C.A
SUB
A.C
RLD
A,C
ADD
DAA
LD
INC
DJNZ
C.A
HL
CHDIG
AND
00001111B
LD
(40H),A
HALT
We could Improve this program even further (it is already shorter than the prevIous version), Since we are dropping the most significant digit at the end anyway. there is no
reason to clear it out each time with the SUB A instruction,
8-22
Initial Conditions
{HLI =4000
{AI =7F
{4oool = 12
After ALD
{AI =71
{40001 =2F
Before
After
Accumulator
Memorv
Rit2
,.. . . . "
fF< [E]
~/I
',
High-order bit = 0,
~::~:
Memory
Accumuiator
I I
: ..
: .." 1-
Non-zero result,
set2toO
After AAD
{AI =72
{4oool = F
Before
After
Memory
Accumulator
Accumulator
- -
Memorv
rn
ADD
DAA
;DOU8LE NUM8ER
;AND MAKE RESULT DECIMAL
DOUBLING
AND HALVING
BINARY
NUMBERS
Remember that the Accumulator can hold only valid decimal digits in the range 0~99.
You cannot use SLA A (Shift Left ArithmetiC AI because that instruction always clears
the Half-Carry (only Add and Subtract instructions set H properly).
You can divide a decimal number by two simply by shifling it right logically and then
subtracting three from any digit that is eight or larger (since 10 BCD IS 16 binary). The
following program divides a decimal number In memory location 0040 by two and
places the result Into memory location 0041.
8-23
DONE:
LD
SRL
BIT
JR
SUB
LD
HALT
A. (40Hl
A
3,A
Z.DONE
3
(41H),A
Try this program and the method on the decimal numbers 28, 30, and 37 Do you understand why It works?
Rounding is simple whether the numbers are binary or decimal. A
binary number can be rounded as follows:
If the most significant bit to be dropped IS 1,
add 1 to the remaining bits. Otherwise. leave
the remaining bits alone.
This rule works because 1 is halfway between 0 and 10 in binary, much as 5 IS halfway
in decimal (note that 0.5 decimal = 0.1 binary).
So, the following program will round a 16-blt number in memory locations 0040 and
0041 (MSBs in 0041) to an 8-bit number in memory location 0041.
DONE,
LD
BIT
JR
INC
INC
HALT
HLAOH
7,(HU
Z.DONE
HL
(HU
If the number IS longer than 16 bits. the rounding must npple through the other bytes
as needed.
Decimal rounding IS a bit more difficult because the crossover
POint IS now BCD 50 and the rounding must produce a decimal
result. The rule IS:
If the most Significant digit IS to be dropped
IS 5 or more, add 1 to the remaining digits.
The following program will round a 4-digit BCD number in memory locatiOns 0040 and
0041 (MSBs in 0041) to a 2-diglt BCD number in memory locatiOn 0041.
DONE,
LD
LD
CP
JR
INC
LD
ADD
DAA
LD
HALT
HL.40H
A.(HU
50H
C,DONE
HL
A.(HU
A.1
Remember that the DAA instruction works only on numbers in the Accumulator. In this
case, we could round with the instruction INC A. since we know that the Carry IS zero
(Why? - remember the JR instruction). Normally, we need the sequence ADD A,l
followed by DAA. since INC A does not affect the Carry.
8-24
SIGN
Very often when performing multibyte twos complement
signed arithmetic. it is necessary to propagate the sign bit
PROPAGATION
through the high-order bytes. This operation can be performed
in a straightforward manner if. as is usually the case. the sign is in the Carry. The SSC
A.A Instruction has the effect of propagating the state of the Carry throughout a word.
Since A-A always equals O. SSC A.A IS equivalent to subtracting the Carry from 0 and
can Yield only the values 0 and FFH.
PROBLEMS
1) Multiple-Precision Subtraction
Purpose: Subtract one multiple-word number from another. The length of the numbers is In memory location 0040. the numbers themselves start (least significant bits first) in memory locations 0041 and 0051. respectively. and the
difference replaces the number starting in memory location 0041. Subtract
the number starting in 0051 from the one starting In 0041.
Sample Problem:
Result:
that
2)
(0040)
04
(0041)
(0042)
(00431
(00441
C3
A7
58
2F
(0051)
(00521
(0053)
(0054)
88
35
DF
14
(0041)
(0042)
(0043)
(0044)
08
72
7C
1A
2F5SA7C3
14DF35S8
1A7C7208
IS.
Decimal Subtraction
Purpose: Subtract one multiple-word decimal (SCDI number from another. The length
of the numbers IS in memory location 0040. the numbers themselves start
(least significant bits first) in memory locations 0041 and 0051. respectively.
and the difference replaces the number starting in memory location 0041.
Subtract the number starting In 0051 from the one starting In 0041.
Sample Problem:
(0040)
04
(0041)
(0042)
(0043)
(00441
85
19
70
36
(0051)
(0052)
(0053)
(0054)
59
34
66
12
8-25
Result:
(0041)
(0042)
(0043)
(0044)
26
85
03
24
36701985
12663459
that IS,
24038526
3)
Purpose: Mu Itlply the 16-blt unsIgned number in memory locations 0040 and 0041
(most significant bits In 0041) by the 8-blt unsigned number In memory location 0042. Store the result In memory locations 0043 through 0045, with the
most Significant bits In memory location 0045.
Sample Problems:
a.
Result:
(0040)
(0041)
(0042)
03
00
05
(0043)
(0044)
(0045)
OF
00
00
3 x 5 = 15
that IS,
b.
Result:
(0040)
(0041)
(0042)
6F
72 (29,295 decimal!
61 (97 decimal!
(0043)
(0044)
(0045)
OF
29.295 x 97
that IS,
4)
5C
28
2,841.615
Purpose: Divide the 16-blt signed number in memory locations 0040 and 0041 (most
Significant bits in 0041 by the 8-blt signed number In memory location 0042.
The numbers are normalized so that the magnitude of memory location 0042
IS greater than the magnitude of memory location 0041. Store the quotient
(Signed) In memory location 0043 and the remainder (always positive) In
memory location 0044.
Sample Problems:
a.
Result:
Result:
co
(0040)
(0041)
(0042)
FF (-64)
08
(0043)
(0044)
F8 (-8) quotient
00 (0) remainder
(0040)
(0041)
(0042)
ED (-4717)
47 (71 deCimal!
(0043)
(0044)
SO (-67 deCimal!
28 (+40 deCimal!
93
Hint: Determine the sign of the result. perform an unsigned diVision, and adlust the quotient and remainder properly.
8-26
5)
Purpose: Calculate a checksum digit from a string of BCD digits. The length of the
string of digits (number of words) IS In memory location 0041. the string of
digits (2 BCD digits to a word) starts in memory location 0042. Calculate the
checksum digit by the Aligned 1. 3. 7 Mod 10 method and store It In memory
location 0040.
The Aligned 1. 3. 7 Mod 10 technique works as follows:
1)
2)
3)
Multiply the next digit by 3 and add the result to the checksum.
41
5)
6)
Multiply the next digit by 7 and add the result to the checksum.
Continue the process (Steps 2-4) until you have used all the digits.
The self-checking digit IS the least significant digit of the checksum.
5 + 3 x 4 + 7 x 9 + 3 + 3 x 2 + 7 x 1 = 96
Sample Problems:
a.
(0041)
(0042)
(0043)
(0044)
03
36
68
51
Result: Checksum = 3 + 3 x 6 + 7 x 6 + 8 + 3 x 5 + 7 x 1 = 93
03
(0040)
b.
(0041)
(0042)
(0043)
(0044)
(0045)
04
50
29
16
83
Result: Checksum = 5 + 3 x 0 + 7 x 2 + 9 + 3 x 1 + 7 x 6 + 8
+3x3=90
(0040) = 00
8-27
REFERENCES
1.
2.
Some microprocessors (such as the 9900. 8086. and Z-8000) have hardware
multiplication Instructions that are somewhat faster. but maximum speed requires
the addition of external hardware.
Other methods for implementing multiplication. divIsion. and other arithmetic tasks
are discussed In:
Geist. D. J.. "MOS Processor Picks up Speed with Bipolar Multipliers," Electronics.
Ju Iy 7. 1977, pp. 113-115.
Kolodzlnski. A. and D. Wainland. "MultiplYing with a Microcomputer," Electronic
Design. January 18. 1978. pp. 78-83.
Mick. J. R. and J. Springer. "Single-chip MUltiplier Expands Digital Role in Signal
Processing," Electronics. May 13. 1976. pp. 103-108.
Parasuraman. B.. "Hardware Multiplication Techniques for Microprocessor
Systems." Computer Design. April 1977, pp. 75-82.
Tao. 1. F. et al.. "Applications of Microprocessors in Control Problems," 1977 Joint
Automatic Control Conference Proceedings. San Francisco. CA" June 22-24. 1977
Waser. S.. "State-of-the-art in High-Speed Arithmetic Integrated Circuits," Computer Design. July 1978. pp. 67-75.
-Weissberger. A. J. and T. Toal. "Tough Mathematical Tasks Are Child's Play for
Number Cruncher," Electronics. February 17, 1977. pp. 102-107
3.
8-28
Chapter 9
TABLES AND LISTS
Tables and lists are two of the basic data structures used with all computers. We have
already seen tables used to perform code conversions and anthmetlc. Tables may also
be used to identify or respond to commands and instructions. lineanze data. provide access to files or records. define the meaning of keys or sWitches. and choose among
alternate programs. lists are usually less structured than tables. lists may record tasks
that the processor must perform. messages or data that the processor must record. or
conditions that have changed or should be monitored. Tables are a simple way of making decisions or solving problems. since no computations or logical functions are
necessary. The task. then. reduces to organizing the table so that the proper entry IS
easy to find. lists allow the execution of sequences of tasks. the preparation of sets of
results. and the construction of interrelated data files (or data bases), Problems Include
how to add elements to a list and remove elements from it.
EXAMPLES
Add Entry to List
Purpose:
Add the contents of memory location 0040 to a list if It IS not already present in the list. The length of the list is In memory location 0041 and the list
Itself begins In memory location 0042.
Sample Problems:
Resu It:
~04m
68
(0041)
(0042)
(0043)
(0044)
(0045)
04
37
61
38
1D
(0041)
(0046)
05
68
The entry is added to the list. since it IS not already present. The length of the list IS increased by 1.
b.
(0040)
(0041)
(0042)
(0043)
(0044)
(0045)
Result: No change.
68
04
37
68
38
lD
since the entry IS already In the list.
9-1
Flowchart:
Entry
Count
(40)
(411
Pointer
42
Pointer
Count
= POinter
+ 1
= Count - 1
(Pointed
(411
=
~
Entry
(411 + 1
Source Program:
SRLST
DONE:
LD
LD
INC
LD
INC
CP
JR
INC
DJNZ
LD
LD
INC
HALT
HLAOH
A,(HU
HL
B,(HU
HL
(HU
Z.DONE
HL
SRLST
(HU,A
HLA1H
{HU
:POINT TO ENTRY
:GET ENTRY
:POINT TO COUNT
:COUNT = LENGTH OF LIST
:POINT TO START OF LIST
:IS ENTRY = ELEMENT IN LIST?
:YES. THROUGH
:NO. GO ON TO NEXT ELEMENT
:ADD ENTRY TO LIST
:ADD 1 TO LIST LENGTH
9-2
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
0000
OOOE
OOOF
0010
0011
0012
21
40
00
7E
23
46
23
BE
28
08
23
10
Instruction
(Mnemonic)
SRLST
LD
HLAOH
LD
INC
LD
INC
CP
JR
A.(HL)
HL
B.(HL)
HL
(HL)
Z.DONE
INC
DJNZ
HL
SRLST
LD
LD
(HL).A
HLA1H
INC
HALT
(HL)
FA
77
21
41
00
34
76
ADELM:
DONE.
In
Source Program:
DONE:
LD
LD
INC
LD
LD
INC
CPIR
JR
LD
LD
HALT
HL.40H
A.(HU
HL
B.O
C.(HL)
HL
Z.DONE
(HL).A
HLAl H
;POINT TO ENTRY
:GET ENTRY
;POINT TO COUNT
;COUNT = LENGTH OF LIST (16 BITS)
:POINT TO START OF LIST
:LOOK FOR ENTRY IN LIST
:DONE IF ENTRY FOUND
;OTHERWISE. ADD ENTRY TO LIST
:ADD 1 TO LIST LENGTH
9-3
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
0000
OOOE
OOOF
0010
0011
0012
40
00
7E
23
06
00
4E
23
ED
Bl
28
05
77
21
41
00
34
76
Instruction
(Mnemonic)
21
DONE:
LD
HLAOH
LD
INC
LD
A.(HL)
HL
B.O
LD
INC
CPIR
C.(HL)
HL
JR
Z.DONE
LD
LD
(HL).A
HL,41 H
INC
HALT
(HU
Remember that CPIR automatically repeats the basic Search instruction until either BC
is decremented to zero or a true comparison occurs (i.e.. A = (HUl.
Be careful of the following slight differences from the prevIous version:
1)
2)
BC is a 16-blt counter. Thus. CPIR can handle strings longer than 256 bytes.
The Panty/Overflow bit (P/0) IS cleared if BC is decremented to zero. and set otherwise.
9-4
The program does not work if the length of the list could be zero (what happens?). We
cou Id avoid this problem by checking the length Initially. The initialization procedure
for the first program would then be:
ADELM:
LD
LD
INC
LD
INC
INC
DEC
JR
HLAOH
A.(HL)
HL
8.(HL)
HL
8
8
Z.ADELM
:POINT TO ENTRY
:GET ENTRY
:POINT TO LENGTH
:COUNT = LENGTH OF LIST
:POINT TO START OF LIST
:IS COUNT ZERO?
LD
{HL),A
Note that the sequence INC. DEC IS an easy way to check for a zero value In a register
without using the Accumulator or changing the value in the register.
The procedure:
LD
INC
HL.ADDR
(HU
IS a quick way to add 1 to a counter in memory location ADDR without uSing the Accumulator. You can use DEC (HL) in a similar manner to subtract 1 from the counter. LD
(HL),CONST can place a starting value (such as zero) In the counter. Memory locations
should. of course. be used for counters only when no readily accessible registers are
available.
If each entry were longer than one word. a pattern-matching program would be necessary. The program would have to proceed to the next entry if a match failed: that is. skip
over the last part of the current entry once a mis-match was found.
Sample Problems:
a.
Resu It:
b.
(0041)
(0042)
(0043)
(0044)
(0045)
(0046)
68
04
(0040)
(0041)
(0042)
(0043)
(0044)
(0045)
(0046)
Result:
(0040)
37
55
7D
A1
37
55
68
A1
00. since 68 IS In the list.
9-5
Flowchart:
Entry
Count
Pointer
Mark
Pointer
Count
=
=
(4t)
(42)
43
0
+'
Pointer
Count - 1
Mark = FF (Hax)
(40) = Mark
The searching process IS a bit different here since the elements are ordered. Once we
find an element larger than the entry. the search is over, since subsequent elements will
be even larger. You may want to try an example to convince yourself that the procedure
works.
As In the prevIous problem. a table or other method that could
choose a good starting point would speed up the search. One
method would be to start in the middle and determme which half
of the list the entry was In. then divide the half into halves. etc. This method IS called a
binary search. since it divides the remaining part of the list In half each time.'
9-6
Source Program:
SRLST"
NOTIN:
DONE,
LD
LD
INC
LD
LD
INC
CP
JR
JR
INC
DJNZ
LD
LD
LD
HALT
HL,41H
A,(HL)
HL
B,(HL)
C,O
HL
(HL)
Z,DONE
C,NOTIN
HL
SRLST
C.OFFH
A.C
(40Hl.A
;POINT TO ENTRY
:GET ENTRY
;POINT TO LENGTH
;COUNT = LENGTH OF LIST
;MARK = ZERO FOR IN LIST
;POINT TO START OF LIST
:IS ENTRY = ELEMENT IN L1sn
;YES SEARCH COMPLETED
;ENTRY NOT IN LIST IF LESS THAN ELEMENT
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
OOOD
21
41
00
7E
23
46
OE
00
23
BE
28
07
38
03
23
10
F8
OE
FF
79
32
40
00
76
OOOE
OOOF
0010
0011
0012
0013
0014
0015
0016
0017
Instruction
(Mnemonic)
SRLST:
NOTIN:
LD
HL.41 H
LD
INC
LD
LD
A,(HL)
HL
B.(HL)
C.O
INC
CP
JR
HL
(HL)
Z.DONE
JR
C,NOTIN
INC
DJNZ
HL
SRLST
LD
C.OFFH
LD
LD
A.C
(40Hl.A
HALT
The Z80 block search instructions are not as usefu I here as In the prevIous example
because we want to do more than a simple search. Now we also want to check to see if
we have examined the relevant part of the list (j.e.. the part where the elements are less
than or equal to the entry). Try rewriting the program to use CPl. Remember that you
must use the Parity/Overflow flag to determine if the byte counter has been decremented to zero.
9-7
Memory locations 0042 and 0043 contain the address of the head of the
queue (MSBs In 0043). Place the address of the first element (head) of a
queue into memory locations 0040 and 0041 (MSBs in 0041) and update
the queue to remove the element. Each element In the queue IS two bytes
long and contains the address of the next two-byte element in the queue.
The last element In the queue contains zero to Indicate that there IS no next
element.
Queues are used to store data In the order in which it will be used. or tasks in the order
In which they will be executed. The queue IS a first-in. first-out data structure: I.e.. elements are removed from the queue In the same order In which they were entered.
Operating systems place tasks in queues so that they will be executed in the proper
order. I/O drivers transfer data to or from queues so that it will be transmitted or
handled in the proper order. Buffers may be queued so that the next available one can
easily be found and those that are released can easily be added to the available storage.
Queues may also be used to link requests for storage. timing. or I/O so that they can be
satisfied In the correct order.
In real applications each element in the queue will typically contain a large amount of
information or storage space besides the address required to link the element to the
next one.
Sample Problems:
a.
Result:
b.
Result:
(0042)
(0043)
(0046)
(0047)
(004D)
(004E)
6~}
6~}
~~}
end of queue
(0040)
(0041)
(0042)
(0043)
~g}
6~}
(0042)
(0043)
~~}
empty queue
(0040)
(0041)
~~}
9-8
Flowchart:
(42)
{431
= (Pointad
= (Pointer + 1I
Source Program:
DONE:
LD
LD
LD
OR
JR
LD
INC
LD
LD
HALT
HL.(42H)
(40H).HL
A.H
L
Z.DONE
E.(HL)
HL
D.(HL)
{42H).DE
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
0000
OOOE
OOOF
0010
0011
2A
42
00
22
40
00
7C
B5
28
07
5E
23
56
ED
53
42
00
76
Instruction
(Mnemonic!
DONE:
9-9
LD
HL.(42H)
LD
(40Hl.HL
LD
OR
JR
A.H
L
Z.DONE
LD
INC
LD
LD
E.(HL)
HL
D.(HL)
(42H).DE
HALT
Queuing can handle lists that are not in sequential memory locations. Each element
must contain the address of the next element. Such lists allow You to handle data or
taskS In the proper order. change variables. or fill In definitions In a program. Extra
storage IS required. but elements can easily be added to the queue or deleted from It.
Note the use of the sequence:
LD
OR
A.H
L
to determine if the contents of a 16-blt register pair is zero. Remember that INC and
DEC do not affect any flags when applied to a register pair. Try to devise some other sequences that could handle this problem - I t obviously occurs whenever you use a 16bit counter rather than the 8-blt counter that we have used In most of the examples.
One problem is that there IS no instruction that loads a register pair uSing the address in
a register pair. A sequence of instructions is necessary whenever a register pair must be
loaded directly.
It may be useful to maintain pOinters to both ends of the queue rather than Just to its
head. The data structure may then be used in either a first-in. first-out manner or in a
last-In. first-out manner. depending on whether new elements are added to the head or
the tail. How would you change the program example so that memory locations 0044
and 0045 contain the address of the last element (tail) of the queue?
If there are no elements in the queue. the program clears memory locations 0040 and
0041. A program that requested an element from the queue would then have to check
those memory locations to see if its request had been satisfied. Can you suggest other
ways to provide this Information?
8-Bit Sort
Purpose:
Sort an array of unsigned binary numbers into descending order. The length
of the array is In memory location 0040 and the array Itself beginS In memory location 0041.
Sample Problem:
Resu It:
(0040)
(0041)
(0042)
(0043)
(0044)
(0045)
(0046)
06
2A
B5
60
3F
D1
19
(0041)
(0042)
(0043)
(0044)
(0045)
(0046)
D1
B5
60
3F
2A
19
SIMPLE
SORTING
ALGORITHM
Step 1)
Step 2)
Step 3)
9-10
INTER will be set if any consecutive pair of numbers IS out of order. Therefore. if INTER = 0 at the end of a pass through the entire array. the array IS In proper order.
This sorting method is referred to as a "bubble sort" It IS an easy algorithm to implement. 'However. other sorting techniques should be considered when sorting long lists
where speed IS Important. 2
The technique operates as follows In a simple case. Let us assume that we want to sort
an array into descending order; the array has four elements - 12, 03, 15, 08.
1st Iteration;
Step 1)
INTER = 0
Step 2)
2nd Iteration;
Step 1)
Step 2)
INTER = 0
Final order of the array IS;
15
12
08
03
since the first pair (12,15) is exchanged. INTER = 1.
3rd Iteration;
Step 1)
INTER = 0
Step 2)
The elements are already in order. so no exchanges are necessary and INTER
remains zero.
9-11
Flowchart:
Inter
Count
POinter
o
(4Ol-1
41
Temp = (Pointer)
(Pointer) = (Pointer + 1)
IPointer+1l = Temp
Inter = 1
Pointer
= Pointer
+1
Count = Count - 1
No
No
9-12
Source Program:
SORT:
PASSl
CNT
LD
LD
LD
DEC
INC
LD
INC
CP
JR
LD
LD
DEC
LD
INC
LD
DJNZ
DEC
JR
HALT
C.O
HL,40H
B.(HU
B
HL
A,(HL)
HL
(HU
NC.CNT
D.(HL)
(HL).A
HL
(HU.D
HL
C.l
PASSl
C
Z.SORT
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
0008
OOOC
0000
OOOE
OOOF
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
OE
Instruction
(Mnemonic)
SORT
00
21
40
00
46
05
23
7E
23
BE
30
07
56
PASSl
77
28
72
23
DE
01
10
F2
OD
28
E7
76
CNT
LD
C.O
LD
HLAOH
LD
DEC
INC
LD
INC
CP
JR
B.(HU
B
HL
A,(HU
HL
(HL)
NC.CNT
LD
LD
DEC
LD
INC
LD
D.(HL)
(HL).A
HL
(HL).D
HL
C.l
DJNZ
PASSl
DEC
JR
C
Z.SORT
HALT
The case where two elements In the array are equal is very Important here. The program
should not perform an interchange in that case. since that Interchange would occur in
every pass. The result would be that every pass would set the Interchange flag. thus
producing an endless loop.
9-13
The program must reduce the counter by 1. since the number of consecutive pairs I'
one less than the number of elements (the last element has no successor). Before start
ing each sorting pass, we must be careful to relnitialize the counter, pOinter. and in
terchange flag.
There are many possible minor variations on this program. For example, we could use
RES O,C and SET O,C to clear and set the interchange flag instead of LD C.O and LD C.1.
We could also use the sequence MOV B.C followed by DJNZ SORT to check the in
terchange flag.
Note that Register B should be used for the Inner counter, since that counter is decremented most frequently. This allows us to take maximum advantage of the DJNZ instruction.
Indexing would be a convenient way to perform the Interchange if the Z80's Index
registers were more accessible. Try rewriting the program so as to use one of the Index
registers and compare the execution time and memory usage of the rewritten program
to those of the Original program.
Purpose:
Sample Problem:
(0040)
(0041)
(0042)
(0043)
(0044)
(0045)
(0046)
(0047)
(0048)
Resu It:
(PC)
02
48
00
4C
00
50
00
54
00
0050, since that is entry #2.
(starting from zero) In the Jump table.
Flowchart:
,
,
Start
Index
Base
=
=
(401 x Z
41
JELEM =
Base + Index
t
(PCl =
(JELEMl (JELEM + 11
9-14
The last box results in a transfer of control to the address obtained from the table.
Source Program:
LD
LD
ADD
LD
LD
INC
ADD
LD
INC
LD
EX
JP
HLAOH
A.(HL)
A.A
E.A
D.O
HL
HL.DE
E.(HL)
HL
D.(HL)
DE.HL
(HL)
;POINT TO INDEX
:GET INDEX
:DOUBLE INDEX FOR 2-BYTE TABLE
;EXTEND INDEX TO 16 BITS
;BASE ADDRESS OF JUMP TABLE
:INDEX INTO JUMP TABLE
:GET LSB'S OF DESTINATION ADDRESS
;GET MSB'S OF DESTINATION ADDRESS
:TRANSFER CONTROL TO DESTINATION
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
OOOD
OOOE
21
40
00
7E
87
5F
16
00
23
19
5E
23
56
EB
E9
Instruction
(Mnemonic)
LD
HLAOH
LD
ADD
LD
LD
A.(HL)
AA
E.A
D.O
INC
ADD
LD
INC
LD
EX
JP
HL
HL.DE
E.(HL)
HL
D.(HL)
DE.HL
(HL)
Jump tables are very useful in situations where one of several routines must be
selected. Such situations arise in decoding commands. selecting test programs. choosing alternate methods. or selecting an I/O configu ration.
The lump table replaces a whole series of conditional Jump operations. The program
that accesses the jump table could be used to access several different tables merely by
changing the starting address. 3
The data must be multiplied by two to give the correct Index. since each entry in the
Jump table IS a two-byte address.
The instruction JP (HL). which transfers the contents of Register
Pair HL to the Program Counter. IS an indirect Jump that IS very
handy in Jump tables and monitor programs. Note that JP (HL) is a
Jump instruction. Since it places a new value into the Program Counter; however. it
allows us to place a vanable address directly into the Program Counter. All of the Conditional Jump instructions (and the Call instructions) use fixed addresses. The only Jump
Instructions with similar flexibility are the two-word instructions JP (IX) and JP (lY).
No ending operation IS necessary. Since JP (HL) transfers control to the address obtained from the Jump table.
9-15
PROBLEMS
1)
Purpose:
Sample Problems:
(0040)
(0041)
(0042)
(0043)
(0044)
(0045)
a.
68
04
37
61
28
10
b.
Resu It:
(0041)
(0042)
(0043)
(0044)
68
04
37
68
28
1D
03
37
28
1D
The entry is removed from the list and the ones below It are moved up one position. The
length of the list IS reduced by 1.
9-16
2)
Purpose:
Place the contents of memory location 0040 Into an ordered list if It IS not
already there. The length of the list IS in memory location 0041, and the list
itself beginS In memory location 0042, which consists of unsigned binary
numbers in increaSing order. Place the new entry in the correct position In
the list. adjust the elements below It down, and Increase the length of the
list by 1.
Sample Problems:
a.
Result:
b,
(0040)
(0041)
(0042)
(0043)
(0044)
(0045)
6B
04
37
55
70
A1
(0041)
(0044)
(0045)
(0046)
05
6B
70
A1
(0040)
(0041)
(0042)
(0043)
(0044)
(0045)
6B
04
37
55
6B
A1
3)
Purpose:
Add the address In memory locations 0040 and 0041 (MSBs In 0041) to a
queue, The address of the first element of the queue is in memory locations
0042 and 0043 (MSBs In 0043). Each element in the queue contains either
the address of the next element in the queue or zero if there IS no next element; all addresses are 16 bits long with the most significant bits In the
second word of the element. The new element goes at the end {taill of the
queue; ItS address will be In the element that was at the end of the queue
and it will contain zero to Indicate that it is now the end of the queue,
Sample Problem:
(0040)
(0041)
(0042)
(0043)
(0046)
(0047)
Result;
(0046)
(0047)
(0040)
(004E)
6~}
6~}
How would you add an element to the queue if memory locations 0044 and 0045 contained the address of the tail (last element) of the queue?
9-17
4)
16-Bit Sort
Purpose:
Sort an array of unsigned 16-blt binary numbers into descending order. The
length of the array IS m memory location 0040 and the array itself begins In
memory location 0041. Each 16-blt number is stored with the least significant bits In the first word.
Sample Problem:
Result:
(0040)
(0041)
(0042)
(0043)
(0044)
(0045)
(0046)
03
01
19
60
3F
2A
B5
(0041)
(0042)
(0043)
(0044)
(0045)
(0046)
2A
B5
60
3F
Dl
19
5)
Purpose:
Use the contents of memory location 0040 as the key to a lump table starting In memory location 0041. Each entry in the Jump table contains an 8-blt
key value followed by a 16-bit address (MSBs m second word) to which the
program should transfer control if the key IS equal to that key value.
Sample Problem:
Result:
(0040)
(0041)
(0042)
(0043)
(0044)
(0045)
(0046)
(0047)
(0048)
(0049)
(PC)
38
32
4B
00
35
40
00
38
4F
00
004F. since that address corresponds
to key value 38.
Try wrltmg the program with and without the CPIR instruction. Can you think of a way
to simplify the version that uses the CPIR instruction? Hint: place all the corresponding
8-blt words into separate tables so that the pr~ram only has to add 1 to the table
pomter to move from one key value to the next.
9-18
REFERENCES
1.
Knuth describes other searching techniques In his book The Art of ComQuter Programming. Volume III: Sorting and Searching, Addison-Wesley, Reading, Mass"
1978. Knuth also has discussed searching and hashing In a more elementary way
In an article entitled"Algorithms" (see the April 1977 Issue of Scientific American).
2.
There are many sorting algorithms that vary wideIv in efficiency. Knuth describes
some in the book mentioned above (The Art of Comguter Programming. Volume
III: Sorting and Searching). Sorting and searching algorithms are also discussed In
K. A. Schember and J. R. Rumsey. "Minimal Storage Sorting and Searching Techniques for RAM Applications. a Tutorial". Comguter. June 1977. pp. 92-100.
3.
There are additional examples of the use of jump tables in L. A. Leventhal. "Cut
Your Processor's Computation Time". Electronic DeSign. August 16. 1977, pp.
82-89. and in Chapter 7 of J. B. Peatman, Microcomputer-Based Design.. McGrawHill. New York, 1977
4.
This method is discussed by T. Dollhoff In "Microprocessor Software: How to Optimize Timing and Memory Usage: Part Four: Techniques for the Zilog Z80".
Q!gltal Design.. February 1977. pp. 48-49.
9-19
Chapter 10
SUBROUTINES
None of the examples that we have shown so far IS typically a program all by Itself.
Most real programs perform a series of tasks. many of which may be the same or may
be common to several different programs. We need a way to formulate these tasks once
and make the formulations conveniently available both in different parts of the current
program and In other programs.
SUBROUTINE
The standard method IS to write subroutines that perform particuLIBRARY
lar tasks. The resulting sequences of instructions can be written
once. tested once. and then used repeatedly. They can form a
subroutine library that provides documented solutions to common problems.
SUBROUTINE
Most microprocessors have special instructions for transferring
control to subroutines and restoring control to the main proINSTRUCTIONS
gram. We often refer to the special Instruction that transfers
control to a subroutine as Call. Jump-to-Subroutine. Jump and Mark Place. or Jump
and Link. The special instruction that restores control to the main program is usually
called Return. On the
microprocessor. the Call Instruction (CALL) saves the old
value of the Program Counter In the RAM Stack before placing the starting address of
the subroutine into the Program Counter; the Return instruction (RET) gets the old
value from the Stack and puts it back In the Program Counter. The effect is to transfer
program control. first to the subroutine and then back to the main program. Clearly the
subroutine may itself transfer control to a subroutine. and so on.
zao
In order to be really useful. a subroutine must be general. A routine that can perform
only a specialized task. such as looking for a particular letter in an input string of fixed
length. will not be very useful. If. on the other hand. the subroutine can look for any letter in strings of any length. it will be far more helpful. We call the data or addresses that
the subroutine allows to vary "parameters" An Important part of writing subroutines is
deciding which variables should be parameters.
PASSING
One problem IS transferring the parameters to the subroutine: this
PARAMETERS
process is called passing parameters. The simplest method is for
the main program to place the parameters Into registers. Then the
subroutine can simply assume that the parameters are there. Of course. this technique
is limited by the number of registers that are available. The parameters may, however.
be addresses as well as data. For example, a sorting routine could begin with the startIng address of an array in Register Pair HL.
Other methods are necessary when there are more parameters. One possibility is to use
the Stack. The main program can place the parameters into the Stack and the
subroutine can retrieve them. The advantages of this method are that the Stack is essentially unlimited In size. and that data In the Stack IS not lost even if the Stack is used
again.
zao
10-1
REENTRANT
A subroutine is reentrant if it can be interrupted and called by the
SUBROUTINE
interrupting program and still give the correct results for both the
interrupting and interrupted programs. Reentrancy is important for
standard subroutines In an interrupt-based system. Otherwise the Interrupt service
routines cannot use the standard subroutines without causing errors. Microprocessor
subroutines are easy to make reentrant. since the Call instruction uses the Stack and
that procedure is automatically reentrant. The only remaining requirement IS that the
subroutine use the registers and Stack rather than fixed memory locations for temporary storage. This IS a bit awkward. but usually can be done.if necessary.
A subroutine is recursive if it calls itself. Such a subroutine clearly must also be reentrant. However. recursive subroutines are uncommon in microprocessor applications.
Most programs consist of a main program and several subroutines. This IS advantageous because you can use proven routines and debug and test the other subroutines
separately. You must. however. be careful to use the subroutines properly and remember their exact effects on registers and memory locations.
SUBROUTINE DOCUMENTATION
Subroutine listings must provide enough information so that
users need not examine the subroutine's internal structure.
Among the necessary specifications are:
DOCUMENTING
SUBROUTINES
EXAMPLES
It IS important to note that the following examples all reserve an area of memory for the
RAM Stack. If the monitor invour microcomputer establishes such an area. you may use
it instead. If you wish to try establishing your own Stack area. remember to save and
restore the monitor's Stack Pointer in order to produce a proper return at the end of
your main program.
To save the monitor Stack POinter. use the Instruction LD (addr),SP. To restore the
monitor Stack Pointer. use the instruction LD SP.{addr). Both of these instructions require a two-bvte operation code (ED 78 for loading the Stack Pointer. ED 73 for storing
it) in addition to the two bytes of address.
We have used address 0080 (hex) as the starting point for the Stack. You may have to
consistently replace that address with one more suitable for your configuration. You
should consult your microcomputer's manual to determine the required changes.
10-2
Hex to ASCII
Purpose: Convert the contents of the Accumulator to an ASCII character. Place the
result in the Accumulator. Assume that the Accumulator contains a single
hexadecimal digit.
Sample Problems:
a.
Result:
b.
Result:
(A)
OC
(A)
43
(A)
06
(A)
36
'C'
'6'
Flowchart:
Source Program:
The calling program starts the Stack at memory location 0080. gets the data from
memory location 0040. calls the conversion subroutine, and stores the result In memory
location 0041.
ORG
LD
LD
CALL
LD
HALT
o
SP,80H
A, (40H)
ASDEC
(41H),A
ASDEC:
ASCZ:
CP
JR
ADD
ADD
20H
10
C,ASCZ
A:A'-'9'-1
A:O'
Rn
10-3
Subroutine Documentation:
, SUBROUTINE ASDEC
PURPOSE: ASDEC CONVERTS A HEXADECIMAL
DIGIT IN THE ACCUMULATOR TO AN
ASCII DIGIT IN THE ACCUMULATOR
. INITIAL CONDITIONS: HEX DIGIT IN A
: FINAL CONDITIONS: ASCII CHARACTER IN A
. REGISTERS USED: A
SAMPLE CASE
INITIAL CONDITIONS: 6 IN ACCUMULATOR
FINAL CONDITIONS, ASCII 6 (HEX 36)
IN ACCUMULATOR
Object Program:
Memory Address
(Hex)
1)
Instruction
{Mnemonic}
Calling program
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
0008
OOOC
2)
Memory Contents
(Hex)
31
80
00
3A
40
00
CD
20
LD
SP,80H
LD
A.(40H)
CALL
ASDEC
LD
(41Hl,A
00
32
41
00
76
HALT
Subroutine
0020
0021
0022
0023
0024
0025
0026
0027
0028
ASDEC.
FE
OA
38
02
C6
07
C6
30
C9
ASCZ:
CP
10
JR
C,ASCZ
ADD
A:A'-'9'-1
ADD
A:O'
RET
The Instruction LD SP.80H starts the Stack at memory location 0080. Remember that
the Stack grows downward {to lower addresses!' We usually place the Stack at the high
10-4
end of RAM (i.e.. the highest address) so that It will not Interfere with other temporary
storage.
The Call instruction places the subroutine starting address (0020 hex) Into the Program
Counter and saves the old Program Counter (0009 hex) In the Stack. The procedure IS:
STEP 1 -
STEP 2 -
Note that the Z80 Stack POinter always contains the address of the last occupied Stack
location.
The result In this case IS:
(OOlF)
(OOlE)
(SP)
00
09
OOlE
The value that IS saved IS the value of the Program Counter after the processor has
fetched the entire Call instruction from memory. Note that the address ends up stored
lust like other Z80 addresses. with the least significant bits In the lower address.
The Return instruction loads the Program Counter with the contents of the bottom two
memory locations In the Stack. The procedure IS:
STEP 1 -
Load eight bits from Stack Into LSBs of Program Counter. Increment Stack
POinter.
STEP 2 -
Load eight bits from Stack into MSBs of Program Counter. Increment Stack
POinter.
This subroutine has a single Input parameter and produces a single result. The Accumulator IS the obvious place to put both.
The calling program Involves three steps: plaCing the data Into the Accumulator. callIng the subroutine. and stOring the result. The overall initialization must also place the
Stack In the appropriate area of memory.
The subroutine is reentrant. since It uses no data memory; It IS relocatable. since the
address ASCZ is relative.
Note that the CALL instruction results In the execution of four or five instructions taking
36 or 38 clock cycles. A subroutine call can take a long lime even though It appears to
be a Single instruction In the program.
If you plan to use the Stack for parameters. remember that CALL places the return address at the top of the Stack. You can increment the Stack POinter tWice (INC SP) to get
past the return address. but you must also remember to adjust the Stack POinter properly before returning. You can also move the Stack POinter to Registers Hand L With
the sequence:
LD
ADD
HL.O
HL.SP
Now yOU can use Implied memory addreSSing With Hand L to ac-cess data in the Stack.
Another alternative IS to move the Stack POinter to an Index register (say IX) with the sequence:
LD
ADD
IX.O
IX.SP
10-5
This alternative has the advantage that you can now access data and addresses in the
Stack with Indexed offsets. Furthermore, Register Pair HL is Immediately available for
use in the subroutine. Note that you can use the instructions LD SP,HL or LD SP,IX to
return an adiusted value to the Stack POinter.
a.
Result:
0043
00
(A)
00
(HU
(0043)
(0044)
(0045)
(0046)
(0047)
(0048)
(0049)
b.
Result:
0043
52 'R'
41 'A'
54 'T'
48 'H'
45 'E'
52 'R'
00 CR
(A)
06
Flowchart:
Pointer
Count
HL
Ves
Count
Count + 1
PQlnter
Pomter + ,
10-6
Source Program:
The calling program starts the Stack at memory location 0080. gets the starting address
of the string from memory locations 0040 and 0041. calls the string length subroutine.
and stores the result in memory location 0042.
LD
LD
CALL
LD
HALT
SP.80H
HL.(40H)
STLEN
(42Hl.A
The subroutine determines the length of a string of ASCII characters and places the
length Into the Accumulator.
STLEN:
CHKCR:
20H
B.O
AODH
(HL)
Z.DONE
ORG
LD
LD
CP
JR
INC
INC
HL
CHKCR
A.B
JR
DONE:
ZERO
:STRING LENGTH
:GET ASCII CARRIAGE RETURN
:IS CHARACTER A CARRIAGE RETURN?
:YES. END OF STRING
:NO. ADD 1 TO STRING LENGTH
LD
RET
Subroutine Documentation:
:SUBROUTINE STLEN
:PURPOSE. STLEN DETERMINES THE LENGTH OF A
STRING (NUMBER OF CHARACTERS BEFORE
A CARRIAGE RETURN)
:INITIAL CONDITIONS. STARTING ADDRESS OF
STRING IN REGISTER PAIR HL
:REGISTERS USED: A.B.H.L
;SAMPLE CASE:
ST ARTING CONDITIONS: (HL) = 0043
(0043)
= 35.
(0044)
FINAL CONDITIONS
= 46.
(0045)
= 00
(A) = 02
10-7
Object Program:
Memorv Address
(Hex)
1)
Instruction
(Mnemonic)
Calling program
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
2)
Memorv Contents
(Hex)
31
80
00
2A
40
00
CD
20
00
32
42
00
76
LD
SP.80H
LD
HL.(40H)
CALL
STLEN
LD
(42Hl.A
HALT
Subroutine
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
002A
002B
002C
06
00
3E
OD
BE
28
04
04
23
18
F9
78
C9
STLEN:
CHKCR:
DONE:
LD
B.O
LD
AODH
CP
JR
(HU
Z.DONE
INC
INC
JR
B
HL
CHKCR
LD
RET
A.B
The calling program Involves four steps: initializing the Stack POinter. placing the startIng address of the strrng Into Register Pall HL. calling the subroutine. and storrng the
result.
The subroutine is reentrant. since it does not change the contents of any memory locations. It is relocatable. since all the Jump instructions use relative addresses.
The subroutine changes Register B and the address in Register Pair HL as well as the
Accumulator. The programmer must be aware that data previously stored in Register B
and the address previously loaded into HL will be lost: the subroutine documentatIOn
must describe what registers are used.
An alternative to destroYing register contents In the subroutine IS to save them In the
Stack and then restore them before returning. This approach makes the calling routine
Simpler. but costs extra time and memory (in the program and In the Stack).
This subroutine has a single Input parameter. which is an address. The best way to pass
thiS parameter IS through a register pall and. since the HL pall is certainly the most flexible as far as addressing options are concerned. it is the obvIous choice.
The subroutine contains an unconditional Jump instruction. JR CHKCR. By alterrng the
initial conditions prior to entering the subroutlne's loop. can yOU eliminate this iump?
If the terminating character were not always an ASCII carriage return. we could make
that character into another parameter. Now the calling program would have to place
10-8
the terminating character into the Accumulator and the starting address of the string
Into Register Pair HL before calling the subroutine.
One way to pass parameters that do not depend on vanable data is to place the values
In program memory immediately after the Call Instruction. You can use the old Program
Counter (saved at the top of the Stack) to access the data. but you must adjust itS value
properly before returning control to the main program. For example. we could pass the
value of the terminating character this way. The main program and subroutine would
be:
Calling program:
ORG
LD
LD
CALL
DEFB
LD
HALT
0
SP.80H
(HU.40H
STLEN
(42H).A
Subroutine:
STLEN:
CHKCR:
DONE:
ORG
POP
LD
INC
PUSH
LD
CP
JR
INC
INC
JR
LD
RET
20H
DE
A.(DE)
DE
DE
B.O
(HU
Z.DONE
B
HL
CHKCR
A.B
This subroutine is longer and uses Register Pair DE. but the calling program need not
load the terminating character Into a register. The INC DE instruction is necessary to
force a return to the next instruction. rather than to the parameter list. 1
PUSH and POP transfer the contents of register pairs or index registers to and from the
RAM Stack. The eight least significant bits are removed first and stored last to retain
consistency With the Z80's upside-down method of storing 16-bit addresses. Remember that the RAM Stack grows downward (to lower addresses!.
even.
10-9
Sample Problem:
(A)
(0041)
(0042)
(0043)
(0044)
(0045)
(0046)
06
0041
31
32
33
34
35
36
(0041)
(0042)
(0043)
(0044)
(0045)
(0046)
81
82
33
84
35
36
(HLl
Result:
Flowchart:
POinter
fHLl
Count
lA)
(Pointer)
(POinter)
OR lOOOOOOOB
(Set Panty Bid
POinter
=Pomter
Count
= Count
10-10
+ 1
- 1
Source Program:
The calling program starts the Stack at memory location 0080, sets the starting address
of the string to 0041, gets the string length from memory location 0030, and calls the
even parity subroutine.
ORG
LD
lD
LD
CALL
HALT
o
SP.80H
HL.41H
A,(30H)
EPAR
CHCNT
ORG
LD
lD
LD
OR
JP
LD
INC
DJNZ
HALT
20H
B,A
C.l0000000B
A,(HL)
C
PO.CHCNT
(HL),A
HL
SETPR
Subroutine Documentation:
:SUBROUTINE EPAR
:PURPOSE: EPAR ADDS EVEN PARITY
TO A STRING OF 7-BIT ASCII
. CHARACTERS
:INITIAL CONDITIONS: STARTING ADDRESS
OF STRING IN HL. LENGTH OF STRING
. IN A
:FINAL CONDITIONS: EVEN PARITY IN
MSB OF EACH CHARACTER
:REGISTERS USED: A,B,C.H,L
:SAMPLE CASE:
INITIAL CONDITIONS: (HL) = 0041
(A) = 2. (0041) = 32, (0042) = 33
FINAL CONDITIONS: (0041) = B2, (0042) = 33
This subroutine has two parameters, an address and a number. Register Pair HL IS used
to pass the address and the Accumulator to pass the number. No explicit results are
returned, since the subroutine affects only the MSB of each character In the string.
10-11
The calling program must place the starting address of the string mto Register Pair HL
and the length of the string mto the Accumulator before transferring control to the
subroutine.
The subroutine changes the values In Registers A. H. and L and uses Registers Band C
for temporary storage. It IS reentrant. since it does not use any fixed memory locations
for temporary storage.
Object Program:
Memory Address
(Hex)
1)
Instruction
(Mnemonic)
Calling program
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
2)
Memory Contents
(Hex)
31
80
00
21
41
00
3A
30
00
CD
20
00
76
LD
SP.80H
LD
HL,41 H
LD
A.(30H)
CALL
EPAR
HALT
Subroutine
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
Q02A
002B
002C
47
OE
80
7E
Bl
E2
29
00
EPAR:
LD
LD
B.A
C.l0000000B
SETPR:
LD
OR
JP
A,(HL)
C
PO.CHCNT
LD
INC
DJNZ
(HL),A
HL
SETPR
77
23
10
F7
C9
CHCNT
RET
Pattern Match
Purpose: Compare two strings of ASCII characters to see if they are the same. The
length of the strings is in the Accumulator. The starting address of one string
IS in Register Pair HL: the starting address of the other IS in Register Pair DE.
If the two strings match, clear the Accumulator: otherwise. set theAccumulator to FF (hex).
10-12
Sample Problems:
(A)
a.
Result:
b.
Result:
(DE)
(HU
03
50
60
(0050)
(0051)
(0052)
43
41
54
'C'
'A'
(0060)
(0061)
(0062)
43
41
54
'C'
'A'
(A)
(A)
(DE)
(HU
03
50
60
(0050)
(0051)
(0052)
52
41
54
(0060)
(0061)
(0062)
43
41
54
(A)
'R'
'A'
T
'C'
'A'
Flowchart:
Pointer 1
Pointer 2
Count
fDEI
fHLI
fA)
No
Pomter 1. ;;
Pointer 1 + 1
Pomter 2 =
Pointer 2 + 1
Count = Count - 1
fA)=O
10-13
fA) = FF fhe.)
Source Program:
The calling program starts the Stack at memory location 0080. sets the starting addresses of the stnngs to 0050 and 0060. respectively. gets the string length from
memory location 0040. calls the pattern match subroutine. and places the result into
memory location 0041.
ORG
LD
LD
LD
LD
CALL
LD
HALT
o
SP.80H
DE.60H
HL.50H
A.(40H)
PMTCH
(41Hl.A
DONE.
ORG
LD
LD
LD
CP
JR
INC
INC
DJNZ
LD
LD
RET
20H
8.A
C,OFFH
A.(DE)
(HU
NZ,DONE
DE
HL
CHCAR
C,O
AC
Subroutine Documentation:
:SUBROUTINE PMTCH
:PURPOSE: PMTCH DETERMINES IF TWO
.
10-14
Object Program:
Memory Address
(Hex)
1)
Instruction
(Mnemonic)
Calling program
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
0000
OOOE
OOOF
0010
0011
0012
2)
Memory Contents
(Hex)
31
80
00
11
60
00
21
50
00
3A
40
00
CD
20
00
32
41
00
76
LD
SP,80H
LD
DE,60H
LD
HL,50H
LD
A.(40H)
CALL
PMTCH
LD
(41H!.A
HALT
Subroutine
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
002A
002B
002C
0020
002E
47
OE
FF
1A
BE
20
06
13
23
10
F8
OE
00
79
C9
PMTCH:
LD
LD
B,A
C,OFFH
CHCAR:
LD
CP
JR
A,(DE)
(HU
NZ,DONE
INC
INC
DJNZ
DE
HL
CHCAR
LD
C,O
LD
RET
A.C
DONE:
This subroutine, like the preceding ones, changes all of the flags. You should generally
assume that a subroutine call changes the flags unless it IS specifically stated otherwise. If the main program needs the old flag values (for later checking!. it must save
them In the Stack prior to calling the subroutine. This is accomplished with the PUSH
AF instruction.
The subroutine is reentrant and changes all the main registers except C.
This subroutine has three parameters - the two starting addresses and the length of
the strings. These parameters use five general-purpose registers.
10-15
Multiple-Precision Addition
Purpose: Add two multiple-byte binary numbers. The length of the numbers in bytes
is In the Accumulator. The starting addresses of the numbers are in Register
Pairs DE and HL. The starting address of the result IS in Index Register IX. All
the numbers begin with the least Significant bits.
Sample Problem:
(A)
(DE)
(IX)
04
51
61
71
(0051)
(0052)
(0053)
(0054)
(0061)
(0062)
(0063)
(0064)
C3
A7
58
2F
8S
35
DF
14
Result:
(0071)
(0072)
(0073)
(0074)
(HL)
I.e.
= 78
= DD
=3A
=44
2F58A7C3
14DF358S
443ADD78
Flowchart:
Count
POinter 1
POinter 2
POinter 3
(DEI
(HLI
(IX)'
-ea
(POinter 3)
(Pointer 1)
+ (Pointer 2)
+ earrv
---"l!""--'"
Pointer 1::: Pointer 1+1
POinter 2;= POinter2+1
10-16
Source Program:
The calling program starts the Stack at memory location 0080, sets the starting addresses of the vanous numbers to 0050,0060, and 0070, respectively, gets the length
of the numbers from memory location 0040, and calls the multiple-precision addition
subroutine.
ORG
LD
LD
LD
LD
LD
CALL
HALT
o
;START STACK AT LOCATION 0080
:GET STARTING ADDRESS OF FIRST NUMBER
:GET STARTING ADDRESS OF SECOND NUMBER
:GET STARTING ADDRESS OF RESULT
:GET LENGTH OF NUMBERS IN BYTES
:MULTIPLE-PRECISION ADDITION
SP.80H
HL,50H
DE.60H
IX,70H
A,(40H)
MPADD
20H
B,A
A
A,(DE:
A,(HL)
(lXl.A
DE
HL
IX
AD OW
Subroutine Documentation:
:SUBROUTINE MPADD
:PURPOSE. MPADD ADDS TWO
.
= C3.
(0051)
= A7,
(0060)
= B8,
(0061)
10-17
= 35
Object Program"
Memory Address
(Hex)
1)
Instruction
(Mnemonic)
Calling program
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
OOOD
OOOE
OOOF
0010
0011
0012
0013
2)
Memory Contents
(Hex)
31
80
00
21
50
00
11
60
00
DD
21
70
00
3A
40
00
CD
20
00
76
LD
SP.80H
LD
HL,50H
LD
DE.60H
LD
IX,70H
LD
A,(40H)
CALL
MPADD
HALT
Subroutine
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
002A
002B
002C
002D
47
A7
lA
8E
DO
77
00
13
23
DD
23
10
F5
C9
LD
AND
LD
ADC
LD
B,A
A
A,(DE)
A,(HL)
(IXl.A
INC
INC
INC
DE
HL
IX
DJNZ
ADDW
RET
We use Index Register IX to hold the result address. Try changing the program to use
Register Pair BC for this purpose. What happens to the counter?
We could also place the result address at the top of the Stack. The instruction EX
(SPl.HL exchanges the top of the Stack and Register Pair HL. Change the program so
that it uses thiS instruction, but remember to increment all three pOinters after each
Iteration.
This subroutine has four parameters - three addresses and the length of the numbers.
Six 8-blt registers and the 16-bit Index Register IX are used for passing parameters.
10-18
PROBLEMS
Note that you are to write both a calling program for the sample problem and a properly
documented subroutine.
1)
ASCII to Hex
Purpose: Convert the contents of the Accumulator from the ASCII representation of a
hexadecimal digit to the 4-blt binary representation of the digit. Place the
result Into the Accumulator.
Sample Problems:
a.
Result:
b.
Result:
2)
(A)
43
(A)
OC
(A)
36
(A)
06
'C'
'6'
Result:
3)
(A)
0041
49
02 STX
47 'G'
4F '0'
03 ETX
02
Purpose: Check the even parity of a string of ASCII characters. The length of the string
is In the Accumulator and the starting address of the string is in Register Pair
HL If the panty of all the characters in the string IS correct clear the Accumulator: otherwise, set the Accumulator to FF hex (all ones).
Sample Problems:
a.
(A)
(Hl)
(0042)
(0043)
(0044)
Result:
b.
82
33
(A)
(A)
03
0042
81
(HU
(0042)
(0043)
(0044)
Result:
03
0042
81
(A)
86
33
FF, since the character In memory location 0043
does not have even parity
10-19
4)
String Comparison
Purpose: Compare two strings of ASCII characters to see which IS larger (i.e., which
would follow the other in 'alphabetlcal' ordering).
a,
b.
c.
5)
(A)
(DE)
(HL)
(0050)
(0051)
(0052)
03
0060
0050
43 'C'
41 'A'
54 T
(0062)
(0063)
(0064)
42
41
54
Result
(A)
(DE)
(HL)
(0050)
(0051)
(0052)
03
0060
0050
44 'D'
4F '0'
47 'G'
(0060l
(0061)
(0062)
44
4F
47
'B'
'A'
'D'
'0'
'G'
Result
(A)
(DE)
(HU
(0050)
(0051)
(0052)
03
0060
0050
43 'C'
41 'A'
54 T
(0060)
(0061)
(0062)
43
55
54
Result
'C'
'U'
Decimal Subtraction
Purpose: Subtract one multiple-digit decimal (BCD) number from another. The length
of the numbers (in bytes) IS In the Accumulator and the starting addresses of
the numbers are In Register Pairs DE and HL. Subtract the number with the
starting address In HL from the one with the starting address In DE. The
starting address of the result is in Index Register IX. All the numbers begin
with the least significant digits. The sign of the result IS returned In the Accumulator - zero if the result IS positive. FF (hex) if It is negative.
10-20
Sample Problem:
(A)
04
0050
0060
0070
(DE)
(HU
(IX)
(0050)
(0051)
(0052)
(0053)
(0060)
(0061)
(0062)
(0063)
Result:
85
19
70
36
59
34
66
12
00 (positive)
26
85
03
24
(A)
(0070)
(0071)
(0072)
(0073)
36701985
12663459
I.e..
24038526
10-21
REfERENCES
1.
Other examples of this technique (for the 8080 microprocessor) are In S. Mazor and
C. Pitchford, "Develop Cooperative Microprocessor Subroutines," Electronic
Design, June 7,1978, pp. 116-118.
10-22
Chapter 11
INPUT/OUTPUT
There are two problems In the design of input/output sections: one IS how to interface
peripherals to the computer and transfer data. status. and control signals: the other IS
how to address I/O devtces so that the CPU can select a particular one for a data
transfer. Clearly. the first problem is both more complex and more interesting. We will
therefore discuss the Interfacing of peripherals here and leave addressing to a more
hardware-oriented book.
In theory. the transfer of data to or from an I/O device is similar to
the transfer of data to or from memory. In fact. we can consider the
memory as Just another I/O device. The memory is. however.
special for the following reasons:
1)
2)
3)
4)
5)
Most I/O devices do not have such convenient features. They may operate at speeds
much slower than the processor: for example. a teletypewriter can transfer onlY 10
characters per second, while a slow processor can transfer 10.000 characters per second. The range of speeds is also very wide - sensors may provide one reading per
minute. while video displays or floppy disks may transfer 250.000 bits per second.
Furthermore. I/O devices may require continuous signals (motors or thermometers), currents rather than voltages (teletypewriters), or voltages at far different levels than the
signals used by the processor (gas-discharge displays). I/O devices may also require
special formats. protocols. or control signals. Their word lengths may be much shorter
or much longer than the word length of the computer. These variations make the
deSign of I/O sections difficult and mean that each peripheral presents its own special
interfacing problem.
We may. however, provide a general description of devices and interfacing methods. We may roughly separate devices into three
categories, based on their data rates:
1)
2)
3)
Slow devices that change state no more than once per second. Changing their
states typically requires milliseconds or longer. Such devices Include lighted displays. sWitches. relays. and many mechanical sensors and actuators.
Medium-speed devices that transfer data at rates of 1 to 10.000 bits per second.
Such devices Include keyboards, printers, card readers. paper tape readers and
punches. cassettes, ordinary communications lines, and many analog data acquiSItion systems.
High-speed deVices that transfer data at rates of over 10.000 bits per second. Such
devices include magnetic tapes. magnetic disks. high-speed line printers, hlghspeed communications lines, and video displays.
11-1
2)
3)
Input data need not be latched. since the input device holds the data for an enormous length of time by computer standards. Output data must be latched. since
the output device will not respond to data that is present for only a few CPU clock
cycles.
Input transitions cause problems because of their duration; brief output transitions
cause no problems because the output devices (or the observers) react slowly.
The major constraints on input are reaction time and responsiveness. the major
constraints on output are response time and observability.
11-2
Data Outputs 0
)
Data Bus
Data
Output
Inputs
Port
Data Outputs 1
Strobe
)
y
>
Clock
y
Select
Counter
Inputs
Data Outputs 3
..
Data Outputs 0
Data
Data
Port
.Inputs
Data Outputs 1
>
Oata Bus
Demultiplexer
Data Outputs 2
Control
Port
Select
Inputs
..
Data Outputs 3
)
The- CPU sends control information to the Control Port; that port detennjnes
where the Demultiplexer sends the data.
11-3
Oata Inputs 0
Data Bus
Oata
Outputs
Input
Port
Data Inputs 1
~
Multiplaxar
A
Data Inputs 2
Cock
~
Salact
Countar
Data Inputs 3
Inputs
The Countar controls which input the Multiplexer lJllleS to the Input Port.
Dat8 Inputs 0
Input Data
""~
Bus
Data
Data
Port
Outputs
A
Data Ihputs 1
Data Inputs 2
Multiplexer
.l
Selsct
Control
Port
Inputs
.. Oata Inputs 3
;/
~
The control information which the CPU sends to the Control Port (with an output oparationi
determines which input the Muhiplexar routes to the Oata Port.
11-4
Figures 11-5 and 11-6 show typical input and output operations using the handshake
method. The procedure whereby the CPU checks the readiness of the peripheral before
transferring data is called "polling" Clearly. polling can occupy a large amount of processor time if there are many I/O devices. There are several ways of providing the
handshake signals. Among these are:
Separate dedicated I/O lines. The processor may handle these as additionalI/O ports
or through special lines or interrupts. The
processor does not have serial I/O lines.
but the
ParaliellnputiOutput device' (or PIO) does.
Special patterns on the I/O lines. These may be single start and stop bits or entire
characters or groups of characters. The patterns must be easy to distinguish from
background nOise or inactive states.
zao
zao
ISTROBE I
Many peripherals transfer data at regular intervals; i.e.. synchronously. Here the only
problem is starting the process by lining up to the first Input or marking the first output.
In some cases. the peripheral provides a clock input from which the processor can obtain timing information.
Transmission errors are a problem with medium-speed devices.
Several methods can lessen the likelihood of such errors; they
include:
REDUCING
TRANSMISSION
ERRORS
11-5
Input
Acknowledge
Data
Data Bus
CPU
~
Data Readv
a)
Peripheral provides data and Data Ready signal to computer I/O section.
Input
Acknowledge
Data
Data Bus
Penpheral
Data Ready
bi
CPU reads Data Readv signal from I/O section (this may be a hardware interrtJpt connection).
Input
Acknowledge
Data
Data Bus
Peripheral
Data Ready
ci
Input
Acknowledge
Data Bus
Data
Data Ready
d)
CPU sends Input Acknowledge signal to I/O sectIon, which then provides Input Acknowledge signal
to Peripheral (this may be a hardware connection).
11-6
Data Bus
Data
CPU
al
Output Ready
Data Bus
Data
Penpheral
Peripheral Ready
bi
CPU reads Penpheral Ready sIgnal from 110 section (this may be a hardware Interrupt connection),
Output Ready
Data Bus
Data
"
Peripheral Ready
ci
Output Ready
Data Bus
Data
.
Peripheral Ready
d)
CPU sends Output Ready signal to Penpheral (this may be a hardware connection).
2)
3)
zao
The choice among these three methods depends on your application. The software method is Inexpensive but may overburden the
processor. The programmable timers are relatively expensive. but
are easy to Interface and may be able to handle many complex
timing tasks.
CHOOSING
A TIMING
METHOD
DELAY ROUTINES
A simple delay routine works as follows:
Step 1)
Step 2)
Step 3)
BASIC
SOFTWARE
DELAY
This routine does nothing except use time. The amount of time used depends upon the
execution time of the various instructions. The maximum length of the delay is limited
by the size of the register; however. the entire routine can be placed Inside a similar
routine that uses another register. and so on.
The following example uses Register C and the Accumulator to
TRANSPARENT
provide delays as long as 255 ms. The chOice of registers is arDELAY
bitrary. You may. in fact. find the use of a register pair (e.g.. BC)
ROUTINE
more convenient. A PUSH BC instruction at the start of the
delay routine and a POP BC at the end will result In a routine that does not affect any
registers at all. Such a routine IS said to be "transparent" to the calling program. Note
that the PUSH and POP instructions must be included in the time budget
11-8
EXAMPLE
Delay Program Using Accumulator
Purpose: The program provides a delay of 1 ms times the contents of Accumulator.
Flowchart:
Count = MSCNT
Count
= Count -
(A) =(A)- 1
No
The value of MSCNT depends on the speed of the CPU and the memory cycle.
11-9
Source Program:
DELAY:
DLY1'
LD
DEC
JR
DEC
JR
RET
C.MSCNT
C
NZ.DLYl
A
NZ.DELAY
Object Program:
Memorv Location
(Hex)
Memorv Contents
(Hex)
0030
0031
0032
0033
0034
0035
0036
0037
0038
OE
MSCNT
00
20
FD
3D
20
F8
C9
Instruction
(Mnemonic)
DELAY:
LD
C.MSCNT
DLY1:
DEC
JR
C
NZ.DLYl
DEC
JR
A
NZ.DELAY
RET
Time Budget:
Instruction
LD
DEC
JR
DEC
JR
RET
C.MSCNT
(A)
(A) x MSCNT
(A) x MSCNT
(A)
(A)
NZ.DLYl
A
NZ.DELAY
The total time used should be (A) x 1 ms. If the memory IS operating at full speed. the
Instructions require the following numbers of clock cYcles.
LD
DEC
JR
RET
C.MSCNT
C or DEC A
NZ
7
4
70r12
10
The alternative times for JR are for the condition being met (12) or not met (7).
Ignoring the CALL and RET instructions (which occur only once). the program takes:
(A) x (7+16 x MSCNT - 5 + 16) - 5
clock cycles. The -5's are caused by the fact that JR takes less time during the final
Iteration when the condition is not met.
So. to make the delay 1 ms.
13 + 16 x MSCNT = Nc
where Nc IS the number of clock cycles per millisecond. At the
standard 4 MHz Z80 clock rate. Nc = 4000. so:
16 x MSCNT = 3987
IMSCNT - 249 (hex F9) at a Z80 clock rate of 4 MHzl
11-10
zao DELAY
LOOP
CONS'rANT
(PIO)
The key element in most Z80 Input/output sections IS the Z80 Parallel Input/Output Circuit or PIO. This device combines latches, buffers, flip-flops, and other logic circuits
needed for handshaking and other simple interfacing techniques. The PIO contains
many logic connections, certain sets of which can be selected according to the contents of programmable registers. Thus, the deSigner has the eqUivalent of a Circuit
DeSigner's Casebook under his control. The initialization phase of the program places
the appropriate values into registers to select the required logic connections. An input/output section based on PIOs can handle many different applications, and changes
or corrections can be made in software rather than by rewIring.
Figure 11-7 is the block diagram of a PIO. The deVice contains two nearly identical 8-blt
ports - A, which IS usually an input port. and B, which IS usually an output port. Each
port (see Figure 11-8) contains:
An 8-bit Data Output register
PIO REGISTERS
AND
An 8-bit Data Input register
CONTROL LINES
A 2-bit Mode Control register, which indicates whether the
port IS In an output. input. bidirectional. or control mode
An 8-bit Input/Output Control register, which determines whether the corresponding
data pins are inputs (1) or outputs (0) in the control mode
Two control lines (STB and ROY) that are configured by the Mods Control register.
These lines can be used for the handshaking signals shown in Figures 11-5 and 11-6.
A 2-bit Mask Control register (used only in the control mode) that determines the active polarity of the inputs and whether they will be logically ORed or ANDed to form
an interrupt signal
An 8-bit Mask register (used only in the control model that determines which port
lines will be monitored to form the interrupt signal
An 8-bit Vector Address register used with the interrupt system
For now, we will be concerned only with the Mode Control registers, the Input/Output
Control registers, and the control lines. We will discuss the interrupt-related features of
the PIO in Chapter 12.
The meanings of the bits In the various control and mask registers are related to the underlying hardware and are entirely arbitrary as far as the assembly language programmer is concerned. You must either memorize them or look them up In this chapter and
in Chapter 12.
Each PIO occupies four input port addresses and four output port
addresses. The B/A SEL (Port B or A select) and C/O SEL (Control
or Data Select) lines choose one of the four ports as described in
Table 11-1. Most often, designers attach address bit AO to the B/A SEL input and address bit A1 to the C/O SEL input. The PIO then occupies four consecutive port addresses as described in the last column of Table 11-1.
Clearlv there are far more internal control registers than there are port addresses for
them, In fact. all the control registers for each port occupy one address according to the
C/O SEL connection. So some of the data bits sent to a control register are actually used
for addressing purposes. Note the following situations (see Table 11-2):
DO = 0 means that the remaining data bits are loaded into the Interrupt Vector register.
11-11
<I>
+5V GND
~~~
Penpheral
Interface
~
- , :.-
Intemal
Control
CPU
~
Data
I/O
L_
Internal
8us
CPU
......-
"
}HandShake
,_.
6
J
--
'---
8us
I/O
Control
Port A
r-V
8us
Pia
Control
,............,
Logic
Interface
Data or
Data or
Control
lines
Port 8
Interrupt
Control
Interrupt
Control lines
11-12
I/O
~
} Handshake
Mode
Control Reg
(2 Bits!
Input/Output
Select Reg
(8 Bitsl
)?
Intemal8us
L--
.AMask
Control
-"
Reg
"V
12 Bitsl
Reg
(8 Bits!
Data Input
Input
(2 Bitsl
Data
I--
Penpheral
..,) Data or
Control Bus
Mesk
Reg
8-Bit
...
Data Output
Reg
(8 Bits!
<::-.
READ\
Handshake
Control
Logic
11-13
Handshake
}
14--
Lines
PORT B OR A
SELECT
REGISTER
ADDRESSED
PORT ADDRESS
(STARTING WITH PIOADD)
0
0
1
1
0
1
Data Register A
Data Register B
Control A
PIOADD
PIOADD+1
PIOADD+2
PIOADD+3
0
1
Control B
The port addresses assume that C/D SEL IS tied to Al and B/A SEL to AO.
ADDRESSING
MODE CONTROL
INPUT/OUTPUT CONTROL
D3 = D2 = D1 = DO = 1
NEXT WORD AFTER MODE CONTROL
SETS MODE 3
D3 = 0, D2 = 01 = DO = 1
NEXT WORD AFTER MASK CONTROL
REGISTER ACCESSED WITH D4 = 1
INTERRUPT ENABLE
D3=02=0, D1 =DO=1
INTERRUPT VECTOR
00= 1
03 = 0, D2 = D1 = DO = 1 means that the remaining data bits are loaded into the Mask
Control register. If D4 = 1, the next control word is loaded into the Interrupt Mask
register. Interrupts can be enabled or disabled with D3 = 02 = 0, D1 = DO = 1.
D3 = D2 = D1 = DO = 1 means that the remaining data bits are loaded into the Mode
Control register. If 07 = 06 1 (control model. the next control word is loaded into the
Input/Output Control register.
2)
The programmer must be very careful of the order of operations. The meaning of a
particular Output instruction depends on the sequence in which it occurs.
The programmer should document the PIO configuration in detail. The device is
complex, and a reader is unlikely to be able to make much sense out of the sequence of operations that configures it.
We should note that one usually configures the control registers of the PIO just once in
the initialization phase of the program. The rest of the program then uses only the PIO
data registers.
11-14
(Pi'O'\
The mode of operation of a PIO is established by writing a control
word to the PIO in the form shown in Figure 11-1. Table 11-3 de~
scribes the meanings of the various modes and the control words
required to establish them. Note that bits 05 and 04 are not used. When power IS
turned on. the PIO comes up in mode 1 (input).
We may summarize the modes as follows:
1)
Mode 0 -
PIO
OUTPUT
OUTPUT
Writing data Into the port Output register latches the data and
MODE
causes it to appear on the port Data Bus. The READY (ROY)
line goes high to indicate Data Ready: it remains high until the
peripheral sends a rising edge on the STROBE (STB) line to indicate Data Accepted
or Device Ready. The rising edge of STB causes an interrupt if the Interrupt has
been enabled.
2)
Mode 1 -INPUT
PIO
INPUT
The peripheral latches data into the port Input register using
MODE
the STROBE signal. The rising edge of STB causes an Interrupt
(if enabled) and deactivates ROY. When the CPU reads the
data. ROY goes high to indicate Data Accepted or Input Register Empty. Note that
the peripheral can strobe data into the register regardless of the state of ROY. The
programmer must thus handle the problem of overrun. Le.. new data being placed
Into the register before the old data IS read.
3)
Mode 2 - BIDIRECTIONAL
PIO
BIDIRECTIONAL
This mode uses all four handshake lines. so It is allowed
MODE
only on Port A. The Port A ROY and STB signals are used
for output control and the Port B ROY and STB signals are
used for Input control. The only difference between this mode and a combination of
modes 0 and 1 is that data from the Port A Output register IS enabled onto the port
Data Bus only when A STB is active. This allows the Port A bus to be used bidirectionally under the control of A STB (Output Data Request) and B STB (Input Data
Available). Note that the B side control signals are governed bY Input Register A in
this mode.
4)
Mode 3 - CONTROL
This mode does not use the ROY and STB signals. It is intended for status and control applications in which each
bit has an individual meaning. When mode 3 IS selected.
the next control word sent to the PIO defines the directions
of the port data bits (Figure 11-9l. A '1' in a bit position
makes the corresponding bus line an input. while a '0'
makes it an output.
11-15
PIO
CONTROL
MODE
PIO
DIRECTIONS IN
CONTROL MODE
Set Mode
Ml
MO
Mode
Output
Input
1
1
0
1
Bidirectional
Bit Control
~
I/O = 1 Sets bit to Input
I/O = 0 Sets bit to Output
PIO Mode
Meening
.Control Word
(Binaryl (Hexl
Output
00001111
Input
Bidirectional
01001111
Control
11001111
2
3
10001111
OF
4F
BF
CF
2)
3)
4)
5)
6)
FEATURES OF
PIO MODES
In modes 0.1, and 2 the peripheral indicates Data Ready.
DeVice Ready. or Data Accepted with a rising edge on the
STB line. This edge also causes an interrupt if the Interrupt IS enabled.
In modes 0.1. and 2 the PIO indicates Data Ready. Input Buffer Empty. or Data Accepted by sending ROY high. This signal remains high until the next rising edge on
STB.
Only Port A can be used bidirectionally. If Port A IS In mode 2 (bidirectional), Port B
can only be in mode 3 (cantrall since no handshake lines are available.
The control mode (3) IS the only mode In which the Input/Output Control register IS
used. Otherwise. the entire port IS used for either Input or output.
There is no way for the processor to determine if a pulse has occurred on STB if interrupts are not being used. The PIO is designed for use In interrupt-driven rather
than polling systems (see Chapter 12). STB should be tied low if it is not being
used.
The processor cannot directly control the ROY lines. The ROY line on a port goes
!:!.!@1 when data is transferred to or from the port and goes Iowan the rising edge of
STB.
7)
The contents of the data Output register can be read if the port is in the output or
bidirectional mode. If the port IS In the control mode. the output register data from
the lines assigned as outputs can be read. The contents of control registers cannot
be read.
S)
If the ROY output IS tied to the STB input on a port in the output mode. ROY will go
high for one clock period after each output operation. This brief pulse can be used
to multiplex displays as shown In Figure 11-1.
11-16
2)
STEPS IN
Establish the mode of operation by writing the apCONFIGURING A
propriate control words to the Mode Control register.
PIO
Interrupt control as well as I/O mode Information may
have to be sent.
If In mode 3. establish the directions of the I/O pins by writing a control word to the
Input/Output Control register. This word must follow the control word that selected
mode 3.
2)
3)
OUTPUT PORT
LD
A.00001111 B
OUT
(PIOCRBl.A
INPUT PORT
LD
OUT
A.01 001111 B
(PIOCRAl.A
BIDIRECTIONAL PORT
LD
A.10001111 B
OUT
(PIOCRAl.A
Remember that only Port A can be bidirectional and that Port B must then be a control
port.
4)
5)
6)
11-17
2)
3)
4)
Instructions that use absolute addressing. IN A. (portl and OUT (portlA transfer
eight bits of data between the Accumulator and the port addressed by the second
byte of the instruction.
Single-byte instructions that use register Indirect addressing. IN reg. (C) and OUT
(Cl,reg transfer eight bits of data between the specified register and the port addressed by Register C.
Block I/O instructions. INI and OUTI transfer eight bits of data between the memory
location addressed by Register Pair HL and the port addressed by Register C. Both
instructions then increment Register Pair HL and decrement the byte counter in
Register B. The Z flag IS set if B IS decremented to zero and reset otherwise. INO and
OUTO are the same instructions except that they decrement Register Pair HL instead of Incrementing it.
Repeated.Block I/O instructions. INIR and OTIR repeat the effects of INI and OUTI.
respectively. until B IS decremented to zero. INOR and OTOR have the same relationship to INO and OUTO.
2)
1':1:":/0::::------.,
~~~R~~~~L~~
ADDRESSING
1/0
INSTRUCTIONS
WITH INDIRECT
ADDRESSING
IN reg.(C) sets the Sign (S). Zero (Zl. and Parity (P/O)
flags according to the value of the Input data. The Carry flag (C) is not modified.
but the Half Carry (H) and Negative (N) flags are reset. OUT (Cl,reg does not
affect any flags.
11-18
3)
BLOCK I/O
2)
3)
C.2
HLAOH
11-19
I/O
INSTRUCTION
EXAMPLES
4)
c.
Using block
110
LD
LD
OUTI
5)
C.5
HLAOH
INBYTE:
C.
LD
LD
LD
INI
JR
HLAOH
B.8
C,2
NZ,INBYTE
HL,40H
lD
B,8
LD
INIR
C,2
1120
6)
Send the contents of memory locations 0040 through 0047 to Output Port 5.
a. USing absolute addressing
LD
HL40H
:GET STARTING ADDRESS OF DATA
LD
B.8
:GET BYTE COUNTER
OTBYTE: LD
A.(HL)
:FETCH BYTE FROM MEMORY
OUT
(51.A
:OUTPUT BYTE
INC
HL
DJNZ
OTBYTE
b. Using block I/O
LD
HLAOH
:GET STARTING ADDRESS OF DATA
B.8
:GET BYTE COUNTER
LD
LD
C.5
:GET PORT NUMBER
OTBYTE: OUTI
:OUTPUT BYTE FROM MEMORY
NZ.OTBYTE
JR
C.
11-21
+5V
0
.1>
'1>
To CPU
"-
PIO
r
Pushbutton
-1
()
EXAMPLES
A Pushbutton Switch
Purpose: To Interface a single pushbutton switch (or a single-pole. single-throw (SPST)
sWitch) to a Z80 microprocessor. The pushbutton IS a mechanical switch that
provides a single contact closure (i.e.. a logic zero) while pressed.
Circuit Diagram:
Figure
a Z80
closed
pullup
11-10 shows the circuitry required to interface the pushbutton. It uses one bit of
PIO that acts as a buffer: no latch is needed. since the pushbutton remains
for many CPU clock cycles. Pressing the button grounds the PIO Input bit The
resistor ensures that the input bit is one if the button IS not being pressed.
Programming Examples:
We will perform two tasks With this circuit They are:
a)
b)
2)
= (0040) = 00
11-22
Flowchart:
(0040) = 0
data
(00401 = 1
Source Program:
LD
OUT
LD
LD
IN
AND
JR
DONE:
INC
HALT
A.Ol00llllB
(PIOCRAl.A
HL.40H
(HU,O
:MARKER =0
A.(PIODRA)
MASK
Z,DONE
(HU
11-23
Object Program:
Memory Location
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
0000
OOOE
OOOF
0010
3E
4F
03
PIOCRA
21
40
00
36
00
DB
PIODRA
E6
MASK
28
01
34
76
Instruction
(Mnemonic)
LD
A.Ol00llllB
OUT
(PIOCRA).A
LD
HL,40H
LD
(HU,O
IN
A.(PIODRA)
AND
MASK
JR
Z,DONE
INC
HALT
(HU
The port addresses PIOCRA and PIODRA depend on how the PIO is connected in your
microcomputer. The PIO control lines are not used In this example. In fact. we could
place the A side of the PIO In the control mode With the starting sequence:
LD
OUT
LD
OUT
A.ll00l111B
(PIOCRA).A
A.OFFH
(PIOCRA).A
:MAKEPORT A CONTROL
:ALL BITS INPUTS
IS
Mask
Button Position
(Bit Number)
Binary
Hex
0
1
2
3
4
5
6
7
00000001
00000010
00000100
00001000
00010000
00100000
01000000
10000000
01
02
04
08
10
20
40
80
If the button is attached to bit 0 or bit 7 of the Input port. the program can use a Shift
instruction to set the Carry and thereby determine the button's state. For example.
Bit 7
IN
RLA
JR
Bit 0
IN
RRA
JR
A.(PIODRA)
NC.DONE
A,(PIODRA}
NC,DONE
11-24
The procedure for bit 7 is even simpler if we have the address of the PIO data register in
Register C. This is because the Input instructions using register indirect addressing
(e.g.. IN A,(C)) affect the Sign flag. The required sequence is:
..!.!l. (PIODRA
IN
JP
in Register C)
A,(C)
:READ BUTTON POSITION
P,DONE
:DONE IF BUTTON CLOSED (ZERO)
If the button is attached to bits 6 or 7 of the input port. the program can use the Sign bit
to determine the button's state. For example.
Bit 7
IN
AND
JP
A, (PIODRAl
A
P.DONE
IN A,(port} does not affect the flags: therefore, we must use the AND A instruction to
set the flags without changing the Accumulator.
Bit 6
IN
ADD
JP
A,(PIODRA)
A.A
P.DONE
RLA cannot be used because it does not affect the Sign bit.
11-25
DEBOUNCING
The program can debounce the pushbutton by waiting after it
finds a closure. The reqUired delay IS called the debouncing
IN SOFTWARE
time and IS part of the specifications of the pushbutton. It IS
tYPically a few milliseconds long. The program should not examine the pushbutton durIng this period because It might mistake the bounces for new closures. The program
may either enter a delay routine like the one described previously or may simply perform other tasks for the specified amount of time.
Even after debouncing. the program must still wait for the present closure to end before
looking for a new closure. ThiS procedure avoids double counting. The following program uses a software delay of 1 ms to debounce the pushbutton. You may want to try
varYing the delay or eliminating It entirely to see what happens. To run thiS program.
you must also enter the delay subroutine into memory starting at location 0030.
Flowchart:
Count =0
No
Count
= Count
+ 1
Debounce button
with 1 ms walt
11-26
Source Program:
CHKCL:
CHKOP:
LD
OUT
LD
LD
IN
AND
JR
INC
CALL
IN
AND
JR
JR
A.Ol00llll B
(PIOCRAl.A
HLAOH
(HL).O
A. (PIODRA)
MASK
NZ.CHKCL
(HL)
DELAY
A.(PIODRA)
MASK
Z.CHKOP
CHKCL
Object Program:
Memory Location
(Hex)
Memorv Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
DODD
OOOE
OOOF
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
001A
3E
4F
D3
PIOCRA
21
40
00
36
00
DB
PIODRA
E6
MASK
20
FA
34
CD
30
00
DB
PIODRA
E6
MASK
28
FA
18
EE
Instruction
(Mnemonic)
CHKCL:
CHKOP;
LD
A.Ol00lll1B
OUT
(PIOCRAl.A
LD
HLAOH
LD
(HL).O
IN
A.(PIODRA)
AND
MASK
JR
NZ.CHKCL
INC
CALL
(HU
DELAY
IN
A.(PIODRA)
AND
MASK
JR
Z.CHKOP
JR
CHKCL
The three instructions beginning with the label CHKOP are used to determine when the
sWitch reopens.
Clearly we do not really need a PIO for this simple interface. An addressable tn-state
buffer wou Id do the Job at far lower cost.
11-27
A Toggle Switch
Purpose: To interface a single-pole. double-throw (SPOT) toggle switch to a Z80
microprocessor. The toggle IS a mechanical device that is either in the normally closed (NC) position or the normally open (NO) position.
Circuit Diagram:
DEBOUNCING
Figure 11-11 shows the circuitry required to Interface the
WITH
switch. Like the pushbutton. the switch uses one bit of a Z80
PIO that serves as an addressable buffer. Unlike the button. the
CROSSCOUPLED
NAND GATES
switch may be left in either position. Typical program tasks are
to determine the switch position and to see if the position has
changed. Either a one-shot with a pulse length of a few milliseconds or a pair of crosscoupled NAND gates (see Figure 11-12) can debounce a mechanical switch.
The circuits will produce a single step or pulse In response to a change in switch position even if the switch bounces before settling into its new position.
Programming Examples:
We will perform two tasks involving this circuit. They are:
1)
2)
Flowchart:
lOO4OJ = 0
(0040J
11-28
+5V
To CPU
Debounce
SWitch
Pia
Circuit
NO
+5V
SWItch
11-29
Source Program:
WAITC:
LD
OUT
LD
LD
IN
AND
JR
INC
HALT
A.01001111B
(PIOCRA),A
HLAOH
(HU.O
A.(PIOORA)
MASK
NZ.WAITC
(HU
Object Program:
Memorv Location
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
0000
OOOE
OOOF
0010
Instruction
(Mnemonic)
Memorv Contents
(Hex)
3E
4F
03
PIOCRA
21
40
00
36
00
DB
PIODRA
E6
MASK
20
FA
34
76
11-30
WAITC:
LD
A.01001111B
OUT
(PIOCRA),A
LD
HLAOH
LD
(HU,O
IN
A.(PIODRA)
AND
MASK
JR
NZ,WAITC
INC
HALT
(HU
(401 = 0
Old data =
switch position
New data =
switch position
(401 = 1
Source Program:
SRCH:
LD
OUT
LD
LD
IN
AND
LD
IN
AND
CP
JR
INC
HALT
A.Ol00llllB
(PIOCRAl.A
HL,40H
(HL).O
A.(PIODRA)
MASK
B.A
A, (PIODRA)
MASK
B
Z.SRCH
(HL)
:MARKER = ZERO
:GET OLD SWITCH POSITION
11-31
Object Program:
Memory Location
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
0008
OOOC
0000
OOOE
OOOF
0010
0011
0012
0013
0014
4F
03
PIOCRA
21
40
00
08
PIODRA
E6
MASK
47
08
PIOORA
E6
MASK
88
28
F9
34
76
Instruction
(Mnemonic!
3E
SRCH:
LD
A.01001111B
OUT
(PIOCRAl.A
LD
HL,40H
IN
A.(PIODRA)
AND
MASK
LD
IN
8.A
A.(PIOORA)
AND
MASK
CP
JR
8
Z.SRCH
INC
HALT
(HL)
A Subtract or Exclusive OR could replace the Compare in the program. Either of these
Instructions would. however. change the contents of the Accumulator. The Exclusive
OR would be useful if several SWitches were attached to the same Pia. since it would
produce a one bit for each switch that changed state. How would you rewrite thiS program so as to debounce the switch in software?
11-32
7
S
S
4
..
3
2
1
A7
AS
AS
A4
A3
A2
A1
AO
To CPU
PIO
Common
Figure 11-13 shows the circuitry required to Interface an 8-posltlon sWitch. The sWitch
uses all eight data bits of one side of a PIG. TYPical tasks are to determine the position
of the sWitch and to check whether or not that position has changed. Two special situations must be handled:
1)
2)
The first of these situations can be handled by waiting until the input is not all ones. I.e..
until a switch lead is grounded. We can handle the second situation by examining the
switch again after a delay (such as 1 or 2 seconds) and only accepting the Input when
It remains the same. This delay will not affect the responsiveness of the system to the
switch. We can also use another SWitch (i.e.. a Load sWitch) to tell the processor when
the selector sWitch should be read.
Programming Examples:
We will perform two tasks involVing the circuit of Figure 11-13. These are:
a)
b)
Monitor the switch until it is In a definite position. then determine the posItion and
store its binary value in a memory location.
Walt for the position of the SWitch to change. then store the new position in a
memory location.
If the SWitch IS in a position. the lead from that position IS grounded through the common line. Pullup resistors on the input lines aVOid problems caused by noise.
11-33
SWitch POSition
0
1
2
3
4
5
6
7
Binary
Hex
11111110
11111101
11111011
11110111
11101111
11011111
10111111
01111111
FE
FD
FB
F7
EF
DF
BF
7F
Purpose: The program waits for the sWitch to be In a specific position and then places
the number of that posItion Into memory location 0040.
Table 11-3 contains the data Inputs corresponding to the various switch positions.
This scheme IS inefficient. since It requires eight bits to distinguish among eight
different posItIOns.
A TTL or MOS encoder could reduce the number of bits needed.
Figure 11-14 shows a cirCUit uSing the 74LS148 TTL 8-to-3 encoder 4 We attach the sWitch outputs in Inverse order. since the
74LS148 deVice has active-low inputs and outputs. The output of
the encoder cirCUit IS a 3-blt representation of the sWitch position. Many
clude encoders so that their outputs are coded. usually as a BCD digit
logic!.
USING
A TTL
ENCODER
switches in(in negative
The encoder produces active-low outputs. so. for example. switch position 5. which IS
attached to Input 2. produces an output of 2 in negative logic (or 5 In posItive logic!.
You may want to verify the double negative for yourself.
01
t
iQ
6
5
4
1,
12
13
3
2
'4
'is5
74LS146
8-10-3
~,
Encoder
00
17
::
A2
A,
To CPU
PIO
Ao
Common
"::?'
Figure 11-14. A Multiple-POSItIOn Switch with an Encoder
11-34
Flowchart:
Data
SWltch position
Position~O
Shift data
nght 1 bit
Yes
Position
=
(0040)
Positkln + 1
PositIon
Source Program:
LD
OUT
CHKSW: IN
CP
JR
LD
CHPOS. RRA
JR
INC
JR
DONE:
LD
LD
HALT
A,01001111B
(PIOCRAl.A
A,(PIODRA)
OFFH
Z,CHKSW
B,O
NC,DONE
B
CHPOS
HLAOH
(HL),B
11-35
Object Program:
Memorv Location
(Hex)
Memorv Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
0000
OOOE
OOOF
0010
0011
0012
0013
0014
0015
0016
3E
4F
03
PIOCRA
DB
PIODRA
FE
FF
28
FA
06
00
1F
30
03
04
18
FA
21
40
00
70
76
Instruction
(Mnemonic)
LD
A.01001111B
OUT
(PIOCRA).A
CHKSW: IN
CHPOS:
DONE:
A,(PIODRA)
CP
OFFH
JR
Z.CHKSW
LD
B.O
RRA
JR
NC.DONE
INC
JR
B
CHPOS
LD
HLAOH
LD
HALT
(HU.B
Suppose that a faulty switch or defective PIO results in the input always being OFF16.
How could you change the program so that it would detect this error?
There IS an unconditional jump. JR CHPOS. In the source program. Can vou change the
Initial conditions so as to make this Instruction unnecessary?
This example assumes that the sWitch is debounced in hardware. How would you
change the program to debounce the sWitch in software?
11-36
Flowchart:
Old data
Position
SWitch posi~ion
= -1
New data
Position
=
Position + 1
SWitch position
(0040)
11-37
= PositIon
Source Program:
CHFST
LD
OUT
IN
CP
JR
CHSEC:
LD
IN
CP
JR
CP
JR
CHPOS.
LD
INC
RRA
JR
LD
LD
HALT
A,01001111B
(PIOCRA),A
A,(PIODRA)
OFFH
Z,CHFST
B,A
A,(PIODRA}
OFFH
Z.CHSEC
B
Z,CHSEC
B.OFFH
B
C,CHPOS
HL40H
(HLLB
11-38
Object Program:
Memorv Location
(Hex)
Memorv Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
DODD
OOOE
OOOF
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
001A
001B
001C
001D
DOlE
3E
4F
D3
PIOCRA
DB
PIODRA
FE
FF
28
FA
47
DB
PIODRA
FE
FF
28
FA
B8
28
F7
06
FF
04
IF
38
FC
21
40
00
70
76
Instruction
(Mnemonic)
CHFST
CHSEC:
CHPOS;
LD
A.Ol00llll B
OUT
(PIOCRAl.A
IN
A,(PIODRA)
CP
OFFH
JR
Z,CHFST
LD
IN
B.A
A,(PIODRA)
CP
OFFH
JR
Z,CHSEC
CP
JR
B
Z,CHSEC
LD
B.OFFH
INC
RRA
JR
B
C,CHPOS
LD
HLAOH
LD
HALT
(HU.B
A,(PIODRA)
A
Z,CHKSW
11-39
A Single LED
zao
Send a LogiC One to the LED (turn a pOSitive display on or a negative display off).
Source Program:
A.00001111 B
(PIOCRBl.A
A.MASKP
(PIODRB),A
A, (PIODRB)
LED,A
(PIODRBl.A
MASKP has a one bit In the LED position and zeros elsewhere. Note that we can read
the PIO Data Output register when the PIO IS In the output mode. We can also read any
combination of input data and output register data when the PIO is in the control
mode: the combination is defined by the assignment of Inputs and outputs.
11-40
A_nO_d-1e~lde
+svO~------------''VV'w
a)
BaSIC LED CircUItry, The resistor R should limit the maximum current to 50 rnA and
From CPU
R
Dnver
PIO
hI
Interfacing an LED with positive logic. A iOglC T from the CPU turns the LED on.
~R
From CPU
PIO
cl
sv
Dnver
Interfacing an LED with negatlve logic. A logic '0' from the CPU turns the LEO on. The driver or the CPU
may Invert the logic levels.
11-41
Object Program:
Memory Location
(Hex)
Memory Contents
(Hex)
Instruction
(Mnemonic)
3E
OF
03
PIOCRB
3E
MASKP
D3
PIODRB
76
LO
A.0000ll11 B
OUT
(PIOCRBl.A
LD
A.MASKP
OUT
(PIOORBl.A
HALT
(update data)
0009
OOOA
OOOB
OOOC
OOOD
OOOE
OOOF
B.
OB
PIOORB
CB
LEO
D3
PIOORB
76
IN
A.(PIODRB)
SET
LEO.A
OUT
(PIOORB).A
HALT
Send a Logic Zero to the LEO !turn a positive display off or a negative displav on),
The differences are that MASKP must be replaced by its logical complement
MASKN and SET LEO.A must be replaced by RES LEO A Note that the second byte
of the object code for SET LED.A and RES LED.A depends on the actual bit position
to which the name LEO refers.
MASKN has a zero bit
In
11-42
B6
BS
f
e
B4
From. CPU
.J\.
PIO
-y
B3
Dnvers
Display
B2
B,
BO
Common
__1__
!CommonCathode I
l6+sv
(CommonAnode)
IS
9
e
11-43
a)
Common-cathode
go----..It/'I/Ir---tiIDlIt-----,
eo-----~"'"""--t.tt_----...,
d o-----"rw--~ti-----...
...
b o-----J\rw--~ti-----
oo------'\iM~--ti.H_----_...
-b)
Common-anode
+5V
go-----"M---ttllBI-----4j9
o------I\il\J\l---ti.H_------f
do-----J\rw--tfIIIltt-----...
...IH----__f
co----'tN~-
bo----....l\M---HllIli-------1
oo-----..I'/i~---tt.tt_------I
11-44
Hexadecimal Rep.resentation
Common-anode
Common-cathode
3F
06
5B
4F
66
6D
7D
07
7F
67
40
79
24
30
19
12
02
78
00
18
Bit 7 is always zero and the others are g. f. e. d. c. b. and a in decreasing order of
slgnifi cance.
Note: The seven-segment display IS widely used because it
SEVEN-SEGMENT
contains the smallest number of separately controlled segREPRESENTATIONS
ments that can provide recognizable representations of all
the decimal digits (see Figure 11-18 and Table 11-4). Seven-segment displays can also
produce some letters and other characters (see Table 11-5). Better representations reqUire a substantially larger number of segments and more circuitry. 5 Since seven-segment displays are so popular. low-cost seven-segment decoder/drivers have become
widelY available. The most popular devices are the 7447 common-anode driver and the
7448 common-cathode dnver6 : these devices have Lamp Test Inputs (that turn all the
segments on) and blanking inputs and outputs (for blanking leading or trailing zeros).
11-45
0: Segments f. e, d. c, b. a
3: Segments g. d. c, b. a Q!J
1: Segments
C,
4: Segments 9. f,
C,
b 2!:1
5: Segments 9, f, d, C, a 2!)
2: Segments 9, e, d, b, a 2!)
I,
11-46
6: Segments g, f, e, d. c. a
e!:'
8: Segments g. f. e, d. c, b. a
11-47
Hexadecimal Representation
Common-cathode
Common-anode
77
C
E
F
H
I
39
79
71
76
06
1E
38
3F
73
3E
66
08
46
06
OE
09
79
61
47
40
OC
41
19
J
L
0
p
U
a
r
u
7
Hexadecimal Representation
Common-cathode
Common-anode
7C
58
5E
74
54
5C
50
1C
40
53
03
27
21
OB
2B
23
2F
63
3F
2C
11-48
(0040) = 05
b.
Result IS 5 on display
(0040) = 66
Result is a blank display
Flowchart:
Code
Data
Blank
(0040)
Code = (SSEG +
Dala)
Send code
to display
11-49
Source Program:
LD
OUT
lD
lD
CP
JR
LD
DSPlY:
LD
LD
ADD
LD
lD
OUT
HALT
A.00001111 B
(PIOCRBl.A
B,BlANK
A,(40H)
10
NC,DSPlY
DE,SSEG
H,O
l,A
HL.DE
B,(HL)
AB
(PIODRBl.A
BLANK is 00 for a common-cathode display, FF for a common-anode display, An alternative procedure would be to put the blank code at the end of the table and replace all
Improper data values with 10, I.e..
lD
CP
JR
CNVRT:
LD
LD
A,(40Hl
10
C,CNVRT
A,10
DE,SSEG
;GET DATA
;IS DATA A DECIMAL DIGIT?
:YES, CONVERT DIRECTLY TO SEVEN-SEGMENT
:NO, GET INDEX FOR BLANK CODE
:GET BASE ADDRESS OF SEVEN-SEGMENT TABLE
11-50
Object Program:
Memory Location
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
OOOD
OOOE
OOOF
0010
0011
0012
0013
0014
0015
0016
0017
0018
0020-0029
3E
OF
D3
PIOCRB
06
BLANK
3A
40
00
FE
OA
30
08
11
20
00
26
00
6F
19
46
78
D3
PIODRB
76
Instruction
(Mnemonic!
DSPLY'
SSEG:
LD
A.OOOOllll B
OUT
(PIOCRB),A
LD
B.BLANK
LD
A.(40H)
CP
10
JR
NC.DSPLY
LD
DE.SSEG
LD
H.O
LD
ADD
LD
LD
OUT
L.A
HL.DE
B.(HL)
A.B
(PIODRBl.A
HALT
(seven-segment code
table)
Several displays may be multiplexed. as shown In Figure 11-19. A brief strobe on the B
RDY line clocks the counter and directs data to the next display. Note that B RDY IS tied
directly back to B STB. Le.. the ready line essentially provides ItS own acknowledgment.
The timing of the PIO IS such that this connection results in a strobe with a duration of
one clock period. Such a brief strobe is exactly what the counter requires. RESET starts
the deCimal counter at nine so that the first output operation clears the counter and
directs data to the first display.
The follOWing program uses the delay routine to pulse each of ten common-cathode
displays for 1 ms.
11-51
From CPU
PIO
B ROY
Oock
Decade
Counter
Reset
4 to 10
Decoder/Dover
B
A
11-52
(0040)
66
(0041)
3F
(0042)
7F
(0043)
7F
(0044)
06
(0045)
5B
(0046)
07
(0047)
4F
(0048)
6D
(0049)
7D
Display reads 4088127356
Source Program:
DRUN:
DSPLY:
LD
OUT
LD
LD
LD
OUT1
CALL
JR
JR
A.00001111 B
(PIOCRB).A
HLAOH
B.10
C.PIODRB
DELAY
NZ.DSPLY
DRUN
Here we must select the PIO output mode. since the circuit uses the handshake signals.
Note that OUTI sends the data to the output port addressed by Register C. Increments
the address in Register Pair HL. and decrements the counter in Register B. We have
assumed that subroutine DELAY does not affect the Z flag so that it can be used afterwards for a conditional branch.
11-53
Object Program:
Memorv Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
0000
OOOE
OOOF
0010
0011
0012
0013
3E
OF
03
PIOCRB
21
40
00
06
OA
OE
PIOORB
ED
Instruction
(Mnemonic)
ORUN:
DSPLY:
A3
CO
30
00
20
F9
18
FO
11-54
LO
A.00001111 B
OUT
(PIOCRBl.A
LO
HLAOH
LD
B.10
LD
C.PIOORB
OUTI
CALL
DELAY
JR
NZ.OSPLY
JR
ORUN
PROBLEMS
1) An On-Off Pushbutton
Purpose: Each closure of the pushbutton complements (inverts) all the bits in memory
location 0040. The location Initially contains zero. The program should continuouslY examine the pushbutton and complement location 0040 with each
closure. You may wish to complement a display output port instead. so as to
make the resu Its easier to see.
Sample Case:
2)
Purpose: Debounce a mechanical switch by waiting until two readings. taken a debounce time apart. give the same result Assume that the debounce time (in
ms) is in memory location 0040 and place the SWitch position into memory
location 0041.
Sample Problem:
3)
Purpose: Another SWitch serves as a Load SWitch for a four-position unencoded rotarY
switch. The CPU walts for the Load switch to close (be zero). and then reads
the position of the rotarY SWitch. This procedure allows the operator to move
the rotary SWitch to ItS final position before the CPU tries to read It The program should place the pOSition of the rotarY SWitch into memory location
0040. Debounce the Load SWitch In software.
Sample Problem:
(0040) = 02
11-55
4)
Purpose: A set of eight sWitches should have their positions reflected In eight LEOs.
That is to say. if the switch is closed (zero). the LED should be on. otherwise
the LED should be off. Assume that the CPU output port is connected to the
cathodes of the LEOs.
Sample Problem:
SWITCH
SWITCH
SWITCH
SWITCH
SWITCH
SWITCH
SWITCH
SWITCH
CLOSED
OPEN
CLOSED
OPEN
OPEN
CLOSED
CLOSED
OPEN
ON
1
2
3
4
5
6
Result:
LED
LED
LED
LED
LED
LED
LED
LED
1 OFF
2 ON
3 OFF
4
OFF
5 ON
6 ON
7
OFF
How would you change the program so that a switch attached to bit 7 of Port A of PIO
#2 determines whether or not the displays are active (j.e., if the control switch is
closed. the displays attached to Port B reflect the switches attached to Port A: if the
control switch is open. the displays are always off)? A control switch is useful when the
displays may distract the operator. as In an airplane.
How would you change the program so as to make the control sWitch an on-off
pushbutton; that IS. each closure reverses the previous state of the displays? Assume
that the displays start In the active state and that the program examines and debounces
the pushbutton before sending data to the displays.
5)
Purpose: The program should count from 0 to 9 continuously on a seven-segment display. starting with zero.
Hint: Try different timing lengths for the displays and see What happens. When does
the count become visible? What happens if the display is blanked part of the time?
11-56
2)
3)
Because of their high data rates. you cannot handle these I/O devices casually. If the
processor does not provide the appropriate service. the system may miss Input data or
produce erroneous output data. You are therefore working under much more exacting
constraints than In dealing with simpler devices. Interrupts are a convenient method
for handling complex I/O devices. as we shall see in Chapter 12.
Peripherals such as keyboards. teletypewnters. cassettes.
SYNCHRONIZING
WITH I/O
and floppy disks produce their own Internal timing. These
DEVICES
devices provide streams of data. separated by specific timIng Intervals. The computer must synchronize the initial input or outPut operation with the penpheral clock and then provide the proper Interval
between subsequent operations. A Simple delay loop like the one shown previously can
produce the timing Interval. The synchronization may require one or more of the followIng procedures:
1)
Looking for a transition on a clock or strobe line provided by the penpheral for timIng purposes. A simple approach would be to tie the strobe to a PIO STB input and
look for a change in the Interrupt (INT) output. However. there is no way to directly
address the INT output (and thus determine ItS value) and no way to clear It other
than through an interrupt service routine. Thus. to use the PIO in a polling system.
one must make the strobe available at an Input port and latch It if necessary. If the
strobe must be latched. a circuit must also be provided to clear the latch as part of
the subsequent Input or output transfer.
2)
Finding the center of the time Interval during which the data is stable. We would
prefer to determine the value of the data at the center of the pulse rather than at
the edges. where the data may be changing. Finding the center requires a delay of
one-half of a transmission Interval (bit time) after the edge. Sampling the data at
the center also means that small timing errors have little effect on the accuracy of
the reception.
3)
Recognizing a special starting code. ThiS is easy if the code IS a single bit or if we
have some timing information. The procedure IS more complex if the code is long
and could start at any time. Shifting will be necessary to determine where the
transmitter is starting ItS bits. characters. or messages (this IS often called a search
for the correct "framing").
4)
Sampling the data several times. This reduces the probability of receiVing data incorrectly from nOIsy lines. MaJonty logic (such as best 3 out of 5 or 5 out of 8) can
be used to deCide on the actual data value.
Reception IS. of course. much more difficult than transmission. since the penpheral controls the receptIOn and the computer must Interpret timing information generated by
the penpheral. In transmiSSion. the computer provides the proper timing and formatting
for a specific penpheral.
Penpherals may require or provide other Information beSides
CONTROL
data and timing. We refer to other information transmitted by
AND STATUS
the computer as "control information". It may select modes of
INFORMATION
operatIOn. start or stop processes. clock registers. enable
buffers. choose formats or protocols. provide operator displays. count operations. or
11-57
Identify the type and prionty of the operation. We refer to other information transmitted
by the peripheral as "status information"; it may Indicate the mode of operation. the
readiness of devices. the presence of error conditions. the format of protocol in use, and
other states or conditions.
The computer handles control and status information just like data. This information
seldom changes, even though actual data may be transferred at a high rate. The control
or status Information may be single bits. digits. words, or multiple words. Often single
bits or short fields are combined and handled by a single input or output port.
Combining status and control information into bytes reduces the total number of I/O
port addresses required by the peripherals. However. the combination does mean that
Individual status Input bits must be separately interpreted and control output bits must
be separately determined. The procedures for Isolating status bits and setting or resetting control bits are as follows:
Separating Out Status Bits
SEPARATING
STATUS
INFORMATION
Step 1)
Step 2)
Step 3)
If the field IS a single bit. Step 2 IS unnecessary since we can test the bit With the BIT instruction. If the single bit is In the most significant. next to most significant. or least significant position. we can use shift logical (AND .A or OR A) instructions to determine its
value. Remember also that the Input instructions With register indirect addressing (e.g..
IN A.(C)) affect the Sign flag. These somewhat more accessible bit positions are often
reserved for the most frequently used status information. You Should try to write the reqUired Instruction sequences for the Z80 processor.
Step 3 is unnecessary if the field is a single bit. since the Zero flag will contain the complement of that bit after Step 2 (try It!). A Shift or Load instruction can replace Step 2 if
the field IS a single bit and occupies the least significant. most significant. or next to
most significant bit position. These POSitions are often reserved for the most frequently
used status information. You should try to wnte the required Instruction sequences for
the 6800 processor.
Setting and Clearing Control Bits
Step 1)
Step 2)
Step 3)
Step 4)
COMBINING
CONTROL
INFORMATION
Here again the procedure is simpler if the field IS a single bit and occupies a position at
the end of the word.
11-58
A 3-bit field In bit positions 2 through 4 of a PIO data register IS a scaling factor.
Place that factor Into the Accu mulator.
A.(PIQDR)
00011100B
The Accumulator contains a 2-blt field that must be placed into bit positions 3 and
4 of a PIO data register.
00011000B
B.A
A.(PIODR)
11100111B
B
(PIODR),A
11-59
6
8
10
10
12
14
16
3x3
4x4
4x6
5x5
6x6
6x8
8x8
16
24
25
36
48
64
EXAMPLES
An Unencoded Keyboard
Purpose: Recognize a key closure from an unencoded 3 x 3 keyboard and place the
number of the key that was pressed mto the Accumu lator.
Keyboards are Just collections of switches (see Figure 11-20). Small numbers of keys are
easiest to handle if each key is attached separately to a bit of an input port. Interfacing
the keyboard IS then the same as mterfacmg a set of sWitches.
Keyboards with more than eight keys require more than one input
port and therefore mu Itibyte operations. This IS partlcu larly
wasteful if the keys are logically separate. as in a calculator or terminal keyboard where the user will only strike one at a time. The number of mput lines
required may be reduced by connectmg the keys into a matrix. as shown in Figure
11-21. Now each key represents a potential connection between a row and a column.
The keyboard matrix requires n + m external lines. where n IS the number of rows and
m is the number of columns. This compares to n x m external lines if each key IS separate. Table 11-6 compares the number of keys required by typical configurations.
A program can determme which key has been pressed by usmg
KEYBOARD
the external lines from the matrix. The usual procedure is a
SCAN
"keYboard scan." We ground Row 0 and examine the column
lines. If any lines are grounded. a key in that row has been pressed. causmg a row-tocolumn connection. We can determine which key was pressed by determming which
column line is grounded: that is. which bit of the input port is zero. If no column line IS
grounded. we proceed to Row 1 and repeat the scan. Note that we can check to see if
any keys at all haye been pressed by grounding all the rows at once and examinmg the
columns.
The keyboard scan requires that the row lines be tied to an output port and the column
lines to an input port. Figure 11-22 shows the arrangement. The CPU can ground a particular row by placmg a zero m the appropriate bit of the output port and ones m the
other bits.
The CPU can determine the state of a particular column by examining the appropriate
bit of the input port.
11-60
_r
_r
_r
Kev 1
..L
Key 2
.1.
Kev3
..L
Each key IS a switch lust like a pushbutton and grounds an Input Qit if it is pressed.
...;-
Column 0
Column 1
Column 2
Row 0
Row 1
Row 2
Each key now serves to connect a row to a column. For Instance. kay 4 connects row 1 to column 1.
11-61
Column 0
Data Bus
(from CPU)
Column 1
Coiumn 2
00 t---If---+---4II------t--~IJ_--t--Row
0,1I---"---t---f1---+---4F---t-- Row
PIO
Output
Port
OzIl---.---t--......IIf---+---4If---t--Row 2
10
PIO
Input
Port
11-62
WAITING
FORA
KEY CLOSURE
1l
2)
3)
Flowchart:
Ground all
keyboard rows
Source Program:
WAITK,
LD
OUT
LD
OUT
SUB
OUT
IN
AND
CP
JR
A.01001111B
(PIOCRA).A
A00001111 B
(PIOCRB),A
A
(PIODRB).A
A,(PIODRA)
00000111B
00000111B
Z,WAITK
HALT
11-63
Object Program:
Memorv Location
(Hex)
Memorv Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
0000
OOOE
OOOF
0010
0011
0012
0013
3E
4F
03
PIOCRA
3E
OF
03
PIOCRB
97
03
PIODRB
DB
PIODRA
E6
07
FE
07
28
F8
76
Instruction
(Mnemonic)
WAITK:
LD
A.01001111B
OUT
(PIOCRAl.A
LD
A.00001111 B
OUT
(PIOCRBl.A
SUB
OUT
A
(PIODRBl.A
IN
A.(PIODRA)
AND
00000111B
CP
00000111B
JR
Z,WAITK
HALT
PIO Port B IS the keyboard output port and Port A IS the Input pon.
Masking off the column bits eliminates any problems that could be caused by the states
of the unused Input lines.
We could generalize the routine by naming the output and masking patterns:
ALLG
EQU
11111000B
OPEN
EQU
00000111 B
These names could then be used In the actual program: a different keyboard would require only a change In the definitions and are-assembly.
Of course. one port of a PIO IS all that IS really necessary for a 3 x 3 or 4 x 4 keyboard.
Try rewriting the program so that It uses only Port A. The PIO must be placed into the
control mode so that lines can be indiVidually selected as inputs or outputs.
11-64
Set key number to -1. counter to number of rows. and output pattern to all ones
except for a zero In bit O.
2)
3)
4)
5)
6)
7)
Ground a row by sending the output pattern to the keyboard output port.
Update the output. pattern by shifting the zero bit left one position.
Fetch the column inputs by reading the input port.
If any column inputs are zero. proceed to Step 8.
Add the number of columns to the key number to reach the next row.
Decrement counter. Go to Step 2 if any rows have not been scanned. otherwise to
Step 10.
Add 1 to key number. Shift columh inputs nght one bit.
If Carry = 1. return to Step 8.
8)
9)
10)
End of program.
Flowchart:
Ground row bv
output of
Scan Pattern
Kev Number =
Kev Number + 1
Shift column inputs
nght 1 bit
Kev Number =
Kev Number +
Number of Columns
Counter = Counter - 1
Ves
11-65
Source Program:
FROW:
FCOL:
DONE,
LD
OUT
LD
OUT
LD
LD
LD
LD
A.01001111B
(PIOCRA).A
A.00001111 B
(PIOCRB).A
B.3
C.PIODRB
D.3
E. 1111111 OB
LD
LD
OUT
RLC
IN
AND
CP
JR
LD
ADD
LD
DJNZ
INC
JR
INC
RRA
JR
HALT
H.00000111 B
L.OFFH
(C),E
E
A.(PIODRA)
H
H
NZ.FCOL
A.L
A.D
L.A
FROW
L
DONE
L
NC.FCOL
11-66
Object Program:
Memorv Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
0000
OOOE
OOOF
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
001A
001B
001C
0010
001E
001F
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
002A
3E
4F
03
PIOCRA
3E
OF
03
PIOCRB
06
03
OE
PIODRB
16
03
1E
FE
26
07
2E
FF
ED
59
CB
03
DB
PIODRA
Instruction
(Mnemonic)
FROW'
A4
BC
20
08
70
82
6F
10
F1
2C
18
04
2C
1F
30
FC
76
FCOL:
LD
A.01001111B
OUT
(PIOCRAl.A
LD
A.00001111 B
OUT
(PIOCRBl.A
LD
B.3
LD
C.PIODRB
LD
0.3
LD
E.11111110B
LD
H.00000111 B
LD
L.OFFH
OUT
(Cl.E
RLC
IN
A.(PIODRA)
AND
CP
JR
H
H
NZ.FCOL
LD
ADD
LD
DJNZ
A.L
A.D
L.A
FROW
INC
JR
L
DONE
INC
RRA
JR
L
NC.FCOL
HALT
Each time a row scan fails. we must add the number of columns to the key number so
as to move past the present row (try It on the keyboard In Figure 11-22).
11-67
What is the result of the program if no keys are being pressed? Note the extra INC L instruction so that the program differentiates between no keys pressed and the last key
being pressed. What IS the final value in the Accumulator for these two cases? Note
that the Zero flag could also be used to distinguish the case where no keys were
pressed. Can you explain how?
An alternative approach would be to use the PIG in Its control mode so that lines could
be changed from Inputs to outputs. The procedure would be:
1)
2)
3)
Use the row and column Inputs together to determine the key number from a table.
11-68
':'0
A7
Data Bus
(
to CPU
PIO
ASTB
B4
.;J
Kevboard Strobe
(---FL --u- )
or
An Encoded Keyboard
Purpose: Fetch data. when it IS available. from an encoded keyboard that provides a
strobe along with each data transfer.
An encoded keyboard provides a unique code for each key. It has Internal electronics
that perform the scanning and Identification procedure of the prevIous example. The
tradeoff IS between the simpler software required by the encoded keyboard and the
lower cost of the unencoded keyboard.
Encoded keyboards may use diode matrices. TTL encoders. or MaS encoders. The
codes may be ASCII. EBCDIC. or a custom code. PROMs are often part of the encoding
CirCUitry.
The encoded keyboard also prOVides a strobe With each data transfer. The strobe signals that a new closure has occurred. Figure 11-23 shows the interface between an encoded keyboard and the Z80 microprocessor. The rising edge of the strobe latches the
data Into the input port. We also tie the strobe to the B Side of the Pia so that the CPU
can determine when a rising edge has occurred. Of course, the B port of one Pia could
hold status Signals from up to eight ports. The software would then have to determine
which ports were active with a shifting and masking operation.
We have assumed In the program that the strobe signal is long enough for the CPU to
handle it in software. If it is not. the Signal will have to be latched and cleared (with
ROY) when the mput or output transfer occurs.
You may have to watch the polarity of the strobe. since the Pia always reacts to a rising
edge. An Inverter gate may be necessary.
11-69
Read
status port
Read
status port
Read
data port
The hardware must hold the control lines in a logic one state during reset to prevent the
accidental setting of status flags.
Source Program:
SRCHL:
SRCHH:
LD
OUT
LD
OUT
LD
OUT
IN
BIT
JR
IN
BIT
JR
IN
HALT
A,01001111B
(PIOCRAl,A
A, 11 001111 B
(PIOCRBl.A
A.OFFH
(PIOCRBl,A
A,(PIODRB)
STB.A
NZ,SRCHL
A(PIODRB)
STB.A
Z,SRCHH
A(PIODRA)
11-70
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
0000
OOOE
OOOF
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
001A
3E
4F
03
PIOCRA
3E
CF
03
PIOCRB
3E
FF
03
PIOCRB
DB
PIOORB
CB
STB
20
FA
DB
PIOORB
CB
STB
28
FA
DB
PIOORA
76
Instruction
(Mnemonic!
SRCHL.
SRCHH:
LO
A.01 001111 B
OUT
(PIOCRA),A
LO
A,11001111B
OUT
(PIOCRBl,A
LO
A.OFFH
OUT
(PIOCRBl,A
IN
A.(PIOORB)
BIT
STB.A
JR
NZ.SRCHL
IN
A.(PIOORB)
BIT
STB.A
JR
Z.SRCHH
IN
A.(PIOORA}
HALT
If the CPU repeats this routine. It will not fetch another character until the next rising
edge occurs on the strobe line. A continuing high level on the strobe line will be ignored.
STB depends on which bit of Port B IS used. Figure 11-23 shows bit 4 being used. but
bits O. 6. and 7 are. as usual. the easiest to examine. Try rewriting the program to use
the more accessible bit positions.
The second bvte of the Bit instructions depends on the value of STB but is not equal to
that value. For example. the second byte IS 4F16 if STB = 1. 5716 if STB = 2. etc.
11-71
A Digital-to-Analog Converter
Purpose: Send data to an 8-blt digltal-to-analog converter. which has an active-low
latch enable.
Oigital-to-analog converters produce the continuous signals required by solenoids.
relays. actuators. and other electrical and mechanical output devices. Typical converters consist of sWitches and resistor ladders with the appropriate resistance values 7
The user must generally provide a reference voltage and some other digital and analog
circuitry. although complete Units are becoming available at low cost.
Figure 11-24 describes the 8-bit Signetlcs NE5018 O/A converter. which contains an
on-chip 8-blt parallel data input latch. A low level on the LE (Latch Enable) input gates
the input data Into the latches. where it remains after LE goes high.
Figure 11-25 illustrates the Interfacing of the device to a Z80 microprocessor. Here the
A side of the Pia is used to generate the Latch Enable signal. The ROY line from the Pia
could be used In the mode where It is tied to the STS line to form a pulse lasting one
clock cycle. However. one clock cycle may not be long enough. since the NE5018 reqUires a 400 ns pulse. Furthermore. the polanty IS the opposite of that needed by the
NE5018.
Note that the Pia latches the output data. The data therefore remains stable during and
after the conversion. The converter typically requires only a few microseconds to produce an analog output. Thus. the converter latch could be left enabled if the port were
not used for any other purpose.
In applications where eight bits of resolution are not enough. 10- to 16-blt converters
can be used. Additional port logic IS required to pass all the data bits: some converters
provide part of this logiC.
The Pia here serves both as a parallel data port and as a serial control port. Of course. if
Port A is used for control. it could actually handle up to eight bits.
Task: Output to converter.
Purpose: Send data from memory location 0040 to the converter.
Flowchart:
} Data = 10040)
Sand data
to converter
Pulse
Latch Enable
11-72
Digital
LE
DB7
MSB
DOO
DBS
DB4
DBJ
DB2
DBl
GND
DBO
LSB
Latches and
Switch Drivers
j
VCC
VREF
Out
Sum
Node
R
>-e--oVOut
OAC Current
Output
I
VREF
Amp
Camp
Adj
o Analog
DAC Switches
--
GND
R
VREF
In
Bipolar
Offset
f"
VCC
Data Bus
from CPU
Analog
Pia
B7
BO
NE5018
D/A
Output
Converter
i:f
A4
Source Program:
LD
OUT
SUB
OUT
LD
OUT
LD
OUT
IN
RES
OUT
SET
OUT
HALT
A.11001111B
(PIOCRAl.A
(PIOCRAl.A
A.00001111 B
(PIOCRBl.A
A.(40H)
(PIODRB},A
A.(PIODRA)
4.A
(PIODRAl.A
4.A
(PIODRA).A
11-74
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
0000
OOOE
OOOF
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
001A
3E
CF
03
PIOCRA
97
03
PIOCRA
3E
OF
03
PIOCRB
3A
40
00
03
PIODRB
DB
PIODRA
CB
A7
03
PIODRA
CB
E7
03
PIODRA
76
Instruction
(Mnemonic)
LD
A.11001111B
OUT
(PIOCRAl.A
SUB
OUT
A
(PIOCRAl.A
LD
A.00001111 B
OUT
(PIOCRBl.A
LD
A.(40H)
OUT
(PIODRB).A
IN
A.(PIODRAl
RES
4.A
OUT
(PIODRAl.A
SET
4.A
OUT
(PIODRAl.A
HALT
The particular bit that must be set and reset depends. of course. on how the Latch Enable IS connected to the control port. Bit is often convenient to use for control purposes
since. if that bit is orlginallv cleared. It can be set with an INC Instruction and reset with
a DEC instruction.
We could use the automatic brief strobe from BACK if the Latch Enable were actlvehigh (and if this strobe were long enough when BACK IS tied back to B STB). The program would then be:
LD
OUT
LD
OUT
HALT
A.00001111 B
(PIOCRB).A
A.(40H)
(PIODRB).A
An Inverter gate could produce an active-low signal. Note how many fewer instructions
are necessary.
11-75
Analog-to-Dlgltal Converter
Purpose: Fetch data from an 8-bit analog-ta-digital converter that requires an Initiate
Conversion pulse to start the conversion process and has a Data Valid line to
indicate the completion of the process and the availability of valid data.
Analog-ta-digital converters handle the continuous signals produced by vanous types
of sensors and transducers. 8 The converter produces the digital input which the computer requires.
One form of analog-to-digital converter IS the successive approximation device. which
makes a direct 1-bit companson during each clock cycle. Such converters are fast but
have little nOise Immunitv. Dual slope integrating converters are another form of
analog-to-digital converter. These devices take longer but are more resistant to noise.
Other techniques. such as the Incremental charge balancing technique. are also used.
Analog-to-digital converters usuallv reqUire some external analog and digital circuitry.
although complete units are becoming available at low cost.
Figure 11-26 shows the 8-bit Teledyne Semiconductor 8703 AID converter. The device
contains a result latch and tristate data outputs. A pulse on the Initiate Conversion line
starts conversion of the analog input; after about two milliseconds the result will go to
the output latches. and the Data Valid output will Indicate this by switching first low
and then high. Data is read from the latches by applying '0' to the ENABLE input.
Figure 11-27 shows the interface for the Z80 processor and the 8703 converter. 9 Port B
is used to prOVide an Initiate Conversion pulse (active-high) of sufficient length. The
Data Valid signal is tied to A STB so that Data Valid going low and then high will latch
the converted data into Port A. The Data Valid signal is also tied to a bit of Port B so that
the CPU can determine ItS value. The important edge on the Data Valid line IS the lowta-hlgh edge. which indicates the completion of the conversion. As in the case of the
encoded keyboard. additional circuitry will be necessary if the pulse on Data Valid is too
short to be handled in software. Note that we are using Port B here for both status and
control.
11-76
0. 1I'!'
~.
+5V
19
INITIAT E
CONVERS ION
...r1-
VIN
21
1
.5V
-..I
-..I
lookfi
201<fi
loon
'
Zero
Adjust
5V
+p-
..!2P AMP
I"
'''''''''':~
~mparator
.......
16
-V
-.
Clock
and
Control
Logic
20pA
18
RREF'
~ 3201<0
Data
Counters
Output
Latches
;.
10
fo4-
8
9
11
12
LS8
la'8it
'r
22
23
ata
BUSY
Data Valid
GNO
Vss
13
5
6
Internal
lkO
-.1-
ENABLE
MS8
.))---------
- -2"
liN = 10pA
iA FoS.... 270pF
........
Binary
Output
VOO
,,17
RBiAS
20
lOOkO
fO'll'!'
l
VREF
l
-5V
.... '*
Components chosen for VIN (FS)
lOV. VREF
-64V
Analog
Data Bus
to CPU
Input
<
PIO
AID
Ao
A STB
85
TBledvn. 8703
":7
82
Converter
----
Data
InitIate
Valid
Conversion
and then read the data and store it in memory location 0040.
Flowchart:
Pulse lniti8te
Conversion
line
100401 = Data
Note that here the PIO serves as a parallel data port. a serial status port. and a serial
control port.
11-78
Source Program:
LD
OUT
LD
OUT
LD
OUT
LD
OUT
SUB
OUT
WTLOW: IN
BIT
JR
WTHI:
IN
BIT
JR
IN
LD
HALT
A.01001111 B
(PIOCRA).A
A.11 001111 B
(PIOCRBl.A
A.00001111 B
(PIOCRBl,A
A.00100000B
(PIODRB),A
A
(PIODRB).A
A. (PIODRB)
2.A
NZ.WTLOW
A.(PIODRB)
:NO. WAIT
:IS DATA AVAILABLE?
2.A
Z.WTLOW
A.(PIODRA)
(40Hl.A
:NO. WAIT
:YES. FETCH DATA FROM CONVERTER
:SAVE CONVERTER DATA
11-79
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
0000
OOOE
OOOF
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
001A
001B
001C
0010
001E
001F
0020
0021
0022
0023
0024
3E
4F
03
PIOCRA
3E
CF
03
PIOCRB
3E
OF
03
PIOCRB
3E
20
03
PIOORB
Instruction
(Mnemonic)
LO
A.01001111 B
OUT
(PIOCRAl.A
LO
A.11 001111 B
OUT
(PIOCRBl.A
LO
A.00001111 B
OUT
(PIOCRBl.A
LO
A.00100000B
OUT
(PIOORBl.A
97
SUB
03
PIOORB
DB
PIOORB
CB
57
20
FA
DB
PIOORB
CB
57
28
FA
DB
PIOORA
32
40
00
76
OUT
(PIOORBl.A
WTLOW: IN
WTHI:
A.(PIOORB)
BIT
2.A
JR
NZ.WTLOW
IN
A.(PIOORB)
BIT
2.A
JR
Z.wTHI
IN
A.(PIOORA)
LO
(40H).A
HALT
One approach to configUring PI Os IS to use the repeated Block Output Instruction OTIR
and a table In memory containing the words to be sent to the Control register. A typical
routine would be:
LO
LO
LO
OTIR
B,LENG
C,PIOCR
HL,CTLTAB
In fact. another table (or the Stack) could be used to hold the number of control words
and the port number for each PIO.
11-80
One state
Zero state
_----...
l'
-...-----...
Start
Bit
'0'
'0'
'0'
'"
'0'
,.-----.,,~-...-~
Panty
Stop
Stop
7 Data Bits
Bit
Bit
Bit
A Teletypewriter (TTY)
Purpose: Transfer data to and from a standard 10-character-persecond serial teletypewriter,
STANDARD
TTY
CHARACTER
FORMAT
Figure 11-28 shows the format. Note that each character requires the transmission of
eleven bits. of which onlY seven contain information, Since the data rate is ten characters per second, the bit rate IS 10 x 11, or 110 Baud, Each bit therefore has a width of
1/110 of a second, or 9.1 milliseconds, This width is an average: the teletypeWriter
does not maintain it to any high level of accuracy.
For a teletypewriter to communicate properly with a computer. the follOWing procedures are necessary.
Receive (flowcharted In Figure 11-29):
Step 1)
Step 2)
Step 3)
Step 4)
Step 5)
TTY
RECEIVE
11-81
Panty
error
Wait
one-ha~
Count =2
bit tune
Count
Oats
Count
Framing
= Count-1
error
Count
= Count -
Generate
received panty
11-82
WTSTB:
RCVB:
LD
OUT
IN
RLA
A.01001111B
(PIOCRA),A
A.(PIODRA)
JR
C.wTSTB
DHALF
D.10000000B
DFULL
A.(PIODRA)
CALL
LD
CALL
IN
RLA
RR
JR
LD
LD
HALT
o
NC.RCVB
A.D
(60H).A
(Delay program)
DHALF'
ORG
PUSH
LD
JR
DFULL:
DLY16:
DLY1.
PUSH
LD
LD
DEC
JR
30H
DE
D.B
DLY16
DE
0.16
E.8DH
E
NZ.DLY1
DEC
JR
NZ.DLY16
DE
POP
RET
11-83
IS
Object Program:
Memory Address
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
OOOD
OOOE
OOOF
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
001A
001B
001C
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
003A
0038
003C
003D
003E
003F
0040
0041
Memory Contents
(Hexl
3E
4F
D3
PIOCRA
DB
PIODRA
17
38
FB
CD
30
00
16
80
CD
35
00
DB
PIODRA
17
CB
lA
30
F6
7A
32
60
00
76
D5
16
08
18
03
D5
16
10
1E
8D
1D
20
FD
15
20
F8
Dl
C9
Instruction
(Mnemonic)
A.Ol00ll11 B
LD
OUT
WTSTB: IN
(PIOCRA),A
A.(P1ODRA)
RLA
JR
C,WTSTB
CALL
DHALF
LD
D.10000000B
CALL
DFULL
IN
A.(PIODRAI
RLA
RR
JR
NC.RCVB
LD
LD
A.D
(60H).A
HALT
PUSH
LD
DE
D.8
JR
DLY16
DFULL:
PUSH
LD
DE
D.16
DLY16:
LD
E.8DH
DLY1:
DEC
JR
E
NZ.DLY1
DEC
JR
D
NZ.DLY16
POP
RET
DE
RCVB:
DHALF:
11-84
This program assumes that the Stack can be used for subroutine calls, i.e., the monitor
must initialize the Stack Pointer. Otherwise you will have to initialize the Stack Pointer
as shown in Chapter 10.
The constants for the delay routine were calculated Just as shown earlier in this chapter.
You might try determining them for yourself. The delays do not have to be highly accurate because the reception is centered, the messages are short, the bit rate is low, and
the teletypewriter is not highly accurate itself.
How would you extend this program to check for the two stop bits? They must both be
one or a framing error has occurred.
You can extend this program to check odd parity by replacing the LD A,D Instruction
with the sequence:
A
SUB
AND
JP
PE,PRERR
11-85
SeIl\j
data to
Output Port
Carry
= 1 {Stop bitl
Count = Count - 1
TTY
TRANSMIT
MODE
The transmission routine must wait one bit time between each operation.
11-86
A.00001111 B
(PIOCRB),A
A, (60H)
:GET DATA
:SHIFT LEFT AND FORM START BIT
;COUNT = 11 BITS
A.A
B.ll
OUT
RRA
SCF
(PIODRB),A
:TRANSMIT A BIT
:UPDATE FOR NEXT BIT
:FORM STOP BIT (LOGIC ONE)
DFULL
TBIT
IS
:DELAY 9.1 MS
:COUNT DOWN 11 BITS
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
DODD
OOOE
OOOF
0010
0011
0012
0013
3E
OF
D3
PIOCRB
3A
60
00
87
06
DB
D3
PIODRB
IF
37
CD
35
00
10
F7
76
Instruction
(Mnemonic)
TBIT:
LD
A.OOOOllll B
OUT
(PIOCRB),A
LD
A, (60H)
ADD
LD
A.A
B.ll
OUT
(PIODRB),A
RRA
SCF
CALL
DFULL
DJNZ
TBIT
HALT
11-87
ADD A.A clears the least significant bit so that It can be used as the start bit. The most
significant bit is saved in the Carry. In actual applications. the startup routine should
place a logic '1' on the teletypewriter line after configuration since that line should normally be In the mark (one) state.
Each character consists of 11 bits. starting with a start bit (zero) and ending with two
stop bits (ones).
This program can easily be extended to generate 7-bit characters with odd parity In the
most significant bit. The parity generation routine (to be Inserted after LD A.(60H)) IS:
STBIT:
ANA
JP
SET
ADD
A
PO.STBIT
7.A
A.A
Ability to handle various bit lengths (usually 5 to 8l. panty options. and numbers of
Stop bits (usually 1. 1-1/2. and 2).
Indicators for framing errors. parity errors. and "overrun errors" (failure to read a
character before another one IS received).
RS-232 11 compatibility: I.e.. a Request-to-Send (RTS) output signal that indicates
the presence of data to commUnications equipment and a Clear-to-Send (CTS) Input signal that indicates. in response to RTS. the readiness of the communications
equipment. There may be provisions for other RS-232 signals. such as Received
Signal Quality. Data Set Ready. or Data T~rmlnal Ready.
4)
5)
6)
UARTs act as four parallel ports: an input data port. an output data port. an input
status port. and an output control port. The status bits Include error Indicators as well
as Ready flags. The control bits select vanous options. UARTs are Inexpensive ($5 to
$50. depending on features) and easy to use.
11-88
THE
The ZSO Serial Input/Output Device or 510 (see Figure 11-31) IS a complete communications contrdller specifically designed for use in ZSO-based microcomputers. It
can serve a variety of communications functions. but we will only discuss ItS use as a
simple asynchronous recelverltransmitter. 12
The 510 has two complete channels (A and B) which can both
receive and transmit serial data (see Figure 11-32). Channels that
can receive and transmit simultaneously are called full-duplex.
Alternatives include half-duplex (able to transmit and receive, but not at the same
time). receive-only, and transmit-only.
An 510 occupies four Input port addresses and four output port
addresses. The B/A (Channel B or A Select) and C/O (Control or
Data Select) li.,es choose one of the four ports as described in Table 11-7. Most often, designers attach address bit AO to the B/A Input and address bit
A1 to the C/O input. The 510 then occupies four consecutive port addresses as described in the last column of Table 11-7
As with the PIO, SIOs have more control registers than adADDRESSING
dresses. In fact. each 510 has eight registers in each chan810 READ AND
WRITE REGISTER
nel for control and three registers for status. Figure 11-33
contains diagrams of each control or Write register: Figure
11-34 contains diagrams of each status or Read register. Two transfers are reqUired to
read or write any of the registers except Write Register O. The first transfer (written Into
Write Register 0) contains three bits that direct the next transfer to or from the selected
register. Note, In Figure 11-33, that these three bits occupy the three least Significant
bit positions and that zeros in the other bit posilions indicate a byte that has no function
other than addressing.
11-89
Senal Data
Internal
+5V GND
<l>
Channel Clock
Channel A
Control
SYNC
LogIc
WAITfRDY
+++
Data
Discrete
CPU
Modem or
Control
Bus
Other
and
I/O
Controls
Status
Control
Senal Data
Internal
....II---Channel Clock
Channel B
Control
Logic
jnterrupt Control
Lines
CHANNEL B OR A
SELECT
REGISTER
ADDRESSED
PORT ADDRESS
(STARTING WITH SIOADDl
0
0
1
1
Data Register A
Data Register B
Control A
Control B
SIOADD
SIOADD+1
SIOADD+2
SIOADD+3
1
1
11-90
IS
TxD
TxC
II
XMIT
Shift and
Bit Insert
CRC
"-
"
SYNC
Generator
RegIsters
XMIT
Buffer
.:
i"
Channel
Internal Bus
Control
II
and
i"
Status
'< '7
REC
FIFO
CRC
SYNC
Checker
Detect
REC
Shift and
Bit Stnp
-v
I 1
RxD
RxC
11-91
Write Regllte..
The ZSO 510 contains eight registers in each channel that are programmed (written into) by the system software
to configure the functional personality of each channal. All Write registers. with the exception of Write Aegister 0,
require two bytes to be property programmed. The first byte contains three bits that pOint to the selected register
(DO~02); the second byte IS the actusl control word that is being written to that register to configure the SIO.
Write Register 0 is 8 special case. RESET (either internal command or external input) will initialize the 510 to Write
Aegister O. All basic commands (CM02-CMDO) and CAC controls (CACO, CACn cen be accessed with a single byte
using Write Register O.
Contained in the first byte of any Write register access are the basic commands (CM02-CMDO) and the CAC controls (CACO,CAC1) so that meximum system control and flexibility is maintained.
Write Regilt" 0
0
0
0
0
0
0
0
1
Aegister 1
Aegister 2
0
0
Aegister 3
Aegister 4
Aegister 5
RegIster 6
Register 7
1
1
o
o
Register 0
0
0
Null Code
o
o
1
1
1
1
0
0
1
1
o
,
Error Aem
Null Code
1
0
Write Regllter 1
0
0
Ax Interrupt Disable
Rx Interrupt on Rrst Charactsr Only or Error
Interrupt on All Rx Characters (Parity Affects Vector)
Interrupt on All Ax Charscters (Parity Doea Not Affect Vector)
WAIT/AEADY on AIT
WAIT FN/AEAOY FN
WAIT/AEAOY Enable
.11-92
Write Register 2
Write Register 3
o
o
Rx 5 Bits/Character
Rx 6 Bits/Character
Rx 7 Bits/Charactar
Rx 6 Bits/Character
VO
V1
V2
V3
V4
V5
V6
V7
Interrupt
Vector
I DO I
Rx Enable
SYNC Character Load Inhibit
Address Search Moda (SOLC)
Rx CRC Enable
Enter Hunt Moda
Auto Enables
Write Register 4
L...
o
o
1
1
o
o
1
1
o
o
1
1
0
1
0
1
0
1
0
1
0
1
0
1
x 1 Clock Mode
x16 Clock Mode
x32 Clock Mode
x64 Clock Mods
11-93
Write Reglater 6
Tx CRC Eneble
RTS
SOlC/CRC-16
Tx Eneble
Send BREAK
0
0
1
1
Tx
Tx
Tx
Tx
1
0
1
7 Bits/Character
6 Bits/Cherecter
6 Bits/Cheracter
OTR
Write Regleter 6
I 07 1D61
II
SYNC Bit 0
SYNC Bit 1
SYNC Bit 2
SYNC Bit 3
SYNC Bit 4
SYNC BitS
SYNC Bit 6
SYNC Bit 7
Write Reglater 7
I 1 I I 1 I I IDO I
D7
D6
DS
D4
03
j
D2
Dl
SYNC Bit 6
SYNC Bit 9
SYNC Bit 10
SYNC Bit 11
SYNC Bit 12
SYNC Bit 13
SYNC Bit 14
SYNC Bit lS
11-94
Read Raglate..
The zao SID contains threa registers thet can be read to obtain the status of each channel. Status information includes error conditions, interrupt vector, and standard communication interface protocol signals. To read the con~
tents of e selected Raad registar. the system software must first write out to the SID the byte containing pointer
information (OO~D2) in exactly the same manner 8S s Write register operation. Then. by issuing a READ operation,
the contents of the addressed Read/Status register can be read by the
zao CPU.
The real power in this type of command structure is that the programmer has complete freedom, after pointing to
the selected register. of either reading or writing to initialize or test that register. By designing software to Initialize
the
zao Sig in a modular. structured fashion. the programmer can use the powerful ZSO BlOCk I/O instructions to
Rx Character Available
Interrupt Pending (Ch-A Oniyi
Tx Buffer Empty
OCO
SYNC/HUNT
CTS
Sending CRC/SYNCS
BREAK/ABORT
Read Register 1
in
1
0
0
1
1
0
0
1
0
1
0
0
0
1
1
1
1
PreVIO us
I-FiBld Bits In
Second Previous
Byte
Bvte
0
0
0
0
0
0
4
5
6
Programmed
6
6
8
Panty Error
Ax Overrun Error
CRC/Framlng Error
Special Rx
Condition
Interrupts
11-95
VO
VI
V2
V3
V4
Interrupt
Vector
VS
V6
V7
11-96
2)
3)
4)
ISiOl
LD
LD
LD
OTIR
5)
6)
7)
8)
B.LENG
C.SIOCRA
HL.CTLTAB
The RS-232 signals are all active-low. However. the SIO control bits for these signals are active-high O.e.. a logic '1' In a control bit sends an RS-232 signal lowl.
The SIO requires an external clock. In asynchronous communications at 110 Baud.
1760 Hz is usually supplied and the X 16 mode is used. The SIO will sample the bits
at the clock frequency for synchronization and to avoid false start bits caused by
nOise on the line.
The Data Ready (Rx Character Available) flag IS bit 0 of Read Register O. The Peripheral Ready (Ix Buffer Empty) flag IS bit 2 of Read Register O.
Error status bits (panty. overrun. and framing) are in Read Register 1.
11-97
EXAMPLES
Teletypewriter 1/0 via a USART
Task 1: Read from teletypewriter through SIO
Purpose: Receive data from a teletypewriter through an SIO and place the data into
memory location 0040. The data IS 7-bit ASCII with odd parity.
Source Program:
WAITD:
LD
OUT
LD
OUT
LD
OUT
LD
OUT
SUB
OUT
IN
RRA
JR
IN
LD
HALT
AA
(SIOCRAl.A
A.01 000001 B
(SIOCRA).A
A.3
(SIOCRA).A
A.Ol000001B
(SIOCRAl.A
A
(SIOCRA).A
A.(SIOCRA)
NC,WAITD
A.(SIODRA)
(40Hl.A
11-98
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
0000
OOOE
OOOF
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
001A
001B
001C
0010
3E
04
03
SIOCRA
3E
41
03
SIOCRA
3E
03
03
SIOCRA
3E
41
03
SIOCRA
97
03
SIOCRA
DB
SIOCRA
1F
30
FB
DB
SIOORA
32
40
00
76
Instruction
(Mnemonic)
WAITO:
LO
AA
OUT
(SIOCRA),A
LO
A,01000001B
OUT
(SIOCRA),A
LO
A,3
OUT
(SIOCRA),A
LO
A,01 000001 B
OUT
(SIOCRA),A
SUB
OUT
A
(SIOCRA),A
IN
A,(SIOCRA)
RRA
JR
NC.WAITO
IN
A, (SIOORA)
LO
(40H),A
HALT
11-99
EXAMPLE
OFSIO
CONFIGURATION
0
1
2
3
if no errors occurred
if a parity error occurred
if an overrun error occurred
if a framing error occurred.
Note that the receiver always checks for one stop bit.
11100
SIO
ERROR
STATUS
WAITR:
LD
OUT
LD
OUT
LD
OUT
LD
OUT
SUB
OUT
IN
BIT
JR
LD
OUT
HALT
AA
(SIOCRA},A
A.01001101B
(SIOCRA},A
A.5
(SIOCRA},A
A.00101000B
(SIOCRA},A
(SIOCRA},A
A. (SIOCRA)
2.A
Z.WAITR
A. (40H)
(SIODRA},A
:GET STATUS
:IS TRANSMITIER READY?
:NO. WAIT
:YES. GET DATA
:AND TRANSMIT IT
11-101
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
0000
OOOE
OOOF
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
001A
001B
001C
OOlD
001E
3E
04
03
SIOCRA
3E
40
03
SIOCRA
3E
05
03
SIOCRA
3E
28
03
SIOCRA
97
03
SIOCRA
OB
SIOCRA
CB
57
28
FA
3A
40
00
03
SIOORA
76
Instruction
(Mnemonic)
WAITR:
LO
A,4
OUT
(SIOCRA).A
LD
A,01001101B
OUT
(SIOCRA).A
LO
A,5
OUT
(SIOCRA).A
LD
A,00101000B
OUT
(SIOCRA),A
SUB
OUT
A
(SIOCRA).A
IN
A,(SIOCRA)
BIT
2,A
JR
Z,WAITR
LO
A,(40H)
OUT
(SIOORA).A
HALT
11102
STANDARD INTERFACES
Other standard Interfaces besides the TTY current-loop and
RS-232 can also be used to connect peripherals to the microcomputer. Popular ones include:
1)
2)
The 8-bit parallel General Purpose Interface Bus, also known as IEEE-488 or
Hewlett-Packard Interface Bus (HPIBl.14
The S-l 00 or Altalr/lmsai hobbYist bus. 15 This is also an 8-bit bus.
The Intel Multibus. 16 This IS another 8-blt bus that can, however, be expanded to
handle 16 bits In parallel.
3)
4)
PROBLEMS
6) Separating Closures from an Unencoded Keyboard
Purpose: The program should read entnes from an unencoded 3 x 3 keYboard and
place them Into an array. The number of entnes required IS In memory location 0040 and the array starts In memory location 0041.
Separate one closure from the next by waiting for the current closure to end. Remember
to debounce the keyboard (this can be Simply alms wait).
Sample Problem:
(0040)
04
Entnes are 7. 2. 2, 4
Resu It: (0041)
07
(0042)
02
(0043)
02
04
(0044)
7)
Purpose: The program should read entnes from an ASCII keyboard (7 bits with a zero
Parity bit) and place them Into an array until It receives an ASCII penod (hex
2El. The array starts in memory location 0040. Each entry IS marked by a
strobe as In the example given under An Encoded Keyboard.
Sample Problem:
Result:
Entnes are H. E.
(0040)
48
(0041)
45
(0042)
4C
4C
(0043)
(0044)
4F
(0045)
2E
L. L. O..
H
E
L
L
0
11-103
8)
Purpose: The program should generate a square wave, as shown In the next figure,
using a D/A converter. Memory location 0040 contains the scaled amplitude
of the wave, memory location 0041 the length of a half cycle in milliseconds,
and memory location 0042 the number of cycles,
Assume that a digital output of 8016 to the converter results In an analog output of zero
volts, In general. a digital output of D results in an analog output of VOUT = -VREF
(D-80)/80 volts.
Sample Problem:
(0040)
(0041)
(0042)
AO (hex)
04
03
Result:
+VREF I
i
~:: -V:E~ ~----F----t- ---F----\- ----1-- ---\-----1iVREF
-4H'
-VREF
4ms
TI~
9)
Purpose: The program should take four readings from an AID converter ten milliseconds apart and place the average In memory location 0040. Assume that
the AID conversion time can be Ignored.
Sample Problem:
10)
(0040) = 85
A 30 Character-per-Second Terminal
Purpose: Modify the transmit and receive routines of the example given under A
Teletypewriter to handle a 30 cps terminal that transfers ASCII data with one
stop bit and even parity. How could you write the routines to handle either
terminal depending on a flag bit in memory location 0060: e.g., (0060) = 0
for the 30 cps terminal. (0060) = 1 for the 10 cps terminal?
11-104
REFERENCES
1.
2.
3.
For example. the Z80 Direct Memory Access Controller (or DMA) for Z80 based
microcomputers IS described in An IntroductIOn to Microcomputers: Volume 2 Some Real Microprocessors.
4.
The TTL Data Book for Design Engineers. Texas Instruments Inc.. P O. Box 5012.
Dallas. Texas 75222. 1976.
5.
Dilatush. E.. "Special Report: Numeric and Alphanumeric Displays." EON. February 5. 1978. pp. 26-35.
6.
See Reference 4.
Hnatek. E. R.. A User's Handbook of DIA and AID Converters. Wiley. New York.
1976.
8.
See Reference 7
9.
See also D. Guzeman. "Marry Your p.P to Monolithic AIDs." Electronic Design.
-January 18. 1977. pp. 82-86.
10.
For a discussion of UARTs. see P Rony et al.. "The Bugbook Ila." E and L Instruments Inc.. 61 First Street. Derby. CT. 06418: or D. G. Larsen et al..
"INWAS: InterfaCing With Asynchronous Serial Mode," IEEE Transactions on Industrial Electronics and Control Instrumentation. February 1977, pp. 2-12. Also
see McNamara. Reference 2.
11.
12.
13.
11-105
14.
Morrow. G.. and H. Fullmer, "Proposed Standard for the S-100 Bus," Computer,
May 1978. pp. 84-89.
16.
Rolander, T.. "Intel Multibus Interfacing:' Intel Application Note AP-28. Intel CorporatIOn, Santa Clara. CA.. 1977
Smith. M. L.. "Build Your Own Interface:' Kilobaud, June 1977, pp. 22-28.
11-106
Chapter 12
INTERRUPTS
Interrupts are inputs that the CPU examines as part of each instruction cycle. These inputs allow the CPU to react to asYnchronous events in a more efficient manner than
polling each device. When Interrupts are utilized to initiate I/O. generally more hardware than ordinary. programmed I/O IS required. but this provides a faster and more
direct response. 1
Why use Interrupts? Interrupts allow events such as alarms. power
REASONING
failure. the passage of a certain amount of time. and peripherals
BEHIND
haVing data or being ready to accept data to get the immediate atINTERRUPTS
tention of the CPU. The programmer does not need to poll every
deVice. nor need the programmer worry about the system completely missing events.
An interrupt system IS like the bell on a telephone - I t nngs when a calliS received so
that you don't have to pick up the receiver occaSionally to see if someone IS on the line.
The CPU can go about ItS normal business (and get a lot more done!. When something
happens. the interrupt rouses the CPU and forces it to service the input before resuming
normal operations. Of course. this simple description becomes more complicated (just
like a telephone SWitchboard) when there are many Interrupts of varying importance
and there are tasks that cannot be interrupted.
The implementation of Interrupt systems vanes greatly.
Among the questions that must be answered to characterize a particular system are:
CHARACTERISTICS
OF INTERRUPT
SYSTEMS
1)
2)
3)
4)
5)
There are many different answers to these questions. The aim of all the Implementations. however. is to have the CPU respond rapidly to interrupts and resume normal activity afterwards.
The number of Interrupt Inputs on the CPU chip determines the number of different
responses that the CPU can produce Without any additional hardware or software. Each
Input can produce a different Internal response. Unfortunately. most microprocessors
have a very small number (one or two. tYPically) of separate Interrupt Inputs.
The ultimate response of the CPU to an interrupt must be to transfer control to the correct Interrupt service routine and to save the current value of the Program Counter. The
CPU must therefore execute a Jump-to-Subroutine or Call instruction with the beginning of the Interrupt service routine as ItS address. ThiS action will save the return address In the Stack and transfer control to the interrupt service routine. The amount of
external hardware required to produce thiS response vanes greatly. Some CPUs internally generate the instruction and the address: others require external hardware to
form them. The CPU can only generate a different instruction or address for each separate input.
12-1
If the number of Interrupting devices exceeds the number of Inputs. the CPU will need extra hardware or software to identify the
source of the Interrupt. In the simplest case. the software can be a
polling routine which checks the status of the devices that may be
interrupting. The only advantage of such a system over normal polling IS that the CPU
knows that at least one device is active. The alternative solution IS for additional hardware to provide a unique data input (or "vector") for each source. The two alternatives
can be mixed: the vectors can Identify groups of inputs from which the CPU can identify a particular one by polling.
NON-MASKABLE
INTERRUPT
tiVitieS.
DISADVANTAGES
OF INTERRUPTS
1)
2)
Interrupts still require data transfers under program control through the CPU. There
is no speed advantage as there is With DMA.
3)
Interrupts are random inputs. which makes debugging and testing difficult. Errors
may occur sporadically. and therefore may be very hard to find. 2
Interrupts may involve a large amount of overhead if many registers must be saved
and the source must be determined by polling.
4)
zso
INTERRUPT SYSTEM
The zao's internal response to an interrupt IS fairly complex. since there are three
different operating modes. The interrupt system consists of:
1)
2)
An active-low maskable interrupt Input (lNT) and an actlvelow non-maskable Interrupt input (NMi).
Two enable flipcflops (lFF1 and IFF2). IFF1 can be set or reset
to enable or disable interrupts. IFF2 serves as temporary
storage for IFF1 during non-maskable interrupts.
12-2
ZSO
INTERRUPT
INPUTS
The ZSO checks the current status of the interrupt system at the
end of each instruction cycle. If an interrupt is active and enabled.
the response is as follows: 3
1)
2)
lSO
INTERRUPT
RESPONSE
The remainder of the response depends on the Interrupt mode and the source.
Note in particular that the lSO will check for interrupts after each transfer or comparison in a Block Move. Block Compare. or Repeated Block I/O instruction.
The ZSO has the following special instructions for use with the
Interrupt system:
1)
2)
3)
4)
5)
6)
7)
S)
lSO INTERRUPT
INSTRUCTION
Non-Maskable Interrupt
The non-maskable interrupt is an edge-sensitive (negative
lSO
NON-MASKABLE
edge triggered) input. The processor therefore reacts only to
the edge of a pulse on thiS line. and the pulse will not interrupt
INTERRUPT
its own service routine. Non-maskable interrupts are useful for
applications that must respond to loss of power li.e.. must save data in a low-power
memory or switch to a backup battery). Typical applications are commUnications equipment that must retain codes and partial messages and test equipment that must keep
track of partially completed tests.
12-3
Operation Code
(Mnemonic)
Hex!
(Hex)
Destination Address
(Decimal)
RSTO
RST 8
RST 10H
RST 18H
RST 20H
RST 28H
RST 30H
RST 38H
C7
CF
0000
0008
0
08
D7
OF
E7
EF
F7
FF
0010
0018
0020
0028
0030
0038
16
24
32
40
48
56
It clears IFF1, thus disabling all interrupts (but saving the old state of IFF1 In IFF2L
2)
It Ignores the next Instruction fetched from memory and instead jumps to memory
location 006616. saving the old value of the Program Counter in the Stack,
Remember that a RETN instruction at the end of the service routine will restore the old
state of IFF1 from IFF2,
We will not discuss the non-maskable Interrupt further. Henceforth. we will assume
that all Interrupt inputs are tied to INT
Mode 0
In this mode. the CPU uses the data input during the Interrupt Acknowledge cycle as an
instruction, This mode is the same as the 8080 Interrupt response mode,4
The normal data input that must be proVided externally is a RST instruction (see Table
12-lL
RST is useful In Interrupt systems for the following reasons:
1)
RESTART
INSTRUCTION
2)
3)
Its vectors are far enough apart to allow Jump instructions to reach the actual service routines,
4)
It is easy to form. since five of the bits are always '1.' An 8-to-3 encoder can proVide
the other three bits qUite easily,
2)
Its vectors are not far enough apart to allow space for entire Interrupt serYlce
routines,
Its vectors are In a fixed area of memory,
3)
4)
RST 0 has the same destination address as the RESET input and is therefore very
difficult to use, The system needs hardware to differentiate between RESET and
RST O. since the two cannot be distinguished by software alone,
12-4
Remember that RST saves the old Program Counter in the Stack Just as CALL does.
Mode 1
In this mode. the CPU Ignores the data input dUring the Interrupt Acknowledge cycle
and always executes RST 38H. thus Jumping to memory location 003816 and saving
the old Program Counter In the Stack. This mode IS equivalent to Mode 0 if the data input IS always RST 38H (FF16).
The advantage of this mode IS that no external hardware IS required. Its disadvantages
are that there IS no way to directly differentiate among Interrupt sources and the
destination address IS fixed. Mode 1 is useful In applications that have only one or two
interrupt sources and In which minimum hardware cost IS essential.
Mode 2
In this mode. the CPU uses the data input as part of an address from which to get the
starting address of the Interrupt service routine. When an interrupt IS accepted. the
CPU:
1)
2)
3)
4)
Fetches an address from the two memory locations starting with the one referred to
by the pOinter (see Figure 12-1).
5)
3)
A.IPGNO
LA
Mode 2 IS designed to work with Z80 PIOs. SIOs. and CTCs. PIO and SIO Interrupts are
described later In this chapter.
12-5
,\
desired starting address
pointed to by:
Interrupt
8 Bits from
I Register
lo~
7 bits from
Peripheral
Service
Routine
low-order
high-order
> Starting
Address
Table
PIO INTERRUPTS
Most Z80 Interrupt systems involve PIOs. Each port of the PIO has
the follOWing features for use with Interruots:
PIO
INTERRUPTS
1)
2)
3)
An Interrupt Control register used to determine the logical operation performed and
the active polarity monitored for generating Interrupts In the control mode.
4)
An Interrupt Mask register used to determine which data lines will be monitored to
generate Interrupts in the control mode.
The Interrupt Vector register In each port can be accessed by WritIng a control word with a zero In ItS least Significant bit. as shown
below (see also Table 11-2):
07 06
05
04
03
02
01
PIO
INTERRUPT
VECTOR
00
A.IVECT
(PIOCRI.A
where IVECT has a '0' In ItS least Significant bit. The starting address for the Interrupt
service routine IS at address IVECT on the page aSSigned to the table of starting addresses for service routines.
12-6
07
06
04
05
03
02
01
00
07
06
05
04
IS
03
02
01
00
PIO
INTERRUPT
CONTROL
MODE
that any monitored I/O line becoming active will cause an Interrupt (i.e.. a logical
OR)
Note that an Interrupt occurs only if the logical equation IS true when Interrupts are
enabled or if It changes from false to true while Interrupts are enabled.
2)
31
05 defines the active polaflty (high or lowl of the monitored I/O lines 05
means active high. 05 = 0 means active low.
04 = 1 means that the next control word IS an Interrupt mask (Figure 12-3) Only
lines with a mask bit of zero will be monitored. 04 = 0 means that the mask does
not follow.
ENABLING AND
Bit 7 of the Interrupt control word determines the value of the
DISABLING PIO
Interrupt enable flip-flop for the port. Interrupts may be generINTERRUPTS
ated if the flip-flop IS set. Power-on resets thiS flip-flop. but
remember that the PIO has no RESET input. The Interrupt enable flip-flop may be set or reset without affecting the rest of the Interrupt control word
by writing a control word with the flip-flop value in bit 7 and 0011 in the four least significant bits.
Setting bit 4 of the Interrupt control word clears any pending Interrupts. ThiS can be
used to clear interrupts that may have occurred Inadvertently dUfing a reset.
12-7
Examples
1)
A.OOOOllll B
(PIOCRB).A
A.80H
(PIOCRA).A
A, 10000011 B
(PIOCRBl.A
EXAMPLES OF
PIO INTERRUPT
CONFIGURATION
An alternative that clears pending Interrupts as well as enabling interrupts from the
port IS:
LD
OUT
A, 10010111 B
(PIOCRA).A
A,Ol 001111 B
(PIOCRA).A
A,60H
(PIOCRA).A
A,l 0000011 B
(PIOCRA).A
Interrupting control port with vector located at address 4816. An Interrupt will,_\Je
generated if data lines A4 and A7 both go low.
LD
OUT
LD
OUT
LD
OUT
LD
OUT
LD
OUT
A.ll0011118
(PIOCRA).A
Al0001000B
(PIOCRA).A
AA8H
(PIOCRA).A
A.ll010111B
(PIOCRA).A
A,0111 0111 B
(PIOCRA),A
12-8
4)
Interrupting control port with vector located at address 2816. An Interrupt will be
generated if any of the data lines go high.
LD
OUT
LD
OUT
LD
OUT
LD
OUT
SUB
OUT
A.11001111B
(PIOCRB},A
A.OFFH
(PIOCRB},A
A.28H
(PIOCRB},A
A.10110111B
(PIOCRB},A
A
(PIOCRB},A
Obviously a repeated Block Output instruction could be used to shorten these programs
considerably.
Each PIO also has a single interrupt output and enable signals
for daisy chaining. The INT output is active-low when the PIO
has an interrupt request. The enable signals are:
lEI" (Interrupt Enable In) - high if no other deVices of higher
priority are being serviced by a CPU interrupt service routine.
lEO (Interrupt Enable Out) from this PIO
lEI and lEO can be used to form a daiSY chain (see Volume 1 of.ill
Introduction to Microcoml;1uters) in which PIOs and other devices
that are connected to the chain closer to the CPU can block interrupt requests from devices further from the CPU. The advantages
of the daiSY chain are:
1)
2}
3)
DAISY
CHAINING
Pia
INTERRUPTS
Pia DAISY
CHAIN
SIGNALS
ADVANTAGES
AND
DISADVANTAGES
OF DAISY CHAIN
INTERRUPTS
The Z80 automatically waits long enough for the signals to ripple through a chain of up
to four devices when operating in Interrupt Mode 2. Additional hardware can be added
to allow longer chains.
12-9
DEVICE
OPERATION
1)
2)
It subsequently brings ItS Interrupt Enable Out high (enabling lower priority
devices) only if a RETI Instruction is executed while ItS Interrupt Enable In IS high.
Thus. a particular device will be serViced only when it has the highest priority request
and will block lower-priority requests until ItS service routine has been completed. A
hlgher-prlorltv device can Interrupt a lower-priority service routine without any
difficulty. Note that a RETI Instruction at the end of the high Priority routine will not be
recognized by the lower-priority device.
SIO INTERRUPTS
The 510 can also serve as a source for interrupts. You should note
the follOWing features of the 510 Interrupt-based systems:
1)
2)
3)
4)
SIO
INTERRUPTS
Within an 510. Channel A Interrupts have priority over Channel B interrupts. receiver Inl
terrupts have priOrity over transmitter interrupts. and transmitter Interrupts have
priOrity over external or status interrupts.
SIOs can be used in a polling Interrupt system. The CPU must
check each 510 for activity by examining bit 1 of Read Register 9
on Channel A. I.e.,
SUB
OUT
IN
BIT
JR
A
(SIOCRAl.A
A.(SIOCRA)
1.A
NZ.SERVE
2)
POLLING
INTERRUPT
SYSTEMS
WITH SIOs
The first Interrupt examined has the highest priority. since the remaining interrupts
will not be examined if the first one is active. The second interrupt has the next
highest priOrity. and so on.
The service routine must clear the SIO Interrupt by reading or writing the appropriate data register even if a data transfer is otherwise unnecessary.
12-10
IS
1. the vector retumed from an Interrupt acknowtedge cycle will be venable according to the following:
Ch B
Ch A
V3
V2
Vl
0
0
0
0
0
0
1
Ch B ExtemaJfStatus Change
0
0
1
1
Ch A External/Status Change
0
1
1
1
Panty Error or
Rx Overrun Error or
CRC/Framing Error or
Receive Interrupt Mode 0 and Receive Interrupt Mode 1 together specify the venous character available conditions:
Mode
0
1
04
03
Rec Int
Mode 1
Rec Int
0
0
Mode 0
0
1
12-11
INTERRUPT EXAMPLES
A Startup Interrupt
Purpose: The computer waits for a Pia interrupt to occur before starting actual operations.
Many systems remain inactive until the operator actually starts them or a DATA READY
signal IS received. On RESET. such systems must initialize the Stack Pointer. enable the
startup interrupt. and execute a HALT Instruction. Remember that RESET disables the
processor interrupt and power-on disables all Pia Interrupts. In the flowchart. the decision as to whether startup is active is made In hardware O.e.. by the CPU examining the
interrupt Input Internally) rather than in software.
Flowchart:
Source Program:
Main Program:
RESET
EQU
ORG
LD
LD
OUT
LD
OUT
EI
HALT
o
RESET
SP.l00H
A.Ol 001111 B
(PIOCRA).A
A.l 0000111 B
(PIOCRA).A
JP
INTRP
SP.l00H
START
12-12
Object Program:
Memory Address
(Hex)
Memory Contents
(Hexl
Main Program:
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
Instruction
(Mnemonic)
31
00
01
3E
4F
03
PIOCRA
3E
87
03
PIOCRA
FB
76
LD
SP.100H
LD
A.01001111 B
OUT
(PIOCRA),A
LD
A.10000111 B
OUT
(PIOCRA),A
31
00
01
C3
LD
SP.100H
JP
START
EI
HALT
START
The main program must initialize the Stack Pointer. since the interrupt response always
stores the old Program Counter in the Stack. Here the service routine simply reinltializes
the Stack Pointer before the actual startup routine is executed. An alternative would be
to increment the Stack POinter twice before jumping to the startup routine. Remember
that the Z80 comes up In Interrupt Mode O. Any other mode would require the execution of an 1M Instruction.
INTERRUPTS ON
The exact location of the Interrupt service routine varies
PARTICULAR
with the microcomputer. If your microcomputer has no
MICROCOMPUTERS
monitor. you can start the interrupt service routine
wherever the external hardware or vector table directs the
CPU. Of course. yOU should place the routine so that It does not Interfere with fixed addresses or with other programs.
If your microcomputer has a monitor. the monitor will often occupy the RESET and Interrupt service addresses. It will then supply
service routines or the addresses of those routines. A typical monitor routine initialization would be:
.
MONIN:
PUSH
LD
JP
HL
HL.USRINT
(HU
INTERRUPT
HANDLING
BY
MONITORS
You must then place the address of your service routine into memory locations USRINT
and USRINT+1. using the normal Z80 address format with the least significant bits at
the lower address. Remember that MONIN is an address in the monitor program.
12-13
You can Include the loading of memory locations USRINT and USRINT+1 in your main
program: I.e..
LD
HUNTRP
LD
(USRINTl.HL
A Keyboard Interrupt
Purpose: The computer waits for a keyboard interrupt and places
the data from the keyboard into memory location 0040.
Sample Problem:
Keyboafd data
Result:
(0040)
06
06
Flowchart:
1401
data
12-14
Source Program:
Main Program:
RESET
HERE:
EQU
ORG
LD
LD
OUT
LD
OUT
EI
JR
o
RESET
SP,lOOH
A.01001111B
(PIOCRA).A
A,10000111B
(PIOCRA).A
HERE
INTRP
AF.AF'
A.(PIODRA)
(40H).A
AF.AF
Object Program:
Memorv Address
(Hex)
Memorv Contents
(Hex)
Instruction
(Mnemonic)
Main Program:
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
0008
OOOC
OOOD
31
00
01
3E
4F
D3
PIOCRA
3E
87
D3
PIOCRA
FB
18
FE
HERE:
LD
SP,100H
LD
A.01001111 B
OUT
(PIOCRA).A
LD
A,10000111B
OUT
(PIOCRA).A
EI
JR
HERE
EX
IN
AF.AF'
A.(PIODRA)
LD
(40Hl.A
EX
EI
RETI
AF,AF'
08
DB
PIODRA
32
40
00
08
FB
ED
4D
12-15
The JR HERE is an endless loop (jump-ta-self) instruction that is used to represent the
main program. After interrupts are enabled in a working system, the main program goes
about its business until an Interrupt occurs and then resumes execution after the interrupt service routine is completed.
The RET instruction at the end of the service routine transfers
control back to the JR instructiOn. If you want to avoid this, you
can simply Increment the Program Counter In the Stack, e.g..
EX
INC
INC
EX
(SP).HL
HL
HL
(SPl.HL
CHANGING THE
RETURN
ADDRESS
The RET instruction will now transfer control to the Instruction follOWing the JR. Note
the use of EX (SP).HL: this instruction exchanges the contents of Register Pair HL with
the contents of the memory locatiOns at the top of the Stack. By using It we can adjust
the return address without affecting the contents of Register Pair HL.
Since the zao does not automatically save its registers, you can use them to pass
parameters and results between the main program and the interrupt service routine. So,
yOU could leave the data in the Accumulator instead of in memory location 0040. This
is, however, a dangerous practice that should be aVOided in all but the most triVial
systems. In most applications, the processor is using its registers during normal program execution: having the Interrupt service routines randomly change the contents of
those registers would surely cause havoc. In general. no Interrupt service routine should
ever alter any register unless that register's contents have been saved prior to its alteration and will be restored at the completion of the routine.
Note that you must explicitly re-enable the Interrupts anhe end of the service routine,
since the processor disables the interrupt system when It accepts an interrupt. Servicing a PIO interrupt deactivates the interrupt Signal so that the same Interrupt is not serviced again.
SAVING
If interrupt service routines are never themselves interrupted (i.e..
there is only one level of interrupts). the instructions EX AFAF
VALUES IN
and EXX are a convenient way to save and restore the old contents
PRIMED
of the user registers. EXX exchanges the contents of BC, DE, and
REGISTERS
HL with the contents of their primed equlyalents. The two instructions together take only two bytes of memory and eight clock cycles. However, this
method cannot be used if there are other interrupt levels (since there IS only a single set
of primed registers) or if the primed registers are needed In either the main program or
the Interrupt service routine.
A more general approach to saving and restoring registers is to use the Stack. PUSH
sayes the contents of a register pair and POP restores the contents. Howeyer, PUSH
takes 11 clock cycles and POP 10, so this approach is slower. It also uses extra memorv
locations In the Stack. The advantage of this method IS that it can be expanded indefinitely (as long as there is room in the Stack) since nested service routines will not
destroy the data saved by the earlier routines.
An alternative approach would be for the Interrupt routine to
maintain control until it received an entire line of text (e.g.. a string
of characters ending with a camage return). The main program
would be:
12-16
FILLING A
BUFFER VIA
INTERRUPTS
Main Program:
RESET
HERE:
EQU
ORG
LD
LD
OUT
LD
OUT
LD
LD
EI
JR
0
RESET
SP,100H
A.01001111 B
(PIOCRA),A
A.10000111 B
(PIOCRA),A
HL.70H
(40Hl.HL
HERE
ENDL:
ORG
EX
EXX
LD
IN
LD
CP
JR
INC
LD
EXX
EX
EI
RETI
JP
INTRP
AF,AF
HL.(40H)
A.(PIODRA)
(HL).A
CR
Z,ENDL
HL
(40Hl.HL
;SAVE A. FLAGS
:SAVE OTHER REGISTERS
:GET BUFFER POINTER
;GETKEYBOARD DATA
:SAVE DATA IN BUFFER
:IS DATA A CARRIAGE RETURN?
:YES. END OF LINE
:NO. INCREMENT BUFFER POINTER
AF.AF
LPROC
When the processor receives a carriage return. It leaves the Interrupt system disabled
while It handles the line,
An alternative approach would be to fill another buffer while handling the first one: this approach is called double buffering.
The line processing routine IS begun at address LPROC with interrupts disabled. the old register contents in the primed registers. and the onglnal return
address at the top of the Stack.
In a real application. the CPU could perform other tasks between interrupts. It could. for
Instance. edit. move. or transmit a line from one buffer while the Interrupt was filling
another buffer.
A Printer Interrupt
Purpose: The computer waits for a printer interrupt and sends the data from memory
location 0040 to the printer.
Sample Problem:
(0040)
51H
Result:
12-17
Flowchart:
Source Program:
Main Program:
RESET
HERE:
EQU
GRG
LD
LD
OUT
LD
OUT
EI
JR
a
RESET
SP,100H
A.00001111 B
(PIOCRA),A
A.1 0000111 B
(PIOCRA),A
HERE
EX
LD
OUT
EX
EI
RETI
INTRP
AF.AF
A. (40H)
(PIODRA),A
AF.AF
12-18
Object Program:
Memorv Address
(Hex)
Memorv Contents
(Hex)
Instruction
(Mnemonic)
Main Program:
0000
0001
0002
0003
0004
0005
0006
0007
OOOS
0009
OOOA
OOOB
OOOC
0000
31
00
01
3E
OF
03
PIOCRA
3E
S7
03
PIOCRA
FS
lS
FE
HERE:
LD
SP.l00H
LD
A.OOOOllll B
OUT
(PIOCRA).A
LD
A.l0000111 B
OUT
(PIOCRA).A
EI
JR
HERE
EX
lD
AF.AF
A.(40H)
OUT
(PIODRA).A
EX
EI
RETI
AF.AF
INTRP
INTRP+l
INTRP+2
INTRP+3
INTRP+4
INTRP+5
INTRP+6
INTRP+7
INTRP+S
INTRP+9
Here. as with the keyboard. you could have the printer continue to
interrupt until it transferred an entire line of text. The main program and the service routine would be:
EMPTYING A
BUFFER WITH
INTERRUPTS
Main Program:
RESET
HERE:
EQU
ORG
LD
LD
OUT
lD
OUT
LD
LD
EI
JR
o
RESET
SP.l00H
A.00001111 B
(PIOCRA).A
A.10000111 B
(PIOCRA),A
HL,70H
(40H).HL
HERE
12-19
ENOL:
ORG
EX
EXX
LD
LD
OUT
CP
JR
INC
LD
EXX
EX
EI
RETI
JP
INTRP
AF.AF
HL.(40H)
A.(HU
(PIODRAl.A
CR
ENOL
HL
(40Hl.HL
;SAVE A. FLAGS
;SAVE OTHER REGlSTERS
:GET BUFFER POINTER
:GET A BYTE OF DATA FROM BUFFER
:SEND DATA TO PRINTER
:IS DAtA A CARRIAGE RETURN?
;YES. END OF LINE
:NO. INCREMENT BUFFER POINTER
AF.AF'
LCOMP
Again. double buffering could be used to allow I/O and processing to occur at the same
time without ever halting the CPU
A real-time clock simply provides a regular series of pulses. The interval between the pulses can be used as a time reference. Real-time clock interrupts
can be counted to give any multiple of the baSIC time Interval. A real-time clock can be
produced by dividing down the CPU clock. by uSing a separate timer or a programmable timer like the CTC for ZBO-based microcomputers. or by using external sources such
as the AC line frequency.
FREQUENCY
Note the tradeoffs involved in determining the frequency of the
real-time clock. A high frequency (say 10 kHz) allows the creaOF REAL-TIME
CLOCK
tion of a wide range of time intervals of high accuracy. On the
other hand. the overhead Involved In counting real-time clock
Interrupts may be considerable. and the counts will quickly exceed the capacity of a
single B-bit register or memory location. The choice of frequency depends on the precIsion and timing requirements of your application. The clock may. of course. consist
partly of hardware: a counter may count high frequency pulses and Interrupt the processor only occasionally. A program will have to read the counter to measure time to
high accuracy.
SYNCHRONIZATION
WITH REAL-TIME
CLOCK
Start the CPU and clock together. RESET or a startup interrupt can start the clock as
well as the CPU.
Allow the CPU to start and stop the clock under program control.
3)
Use a high-frequency clock so that an error of less than one clock period will be
small.
4)
line up the clock (by waiting for an edge or interrupt) before starting the measurement.
12-20
a)
PRIORITY
OF REAL-TIME
CLOCK
so that it does not
Source Program:
Main Program:
RESET
HERE:
EQU
ORG
LD
LD
OUT
LD
OUT
EI
JR
o
RESET
SP.100H
A.01001111B
(PIOCRAl.A
A,10000111B
(PIOCRAl.A
HERE
INTRP
;END CLOCK INTERRUPT
Object Program:
Memory Address
(Hex)
Instruction
(Mnemonic)
Memory Contents
(Hex)
Main Program:
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
OOOD
31
00
01
3E
4F
D3
PIOCRA
3E
87
D3
PIOCRA
FB
18
FE
HERE:
LD
SP,100H
LD
A,01001111B
OUT
(PIOCRAl.A
LD
A.10000111B
OUT
(PIOCRAl.A
EI
JR
HERE
HALT
76
The service routine does not have to do anything, since servicing the PIO Interrupt automatically clears It and there is no data to send or receive.
The real-time clock interrupt always occurs on'a rising edge if a PIO STROBE signal is
used for the clock input.
12-21
bl
Source Program:
Main Program:
RESET
EQU
ORG
LD
LD
OUT
LD
OUT
LD
LD
LD
EI
WTTEN: CP
JR
o
RESET
SP,100H
A0100llllB
(PIOCRAl.A
A.l0000lllB
(PIOCRAl.A
HL.40H
(HL).O
Al0
(HL)
NZ.wTTEN
HALT
= ZERO
:NUMBER OF COUNTS = 10
:ENABLE CPU INTERRUPTS
:HAVE TEN COUNTS ELAPSED?
:NO. WAIT
:YES. DONE
EXX
EX
LD
INC
EX
EXX
EI
RETI
INTRP
AF.AF'
HL.40H
(HL)
AF.AF'
12-22
Object Program:
Memory Address
(Hex)
M!lmory Contents
(Hex)
Instruction
(Mnemonic)
Main Program:
0000
0001
0002
0003
0004
0005
0006
0007
OOOS
0009
OOOA
OOOB
OOOC
OOOD
OOOE
OOOF
0010
0011
0012
0013
0014
0015
0016
31
00
01
3E
4F
D3
PIOCRA
3E
S7
D3
PIOCRA
21
40
00
36
00
3E
OA
FB
BE
20
WTTEN:
LD
SP.l00H
LD
A.Ol001111 B
OUT
(PIOCRAl.A
LD
A,10000111B
OUT
(PIOCRAl.A
LD
HLAOH
LD
(HL).O
LD
A.l0
EI
CP
JR
(HL)
NZ.WTTEN
FD
76
HALT
U9
EXX
EX
LD
OS
21
40
00
34
OS
D9
FB
ED
4D
INC
EX
EXX
EI
RETI
AF.AF'
HLAOH
(HL)
AF.AF'
An alternative approach uses the Stack to save and restore register values. To save H.
and the flags requires:
PUSH
PUSH
HL
AF
L.
AF
HL
Note that. if the Stack IS used. registers must be restored In the opposite order from that
which they were saved. Clearly the order In which EXX and EX AF.AF' are executed
does not matter.
In
12-23
This Interrupt service routine merely updates the counter In memorv location 0040. It
transparent to the main program.
hundredths of seconds
seconds
minutes
hours
IS
Hundredths
Seconds
Seconds + 1
Seconds = 0
Minutes
Minutes + 1
Minutes
Hours
Hours
End
12-24
+ 1
IS
MAINTAINiNG
REAL TIME
Source Program:
DONE:
ORG
PUSH
PUSH
LD
INC
LD
CP
JR
LD
INC
INC
LD
CP
JR
LD
INC
INC
CP
JR
LD
INC
INC
LD
JR
LD
POP
POP
EI
RETI
INTRP
AF
HL
HLAOH
(HL)
A.100
(HL)
NZ.DONE
(HL).O
HL
(HL)
A.50
(HL)
NZ.DONE
(HL).O
HL
(HL)
(HL)
NZ,DONE
(HL).O
HL
(HL)
A.24
NZ,DONE
(HL).O
HL
AF
:SAVE REGISTERS
:UPDATE HUNDREDTHS OF SECONDS
Now a walt of 300 ms could be produced in the main program with the routine:
WT30:
LD
LD
ADD
CP
JR
SUB
CP
JR
HLAOH
A.(HL)
A.30
100
100
(HL)
NZ,WT30
c,Wno
Be careful In thiS program of the difference between INC HL and INC (HL). INC HL adds
1 to the 15-blt contents of Register Pair HL. while INC (HU adds 1 to the B-bit contents
of the memory location addressed by HL.
Of course, the program could perform other tasks and check the elapsed time only occasionally. How would you produce a delay of seven seconds? Of three minutes?
Sometimes you may want to keep time either as BCD digits or as ASCII characters. How
would you revise the last program to handle these alternatives?
You can disable the clock Interrupt (or any other interrupt) when it
IS no longer needed In any of the following ways.
DISABLING
INTERRUPTS
1)
By executing a 01 instruction in the main program. This disables the entire interrupt system.
2)
By clearing bit 7 of the interrupt control word dUring the service routine or dUring
the main program. This disables only the interrupt from one port of one PIO.
12-25
3)
Remember that the CPU automatically disables interrupts upon accepting one. Thus.
the interrupt system is disabled unless the service routine explicitly re-enables it. Note.
however. that you must be very careful about not re-enabling the interrupts. since the
main program would be completely unaware that interrupts were no longer allowed. In
general. all Interrupt service routines should 're-enable the interrupts before returning; any other policy means that the service routines are not transparent to the main
program.
A Teletypewriter Interrupt
Purpose: The computer walts for data to be received from a teletypewriter and stores
the data in memory location 0040.
al
Using an SIO
SIO
INTERRUPT
ROUTINE
Source Program:
Main Program:
RESET
HERE:
EQU
LD
OUT
LD
OUT
LD
OUT
LD
OUT
LD
OUT
LD
OUT
EI
JR
0
AA
(SIOCRA).A
A01000001B
{SIOCRA).A
A.3
{SIOCRA).A
A.01000001B
{SIOCRA).A
A1
(SIOCRA).A
A.00011000B
(SIOCRA).A
HERE
INTRP
AF
A.(SIODRA)
(40H).A
AF
12-26
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
Instruction
(Mnemonic)
Main Program:
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
OOOD
OOOE
OOOF
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
001A
3E
04
D3
SIOCRA
3E
41
D3
SIOCRA
3E
03
D3
SIOCRA
3E
41
D3
SIOCRA
3E
01
D3
SIOCRA
3E
18
D3
SIOCRA
FB
18
FE
HERE:
LD
AA
OUT
(SIOCRA).A
LD
A01000001B
OUT
(SIOCRA).A
LD
A.3
OUT
(SIOCRAl.A
LD
AOl 000001 B
OUT
(SIOCRA),A
LD
A.l
OUT
(SIOCRAl.A
LD
A.00011000B
OUT
(SIOCRA).A
EI
JR
HERE
PUSH
IN
AF
A.(SIODRA)
LD
(40Hl.A
POP
EI
RETI
AF
F5
DB
SIODRA
32
40
00
Fl
FB
ED
4D
This service routine assumes that only the receive interrupt from one channel of the SIO
has been enabled. Otherwise. either further vectoring will be reqUired by changing control bits 02. 03. and 04 of Write Register 0 (see the discussion of SIO interrupts earlier
in this chapter) or the routine will have to examine the status bits in Read Register O.
The key status bits are:
Bit 0 - Receive Character Available -1 when at least one character is available in the
receive buffers.
Bit 1 - Interrupt pending (Channel A only) -1 if any interrupt IS pending in the entire
SIO.
12-27
B.6
C,SIOCRA
HL.SIOTBL
This method requires 9 bytes of memory for the program and 6 bytes for the table, as
compared to the 23 bytes used In the example to configure the SIO.
The program establishes the SIO registers as follows:
WRITE REGISTER 4
Bit 7
= 0,
bit 6
= 1 for X16
clock mode
bl
Using a PIO
HERE:
LD
OUT
LD
OUT
LD
OUT
LD
OUT
EI
JR
A,11001111B
(PIOCRA),A
A,10000000B
(PIOCRAl.A
A,10010111B
(PIOCRA).A
A,01111111B
(PIOCRAl.A
HERE
INTRP
AF
A,000001118
(PIOCRAl.A
TTYRCV
A,100001118
(PIOCRAl.A
AF
12-28
Object Program:
Memory Address
(Hex)
Memory Contents
(Hex)
Instruction
(Mnemonic)
Main Program:
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
OOOA
OOOB
OOOC
0000
OOOE
OOOF
0010
0011
0012
LO
A,11 001111 B
OUT
(PIOCRAl.A
LO
A, 1OOOOOOOB
OUT
(PIOCRAl.A
LO
A,10010111B
OUT
(PIOCRA).A
LO
A.Olll1111 B
OUT
(PIOCRAl.A
EI
JR
HERE
F5
3E
07
03
PIOCRA
CO
TIVRCV
PUSH
LO
AF
A.OOOOOlll B
OUT
(PIOCRA).A
CALL
TIVRCV
3E
87
03
PIOCRA
Fl
FB
EO
40
LO
A.l0000lllB
OUT
{PIOCRA).A
POP
EI
RETI
AF
3E
CF
03
PIOCRA
3E
80
03
PIOCRA
3E
97
03
PIOCRA
3E
7F
03
PIOCRA
FB
18
FE
HERE:
These programs assume that the monitor initializes the Stack Pointer. Otherwise. it will
have to be loaded In the main program.
Subroutine TTVRCV is the TTV receive routine shown In the prevIous chapter.
The edge used to cause the Interrupt IS very Important here. An Interrupt must occur
when the data line changes from the normal MARK or '1' state to the SPACE or '0' state.
since this transition identifies the start of the transmission.
The service routine must disable the PIO Interrupt. since otherwise each '1' -to-'O' transition In the character will cause an interrupt. Of course. you must re-enable the PIO interrupt after the entire character has been read.
12-29
3)
4)
The net result is for an interrupt to be generated if bit 7 is zero or changes from one to
zero. Note that further interrupts occur only when a change occurs In the status of the
logical equation. Here again. the PIO could be configured by using a table and the repeated block output Instruction.
TASKS FOR
GENERAL SERVICE
ROUTINES
Remember that the Z80 Push instruction transfers a register pair (or an index register)
to the Stack. PUSH AF (F is the Flag register) transfers the Accumulator and flags to the
Stack.
AF
BC
DE
HL
IX
IY
AF.AF'
:SAVE
:SAVE
;SAVE
:SAVE
:SAVE
;SAVE
ACCUMULATOR. FLAGS
REGISTERS B.C
REGISTERS D.E
REGISTERS H.L
INDEX REGISTER IX
INDEX REGISTER IY
AF
BC
DE
HL
;SAVE
;SAVE
;SAVE
;SAVE
PRIMED
PRIMED
PRIMED
PRIMED
ACCUMULATOR. FLAGS
REGISTERS B.C
REGISTERS D.E
REGISTERS H.L
Of course. only those registers that are used by the Interrupt service routine must be
saved.
2)
3)
RestOring all registers from the Stack after completing the Interrupt service routine.
Remember that registers must be restored In the opposite order from that In which
they were saved.
Enabling and disabling interrupts appropriately. Remember that the CPU automatically disables ItS Interrupts upon accepting one.
The service routines should be transparent as far as the Interrupted program IS concerned (j.e.. they should have no incidental effects).
12-30
Any standard subroutines that are used by an Interrupt service routine must be
reentrant. If some subroutines cannot be made reentrant. the interrupt service routine
must have separate versions to use. 5
PROBLEMS
1) A Test Interrupt
Purpose: The computer walts for a Pia interrupt to occur. then executes the endless
loop instruction:
HERE:
JR
HERE
2)
A Keyboard Interrupt
Purpose: The computer waits for a 4-digit entry from a keyboard and places the digits
Into memory locations 0040 through 0043 (first one received in 0040). Each
digit entry causes an Interrupt. The fourth entry should also result in the disabling of the keyboard interrupt.
Sample Problem:
3)
(0040)
(0041)
(0042)
(0043)
04
06
01
07
A Printer Interrupt
Purpose: The computer sends four characters from memory locations 0040 to 0043
(starting with 0040) to the printer. Each character is requested by an interrupt. The fourth transfer also disables the printer interrupt.
4)
Purpose: The computer clears memory location 0040 initially and then complements
memory location 0040 each time the real-time clock Interrupt occurs.
How would you change the program so that it complements memory location 0040
after every ten Interrupts? How would you change the program so that it leaves memory location 0040 at zero for ten clock penods. FF16 for five clock periods, and so on continuously? You may want to use a display rather than memory location 0040 so that it
will be easier to see.
5)
A Teletypewriter Interrupt
Purpose: The computer receives TTY data from an interrupting SIO and stores the
characters In a buffer starting in memory location 0040. The process continues until the computer receives a carriage return (OD16).
Assume that the characters are 7-blt ASCII With odd panty. How would you change
your program to use a Pia? Assume that subroutine TTYRCV is available. as In the example. Include the carriage return as the final character in the buffer.
12-31
REfERENCES
1.
2.
3.
4.
5.
For further discussion and some real-life examples of designing Z80-based systems
with Interrupts, see pp. 5-24 through 5-37 of Z80 Programming for Logic Design
and the following:
Baldridge, R. L..""lnterrupts Add Power, Complexity to Microcomputer System
Design," EDN. August 5, 1977. pp. 67-73.
Pond, R. M.. "Let Microprocessors Communicate:' Electronic Design, Noyember
1977, pp. 88-90.
--
8.
12-32
Chapter 13
PROBLEM DEFINITION AND
PROGRAM DESIGN
THE TASKS OF SOFTWARE DEVELOPMENT
In the previous chapters. we have concentrated on the writing of short programs in assembly language. While this is an Important tOpiC. it IS only a small part of software
development. Although writing assembly language programs IS a major task for the
beginner. it soon becomes Simple. By now. you should be familiar with standard
methods for programming In assembly language on the l80 microprocessor. The next
four chapters will describe how to formulate tasks as programs and how to combine short programs to form a working system.
STAGES OF
SOFTWARE
DEVELOPMENT
Problem definition
Program design
Coding
Debugging
Testing
Documentation
Maintenance and redeSign
Each of these stages IS Important in the construction of a working system. Note that
coding. the writing of programs in a form that the computer understands. is only one of
seven stages.
In fact. coding is usually the easiest stage to define and perRELATIVE
IMPORTANCE
form. The rules for writing computer programs are easy to learn.
They vary somewhat from computer to computer. but the basIc
OF CODING
techniques remain the same. Few software prOjects run into trouble because of coding: indeed. coding IS not the most time-consuming part of software
development. Experts estimate that a programmer can write one to ten fully debugged
and documented statements per day. Clearly. the mere coding of one to ten statements
is hardly a full day's effort. On most software prolects. coding occupies less than 25% of
the programmer's time.
13-1
Problem definition
Program design
Debugging
Testing
No
No
Documentation
Maintenence end
nedesign
13-2
Ir"'C-O-D-I-N-G""1
PROBLEM DEFINITION
Typical microprocessor tasks require a lot of definition. For example. what must a program do to control a scale. a cash register. or a signal generator? Clearly. we have a
long way to go Just to define the tasks involved.
13-3
What is ItS form; I.e., what signals will the computer actually
receive?
2)
When is the Input available and how does the processor know It is available? Does
the processor have to request the Input with a strobe signal? Does the input proVide Its own clock?
How long is It available?
3)
4)
5)
6)
How often does it change. and how does the processor know that it has changed?
Does the Input consist of a sequence or block of data? Is the order Important?
What should be done if the data contains errors? These mav include transmission
errors. incorrect data. sequencing errors. extra data. etc.
7)
What IS ItS form; I.e.. what signals must the computer produce?
When must it be available. and how does the peripheral know It IS available?
How long must it be available?
4)
5)
How often must It change. and how does the peripheral know that it has changed?
Is there a sequence of outputs? Is the order Important?
6)
What should be done to avoid transmission errors or to sense and recover from peripheral failures?
7)
13-4
PROCESSING SECTION
Between the reading of input data and the sending of output results IS the processing
section. Here we must determine exactly how the computer must process the input data. The questions are:
1)
2)
3)
4)
5)
6)
7)
ERROR HANDLING
An important factqr in many applications is the handling of errors. Clearly. the
deSigner must make provisions for recovering from common errors and for diagnosing
malfunctions. Among the questions that the designer must ask at the definition
stage are:
1)
2)
3)
4)
5)
6)
Another question is: How can the field techniCian systematically find the source of
malfunctions Without being an expert? Built-In test programs. special diagnostics. or
signature analYSIS can help.1
13-5
HUMAN FACTORS
Many microprocessor-based systems Involve human interaction.
Human factors must be considered throughout the development process for such systems. Among the questions that the
designer must ask are:
1)
2)
3)
4)
OPERATOR
INTERACTION
What input procedures are most natural for the human operator?
Can the operator easily determine how to begin. continue and end the input
operations?
How IS the operator Informed of procedural errors and equipment malfunctions?
What errors is the operator most likely to make?
6)
How does the operator know that data has been entered correctly?
Are displays In a form that the operator can easily read and understand?
7)
8)
5)
9)
10)
11)
Building a system for people to use is difficult. The microprocessor can make the
system more powerful. more flexible. and more responsive. However. the designer still
must add the human touches that can greatly increase the usefulness and attractiveness of the system and the productivity of the human operator. 2
EXAMPLES
Response to a Switch
Figure 13-2 shows a simple system in which the input is from
a single SPST switch and the output is to a single LED display.
In response to a switch closure. the processor turns the display on for one second. ThiS system should be easy to define.
DEFINING
SWITCH AND
LIGHT
SYSTEM
Let us first examine the input and answer each of the questions
previously presented:
SWITCH AND
LIGHT INPUT
1)
2)
3)
The input IS available for at least several milliseconds after the closure.
4)
The input will seldom change more than once every few seconds. The processor
has to handle only the bounce In the SWitch. The processor must monitor the
SWitch to determine when it is closed.
There is no sequence of Inputs.
5)
6)
7)
The obVIOUS Input errors are switch failure. failure In the input CirCUitry, and the
operator attempting to close the SWitch again before a sufficient amount of time
has elapsed. We will discuss the handling of these errors later.
The Input does not depend on any other Inputs or outputs.
13-6
+5V
Input
Port
+5V=--=!-
cpu
Output
Port
SWITCH
AND LIGHT
OUTPUTS
1)
The output IS a single bit which is '0' to turn the display on,
'1' to turn it off.
2)
There are no time constraints on the output. The peripheral does not need to be informed of the availability of data.
If the displav is an LED, the data need be available for only a few milliseconds at a
pulse rate of about 100 times per second. The observer will see a continuously lit
display.
The data must change (go off) after one second.
There is no sequence of outputs.
The possible output errors are display failure and failure In the output circuitry.
The output depends only on the switch input and time.
3)
4)
5)
6)
7)
The processing section is extremely simple. As soon as the switch input becomes
a logic '0', the CPU turns the light on (a logic '0') for one second. No time or memory constraints exist.
Let us now look at the possible errors and malfunctions. These
are:
SWITCH AND
LIGHT ERROR
HANDLING
Surely the first error IS the most likely. The simplest solution is for the processor to Ignore switch closures until one second has elapsed. This bnef unresponsive penod will
hardly be noticeable to the human operator. Furthermore, ignoring the switch dunng
this period means that no debouncing cirCUitry or software IS necessary, since the
system will not react to the bounce anyway.
13-7
Clearly. the last three failures can produce unpredictable results. The display may stay
on. stay off. or change state randomly. Some possible ways to Isolate the failures would
be:
Lamp-test hardware to check the display; i.e.. a button that turns the light on
independently of the processor
A direct connection to the sWitch to check ItS operation
A diagnostic program that exercises the input and output circuits
If both the display and sWitch are working. the computer is at fault. A field technician
with proper equipment can determine the cause of the failure.
We shall first examine the inputs. The characteristics of the sWitches are the same as
in the previous example; however. here there IS a distinct sequence of inputs, as
follows:
1)
2)
3)
4)
5)
6)
The operator must set the data sWitches according to the eight most significant
bits of an address, then
press the High Address button. The high address bits will &ppear on the lights, and
the program will interpret the data as the high byte of the address.
Then the operator must set the data switches with the value of the least significant
byte of the address and
press the Low Address button. The low address bits will appear on the lights, and
the program will consider the data to be the low byte of the address.
Finally, the operator must set the desired data into the data sWitches and
press the Data button. The display will now show the data. and the program stores
the data in memory at the previously entered address.
The operator may repeat the process to enter an entire program. Clearly, even in this
simplified situation, we will have many possible sequences to consider. How do we
cope with erroneous sequences and make the system easy to use?
Output is no problem. After each input, the program sends to the displays the
complement (since the displays are active-Iowl of the input bits. The output data
remainS the same until the next input operation.
The processing section remains quite simple. There are no time or memory constraints. The program can debounce the switches by waiting for a few milliseconds. and
must provide complemented data to the displays.
13-8
Input
Port
DPORT
Data
Bus
High Address
Input
CPU
Low Address
Port
CPORT
Data
+5V
Output
Port
13-9
Incorrect entries
Incorrect order
Incomplete entries; for example. forgetting the data
MEMORY
LOADER
ERROR
HANDLING
OPERATOR
ERROR
CORRECTION
IN MEMORY
LOADER
Erroneous data
Wrong order of entries or sWitches
Trying to go on to the next entry Without completing the current
one
The operator will presumably notice erroneous data as soon as it appears on the displays. What IS a viable recovery procedure for the operator? Some of the options are:
1)
2)
3)
The operator must complete the entry procedure; I.e.. enter Low Address and Data
if the error occurs In the High Address. Clearly. this procedure is wasteful and
would only serve to annoy the operator.
The operator mav restart the entry process by returning to the high address entry
steps. ThiS solution IS useful if the error was in the High Address. but forces the
operator to re-enter earlier data if the error was in the Low Address or Data stage.
The operator may enter any part of the sequence at any time simply by setting the
Data switches With the desired data and pressing the corresponding button. ThiS
procedure allows the operator to make corrections at any point in the sequence.
ThiS type of procedure should always be preferred over one that does not allow Immediate error correction. has a variety of concluding steps. or enters data Into the svstem
without allowing the operator a final check. Any added complication in hardware or
software will be justified in increased operator efficiency. You should always prefer to
let the microcomputer do the tedious work and recognize arbitrary sequences; It never
gets tired and never forgets what was in the operating manual.
A further helpful feature would be status lights that would define the meaning of the
displav. Three status lights. marked "High Address", "Low Address", and "Data".
wou Id let the operator know what had been entered without haVing to remember which
button was pressed. The processor would have to monitor the sequence. but the added
complication in software would simplify the operator's task. Clearly. three separate sets
of displays pius the ability to examine a memory location would be even more helpful to
the operator.
13-10
,---
Kevboard
Input Port
Kevboard Strobe
A-
I Kevboard Data
.....
~
CPU
---.
Dispiay
.....
Output POrUsi
"Y
> Displav
.-
XMIT
Output Port
"--
RCV
Input Port
Data Strobe
..oL
.....
.
Status Light
Output Port
BUSY Displav
READY Displav
A Verification Terminal
DEFINING A
Figure 13-4 is a block diagram of a simple credit-verification
VERIFICAnON
terminal. One input port derives data from a keyboard (see
Figure 13-5): the other input port accepts verification data
TERMINAL
from a transmission line. One output port sends data to a set of
displays (see Figure 13-6): another sends the credit card number to the central
computer. A third output port turns on one light whenever the terminal is ready to
accept an inquiry, and another light when the operator sends the information. The
"Busy" light turns off when the response returns. Clearly. the Input and output of
data will be more complex than In the previous case. although the processing is still
simple.
13-11
ODD
DDDB
DOOG
D
BUSY
READY
The display conSists of ten 7-segment displays. whIch may be multiplexed. controlled bv a shift
register, or addressed separateIv. Two additional lights, READY end BUSY, are also present.
13-12
Additional displays may be useful to emphasize the meaning of the response. Many terminals use a green light for "Yes". a red light for "No". and a yellow light for "Consult
Store Manager." Note that these lights will still have to be clearly marked with their
meanings to allow for a color-blind operator.
VERIFICATION
Let us first look at the keyboard input. This is. of course,
TERMINAL
different from the switch input. since the CPU must have some
INPUTS
way of distinguishing new data. We will assume that each key
closure provides a unique hexadecimal code (we can code
each of the 12 keys into one digiti and a strobe. The program will have to recognize the strobe and fetch the hexadecimal number that identifies the key. There IS a
time constraint. since the program cannot miss any data or strobes. The constraint IS
not serious. since keyboard entries will be at least several milliseconds apart.
The transmission input similarly consists of a series of characters. each identified
by a strobe (perhaps from a UARTI. The program will have to recognize each
strobe and fetch the character. The data being sent across the transmission lines
is usually organized into messages. A possible message format is:
Header
Terminal address
Credit card number
Trailer
A central communications computer may poll the terminals. checking for data
ready to be sent.
The processing in this system involves many new tasks. such as:
IdentifYing the control keys by number and performing the proper actions
Adding the header, terminal address, and trailer to the outgOing message
Recognizing the header and trailer in the returning message
Checking the Incoming terminal address
13-13
Note that none of the tasks involve any complex arithmetic or any
serious time or memory constraints.
VERIFICATION
TERMINAL
ERROR
HANDLING
Many equipment failures are also possible. Besides the displays, keyboard, and
processor, there now exist the problems of communications errors or failures and
central computer failures.
13-14
The data transmission will probably have to Include error checking and correcting procedures. Some possibilities are:
1)
2)
Short messages may use more elaborate schemes. For example. the yes/no
response to the terminal could be coded so as to provide error detection and correction capability.
3)
CORRECTING
TRANSMISSION
/ ERRORS
A communications or central computer failure indicator should also "unlock" the terminal. Le.. allow it to accept another entry. ThiS is necessary if the terminal will not accept entries while a verification is in progress. The terminal may also unlock after a certain maximum time delay. Certain entries could be reserved for diagnostics: Le.. certain
credit card numbers could be used to check the internal operation of the terminal and
test the displays.
13-15
PROGRAM DESIGN
Program design is the stage in which the problem definition is formulated as a program. If the program is small and simple. this stage may involve little more than
the writing of a one-page flowchart. If the program is larger or more complex. the
designer should consider more elaborate methods
We will discuss flowcharting. modular programming. structured programming. and
top-down design. We will try to indicate the reasoning behind these methods. and
their advantages and disadvantages. We will not, however. advocate any particular
method since there is no evidence that one method is always superior to all others. You
should remember that the goal is to produce a good working system. not to follow
religiously the tenets of one methodology or another.
All the methodologies do, however. have some obvious principles in common. Many of these are the same principles that apply
to any kind of design. such as:
1)
2)
3)
4)
5)
6)
7)
8)
BASIC
PRINCIPLES
OF PROGRAM
DESIGN
9)
10)
Have your design completely formulated before you start coding. Resist the
temptation to start writing down instructions: it makes no more sense than making parts lists or laYing out circuit boards before you know exactly what will be In
the system.
11)
Be particu larly carefu I of factors that may change. Make the implementation of
likely changes as simple as possible.
13-16
Input/Output
o
<:>
oo
1
(
Processing operation
(Arithmetic. LogIC. Data Movement)
Decision logiC
Subroutine
Connector pOint
Connector arrows
Terminal pomt
(Beginning or EndingJ
FLOWCHARTING
Flowcharting is certainly the best-known of all program design methods. Programming
textbooks describe how programmers first write complete flowcharts and then start
writing the actual program. In fact. few programmers have ever worked this way, and
flowcharting has often been more of a loke or a nuisance to programmers than a design
method. We will try to describe both the advantages and disadvantages of flowcharts,
and show the place of this technique in program design.
13-17
1)
2)
3)
4)
5)
6)
Standard symbols exist (see Figure 13-7) so that flowcharting forms are widely
recognized.
Flowcharts can be understood by someone without a programming background.
Flowcharts can be used to divide the entire project mto sub-tasks. The flowchart
can then be exammed to measure overall progress.
Flowcharts show the sequence of operations and can therefore aid m locating the
sou rce of errors.
Flowcharting is widely used m other areas besides programming.
There are many tools available to aid In flowcharting. including programmer's
templates and automated drawmg packages.
2)
3)
4)
5)
6)
DISADVANTAGES
OF
FLOWCHARTING
Flowcharts are difficult to deSign, draw, or change in all except the simplest situations.
There is no easy way to debug or test a flowchart.
Flowcharts tend to become cluttered. DeSigners find it difficult to balance between
the amount of detail needed to make the flowchart useful and the amount that
makes the flowchart little better than a program listing.
Flowcharts show only the program organization. They do not show the organization
of the data or the structure of the input/output modules.
Flowcharts do not help with hardware or tlmmg problems or give hmts as to where
these problems might occur.
Flowcharts allow for highly unstructured design. Lines and arrows backtrackmg
and looping all over the chart are the antithesis of good structured design princIples.
Thus, flowcharting is a helpful technique that you should not try to extend too far.
Flowcharts are useful as program documentation. since they have standard forms
and are comprehensible to non-programmers. As a deSign tool. however. flowcharts
cannot provide much more than a starting outline; the programmer cannot debug a
detailed flowchart and the flowchart is often more difficult to design than the program
itself.
13-18
eXAMPLES
Response to a Switch
This simple task, in which a single switch turns on a light
for one second, is easy to flowchart. In fact. such tasks are
typical examples for flowcharting books, although they form a
small part of most systems. The data structure here IS so simple
that It can be safely Ignored.
FLOWCHARTING
SWITCH AND
LIGHT SYSTEM
Figure 13-8 is the flowchart. There is little difficulty In deciding on the amount of
detail required. The flowchart gives a straightforward picture of the procedure, which
anyone could understand.
Note that the most useful flowcharts may Ignore program vanables and ask questions
directly. Of course, compromises are often necessary here. Two versions of the
flowchart are sometimes helpful- one general version in layman's language,
which will be useful to non-programmers, and one programmer's version in terms
of the program variables, which will be useful to other programmers.
13-19
Turn light on
End
FLOWCHARTING
THE
SWITCH-BASED
MEMORY LOADER
The flowchart In Figure 13-9 Includes the improvements we suggested as part of the
problem definition. Clearly. this flowchart is beginning to get cluttered and lose its
advantages over a written description. Adding other features that define the meaning of the entry with status lights and allow the operator to check entries after completion would make the flowchart even more complex. Writing the complete flowchart
from scratch could quicklV become a formidable task. However. once the program has
been written. the flowchart IS useful as documentation.
13-20
High bvte of
Address =
Switches
Ught. = Switches
Walt
Low bvte of
Address =
SWitches
lights
= SvvitchE:s
debounce
Wait
debounce
time
time
Data =SWltches
Ughts = Switches
Store Data
at Address
Walt
debounce
time
No
No
Walt
debounce
time
13-21
of Entrv Array
Key Coonter
=0
Kev= Keyboard
Input Oata
(Key PoInter)
Kev Pointer
= Kev
Key -Point... + 1
Key Counter =
Key Counter + 1
FLOWCHARTING
THE CREDIT
VERIFICATION
FLOWCHARTING
SECTIONS
Let us look at some of the sections. Figure 13-10 shows the keyboard entry process
for the digit keys. The program must fetch the data after each strobe and place the
digit into the display array if there is room for It. If there are already ten digits In the array, the program Simply ignores the entry.
The actual program will have to handle the displays at the same time. Note that either
software or hardware must de-activate the keyboard strobe after the processor reads a
digit.
13-22
Kev = Keyboard
Input Data
1)
If the operator has not entered ten digits. the program must Ignore the Send key
and place anv other key into the entrv.
2)
If the operator has entered ten digits. the program must respond to the Send kev by
transferring control to the Send routine. and Ignore all other keys.
Note that the flowchart has become much more difficult to organize and to follow.
There is also no obvious wav to check the flowchart.
13-23
Kay = Keyboard
Input Data
(Kev POinter)
= Key
Key Pointer =
Key PcMnter + 1
Figure 13-12 shows the flowchart of the keyboard entry process with all the function keys. In this example. the flow of control IS not simple. Clearly. some written
desCriptIOn IS necessary. The organization and layout of complex flowcharts requires
careful planning. We have followed the process of adding features to the flowchart one
at a time. but this still results In a large amount of redrawing. Again we should remember that throughout the keyboard entry process. the program must also refresh the displays if they are multiplexed and not controlled by shift registers or other hardware.
13-24
AJ---......
Header flag = 0
Parity Error flag = 0
Address Match f1ag=O
Address Pointer =Sta
of terminal address
Address Counter = 0
Nmess =0
Panty Error
flag = 1
Messg INmessi =
Character
Nmess = Nmess +
Header flag = 1
Address Counter =
Address Counter + 1
Address POinter =
Address Pointer + 1
light
13-25
Figure 13-13 is the flowchart of a receive routine. We assume that the serra I/para lie I
conversion and error checking are done in hardware (e.g.. by a UART). The processor
must:
1)
2)
3)
4)
5)
This routine Involves a large number of decisions, and the flowchart IS neither simple
nor obvious.
Clearly, we have come a long way from the simple flowchart (Figure 13-8) of the
first example. A complete set of flowcharts for the transaction terminal would be
a major task. It would consist of several interrelated charts with complex logic. and
would require a large amount of effort. Such an effort would be just as difficult as writIng a preliminary program. and not as useful. since you could not check it on the computer.
MODULAR PROGRAMMING
Once programs become large and complex. flowcharting is no longer a satisfactory
deSign tool. However, the problem definitIOn and the flowchart can give you some idea
as to how to diVide the program Into reasonable sub-tasks. The division of the entire
program into sub-tasks or modules is called "modular programming." Clearly. most
of the programs we presented In earlier chapters would typically be modules In a large
system program. The problems that the designer faces in modular programming are
how to divide the program into modules and how to put the modules together.
The advantages of modular programming are obvious:
ADVANTAGES
1)
OF MODULAR
PROGRAMMING
2)
3)
4)
5)
6)
Changes can be Incorporated into one module rather than into the entire system.
Errors can often be Isolated and then attributed to a Single module.
Modular programming gives an Idea of how much progress has been made and
how much of the work IS left.
13-26
DISADVANTAGES
OF MODULAR
PROGRAMMING
1)
Fitting the modules together can be a major problem. particularly if different people wnte the modules.
2)
Modules require very careful documentation. since they may affect other parts of
the program. such as data structures used by all the modules.
Testing and debugging modules separately IS difficult. since other modules may
produce the data used by the module being debugged and still other modules may
use the results. You may have to write special programs (called "dnvers") Just to
produce sample data and test the programs. These drivers require extra programming effort that adds nothing to the system.
Programs may be very difficult to modulanze. If you modulanze the program poorly.
integration will be very difficult. since almost all errors and changes will Involve
several modules.
Modular programs often require extra time and memory. since the separate
modules may repeat functions.
3)
4)
5)
PRINCIPLES OF
MODULARIZATION
Modules that reference common data should be parts of the same overall module.
Two modules In which the first uses or depends on the second. but not the reverse.
should be separate.
A module that IS used by more than one other module should be part of a different
overall module than the others.
4)
Two modules In which the first IS used by many other modules and the second IS
used by only a few other modules should be separate.
5)
Two modules whose frequencies of usage are significantly different should be part
of different modu les.
6)
If you find it very difficult to modularize your program, it is a strong indication that
the problem is poorly defined, and redefinition is called for. Too many special cases.
each requiring special handling. or the use of a large number of variables. each requirIng special processing. are problems that can be most efficiently handled. by redefining
the tasks at hand.
13-27
EXAMPLES
Response to a Switch
This simple program can be divided into two modules:
Module 1 waits for the switch to be turned on and turns
the light on in response.
MODULARIZING
THE SWITCH
AND LIGHT
SYSTEM
MODULARIZING
THE
SWITCH- BASED
MEMORY LOADER
MODULARIZING
THE
VERIFICATION
TERMINAL
A general keyboard and display module could handle many keyboard- and displaybased systems. The sub-modules would perform such tasks as:
Recognizing a new keyboard entry and fetching the data
Clearing the array in response to a Clear key
Entering digits into storage
Looking for the terminator or Send key
0 isplaYlng the dig its
13-28
Although the key Interpretations and the number of digits will vary. the basic entry.
data storage. and data display processes will be the same for many programs. Such
function keys as Clear would also be standard. Clearly. the designer must consider
which modules will be useful in other applications, and pay careful attention to
those modules.
The data transmission module could also be divided Into such sub-modules as:
1)
2)
3)
4)
5)
INFORMATION
Note here how Important it IS that each design decision (such as
the bit rate. message format. or error-checking procedure) be ImHIDING
PRINCIPLE
plemented In only one module. A change I.n any of these decisions
will then require changes only to that Single module. The other
modules should be written so that they are totally unaware of the values chosen or the
methods used in the Implementing module. An important concept here is the "information-hiding principle:'5 whereby modules share only information that is absolutely essential to getting the task done. Other information is hidden within a
single module.
An Important use of thiS principle is In error handling. Whenever a module detects a
lethal error. It should not undertake recovery procedures. Instead. it should pass the error status back up to the calling module and allow It to make the decIsion of how to
recover from the error. The reason for thiS IS that the lower level procedure often does
not have enough Information to adequately decide what recovery procedures are
necessary. For example. suppose we have a module that accepts numeric Input from a
user. ThiS module terminates normally when the user enters a string of numeric digits
terminated by a carnage return. Entry of any non-numeric characters causes the
module to Immediately terminate abnormally. Since the module does not know In what
context It is being used (i.e.. IS It part of an assembler. an Interactive editor. or a file
management system?) It cannot make a valid deCISion of what action to take when encountering an Invalid character. If a single error recovery method was designed Into the
module. it would lose ItS generality and become specifiC to those situations that employ
this error recovery technique.
13-29
2)
3)
4)
5)
RULES FOR
MODULAR
PROGRAMMING
STRUCTURED PROGRAMMING
How do you keep modules distinct and stop them from interacting? How do you
write a program that has a clear sequence of operations so that you can isolate
and correct errors? One answer is to use the methods known as "structured programming" whereby each part of the program consists of elements from a limited
set of structures and each structure has a single entry and a single exit.
Figure 13-14 shows a flowchart of an unstructured program. If an error occurs in
Module B. we have five possible sources for that error. Not only must we check each sequence, but we also have to make sure that any changes made to correct the error do
not affect any of the other sequences. The usual result IS that debugging becomes like
wrestling an octopus. Every time you think the situation IS under control. there IS
another loose tentacle somewhere.
13-30
BASIC
STRUCTURES
OF
STRUCTURED
PROGRAMMING
S1
S2
S3
the computer executes S1 first. S2 second, and S3 third. S1. S2. and S3 may be
single instructions or entire programs.
2)
3)
A conditional structure.
The common one is "if C then S1 else S2." where C is a condition and S1 and S2
are statements or sequences of statements. The computer executes S1 if C IS true,
and S2 if C is false. Figure 13-15 shows the logic of this structure. Note that the
structure has a single entry and a single eXit: there IS no way to enter or leave S1 or
S2 other than through the structure.
A loop structure.
The common loop structure IS "while C do S," where C IS a condition and S IS a
statement or sequence of statements. The computer checks C and executes S if C
IS true. This structure (see Figure 13-16) also has a Single entry and a Single eXIt.
Note that the computer will not execute S at all if C IS onglnally false, since the
value of C IS checked before S is executed.
13-31
S2
Sl
13-32
Start
A case structure.
Although not a primitive structure like sequential. if-then-else. and do-while. the
case structure is so commonly used that we Include it here as an adjunct to the
basIc structure descriptions. The case structure is "case I of SO. S1. .Sn". where I
IS an index and SO. S1.
.Sn are statements or sequences of statements. If I IS
equal to zero then statement SO is executed: if I is equal to 1 then statement S1 is
executed. etc. Only one of the n statements IS executed. After ItS execution. control
passes to the next sequential statement following the case statement group. If I IS
greater than n (j.e.. the number of statements In the case statement). then none of
the statements in the case statement IS executed. and control IS passed directly to
the next sequential statement following the case statement. This is illustrated by
the flowchart In Figure 13-18.
21
31
Only the three basic structures, and possibly a small number of auxiliary
structures, are permitted.
Structures may be nested to any level of complexity so that any program can,
in turn, contain any of the structures.
Each structure has a single entry and a single exit.
S2 Included:
if X ;;:::. 0 then NPOS = NPOS
else NNEG = NNEG + 1
EXAMPLES
OF
STRUCTURES
+1
S2 omitted:
if X;bO then Y = 1/X
Here no action is taken if C (X ;b0) is false. S2 and "else" can be omitted in this case.
13-33
No
so
S1
Sn
1=0
SUM =0
do while 1< N
1=1+1
SUM = SUM
end
+I
The computer executes the loop as long as the character in SENTENCE is not an ASCII
period. The count is zero if the first character is a period.
13-34
ADVANTAGES OF
STRUCTURED
PROGRAMMING
11
21
31
4)
5)
6)
71
11
DISADVANTAGES
OF
Only a few high-level languages (e.g.. PLIM. PASCAL) will
STRUCTURED
directly accept the structures. The programmer therefore
PROGRAMMING
has to go through an extra translation stage to convert the
structures to assembly language code. The structured version of the program. however. is often useful as documentation.
2)
Structured programs often execute more slowlY and use more memory than
unstructured programs.
3)
Limiting the structures to the four basic forms makes some tasks very awkward to
perform. The completeness of the structures only means that all programs can be
Implemented with them: It does not mean that a given program can be Implemented efficiently or conveniently.
4)
The standard structures are often quite confUSing. e.g.. nested "if-then-else" structures may be very difficult to read. since there may be no clear indication of where
the Inner structures end. A series of misted "do-while" loops can also be difficult to
read.
5)
Structured programs consider only the sequence of program operations. not the
flow of data. Therefore. the structures may handle data awkwardly.
Few programmers are accustomed to structured programming. Many find the standard structures awkward and restrictive.
6)
13-35
WHEN TO USE
STRUCTURED
PROGRAMMING
In the future. we expect the cost of memory to decrease. the average size of
microprocessor programs to increase. and the cost of software development to
increase. Therefore. methods like structured programming. which decrease software development costs for larger programs but use more memory. will become
more valuable.
EXAMPLES
Response to a Switch
The structured version of this example is:
SWITCH = OFF
do while SWITCH = OFF
READ SWITCH
end
LIGHT = ON
DELAY 1
LIGHT = OFF
STRUCTURED
PROGRAMMING
IN THE
SWITCH AND
LIGHT SYSTEM
ON and OFF must have the proper definitions for the switch and light. We assume that
DELAY IS a module that provides a delay given by ItS parameter in seconds.
A statement In a structured program may actually be a subroutine. However. in order to
conform to the rules of structured programming. the subroutine cannot have any exits
other than the one that returns control to the main program.
Since "do-while" checks the condition before executing the loop. we set the variable
SWITCH to OFF before starting. The structured program IS straightforward. readable.
and easy to check by hand. However, It would probably require somewhat more memory than an unstructured program. which would not have to initialize SWITCH and could
combine the reading and checking procedures.
STRUCTURED
PROGRAMMING
FOR THE
SWITCH-BASED
MEMORY LOADER
INITIALIZE VARIABLES
HIADDRESS = 0
LOADDRESS = 0
THIS PROGRAM USES A DO-WHILE CONSTRUCT WITH NO CONDITION
(CALLED SIMPLY DO-FOREVER). THEREFORE. THE SYSTEM CONTINUALLY
EXECUTES THE PROGRAM CONTAINED IN THIS DO-WHILE LOOP
do forever
13-36
13-37
NKEYS = 10
STRUCTURED
PROGRAM FOR
THE CREDIT
VERIFICATION
TERMINAL
STRUCTURED
KEYBOARD
ROUTINE
Adding the SEND key means that the program must ignore extra digits after it has
a complete entry. and must ignore the SEND key until it has a complete entry. The
structured program is:
NKEYS = 10
CLEAR ENTRY TO START
do while NKEYS > 0
NKEYS = NKEYS 1
ENTRY(NKEYS) = 0
end
WAIT FOR COMPLETE ENTRY FOLLOWED BY SEND KEY
do while KEY i= SEND or NKEYS i= 10
if KEY STROBE = ACTIVE then
begin
KEYSTROBE = INACTIVE
KEY = KEYIN
if NKEYS i= 10 and KEY i= SEND then
begin
ENTRY(NKEYS) = KEY
NKEYS = NKEYS + 1
end
end
end
13-38
The second if-then is nested within the first one. since keys are only entered after a
strobe is recognized. If the second if-then were on the same level as the first. a
single key could fill the entry. since its value would be entered into the arrav during
each iteration of the do-while loop.
2)
Adding the CLEAR key allows the program to clear the entry originally by simulating the pressing of CLEAR: I.e.. by setting NKEYS to 10 and KEY to CLEAR before
starting. The structured program must also clear only digits that have previously been
filled. The new structured program is:
SIMULATE COMPLETE CLEARING
NKEYS = 10
KEY = CLEAR
WAIT FOR COMPLETE ENTRY AND SEND KEY
do while KEY #SEND or NKEYS # 10
CLEAR WHOLE ENTRY IF CLEAR KEY STRUCK
if KEY = CLEAR then
begin
KEY =0
do while NKEYS > 0
NKEYS = NKEYS - 1
ENTRY(NKEYS) = 0
end
end
GET DIGIT IF ENTRY INCOMPLETE
if KEYSTROBE = ACTIVE then
begin
KEYSTROBE = INACTIVE
KEY = KEYIN
if KEY < 10 and NKEYS # 10 then
begin
ENTRY(NKEYS) = KEY
NKEYS = NKEYS + 1
end
end
end
Note that the program resets KEY to zero after clearing the array. so that the operation IS
not repeated.
13-39
=0
= 0 or CHAR ~TRAILER
=1
13-40
STRUCTURED
RECEIVE
ROUTINE
Now we can add the section that checks the message address against the three
digits in TERMINAL ADDRESS (TERMADDR). If any of the corresponding digits
are not equal, the ADDRESS MATCH flag (ADDRMATCH) is set to 1.
CLEAR HEADER FLAG. ADDRESS MATCH FLAG. ADDRESS COUNTER TO START
HFLAG = 0
ADDRMATCH = 0
ADDRCTR = 0
WAIT FOR HEADER. DESTINATION ADDRESS AND TRAILER
do while HFLAG = 0 or CHAR ;6TRAILER OR ADDRCTR;63
GET CHARACTER IF READY
if RSTB = ACTIVE then
begin
RSTB = INACTIVE
CHAR = INPUT
end
CHECK FOR TERMINAL ADDRESS AND HEADER
if HFLAG = 1 and ADDRCTR ;6 3 then
begin
ADDRMATCH
1
ADDRCTR = ADDRCTR + 1
end
if CHAR = HEADER then HFLAG = 1
end
The program must now walt for a header. a three-digit identification code. and a trailer.
You must be careful of what happens dUring the Iteration when the program finds the
header. and of what happens if an erroneous Identification code character is the same
as the trailer
13-41
A further addition can store the message in MESSG. NMESS is the number of
characters in the message: if it is not zero at the end. the program knows that the
terminal has received a valid message. We have not tried to minimize the logic expressions in this program.
13-42
The program checks for the Identification code only if it found a header during a preVIOUS iteration. It accepts the message only if It has previously found a header and a
complete. matching destination address. The program must work properly during the
iterations when it finds the header. the trailer and the last digit of the destination address. It must not try to match the header with the terminal address or place the trailer
or the final digit of the destination address in the message. You might try adding the
rest of the logic from the flowchart (Figure 13-131 to the structured program. Note
that the order of operations is often critical. You must be sure that the program
does not complete one phase and start the next one during the same iteration.
3)
RULES FOR
STRUCTURED
PROGRAMMING
4)
Use terminators for each structure; e.g.. "end" for the "do-while" and "endif" or
"fi" for the "if-then-else" The terminators plus the indentation should make the
program reasonably clear.
5}
6)
7)
13-43
TOP-DOWN DESIGN
The remaining problem is how to check and integrate modules
or structures. Certainly we want to divide a large task into
sub-tasks. But how do we check the sub-tasks in isolation and
put them together? The standard procedure, called "bottom-up design," requires
extra work in testing and debugging and leaves the entire integration task to the
end. What we need is a method that allows testing and debugging in the actual
program environment and modularizes system integration.
This method is "top-down design." Here we start by writing
the overall supervisor program. We replace the undefined subprograms by program "stubs," temporary programs that may
either record the entry, provide the answer to a selected test
problem, or do nothing. We then test the supervisor program
to see that its logic is correct.
TOP-DOWN
DESIGN
METHODS
STUBS
The overall design may not mesh well with system hardware.
DISADVANTAGES
OF TOP-DOWN
DESIGN
2)
3)
4)
5)
In large programming projects, top-down design has been shown to greatly improve programmer productivity. However, almost all of these projects have used
some bottom-up design in cases where the top-down method would have
resulted in a large amount of extra work.
Top-down design is a useful tool that should not be followed to extremes. It provides the same discipline for system testing and integration that structured programming provides for module design. The method, however, has more general
applicability, since it does not assume the use of programmed logic. However,
top-down design may not result in the most efficient implementation.
13-44
EXAMPLES
Response to a Switch
The first structured programming example actually demonstrates top-down design as well. The program was:
SWITCH = OFF
do while SWITCH = OFF
READ SWITCH
end
LIGHT = ON
DELAY 1
LIGHT = OFF
TOP-DOWN
DESIGN
OF SWITCH
AND LIGHT
SYSTEM
These statements are really stubs. since none of them is fully defined. For example. what does READ SWITCH mean? If the switch were one bit of input port SPORT. it
really means:
SWITCH
where SMASK has a '1' bit in the appropriate position. The masking may. of course. be
implemented with a Bit Test Instruction.
Similarly. DELAY 1 actually means (if the processor Itself provides the delay):
REG = COUNT
do while REG ~O
REG = REG - 1
end
COUNT is the appropriate number to provide a one-second delay. The expanded version of the program is:
SWITCH =0
do while SWITCH = 0
SWITCH = SPORT AND MASK
end
LIGHT = ON
REG = COUNT
do while REG ~O
REG = REG - 1
end
LIGHT = NOT (LIGHT)
Certainly this program is more explicit, and could more easily be translated into
actual instructions or statements.
13-45
2)
3)
Complement
Logical AND with HAM ASK
TOP-DOWN
DESIGN OF
SWITCH-BASED
MEMORY
LOADER
where HAMASK has a '1' in the appropriate bit POSItIOn and 'Os' elsewhere. Similarly
the condition "if DATABUTTON = 1" really means:
1)
2)
Complement
3)
So. the,lnitial stubs could lust assign values to the buttons. e.g"
HIADDRBUTTON = 0
LOADDRBUTTON = 0
OATABUTTON = 0
A run of the supervisor program should show that it takes the implied "else" path
through the "if-then-else" structures. and never reads the SWitches. Similarly. if the
stub were:
HIADDRBUTTON
=1
the supervisor program should stay in the "do while HIADDRBUTTON = 1" loop waitIng for the button to be released. These simple runs check the overall logiC.
Now we can expand each stub and see if the expansion produces a reasonable
overall result. Note "ow debugging and testing proceed in a straightforward and
modular manner. We expand the HIADDRBUTTON = 1 stub to:
READ CPORT
HIADDRBUTTON = NOT (CPORT) AND HAMASK
The program should wait for the HIGH ADDRESS button to be closed. The program
should then display the values of the SWitches on the lights. This run checks for the
proper response to the HIGH ADDRESS button.
We then expand the LOW ADDRESS button module to:
READ CPORT
LOADDIi13UTTON = NOT (CPORT) AND LAMASK
With the LOW ADDRESS button in the closed position. the program should display the
values of the switches on the lights. This run checks for the proper response to the LOW
ADDRESS button.
Similarly. we can expand the DATA button module and check for the proper response
to that button. The entire program will then have been tested.
When all the stubs have been expanded. the coding, debugging, and testing
stages will all be complete. Of course, we must know exactly what results eac"
stub should produce. However, many logical errors will become obvious at each
level without any further expansion.
13-46
Kevboard
ACK =0
No
TransmIt
Displav
Receive
TOP-DOWN
DESIGN OF
VERIFICATION
TERMINAL
Here KEYBOARD, TRANSMIT, RECEIVE, and DISPLAY are program stubs that will
be expanded later. KEYBOARD. for example. could slmplv place a ten-digit verified
number into the appropriate buffer.
13-47
VER =0
No
Complete =0
Yes
KEVIN
KEVOS
Verify
The next stage of expansion could produce the following program for KEYBOARD (see Figure 13-201:
VER = 0
do while VER = 0
COMPLETE =0
do while COMPLETE = 0
KEYIN
KEYDS
end
VERIFY
end
EXPANDING
THE
KEYBOARD
ROUTINE
Here VER = 0 means that an entry has not been verified: COMPLETE = 0 means that
the entry is incomplete. KEYIN and KEYDS are the keyboard Input and display routines
respectively VERIFY checks the entry. A stub for KEYIN would simply place a random
entry (from a random number table or generator) Into the buffer and set COMPLETE to
1.
FORMAT
FOR
TOP-DOWN
DESIGN
1)
2)
3)
4)
5)
6)
7)
8l
9)
In
one step.
13-49
REFERENCES
1.
See. for example. V. P Srinl. "Fault DiagnosIs of Microprocessor Systems." Computer. January 1977. pp. 60-65. For a description of signature analysis. see G. Gordon and H. Nadig. "Hexadecimal Signatures Identify Trouble-spots In
Microprocessor Systems." Electronics. March 3. 1977. pp. 89-96. There IS also an
Application Note (#222) entitled "A DeSigner's GUide to Signature AnalysIs"
available from Hewlett-Packard.
2.
For a brief diSCUSSion of human factors conSiderations. see G. Morns. "Make Your
Next Instrument DeSign Emphasize User Needs and Wants." EON. October 20.
1978. pp. 100-105.
-
3.
D. L. Parnas (see the references below) has been a leader In the area of modular programming.
4.
5.
Formulated by D. L. Parnas.
The follOWing references prOVide additional information on problem definition and program design:
Chaplin. N.. Flowcharts. Auerbach. Princeton. N. J.. 1971.
Dahl. O. J.. C. A. R. Hoave. and E. W. Dijkstra. Structured Programming. Academic
Press. New York. N. Y.. 1972.
Dalton. W. F.. "DeSign Microcomputer Software like Other Systems Electronics. January 19. 1978. pp. 97-101.
Dijkstra. E.
1976.
w..
Systematically."
Halstead. M. H.. Elements of Software SCience. American ElseVier. New Yark. 1977
Hughes. J. K. and J. I. Michtom. A Structured Approach to Programming. Prentice-Hall.
Englewood Cliffs. N. J.. 1977
Morgan. D. E. and D. J. Taylor. "A Survey of Methods for Achieving Reliable Software."
Computer. February 1977. pp. 44-52.
Myers. W.. "The Need for Software Engineering." Computer. February 1978. pp. 12-25.
Parnas. D. L.. "On the Criteria to be Used In Decomposing Systems Into Modules." Communications ~ the ACM. December 1972. pp. 1053-1058.
-Parnas. D. L.. "A Technique for the Specification of Software Modules with Examples."
Communications ! the ACM. May 1973. pp. 330-336.
Schneider. V.. "Prediction of Software Effort and Prolect Duralion - Four New Formulas." SIGPLAN Notices. June 1978. pp. 49-59.
Shnelderman. B. et al.. "Experimental Investigations of the Utility of Detailed Flowcharts in Programming." CommunicatiOns ~ the ACM. June 1977. pp. 373-381.
Ulrickson. R. W .. "Software Modules Are the Building Blocks." Electronic Design.
February 1. 1977. pp. 62-66.
Ulrickson. R. W .. "Solve Software Problems Step-by-Step." Electronic Design. January
18.1977. pp. 54-58.
-Unger. B. W" "Programming Languages for Computer System SimulatiOn." Simulation.
April 1978. pp. 101-110.
13-50
= Programs.
13-51
Chapter 14
DEBUGGING AND TESTING
As we noted at the beginning of the previous chapter. debugging and testing are
among the most time-consuming stages of software development. Even though such
methods as modular programming. structured programming. and top-down design
can simplify programs and reduce the frequency of errors. debugging and testing
still are difficult because they are so poorly defined. The selection of an adequate set
of test data is seldom a clear or sCientific process. Finding errors sometimes seems like a
game of "Pin the tail on the donkey." except that the donkey IS moving and the programmer must position the tail by remote control. Surely. few tasks are as frustrating as
debugging programs.
This chapter will first describe the tools available to aid in debugging. It will then
discuss basic debugging procedures. describe the common types of errors. and
present some examples of program debugging. The last sections will describe
how to select test data and test programs.
We will not do much more than describe the purposes of most of the debugging tools.
There is very little standardization In this area. and not enough space to discuss all the
devices and programs that are currentlv available. The examples should give you some
Idea of the uses, advantages, and limitations of particular hardware or software aids.
If you monitor these lines (either in hardware or in software). you will be able to
see the progression of addresses. instructions. and data as the program executes.
You will be able to tell what kind of operations the CPU is performing. ThiS information will Inform you of such errors as Incorrect Jump instructions. omitted or incorrect addresses. erroneous operation codes. or Incorrect data values. However. you cannot see the contents of registers and flags Without some additional debugging facility
or a speCial sequence of instructions. Many of the operations of the program cannot be
checked In real time.
14-1
Table 14-1
OOH
08H
10H
18H
20H
28H
30H
38H or INT In Mode 1
NMI
Destination Address
(Hex)
C7
0000
0008
0010
0018
0020
CF
07
OF
E7
EF
0028
F7
FF
0030
0038
0066
LIMITATIONS
There are many errors that a single-step mode cannot help you
OF SINGLEto find. These Include timing errors and errors In the Interrupt
STEP MODE
or DMA systems. Furthermore. the single-step mode IS very
slow. typically executing a program at less than one millionth
of the speed of the processor Itself. To single-step through one second of real processor
time would take more than ten days. The single-step mode IS useful only to check the
logic of short Instruction sequences.
14-2
RST1B
lBH
lBH
RST1B
ORG
EQU
JR
WAIT IN PLACE
The simplest method for inserting breakpoints is to replace the first byte of the instruction with a RST instruction or to replace the instruction with a Jump or CALL
instruction. Use of a RST instruction IS preferred on the
since It Involves the
replacement of only a single byte. whereas a JP or CALL Involves three bytes. The JR
instruction IS not sUitable for breakpointlng because you cannot guarantee that the
debug software IS within -126 to + 129 bytes of the instruction being breakpointed.
Multiple-byte Instructions used to Implement breakpoints can cause problems on the
due to the presence of single-byte instructions. To illustrate this program. examine
the program segment shown below:
zao.
zao
Memory Address
(Hex)
Memory Contents
(Hex)
100
101
102
7B
87
87
Instruction
(Mnemonic)
L1
L2:
LD
ADD
ADD
A.E
A.A
AA
If you wish to set a breakpoint at location 10016 using a 3-byte CALL or JP. the code at
locations 10116 and 10216 will also get overlaid by the CALL or JP instruction. This
means that the debugger has to be aware that these locations have also been modified.
Any transfers of control to L1 or L2 while the breakpoint IS set will produce unexpected
results unless the debugger IS designed to catch this case. This added complexity can
be avoided by uSing a RST instruction.
Many monitors have facilities for inserting and removing
INSERTING
BREAKPOINTS
breakpoints implemented via some type of Jump Instruction.
Such breakpoints do not affect the timing of the program until
the breakpoint IS executed. However. note that this procedure will not work if part or all
of the program IS In ROM or PROM. Other monitors Implement breakpoints by actually
checking the address lines or the Program Counter In hardware or In software. ThiS
method allows breakpOints on addresses In ROM or PROM. but It may affect the timing
if the address must be checked In software. A more powerful facility would allow the
user to enter an address to which the processor wou Id transfer control. Another
possibility would be a return dependent on a switch:
RST18
WAITS;
ORG
EQU
PUSH
IN
BIT
JR
POP
RET
18H
18H
AF
A.(PIODRA)
SW.A
NZWAITS
AF
Remember to re-enable the interrupts if the routine uses an external interrupt input.
14-3
= Number of
bytes In register = 22
Data POinter =
Stack Pointer + 20
Data Pointer ==
Data POinter - 1
as 2 hex digits
= COUNT -1
COUNT
14-4
AF
BC
DE
HL
IX
IY
AF.AF
AF
BC
DE
HL
HL.O
HL.SP
DE.20
HL.DE
HL
PRNTl
LD
DEC
LD
CALL
DJNZ
B.22
HL
A,(HU
PRTHEX
PRNTl
;NUMBER OF BYTES = 22
:GET A BYTE FROM STACK
:AND PRINT IT
HL
HL
DE
BC
AF
AF.AF
IY
IX
HL
DE
BC
AF
14-5
10
42
07
(A)
(F)
(B)
3E
23
(C)
(D)
01
17
01
D3
(E)
(H)
(L)
(IX)
58
E2
(lY)
A2
36
(A')
67
(F)
E8
11
(B')
(C')
EB
(D')
09
D7
66
(E')
(H')
(L')
(STACK POINTER)
68
E2
14-6
A Memory Dump is a program that lists the contents of memory on an output device (such as a printer). This is a much more
efficient way to examine data arrays or entire programs than Just
looking at single locations. However. very large memory dumps are not useful (except
to supply scrap paper) because of the sheer mass of information that they produce.
They may also take a long time to execute on a slow printer. Small dumps may.
however, provide the programmer with a reasonable amount of information that
can be examined as a unit. Relationships such as regular repetitions of data patterns or offsets of entire arrays may become obvious.
A general dump IS often rather difficult to write. The programmer should be careful of
the following situations:
1)
2)
The size of the memory area exceeds 256 bytes. so that an 8-bit counter will not
suffice.
The ending location IS an address smaller than the starting location. This can be
treated as an error. or simply cause no output. since the user would seldom want to
print the entire memory contents In an unusual order.
Since the speed of the Memory Dump depends on the speed of the output device, the
effiCiency of the routine seldom matters. The following program will ignore cases
where the starting address is larger than the ending address. and will handle
blocks of any length. We assume that the starting address is in Register Pair DE and
the ending address IS in Register Pair HL.
. STOP IF ENDING ADDRESS BEFORE STARTING ADDRESS
AND
SBC
JR
HL,DE
C.DONE
XCHG
INC
DE
:CLEAR CARRY
:IS ENDING ADDRESS BEFORE STARTING?
:YES. DO NOT DUMP ANYTHING
:GET STARTING ADDRESS INTO HL
:COUNT = NUMBER OF LOCATIONS TO BE
DUMPED
DONE:
LD
CALL
INC
DEC
LD
OR
A.(HLl
PRTHEX
HL
DE
A.E
JR
NZ,DUMP
HALT
Note that the onlY 16-blt Subtract instruction IS SSC. which subtracts the contents of a
register pair and the Carry from Register Pair HL. SBC. like other Subtract Instruclions,
sets the Carry if a borrow is required (contrary to what some Z80 manuals say).
Figure 14-4 shows the output from a dump of memory locations 1000 to 101F.
14-7
23
6E
47
34
IF
42
36
ED
60
38
23
BC
54
17
81
AF
37
59
El
FE
28
44
FF
FF
3E
98
FF
27
00
37
5A
02
The dump
Each line consists of three parts. The line starts With the hexadeCimal address of the
first byte displayed on the line. Following the address are eight or sixteen bytes displayed In hexadecimal form. Last IS the ASCII representation of the same eight or SIXteen bytes. Try rewriting the memory dump program so that it will print the address and
the ASCII characters as well as the hexadeCimal form of the memory contents.
The simulator is the computerized eqUivalent of the pencil-andpaper computer. It is a computer program that goes through the
operating cycle of another computer. keeping track of the contents of all the registers, flags, and memory locations. We could. of course. do thiS
by hand. but It would require a large amount of effort and close attentIOn to the exact
effects of each instruction. The slmu lator program never gets tired or confused. forgets
an instruction or register. or runs out of paper.
Most Simulators are large FORTRAN programs. They can be purchased or used on the
time-sharing serVices. The Z80 Simulator IS available In several versions from different
sources.
14-8
2)
Register and memory dump facilities that can display the values of memory locations. registers. and I/O ports.
3)
A trace facility that will print the contents of particular registers or memory locations whenever the program changes or uses them.
4)
A load facility that allows you to set values initially or change them dUring the
simulation.
Some simulators can also simulate Input/output. Interrupts. and even DMA.
It can provide a complete deSCription of the status of the computer. since the
Simulator program is not restricted by Pin limitations or other characteristics of the
underlYing circuitry.
2)
It can proVide breakpoints. dumps. traces. and other facilities. Without using any of
the processor's memory space or control system. These facilities will therefore not
Interfere with the user program.
3)
4)
All the facilities of a large computer. including peripherals and software. are available to the microprocessor deSigner.
On the other hand, the simulator is limited by its software base and its separation
from the real microcomputer. The major limitations are:
1)
The Simulator cannot help With timing problems. since it operates far more slowly
than real time and does not model actual hardware or Interfaces.
2)
3)
The Simulator is usually qUite slow. Reproducing one second of actual processor
time may require hours of computer time. Using the simulator can be qUite expensive.
The simulator represents the software side of debugging; it has the typical advantages and limitations of a wholly software-based approach. The simulator can
provide insight into program logic and other software problems, but cannot help
with timing, I/O, and other hardware problems.
The logic or microprocessor analyzer is the hardware solution
to debugging. Basically, the analyzer is the parallel digital version of the standard oscilloscope. The analyzer displays information In binary. hexadecimal or mnemonic form on a CRT. and has a variety of triggering
events. thresholds. and Inputs. Most analyzers also have a memory so that they can display the past contents of the busses.
The standard procedure IS to set a triggering event. such as the occurrence of a particular address on the Address Bus or instruction on the Data Bus. For example. one might
trigger the analyzer if the microcomputer tries to store data in a particular address or execute an Input or output Instruction. One may then look at the sequence of events that
preceded the breakpOint. Common problems you can find in this way include short
noise spikes (or glitches). incorrect signal sequences. overlapping wave-forms.
and other timing or signaling errors. Of course, a software simulator could not be
used to diagnose those errors any more than a logic analyzer could conveniently
be used to find errors in program logic.
14-9
IMPORTANT
21
FEATURES
Number of Input lines. At least 24 are necessary to monitor
OF LOGIC
an 8-blt Data Bus and a 16-blt Address Bus. Still more are
ANALYZERS
necessary for control signals. clocks. and other Important inputs.
Amount of memory. Each previous state that IS saved will occupy several bytes.
3)
1)
4)
51
Type and number of triggering events allowed. Important features are pre- and
post-trigger delays; these allow the user to display events occurring before or
after the trigger event.
Methods of connecting to the microcomputer. This may require a rather complex
Interface.
Number of display channels.
6)
7}
8)
9)
Display formats.
10)
111
12)
Probe capacitance.
Single or dual thresholds
All of these factors are Important In comparing different logic and microprocessor
analyzers. since these instruments are new and unstandardized. A tremendous variety
of products IS already available and this variety will become even greater In the future.
Logic analyzers, of course. are necessary only for systems with complex timing.
Simple applications with low-speed peripherals have few hardware problems that
a designer cannot handle with a standard oscilloscope.
14-10
Look at the loops as a whole. Try the tirst and last Iterations by hand: these are often
troublesome special cases. What happens if the number ot Iterations IS zero; I.e., there
IS no data or the table has no elements? Does the program tall through correctly? Programs often will perform one Iteration unnecessarily. or. even worse. decrement counters past zero before checking them.
Check off everything down to the last statement. Don't assume (hopefully) that
the first error is the only cine in the program. Hand checking will allow you to get
the maximum benefit from debugging runs, since you will get rid of many simple
errors ahead of time.
A quick review of the hand checking questions:
1)
2)
Are all registers and memory locations used inside loops inItialized before they are used?
3)
4)
5)
6)
HAND
CHECKING
QUESTIONS
2)
Inverting the logic of a conditional jump, such as uSing Jump on Carry when you
mean Jump on Not Carry. Remember the effects of a comparison or subtraction (A
IS the contents of the Accumulator. M the contents of the register or memory location):
Zero flag
1 if A =M
Carry flag
o if A ~M
1 if A < M
o if A ~
Note particularly that Carry = 0 if A = M. (the equality case). So. Jump on Carry
means Jump if A < M. and Jump on Not Carry means lump if A 2:: M. If you want
the equality case on the other Side. try either reversing the roles of A and M or
adding 1 to M. For example. if you want a IU mp if A ~ 10. use:
CP
JR
10
NC.ADDR
> 10.
use:
11
NC.ADDR
Updating the counters and pointers in the wrong place or not at all. Be sure
that there are no paths through a loop that either skip or repeat the updating instructions.
14-11
4)
Reversing the order of operands. Remember that the LD instruction moves the
second operand into the first operand, For example, LD B,A moves the contents of
A to B, not the other way around.
6)
7)
8)
9)
10)
111
12)
Reversing the order in subtraction. Be careful also with other operations (like
division) that do not commute. Remember that SUB and CP produce A-M. not
M-A.
13)
141
14-12
15)
16)
17)
18)
19)
20)
21)
22)
Forgetting to transfer control past sections of the program that should not be
executed in particular situations.
Remember that the computer will proceed sequentially through the program
memory unless specifically ordered to do otherWise.
14-13
2)
Using the Accumulator before saving it; I.e" PUSH AF must precede any Input
or output operations thal,involve the Accumulator.
3)
Forgetting to save and restore the Accumulator and flags (Register Pair AFI.
4)
AF
BC
DE
HL
HL
DE
BC
AF
5)
6)
7)
Forgetting that RST (and NMII leaves an address in the Stack whether you
use it or not.
You may have to re-Inltlalize or update the Stack POinter.
8)
Hopefully. these lists will at least give you some ideas as to where to look for errors. Unfortunately. even the most systematic debugging can still leave some
truly puzzling problems. particularly when interrupts are involved.3
14-14
Data ={40l
Yes
Result = (SSEG
+ Data)
(41)
Result =0
= Result
14-15
DEBUGGING
A CODE
CONVERSION
PROGRAM
DONE.
SSEG.
LD
CP
JR
LD
LD
ADD
LD
LD
HALT
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
A,40H
9
C,DONE
HL.{SSEG)
D,A
HLDE
A,(HL)
(41H),A
GET DATA
:IS DATA A DECIMAL DIGIT?
:NO, KEEP ERROR CODE
:GET BASE ADDRESS OF 7-SEGMENT TABLE
:FIND ELEMENT BY INDEXING
:GET 7-SEGMENT CODE FROM TABLE
:SAVE 7-SEGMENT CODE OR ERROR CODE
3FH
06H
5BH
4FH
66H
6DH
7DH
07H
7DH
6FH
USing the checklist procedure. we were able to find the follOWing errors:
1)
2)
For example. if the data IS zero. CP 9 sets the Carry, since 0 < 9. However, the lump on
the opposite condition (;.e" JR NC.DONEl still did not produce the correct result. Now
the program handles the equality case Incorrectly since. if the data IS 9. CP 9 clears the
Carry and causes a lump. The correct version IS:
CP
JR
10
NC.DONE
14-16
Second Program:
DONE:
SSEG.
LD
LD
CP
JR
LD
LD
ADD
LD
LD
HALT
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
B.O
AAOH
10
NC.DONE
HL.(SSEG)
D.A
HL.DE
A.(HLI
(41Hl.A
3FH
06H
5BH
4FH
66H
6DH
7DH
07H
7DH
6FH
o
9
10
6B (hex)
The first tnal was with zero In location 0040 (hex). The first error was obvIOus - LD
AAOH loaded the number 40 Into A. not the contents of memory location 0040. The
correct instruction was LD A(40Hl. After this correction was made. the program moved
along with no apparent errors until It tried to execute the LD A.(HLI instruction.
The contents of the Address Bus dunng the data fetch was 0647. an address that did
not even eXist In the microcomputer. Clearly. something had gone wrong.
It was now time for some more hand-checking Since we knew that JR NC.DONE was
correct. the error was beyond that Instruction but before LD A(HLI. A haf)d check
showed:
1)
HL
2)
14-17
Third Program:
LD
LD
CP
JR
DONE.
SSEG.
LD
LD
LD
ADD
LD
LD
HALT
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
B,O
A,(40HI
10
NCDONE
HL,SSEG
E.A
0,0
HL,DE
A,(HU
(41HLA
3FH
06H
5BH
4FH
66H
6DH
7DH
07H
7DH
6FH
Result
00
09
3F
6F
OA
OA
68
68
The program was not clearing the reSUlt if the data was Invalid, I.e., greater than 9. The
program never used the blank code In Register B. Since the program was Simple, It
could be tested for all the decimal digits. The results were:
Data
Result
3F
06
5B
4F
2
3
4
5
6
7
8
9
69
6D
7D
07
70
6F
Note that the result lor number 8 IS wrong - It should be 7F Since everything else IS
correct the error IS almost surely In the table. In fact. entry 8 In the table had been
mlscopled.
14-18
JR
DONE:
SSEG.
LD
LD
LD
ADD
LD
LD
LD
HALT
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
B.O
A.(40HI
10
NC.DONE
HL.SSEG
E.A
D.O
HL.DE
B.(HU
A.B
(41HI.A
3FH
06H
5BH
4FH
66H
6DH
7DH
07H
7FH
6FH
The errors encountered in this program are typical of the ones that
language programmers should anticipate. They include:
1)
2)
zao assembly
3)
4)
51
61
Branching to the wrong place so that one path through the program
7)
IS
Incorrect
Note that straightforward instructions like ADD. SUB. AND. etc. seldom produce any
problems. One particularly annoYing error that you should watch for IS reversing the
operands on LD instructIOns. Many of these errors can be eliminated through the use oi
a low-level system programming language like PLZ/ASM4
14-19
Interchange flag = 1
Count = U,"gt~
of Array
= Start
of Array
Pointer
Interchange (Pointed
(Pointer + 1)
Interchenge fleg = 0
POinter = Pointer
= Count -
Count
14-20
DEBUGGING
A SORT
PROGRAM
PASS1
CNT
LD
LD
LD
LD
LD
INC
CP
JR
LD
INC
DJNZ
DEC
JR
HALT
C.O
A.(40H)
C.A
HL,41 H
A.(HL)
HL
(HL)
C.CNT
(HL).A
HL
PASS1
NZ.PASS1
The hand check shows that all the blocks In the flowchart have been Implemented In
the program and that all the registers have been initialized. The conditional branches
must be examined carefully. The instruction JR C.CNT must force a branch if the new
value IS less than or equal to the old value. Note that the equality case must not result in
an interchange. since this will create an endless loop with the two equal elements
being sWitched back and forth.
Try an example:
(0040) = 30
(0041) = 37
CP (HL) results In the calculatIOn of 30-37 The Carry IS set to one. This example
should result In an interchange but does not.
JR NC.CNT will proVide the proper branch In this case. If the two numbers are equal.
the comparison will clear the Carry and JR NC.CNT is again correct.
How about JR NZ.SORT at the end of the program? If there are any elements out of
order. the interchange flag will be one. so the branch IS wrong. It should be JR Z.SORT.
Now let's hand check the first Iteration of the program. The initialization results In the
following values:
A
B
C
HL
COUNT
COUNT
0
0041
CNT
LD
INC
CP
JR
LD
INC
DJNZ
A.(HL)
HL
(HL)
NC.CNT
(HU.A
HL
PASS1
:A = (0041)
:HL = 0042
:(0041 )-(0042)
:(0042) = (0041)
:HL = 0043
;B = COUNT-1
14-21
Note that we have already checked the Conditional Jump instructions. Clearly the logic
is Incorrect. If the first two numbers are out of order, the results after the first Iteration
should be:
(0041)
(0042)
HL
B
OLD (0042)
OLD (0041)
0042
COUNT-1
(0041)
(0042)
HL
UNCHANGED
OLD (0041)
0043
COUNT-1
The error in HL IS easy to correct. The second INC HL is unnecessary and should be
omitted. The Interchange requires a bit more care and a temporary register, Le.,
LD
LD
DEC
LD
INC
D,(HL)
(HL),A
HL
(HL),D
HL
An Interchange always requires a temporary storage place in which one number can be
saved while the other one IS being transferred.
14-22
PASS1:
CNT
LD
LD
LD
LD
LD
INC
CP
JR
LD
LD
DEC
LD
INC
DJNZ
DEC
JR
HALT
C.O
A (40H)
C.A
HL,41 H
A(HL)
HL
(HU
NC.CNT
D.(HU
(HL).A
HL
(HL).D
HL
PASS1
C
NZ.PASS1
How about the last iteration? Let's say that there are three elements:
(0040)
(0041)
(0042)
(0043)
03
02
04
06
Each time through. the program Increments Register Pair HL by one. So. at the start of
the third Iteration.
(HL)
= 0041
+ 2 = 0043
A.(HL)
HL
(HL)
:A = (0043)
;HL =0044
;(0043)-(0044)
ThiS IS incorrect the program has tned to move beyond the end of the data. The previous Iteration should. In fact. have been the last one. since the number of pairs IS one
less than the number of elements. The correction IS to reduce the number of Iterations
by one: thiS can be accomplished by plaCing DEC B after LD A.(40H).
How about the trivial cases? What happens if the array contains no elements at
all. or only one element? The answer is that the program does not work correctly
and may change a whole block of data improperly and without any warning (try
itO. The corrections to handle the trivial cases are simple but essential; the cost
is only a few bytes of memory to avoid problems that could be very difficult to
solve later.
14-23
PASS1
CNT
LD
LD
CP
JR
LD
DEC
LD
LD
INC
CP
JR
LD
LD
DEC
LD
INC
DJNZ
DEC
JR
HALT
C.O
A.(40H)
2
C.DONE
B.A
B
HL.41 H
A.(HU
HL
(HU
NC.CNT
D.(HU
(HU.A
HL
(HU.D
HL
PASS1
C
NZ.PASS1
Now It'S time to check the program on the computer or on the Simulator. A simple set of
data IS:
(0040)
02
(0041)
00
(0042)
01
ThiS set consists of two elements In the wrong order. The program should take two
passes. The first pass should rearrange the elements. producing:
(0041)
(0042) =
C =
01
00
01
C = 00
ThiS program IS rather long for single stepping, so we'll use breakpoints Instead. Each
breakpoint will halt the computer and pnnt the contents of all the registers. The breakPOints will come:
1)
2)
3)
After the second INC HL (i.e.. lust before the label CNT) to check the Interchange.
4)
After DEC C to check the completion of a pass through the array. The contents oj
the registers after the first breakpoint were:
Register
Contents
A
B
01
C
H
00
00
41
02
These are all correct. so the program is performing the initialization correctly In thiS
case.
14-24
Contents
A
B
C
00
01
00
00
H
L
CARRY
42
1
These results are also correct. The results at the third breakpOint were:
Register
Contents
A
B
00
01
00
01
00
42
o
Checking memory showed:
(0041) = 01
(0042) = 00
The results at the fourth breakpoint were:
Register
Contents
00
01
00
01
00
42
Here. Register C does not contain the correct value - it should have been set to one to
Indicate that an Interchange had occurred. In fact. a look at the program shows that no
instruction ever changes C to mark the Interchange. The correction IS to place the instruction LD C.l after JR NC.CNT
Now the procedure IS to load Register C With the correct value and continue. The second Iteration of the second breakpOint gives:
Register
Contents
A
B
00
00
00
00
43
1
H
L
CARRY
Clearly the program has proceeded Incorrectly Without relnltlalizing the registers (particularly HL). The conditional Jump that depends on the Interchange flag should transfer
control all the way back to the start of the program. not to the label PASS1.
14-25
PASS1
LD
LD
CP
JR
LD
DEC
LD
LD
INC
CP
JR
LD
LD
LD
DEC
LD
INC
CNT
DJNZ
DEC
JR
HALT
C.O
A.(40H)
2
C.DONE
B.A
B
HL,41 H
A.(HU
HL
(HU
NC.CNT
c:,
D.(HU
(HU.A
HL
(HU.D
HL
PASS1
C
NZ.SORT
Clearly we cannot check all the possible Input values for this program. Two other simple
sets of data for debugging purposes are;
1)
2)
02
00
00
02
01
00
14-26
INTRODUCTION TO TESTING
Program testing is closely related to program debugging.
Surely some of the test cases will be the same as the test
data used for debugging, such as:
USING TEST
CASES FROM
DEBUGGING
In the sorting program, the problem is more difficult. The number of elements could
range from 0 to 255. and each of the elements could lie anywhere In that range. The
number of possible cases IS therefore enormous. Furthermore. the program IS
moderately complex. How do we select test data that will give us a degree of confidence in that program? Here testing requires some design decisions. The testing
problem is particularly difficult if the program depends on sequences of real-time data.
How do we select the data. generate It. and present It to the microcomputer in a
realistic manner?
Most of the tools mentioned earlier for debugging are helpful
in testing also. Logic or microprocessor analyzers can help
check the hardware; simulators can help check the software.
Other tools can also be of assistance, e.g.,
1)
2)
3)
4)
5)
6)
7)
8)
I/O simulations that can simulate a variety of devices from a single Input and a
single output device.
In-circuit emulators that allow you to attach the prototype to a development
system or control panel and test It.
ROM simulators that have the flexibility of a RAM but the timing of the particular
ROM or PROM that will be used in the final system.
Real-time operating systems that can provide Inputs or interrupts at specific
times (or perhaps randomly) and mark the occurrence of outputs. Real-time breakPOints and traces may also be included.
Emulations (often on micro programmable computers) that may provide real-time
execution speed and programmable 1/0. 5
Interlaces that allow another computer to control the I/O system and test the
microcomputer program.
Testing programs that check each branch In a program for logical errors.
Test generation programs that can generate random data or other distributions.
Formal testing theorems eXIst. but they are usually applicable only to very short programs.
You must be careful that the test equipment does not invalidate the test by
modifying the environment. Often, test equipment may buffer, latch, or condition
input and output signals. The actual system may not do this, and may therefore
behave quite differently.
14-27
Furthermore. extra software in the test environment may use some of the memory space or part of the interrupt system. It may also provide error recovery and
other features that will not exist in the final system. A software test bed must be
lust as realistic as a hardware test bed. since software failure can be lust as Critical as
hardware failure.
Emulations and simulations are. of course. never precise. They are usually adequate for checking logic. but can seldom help test the interface or the timing. On
the other hand. real-time test equipment does not provide much of an overview of
the program logic and may affect the interfacing and timing.
TESTING
SPECIAL
CASES
TriVial cases
Equality cases
Special situations
The test data shou Id Include all of these.
FORMING
CLASSES
OF DATA
14-28
TESTING
A SORT
PROGRAM
The other special case to be considered IS one in which elements are equal.
There may be some problem here with signs and data length. Note that the array Itself
must contain fewer than 256 elements. The use of the instruction LD C.l or SET 1.C
rather than DEC C to clear the interchange flag means that there will be no difficulty if
the number of elements or interchanges exceeds 128.
We could check the effects of sign by picking half the regular test cases with numbers
of elements between 128 and 255 and half between 2 and 127 All magnitudes should
be chosen randomly so as to avoid unconscious bias as much as possible.
.....
(see Chapter 8)
Here we will presume that a prior validity check has ensured that
the number has the right length and consists of valid digits. Since
the program makes no other distinctions. test data should be
selected randomly. Here a random number table or random number generator will prove Ideal: the range of the random numbers IS 0
~~--..,
TESTING AN
ARITHMETIC
PROGRAM
to 9.
TESTING PRECAUTIONS
The designer can simplify the testing stage by designing programs sensibly. You should use the following rules:
1)
Try to eliminate trivial cases as early as possible without introducing unnecessary distinctions.
21
Minimize the number of special cases. Each special case means additional testing
and debugging time.
Consider performing validity or error checks on the data prior to processing.
3)
4)
51
Check boundary cases by hand. These are often a source of errors. Be sure that the
problem definitIOn specifies what IS to happen In these cases.
Make the program as general as reasonably possible. Each distinction and separate
routine Increases the required testing.
6)
71
DiVide the program and deSign the modules so that the testing can proceed In
steps In cOnlunction With the other stages of software development 7
14-29
CONCLUSIONS
Debugging and testing are the stepchildren of the software development process.
Most projects leave far too little time for them and most textbooks neglect them.
But designers and managers often find that these stages are the most expensive
and time-consuming. Progress may be very difficult to measure or produce.
Debugging and testing microprocessor software is particularly difficult because
the powerful hardware and software tools that can be used on larger computers
are seldom available for microcomputers.
The designer should plan debugging and testing carefully. We recommend the
following procedure:
1)
2)
3)
4)
5)
6)
7)
8)
Try to write programs that can easily be debugged and tested. Modular programming. structured programming. and top-down design are useful techniques.
Prepare a debugging and testing plan as part of the program design. Decide
early what data you must generate and what equipment you will need.
Debug and test each module as part of the top-down design process.
Debug each module's logic systematically. Use checklists. breakpoints. and
the single-step mode. If the program logic is complex. consider using the software simulator.
Check each module's timing systematically if this is a problem. An
oscilloscope can solve many problems if you plan the test properly. If the timing is complex. consider using a logic or microprocessor analyzer.
Be sure that the test data is a representative sample. Watch for any classes of
data that the program may distinguish. Include all special and trivial cases.
If the program handles each element differently or the number of cases is
large. select the test data randomly.S
Record all test results as part of the documentation. If problems occur. you
will not have to repeat test cases that have already been checked.
14-30
REfERENCES
1.
2.
3.
Some guidelines for debugging Interrupt problems are given In R. L. Baldrige. "Interrupts Add Power. Complexity to Microcomputer System Design:' EDN. August
5. 1977, pp. 67-73.
4.
5.
6.
Testing (and debugging) are also discussed in R. A. DeMilio et al.. "Hints on Test
Data Selection: Help for the Practicing Programmer." Computer. April 1978. pp.
34-41 and in W. F, Dalton. "Design Microcomputer Software:' Electronics. January
19.1978. pp. 97-101.
8.
14-31
Chapter 15
DOCUMENTATION AND REDESIGN
The working program is not the only requirement of software development. Adequate documentation is also an important part of a software product. Not only
does documentation help the designer in the testing and debugging stages. it is
also essential for later use and extension of the program. A poorly documented
program will be difficult to maintain. use. or extend.
Occasionally. a program uses too much memory or executes too slowly. The
designer must then improve it. This stage is called redesign. and requires that you
concentrate on the parts of the program that can yield the most improvement.
SELF-DOCUMENTING PROGRAMS
Although no program is ever completely self-documenting. some of the rules that we mentioned earlier can help.
These include:
RULES FOR
SELF-DOCUMENTING
PROGRAMS
W:
LD
LD
LD
LD
OUT
CALL
INC
DJNZ
HALT
A.(2000H)
B.A
HL.l000H
A.(HU
(61.A
XXX
HL
OUTCH:
EQU
EQU
EQU
LD
LD
LD
LD
OUT
CALL
INC
DJNZ
HALT
1000H
2000H
6
A.(COUNT)
B.A
HL.MESSG
A.(HU
(TTYSIOl.A
BITDLY
HL
OUTCH
15-1
--~~---~~ - - - -
Surely this program IS easier to understand than the earlier version~ Even without
further documentation. you could probably guess at the function of the program and
the meanings of most of the variables. Other documentation techniques cannot
substitute for self-documentation.
CHOOSING
1)
USEFUL
2)
3)
4)
COMMENTS
The most obvious form of additional documentation is the comment. However,
few programs (even those used as examples in books!. have effective comments.
You should consider the following guidelines for good comment..s;..
--,
1)
COMMENTING
GUIDELINES
Remember that you know what the operation codes mean and anyone else can
look them up in the manual. The important point is to explain what task the
program is performing.
21
3)
15-2
4)
Don't comment the obvious. A comment on each line simply makes It difficult to
find the Important points. Standard sequences like
INC
HL
DJNZ
SEARCH
need not be marked unless you're dOing something special. One comment will
often suffice for several lines. as In
RRCA
;SWAP DIGITS
RRCA
RRCA
RRCA
LD
LD
LD
A.C
C.B
B.A
5)
Place comments on the lines to which they refer or at the start of a sequence.
6)
Keep your comments up-to-date. If you change the program. change the comments.
7)
Use standard forms and terms In commenting. Don't worry about repetitiveness.
Varied names for the same things are confusing. even if the vanatlons are lust
COUNT and COUNTER. START and BEGIN. DISPLAY and LEOS. or PANEL and
SWITCHES.
There's no real gain In not being consistent. The vanatlons may seem obvious to
you now. but may not be clear later; others will get confused from the very beginning.
8)
9)
Keep improving your comments. If you come to one that you can't read or understand. take the time to change It. If you find that the listing is getting crowded.
add some blank lines. The comments won't Improve themselves; In fact. they will
lust become worse as you leave the task behind and forget exactly what you did.
10)
11)
It IS good practice when modifying working programs to use comments to indicate the date, author, and type of modification made.
Remember, comments are important. Good ones will save you time and effort. Put
some work into comments and try to make them as effective as possible.
15-3
COMMENTING
EXAMPLES
ADDWD:
LD
LD
LD
LD
AND
LD
ADC
LD
INC
INC
DJNZ
HALT
A,(30H)
B,A
HL,41H
DE,51H
A
A,(DE)
A,(HL)
(HL),A
DE
HL
ADDWD
First. comment the important POints. These are typically Inltializations, data fetches,
and processing operations. Don't bother with standard sequences like updating pointers and counters. Remember that names are clearer than numbers, so use them freely.
The new version of the program IS:
:MULTIPRECISION ADDITION
;THIS PROGRAM PERFORMS MULTI-BYTE ADDITION
;INPUTS:
:OUTPUTS:
LENGTH
NUMB1
NUMB2
ADDWD:
30H
41H
51H
LENGTH
B,A
HL.NUMB1
DE,NUMB2
A
A,(DE)
A,(HU
(HL),A
DE
HL
ADDWD
Second, look for any instructions that might not have obvious
functions and mark them. Here, the purpose of AND A IS to clear
the Carry the first time through.
QUESTIONS
FOR
COMMENTING
Third, ask yourself whether the comments tell you what you would
need to know if you wanted to use the program, e.g.:
1)
2)
What parameters are necessary? How and in what form must they be supplied?
15-4
3)
4)
5)
6)
7)
8)
Some of the questions may not be relevant to a particular program and some of the
answers may be obvious. Make sure that you won't have to sit down and dissect the
program to figure out what the answers are. Remember that too much explanation is
lust dead wood that you will have to clear out of the way. Is there anything that you
would add to or subtract from this listing? If so, go ahead - yOU are the one who has to
feel that the commenting IS adequate and reasonable.
. MULTIP RECISION ADDITION
;THIS PROGRAM PERFORMS MULTI-BYTE ADDITION
LOCATION 30H = LENGTH OF NUMBERS (IN BYTES)
LOCATIONS 41H-50H = FIRST ADDEND IN LSB-+MSB ORDER
LOCATIONS 51 H-60H = SECOND ADDEND
OUTPUTS;LOCATIONS 41H-51 H = SUM
INPUTS;
LENGTH
NUMBl
NUMB2
ADDWD:
EQU
EQU
EQU
LOA
LD
LD
LD
AND
LD
ADC
LD
INC
INC
DJNZ
HALT
30H
41H
51H
LENGTH
B,A
HL,NUMBl
DE.NUMB2
A
A,(DE)
A(HLl
(HU,A
DE
HL
ADDWD
;LENGTH OF NUMBERS
;LSB'S OF 1ST NUMBER AND RESULT
;LSB'S OF 2ND NUMBER
;COUNT = LENGTH OF NUMBERS (IN BYTES)
;START AT LSB'S OF 1ST NUMBER
;START AT LSB'S OF 2ND NUMBER
;CLEAR CARRY TO START
;GET 8 BITS OF 2ND NUMBER
;ADD 8 BITS OF 1ST NUMBER
;STORE RESULT IN 1ST NUMBER
TBIT
LD
ADD
LD
OUT
RRA
SCF
CALL
DJNZ
HALT
A,(60H)
AA
B,ll
(PIODRBl.A
BITDLY
TBIT
15-5
TBIT
EQU
EOU
EQU
PIODRB
11
60H
LD
ADD
LD
OUT
RRA
SCF
CALL
DJNZ
HALT
A, (TDATA)
A.A
B.NBITS
(TTYPIO).A
BITDLY
TBIT
Note how easily we could change this program so that It would transfer a whole string
of data. starting at the address in locations DPTR and DPTR + 1 and ending with an
"03" character (ASCII ETX). Furthermore. let us make the terminal a 30 character per
second device with one stop bit (we will have to change subroutine BITDLYl. Try makIng the changes before looking at the listing.
;STRING OUTPUT PROGRAM
:THIS PROGRAM OUTPUTS A STRING TO THE TERMINAL. TRANSMISSION CEASES
WHEN AN ASCII ETX (30H) IS ENCOUNTERED
INPUTS:
EQU
60H
ENOCH
NBITS
TIYPIO
EQU
EQU
EQU
LD
LD
CP
JR
ADD
LD
OUT
RRA
SCF
CALL
DJNZ
INC
JR
HALT
03
11
PIODRB
HL.(DPTR)
A.(HU
ENOCH
Z.DONE
A.A
B.NBITS
(TTYPIO).A
TCHAR:
TBIT:
DONE:
BITDLY
TBIT
HL
TCHAR
15-6
Good comments can make it easy for you to change a program to meet new requIrements. For example. try changing the last program so that it:
Starts each message with ASCII STX (02 hex) followed by a three-digit identification
code stored In memory locations 0030 through 0032
Adds no start or stop bits
Walts 1 ms between bits
Transmits 40 characters. starting with the one located at the address In DPTR and
DPTR+1
Ends each message with two consecutive ASCII ETXs (03 hex)
FLOWCHARTS AS DOCUMENTATION
We have already described the use of flowcharts as a design tool
In Chapter 13. Flowcharts are also useful in documentation. particularly if:
HINTS FOR
USING
FLOWCHARTS
MEMORY MAPS
A memory map is Simply a list of all the memory assignments in a program. The map
allows YOU to determine the amount of memory needed. the locations of data or
subroutines. and the parts of memory not allocated. The map is a handy reference for
finding storage locations and entry points and for dividing memory between different
routines or programmers. The map will also give you easy access to data and
subroutines if YOU need them In later extensions or in maintenance. Sometimes a
graphical map IS more helpful than a listing.
15-7
TYPICAL
MEMORY
MAP
Program Memory
Address
Routine
Purpose
0000-0002
RESET
0038-003A
INTRPT
0040-0265
0270-027F
0280-0290
0300-0340
MAIN
DELAY
DSPLY
KEYIN
1000
1001-1002
1003-1041
1042-1051
1052-105F
10EO-10FF
NKEYS
KPTR
KBFR
DBFR
TEMP
STACK
NUMBER OF KEYS
KEYBOARD BUFFER POINTER
KEYBOARD BUFFER
DISPLAY BUFFER
TEMPORARY STORAGE
RAM STACK
The map may also list additional entry POints and include a specific deSCription of the
unused parts of memory.
1) Separate RAM locations, I/O units, parameters, definitions, and memory system constants.
2)
RULES FOR
DEFINITION
LISTS
Give each parameter that might change a name and include it in the lists. Such
parameters may Include timing constants, inputs or codes corresponding to particular keys or functions, control or masking patterns, starting or ending characters,
thresholds, etc.
4)
Make the memory system constants into a separate list. These constants will
Include Reset and Interrupt service addresses, the starting address of the program,
RAM areas, Stack areas, etc.
5)
Give each port used by an I/O device a name, even though devices may share
ports in the current system. The separation will make expansion or reconfiguratlon
much simpler.
15-8
TYPICAL
DEFINITION
LIST
EQU
EQU
EQU
EQU
EQU
EQU
0
3SH
40H
300H
1000H
1100H
:RESET ADDRESS
:INTERRUPT ENTRY
:START OF MAIN PROGRAM
:KEYBOARD INTERRUPT PROGRAM
:START OF DATA STORAGE
:START OF STACK
EQU
EQU
EQU
EQU
OEOH
OE1H
OEOH
OFOH
. I/O UNITS
DSPLY
KBDIN
KBDOUT
TTYPIO
:RAM LOCATIONS
NKEYS
KBDPTR
KBDBFR
DSPBFR
TEMP
ORG
DEFS
DEFS
DEFS
DEFS
DEFS
RAMST
1
2
40H
lOH
14H
:NUMBER OF KEYS
:KEYBOARD BUFFER POINTER
:KEYBOARD INPUT BUFFER
:DISPLAY DATA BUFFER
TEMPORARY STORAGE
:PARAMETERS
BOUNCE
GOKEY
MSCNT
OPEN
TPULS
EQU
EQU
EQU
EQU
EQU
10
133
OFH
1
:DEBOUNCING TIME IN MS
:IDENTIFICATION OF 'GO' KEY
:COUNT FOR 1 MS DELAY
:PATTERN FOR OPEN KEYS
;PULSE LENGTH FOR DISPLAYS IN MS
EQU
EQU
OFFH
SOH
;DEFINITIONS
ALL1
STCON
Of course, the RAM entnes will usually not be In alphabetical order, since the designer
must order these so as to minimize the number of address changes required In the program.
15-9
LIBRARY ROUTINES
Standard documentation of subroutines will allow you to build up a library of
useful programs. The Idea is to make these programs easily accessible. A standard format will allow you or anyone else to see at a glance what the program does. The best
procedure is to make up a standard form and use It consistently. Save these programs
In a well-organized manner (for example, according to processor, language, and type of
program), and you will soon have a useful set. But remember that without organization and proper documentation. using the library may be more difficult than rewriting the program from scratch. Debugging a system requires a precise understanding
of all the effects of each subroutine.
Among the information that you will need In the standard form IS:
Purpose of the program
Processor used
STANDARD
PROGRAM
LIBRARY
FORMS
Language used
Parameters required and how they are passed to the subroutine
Results produced and how they are passed to the main program
Number of bytes of memory used
Number of clock cycles required. ThiS number may be an average or a tYPical figure,
or It may vary Widely. Actual execution time will. of course, depend on the processor
clock rate
Registers affected
Flags affected
A tYPical example
Error handling
SpeCial cases
Documented program listing
If the program IS complex, the standard library form should also Include a general
flowchart or a structured program. As we have mentioned before, a library program IS
most likely to be useful if It performs a single distinct function In a reasonably general
manner.
LIBRARY EXAMPLES
Library Example 1: Sum of Data
Purpose: The program SUM8 computes the sum of a set of 8-blt unsigned binary numbers.
Language: Z80 assembler.
Initial Conditions: Starting address of set of numbers In Register Pair HL length of set
In Accumulator.
Final Conditions: Sum In Accumulator.
Requirements:
Memory
Time
7 bytes.
13 + 26N clock cycles, where N IS the
length of the set of numbers.
Registers
A, B, H. L
All flags affected.
15-10
0050
03
A
(0050)
(0051)
(0052)
End:
27
3E
26
88
Error Handling: Program Ignores all carries. Carry bit reflects only the last operation.
Initial contents of Accumulator must be 1 or more.
Listing:
:SUM OF 8-BIT DATA
SUM8:
ADD8:
LD
SUB
ADD
INC
DJNZ
RET
B.A
A
A.(HL)
HL
ADD8
In
hexadecimal)
Start:
05
66
End:
Error Handling: Program returns zero in the Accumulator if data IS not a deCimal digit.
15-11
Listing:
:DECIMAL TO SEVEN-SEGMENT CONVERSION
SEVEN:
LD
CP
B.O
10
NC.DONE
L.A
H.O
DE.SSEG
HL.DE
B.(HU
A.B
JR
DONE:
SSEG:
LD
LD
LD
ADD
LD
LD
RET
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
3FH
06H
5BH
4FH
66H
6DH
7DH
07H
7FH
6FH
In
Requirements:
Memory
Time
11 bytes.
13 + 50N clock cvcles. where N is the number of
bytes involved.
Registers - A. B. D. E. H. L.
All flags affected. Carry shows if sum produced a carry.
In
hexadecimall
0060
0050
2
34
55
88
15
22
71
15-12
Error Handling: Program does not check the validity of decimal Inputs. Accumulator
must be 1 or greater.
Listing:
DECSUM:
DECADD:
LD
AND
LD
ADC
DAA
LD
INC
INC
DJNZ
RET
B.A
A
A.(DE)
A.(HL)
(HL).A
DE
HL
DECADD
TOTAL DOCUMENTATION
Complete documentation of microprocessor software will Include all or most of the elements that we have mentioned. So.
the total documentation package may involve:
DOCUMENTATION
PACKAGE
General flowcharts
A written description of the program
A list of all parameters and definitions
A memory map
A documented listing of the program
A description of the test plan and test results
The documentation may also include:
Programmers' flowcharts
Data flowcharts
Structured programs
The documentation procedures outlined above are the minimal acceptable set of
documents for non-production software. Production software demands even
greater documentation efforts. The following documents should also be produced:
Program Logic Manual
User GUide
Maintenance Manual
The program logic manual expands on the written explanation produced with the
software. It should be written for a technically competent individual who may not
possess the detailed knowledge assumed In the written explanation In the software.
The program logic manual should explain what the design goals of the system were.
what algOrithms were chosen to implement these goals. and what tradeoffs had to be
made in achieVing them.
It should then explain In great detail what data structures were employed and how they
are manipulated. It should provide a step-by-step gUide to the inner workings of the
code. Finally. it should contain any special tables or graphs that help explain any of the
concepts embodied in the code. Code conversion charts. state diagrams. translation
matrices. and flowcharts should be Included.
The user guide is probably the most important and most overlooked piece of documentation. No matter how well a system is designed. it is useless If no one can
use it effectively. The user guide should provide all users. sophisticated and unsophisticated. with an introduction to the system. It should then proYide detailed ex-
15-13
planations of system features and their use. Use plenty of examples because a good example can crystallize the information contained In many pages of text. Step-by-step
directions should be given. Test the user guide. I.e.. tryout the step-by-step usage procedures as yOU have documented them. Programmers with detailed knowledge of a
system's design often take shortcuts that are not at all apparent to the general reader.
An entire book could be written about the writing of user guides. and further diSCUSSion
IS beyond the scope of this book. However. remember that yOU can never spend too
much effort In preparing a user gUide. because it will be the most used of all system
documents.
The maintenance manual is designed for the programmer who has to modify the
system. It should outline step-by-step procedures for those reconfiguratlons designed
into the system. In addition. It shou Id outline any prOVISions placed Into code for future
expansion.
Documentation should not be taken lightly or postponed until the end of the software development. Proper documentation. combined with proper programming
practices. is not only an important part of the final product but can also make
development simpler. faster. and more productive. The designer should make consistent and thorough documentation part of every stage of software development.
REDESIGN
Sometimes the designer may have to squeeze the last microsecond of speed or
the last byte of extra memory out of a program. As larger Single-chip memories have
become available. the memory problem has become less serious. The time problem. of
course. is serious only if the application IS time-critical: in many applications the
microprocessor spends most of its time waiting for external devices. and program speed
IS not a malor factor.
Squeezing the last bit of performance out of a program is
seldom as important as some writers would have you believe.
In the first place. the practice is expensive for the follOWing
reasons:
1)
2)
3)
4)
It requires extra programmer time. which is often the Single largest cost in software
development.
It sacrifices structure and Simplicity with a resulting Increase In debugging and
testing time.
The programs require extra documentation.
The resulting programs will be difficult to extend. maintain. or re-use.
In the second place. the lower per-unit cost and higher performance may not really
be important. Will the lower cost and higher performance really sell more units? Or
would yOU do better with more user-oriented features? The only applications that
would seem to justify the extra effort and time are very high-volume. low-cost
and low-performance applications where the cost of an extra memory chip will far
outweigh the cost of the extra software development. For other applications. you
will find that you are playing an expensive game for no reason.
------.....
zao
15-14
Note particularly that saving memory can be critical if it allows a program to fit Into the
limited amount of ROM and RAM available in a Simple one-chip or two-chip microcomputer. The hardware cost for small systems can thus be substantially reduced. if their
requirements can be limited to the memory size and I/O limitations of that particular
one-chip or two-chip system.
2)
Use register operations when possible. But remember the cost of the extra inItialization.
3)
Use the Stack when possible. The Stack Pointer IS automatically updated after
each use so that no explicit updating instructions are necessary.
4)
Eliminate Jump instructions. Try to reorganize the program or use indirect Jumps
(JP (HU or JP (IX or IY)). RST. or RETURN instructions.
5)
6)
Organize data and tables so that you can address them without worrying
about address calculation carries or without any actual indexing. ThiS will
again allow you to manipulate 16-blt addresses as 8-bit quantities. See pages 5-1
to 5-6 of Z80 Programming for Logic Design for an example.
7)
Use the 16-bit instructions to replace two separate 8-bit operations. ThiS
may be particularly useful in initialization or storing results.
8)
9)
Take advantage of such instructions as INC (HU. OCR (HU. LD (HU. RL (HU. and
RR (HU. which operate directly on memory locations without using registers.
10)
11)
12)
Take advantage of the Block Move. Block Search. and Block I/O instructions
whenever you are handling blocks of data.
13)
Watch for special short forms of instructions such as the Accumulator shifts
(RLCA. RLA. RRCA. and RRA) and DJNZ.
14)
15)
16)
Take advantage of the alternate register set to cut down on the use of
storage. This can save time as well.
15-15
SAVING
EXECUTION
TIME
completely eliminating an instruction that is executed only once can save at most a few
microseconds. But a savings in a loop that is executed frequently will be multiplied
many times over.
So, if you must reduce execution time. proceed as follows:
1)
Determine how frequently each program loop is executed. You can do this by
hand or by uSing the software simulator or another testing method.
2)
3)
First. see if there are any operations that can be moved outside the loop. I.e..
repetitive calculations, data that can be placed into a register or the Stack, addresses that can be placed into register pairs or Index registers, special cases or
errors that can be handled elsewhere, etc. Note that this will require extra inItialization and memory but will save time.
4)
Try to eliminate Jump statements. These are very time-consuming. Or. use
Jumps with direct addressing that require more memory but less time than jumps
with relative addressing.
5)
Replace subroutines with in-line code. This will save at least a CALL and a
RETURN Instruction.
6)
7)
Use any of the hints mentioned in saving memory that also decrease execution time. These Include the use of block handling instructions, B-bit addresses,
16-blt instructions, RST, special short forms of instructions, etc.
B)
Do not even look at instructions that are executed only once. Any changes
that you make In such instructions only inVite errors for no appreciable gain.
9)
Avoid indexed and relative addressing whenever possible because they take
extra time.
10)
Use tables rather than algorithms; make the tables handle as much of the tasks
as possible even if many entries must be repeated.
MAJOR REORGANIZATIONS
If you need more than a 26% increase in speed or decrease in memory usage. do
not try reorganizing the code. Your chances of getting that much of an improvement are small unless you call in an outside expert. You are generally better off
making a major change.
The most obvious change is a better algorithm. Particularly if
BETTER
you are doing sorts, searches, or mathematical calculations, you
ALGORITHMS
may be able to find a faster or shorter method in the literature.
libraries of algorithms are available in some lournals and from professional groups. See,
for example, References 1 through 10 at the end of this chapter.
More hardware can replace some of the software. Counters. shift registers,
arithmetic units, hardware multipliers, and other fast add-ons can save both time and
memory. Calculators, UARTs, keyboards, encoders, and other slower add-ons may save
memory even though they operate slowly. Compatible parallel and serial interfaces, and
other devices specially designed for use with the ZBO may save time by taking some of
the bu rden off the CPU.
15-16
OTHER
MAJOR
CHANGES
1)
2)
Versions of the CPU may exist that operate at higher clock rates. But remember that you will need faster memory and I/O ports. and you will have to adjust any
delay loops.
3)
Two CPUs may be able to do the job in parallel or separately if you can diVide the
Job and solve the communications problem.
4)
A specially microprogrammed processor may be able to execute the same program much faster. The cost. however. will be much higher even if you use an offthe-shelf emu lation.
5)
You can make tradeoffs between time and memory. Lookup tables and function
ROMs will be faster than algorithms. but will OCCUPy more memory.
F-"---...,.
15-17
REFERENCES
1.
Collected AlgOrithms from ACM. ACM, Inc.. P O. Box 12105, Church Street StatIOn, New York 10249.
2.
3.
4.
5.
6.
Knuth, D. E.. The Art of Computer Programming, Volume 3: Sorting and Search-
Carnahan, B. et
S.
9.
10.
11.
15-18
Chapter 16
SAMPLE PROJECTS
PROJECT #1: A Digital Stopwatch
Purpose: This project is a digital stopwatch. The operator enters
STOPWATCH
INPUT
two digits (minutes and tenths of minutes) from a
PROCEDURE
calculator-like kevboard and then presses the GO kev.
The system counts down the remaining time on two
seven-segment LED displavs (see Chapter 11 for a description of unencoded
keyboards and LED displays).
Hardware: The project uses one input port and one output port (one Z80 Parallel
Input/Output Device or PIOl. two seven-segment displays. a 12-kev kevboard. a 7404
Inverter. and either a 7400 NAND gate or a 7408 AND gate. depending on the polantv
of the seven-segment displays. The displays may require drivers. inverters. and resIstors. depending on their polantv and configuration.
The hardware is organized as shown In Figure 16-1. Output lines O. 1, and 2 are used to
scan the kevboard. Input lines O. 1. 2, and 3 are used to determine whether anv keys
have been pressed. Output lines 0, 1, 2, and 3 are used to send BCD digits to the sevensegment decod.er/dnvers. Output line 4 IS used to activate the LED displays (if line 4 IS
'1', the displays are lit). Output line 5 is used to select the left or right displav: output
line 5 is '1' if the left display IS being used, '0' if the right display is being used. Thus.
the common line on the left display should be active if line 4 is '1' and line 5 is '1', while
the common line on the right display should be active if line 4 is '1' and line 5 IS '0'
Output line 6 controls the right-hand decimal POint on the left display. It may be dnven
with an inverter or simplv left on.
Keyboard Connections: The keyboard IS a Simple calculator keyboard available for
50 from a local source. It consists of 12 unencoded key-switches arranged in four rows
of three columns each. Since the wiring of the keyboard does not cOincide with the observed rows and columns. the program uses a table to Identifv the keys. Tables 16-1
and 16-2 contain the Input and output connections for the keyboard. The deCimal point
key is present for operator convenience and for future expansion: the current program
does not actually use the kev.
In an actual application. the kevboard would require pullup resistors to ensure that the
inputs would actuallv be read as logic '1's when the keys were not being pressed. It
would also require current-limiting resistors or diodes on the output port to avoid
damaging the drivers In the case where two outputs were dnvlng against each other.
This could occur if two kevs in the same row were pressed at the same time. thus connecting two different column outputs.
16-1
87 r--(not usedl
8e
Output 8S
Port
84
(PIO
83
Port 81 82
81
So
II
Co C1 c 2
Input
Port
(Pia
Port AI
A3
R3
A2
R2
A1
R1
Ao
RO
KeYboard
00 0 1 02 0 3
OP
Display
and
If
DO 0 1 02 0 3
Display
and
Driver
Driyer
Hefti
(rightl
Common
Common
lL=D
lb
Input Bit
1
2
Keys Connected
'0', '2', '3', '4'
'1' 'S', 'g' 'GO'
'5', '6', '1', '..
0
1
2
16-2
Initialization
Identify
key closure
16-3
Display Connections: The displays are seven-segment displays with their own integral decoders. A typical example would be the Texas Instruments TIL309 device.
which has an Internal TTL MSI chip with latch. decoder. and driver. Clearly. standard
seven-segment displays would be cheaper but would require some additional software
(the seven-segment conversion routine shown In Chapter 7). Data is entered into the
display as a single binary coded deCimal digit; the digits are represented as shown in
Figure 11-15. The deCimal POint is a single LED that is turned on when the deCimal
POint Input is a logic '1' You can find more information about displays in References 10
and 11 at the end of this chapter.
Program Description:
The program IS modular and has several subroutines. The emphaSIS IS on clanty and
generality rather than efficiency; obViously. the program does not utilize the full
capabilities of the Z80 processor. Each section of the listing will now be described In
detail.
1)
Introductory Comments
The Introductory comments fully describe the program; these comments are a
reference so that other users can easily apply. extend. and understand the program. Standard formats. indentations. and spacings Increase the readability of the
program.
2)
Variable Definitions
All vanable definitions are placed at the start of the program so that they can easily
be checked and changed. Each vanable IS placed in a list alphabetically With other
variables of the same type; comments describe the meaning of each vanable. The
categones are;
a)
Memory system constants that may vary from system to system depending on
the memory space allocated to different programs or types of memones
b)
cl
d)
The memory system constants are placed In the definitions so that the user may
relocate the program. temporary storage. and memory stack without making any
other changes. The memory constants can be changed to accommodate other
programs or to cOincide With a particular system's allocation of ROM and RAM addresses.
Temporary storage IS allocated by means of DEFS (Define Storage) pseudo-operations. An ORG (ongln) pseudo-operation places the temporary storage locations in
a particular part of memory. No values are placed in these locations so that the
program could eventually be placed In ROM or PROM and the system could be
operated from power-on reset Without reloading.
Each port address occupied by a PIO is named so that the addresses can easily be
changed to handle vaned configurations. The naming also serves to clearly distinguish control registers from data registers.
The definitions clarify the meaning of certain constants and allow parameters to
be changed easily. Each definition is given in the form (binary. hex. octal. ASCII. or
decimailin which ItS meaning IS the clearest. Parameters (such as debounce time)
are placed here so that they can be varied With system needs.
16-4
3)
Initialization
zao
4)
Place a starting value In the Stack Pointer. The Stack IS used only to store
subroutine return addresses.
b)
cl
dl
Initialize the location where the next digit key pressed will be saved to the
start of the digit key array. An indirect procedure IS used. in which KEYAD
contains the address in which the next digit will be placed. Each time a digit
key is recognized. the contents of KEYAD are incremented so that the next
digit key will be placed Into the next memory location.
columns
Key closures are Identified by grounding all the keyboard columns and then
checking for grounded rows (j.e.. column-to-row sWitch closures). Note that the
program does not assume that the unused input bits are all high: Instead. the bits
attached to the keyboard are isolated with a logical AND instruction.
5)
Debounce Key
The program debounces the key closure in software by waiting for two milliseconds. This is usually long enough for a clean contact to be made. Subroutine
DELAY Simply counts with Register C for 1 millisecond. The number of milliseconds IS In the Accumulator. DELAY would have to be adjusted if a slower clock or
slower memories were being used. You could make the change simply by redefinIng the constant MSCNT.
16-5
6)
Ground a keyboard
column bV output of
(pattern pOinter)
Yes
"crement BV ta -e
Key 10 =
(key table pointer)
Use key table pOinter
to get key 10
The particular key closed IS Identified by grounding single columns and observing
whether a closure is found. Once a closure IS found (so the key column is known).
the key row can be determined by shifting the input.
The patterns required to ground single keyboard columns are in a table PATT In
memory. The final pattern In the table IS a marker (ECODE) which Indicates that all
the columns have been grounded Without a closure being found. This pattern also
Indicates to the main program that the closure could not be identified (e.g.. the
key closure ended or a hardware error occurred before we could find the closure).
16-6
IKEY1
8)
9)
16-7
1O}
Right Display
= Right
Display - 1
Left Display =
Left Display - 1
Yes
Right Display
End of timer
=9
program
The value of the less significant digit is reduced by one. If this affects bit 4
(LEDON - used to turn the displays on), the digit has become negative. A borrow
must then be obtained from the more significant digit. If the borrow from the more
significant digit affects bit 4. the count has gone past zero and the countdown is
finished. Otherwise, the program sets the value of the less Significant digit to 9
and continues.
Note that comments describe both sections of the program and indiVidual statements.
The comments explain what the program IS dOing, not what specific Instruction codes
do. Spacing and indentation have been used to Improve readability.
16-8
zao PIO)
16-9
EQU
EQU
LASTM
TEMP
EQU
:RAM TEMPORARY
ORG
KEYAD: DEFS
KEYNO:
DEFS
NKEYS:
DEFS
50H
1000H
800H
STORAGE
TEMP
2
EQU
EQU
EQU
OEOH
OE2H
OE1H
PIOCRB
EQU
OE3H
:DEFINITIONS
DECPT
EQU
16-10
ECODE
EQU
OFFH
GOKEY
LEDON
LEDSL
EQU
EQU
EQU
11
4
MSCNT
MXKEY
EQU
EQU
OF9H
2
NROWS
EQU
OPEN
EQU
00001111B
TPULS
TWAIT
EQU
EQU
2
2
ORG
BEGIN
BEGIN
A.01001111B
(PIOCRA).A
A.00001111 B
(PIOCRBLA
SP.LASTM
A
(NKEYS).A
HL.KEYNO
(KEYAD).HL
CALL
SCANC
A.TWAIT
DELAY
IDKEY
ECODE
Z.START
16-11
LD
LD
B,A
HL,NKEYS
LD
CP
JR
LD
CP
JR
INC
LD
LD
INC
LQ
A,(HU
MXKEY
Z,KEYF
A,B
10
NC,WAITK
(HU
HL,(KEYAD)
(HU,A
HL
(KEYADl.HL
CALL
JR
SCANO
START
LD
CP
JR
A,B
GOKEY
NZ,WAITK
HL,KEYNO
D,(HU
DECPT,D
LEDON,D
LEDSL,D
HL
E,(HL)
LEDON,E
LEDLP'
TLOOP:
LDPUL.
LD
LD
LD
OUT
LD
CALL
OUT
LD
CALL
DJNZ
DEC
JR
C,PIODRB
H,6
B,250
(C),D
A.TPULS
DELAY
(Cl.E
A.TPULS
DELAY
LDPUL
H
NZ.TLOOP
16-12
DEC
BIT
JR
DEC
BIT
JP
LD
SET
JR
E
LEDON.E
NZ.LEDLP
D
LEDON.D
Z.BEGIN
E.9
LEDON.E
LEDLP
SUB
OUT
IN
AND
CP
JR
A
(PIODRBl.A
A.(PIODRA)
OPEN
OPEN
Z.SCANC
RET
;SUBROUTINE DELAY WAITS FOR THE NUMBER OF MILLISECONDS SPECIFIED
. IN REGISTER A
DELAY
DLYl
WTLP'
EXX
LD
DEC
JR
DEC
JR
C.MSCNT
C
NZ.WTLP
A
NZ.DLYl
EXX
RET
LD
LD
LD
BC.PATT
HL.KTAB-l
DE.NROWS
LD
CP
RET
OUT
IN
AND
CP
JR
ADD
INC
JR
A.(BC)
ECODE
Z
(PIODRBl.A
A,(PIODRA)
OPEN
OPEN
NZ.FROW
HL.DE
BC
FCOL
16-13
INC
RRCA
HL
JR
C,FROW
A,(HL)
DEFB
DEFB
DEFB
DEFB
00000110B
00000101B
00000011B
ECODE
:KEYBOARD TABLE
:COLUMNS ARE PRIMARY INDEX, ROWS SECONDARY INDEX
:THE KEYS IN THE COLUMN ATTACHED TO OUTPUT BIT 0 ARE FOLLOWED
BY THOSE IN THE COLUMN ATTACHED TO OUTPUT BIT 1, ETC. WITHIN
A COLUMN. THE KEY ATTACHED TO INPUT BIT 0 IS FIRST FOLLOWED
. BY THE ONE ATTACHED TO INPUT BIT 1. ETC.
:THE DIGIT KEYS ARE 0 TO 9, DECIMAL POINT IS 10, GO IS 11
KTAB:
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
3
2
0
4
8
9
1
11
5
6
7
10
:CO,RO
:CO,R1
:CO,R2
:CO,R3
:C1.RO
:C1.R1
:C1.R2
:C l.R3
:C2.RO
:C2.R1
:C2,R2
:C2,R3
SUB
OUT
IN
AND
CP
JR
(PIODRB),A
A,(PIODRA)
OPEN
OPEN
NZ,SCANO
RET
END
16-14
16-15
67
Output
Port
IPIO
Port 61
11-
"1
6S '--Inot usedl
6SI-
....
..,
64 1 - - - - - - t - - - l I . . - r - - - - - - - ,
63
1-----+--+-1-----......-1-+-----,
6Z II------I----t-+------Gj,...-t---t--t------,
6,
1------t----;-t--
BOI I - - - - - - - t - - - - , H - -
Ip-t-t---;-+---,
-j--t-t--t--j----,
Start
A7
Input
AS
AS
Port
A4
ConversIon
--f
--f
--f
(PIO A3 101111t---t
---t
Port AI AZ
A,
---t
Displav
and
Displav
AID
Converter
Dnver
Dover
lIeftl
(rlghtl
Ao -
Analog l n p u t - - - - - - - - - '
l~m""
and
Common
16-16
R6
50 kil
OFFSET ADJ
.."
.........
15Va
+5 V
+15V
VREF
10 V
R3
200 il
O+15V
R5
2Mil
22
2
R2
5kil
Voo
VCC
OUT1
VREF
+5 V
OUT2
24
CLOCK
COMP
+15 V
C1
JOOOPF
Rl
AD7570J
AID
SRO
Converter
SYNC
Thermistor
(not usedl
9
10
D89
3
ANALOG
INPUT
11
088
12
087
+5V
DB6
20
21
13
25
28
27
26
HBEN
085
LBEN
084
14
15
(not usedl
A7
A6
A5
A4
To PIO
Port A
A3
A2
16
STRT
083
BUSY
DB2
BSEN
A1
17
18
DB1
19
5e8
AGND
AD
(not usedl
(not usedl
-::-0
Note: If positive VREF 's used, the ANALOG INPUT range '5 0 to ,VREF' and the
COMPARATOR's (.J Input should be connected to OUT1 Ipln 41 of the AD7570.
RT
IS
IS:
R8
.15 Volt
R8 + RT
Since RF = 6B kil, the Input's:
1.02 Mil
Volt
RT + 68 kil
RT has a m'OImum value of 34 kil (T=50"C. see Figure 16-41 so full scale ,510 Volt.
16-17
l000000r-----------------------,
-- -- -100 000
T(OC)
R(Ohm)
0
25
365000
100 000
34 000
6000
50
-- ........
100
"Q.. .....
10000 ....
... ..............
..........
..........
-1-
....
.....
25
50
Temperature (OC)
100
r-------------------------,
The curve is linear (i.e.. the resistance is
independent of current) for currents less
than 0.1 milliampere.
10
0.01
0.1
I (milliampere)
16-18
1.0
Start
Initializaoon
-*
'I
Send Start
ConversIon S!gnal
to AID converter
t
Wait1ms
t
Read data from
AID converter
t
Convert data to
degrees Celsius
t
DispIav
temperature on
16-19
Program Description:
1)
Initialization
Location 0 (the Z80 microprocessor RESET location) contains a Jump to the starting
address of the main program.The initialization configures the PIO control registers
and starts the Stack POinter at the highest address in RAM. The Stack is used onlv
to store subroutine return addresses.
2)
3)
4)
16-20
5)
AID converter
Index == 0
Pornter. "" Start of table
Yes
Index = Index + 1
Pointer = Pointer + 1
Temperature
= Index
16-21
6)
Get most
significant digit
The least significant digit is masked off. We set the bit that
turns on the displays. The result IS saved In Register E.
BLANKING
A LEADING
ZERO
The only difference for the most significant digit IS that a leading zero IS blanked (i.e.. the displays show "blank T rather
than "OT for 7C). This simplY involves not setting the bit that turns on the displays if the digit is zero. The result IS saved In Register D.
16-22
7)
Count
= TSAMP
Send most
significant digit
to left displav
Wait 2 ms
Send least
SIgnificant digit
to right display
Walt 2ms
Count
= Count - 1
zao
16-23
16-24
BEGIN
LASTM
EQU
EQU
50H
1000H
EQU
EQU
EQU
EQU
OEOH
OEZH
OE1H
OE3H
EQU
EQU
EQU
EQU
EQU
EQU
ORG
:DEFINITIONS
LEDON
LEDSL
MSCNT
STCON
TPULS
TSAMP
5
OF9H
10000000B
Z
1500
JP
BEGIN
BEGIN
A.Ol00llllB
(PIOCRA).A
A.OOOOllll B
(PIOCRB).A
SP.LASTM
LD
OUT
SUB
OUT
A.STCON
(PIODRBl.A
A
(PIODRBl.A
16-25
A.l
DELAY
A. (PIODRA)
CONVR
B.A
OFH
LEDON.A
E.A
JR
SET
SET
SVMSD: LD
A.B
OFH
Z.SVMSD
LEDON.A
LEDSL.A
D.A
DSPLY
LD
LD
OUT
LD
CALL
OUT
LD
CALL
DEC
LD
OR
JR
JP
C.PIODRB
HL.TSAMP
(CLD
A.TPULS
DELAY
(C).E
A.TPULS
DELAY
HL
A.H
L
NZ.DSPLY
START
16-26
DELAY:
DLY1<
WTLP:
EXX
LD
DEC
C,MSCNT
JR
NZ.wTLP
DEC
JR
NZ.DLY1
EXX
RET
CONVR;
CHVAL:
LD
HL,DEGTB
LD
LD
LD
CP
LD
RET
ADD
DAA
LD
INC
B,A
C.O
A.(HL)
B
A,C
NC
JR
A,l
C.A
HL
CHVAL
16-27
DEGTB:
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
DEFB
END
5B
61
63
66
69
71
74
77
80
84
87
90
93
97
101
104
108
112
116
120
124
12B
132
136
141
145
149
154
158
163
167
172
177
181
186
191
195
200
204
209
214
218
223
227
232
236
241
245
249
253
255
16-28
References
1. A method that uses far less memory IS described In T. A. Selm. "Numencallnterpolatlon for Microprocessor-based Systems:' Computer Design. February 1978. pp. 111-
116.
--
See also:
2. Auslander. D. M. et al., "Direct Digital Process Control: Practice and Algonthms for
Microprocessor ApplicatIOns:' Proceedings of the IEEE. February 1978. pp. 199-208
----
In
In
Analog Input/Output Boards:' Electronics. JanuInstruments and Control, Wiley. New York. 1977
1976
9. Mrozowskl. A.. "Analog Output Chips Shnnk A-D Conversion Software:' Electronics.
June 23. 1977, pp. 130-133
10. The Optoelectronics Data Book. Texas Instruments. Inc.. P,O, Box 5012. Dallas. TX..
1978
11. The Optoelectronic DeSigner's Catalog. Hewlett-Packard Inc.. 1820 Embarcadero
Road. Palo Alto. CA. 94303.1978
12. Peatman. J. B.. Microcomputer-based DeSign. McGraw-HilL New York. 1977
13. Rony. P R. et aL. "Microcomputer InterfaCing: Sample and Hold DeVices:' Computer
Design. December 1977. pp. 106-108
14. Sheingold. D. H. ed.. Analog-Digital Conversion Notes. Analog DeVices. Inc.. P O.
Box 796. Norwood. MA. 02062. 1977
16-29
A.data 3-43
A.reg 3-44
A.(HLI 3-45
A.(lX + displ 3-45
A.(ly + displ 3-45
HL.rp 3-46
Adata 3-47
A.reg 3-48
A.(HLI 3-49
A.(lX + displ 3-49
A(lY + displ 3-49
HL.rp 3-50
XY.rp 3-51
data 3-52
reg 3-53
IHLI 3-54
(IX + disp) 3-54
(lY + displ 3-54
b.reg 3-55
b.(HLI 3-56
b.(lX + displ
b.IIY + disp)
3-56
3-56
3-75
3-76
3-77
3-77
3-77
3-79
1M 0 3-80
1M 1 3-80
1M 2 3-80
IN A.(portl 3-81
INC reg 3-82
INC rp 3-83
INC IX 3-83
INC IY 3-83
INC IHLI 3-84
INC !IX + disp) 3-84
INC !lY + displ 3-84
IND 3-85
INDR 3-85
INI 3-86
INIR 3-86
IN reg.(CI 3-87
JP
JP
JP
JP
JP
JR
JR
JR
JR
JR
label 3-88
condition.label
(HLI 3-90
!IX) 3-90
!lY) 3-90
C.disp 3-91
disp 3-92
NC.disp 3-93
NZ.disp 3-93
Z.disp 3-94
LD
LD
LD
LD
LD
LD
LD
LD
LD
LD
LD
LD
LD
LD
LD
LD
LD
LD
LD
LD
LD
LD
LD
LD
LD
LD
LD
LD
A.I 3-94
A.R 3-94
A.(addr) 3-95
Alrp) 3-96
ds!.src 3-97
HL,(addr) 3-98
rp.(addrl 3-98
IX.laddri 3-98
IY.(addri 3-98
I.A 3-99
R.A 3-99
reg.data 3-100
rp.data 3-101
IX.data 3-101
IY.data 3-101
reg.IHLI 3-102
reg.OX + disp) 3-102
reg.OY + disp) 3-102
SP.HL 3-103
SP.IX 3-103
SP.IY 3-103
laddrl.A 3-104
laddrl.HL 3-105
(addrl.rp 3-105
laddrl.xy 3-105
(HLI.data 3-107
(IX + displ.data 3-107
!lY + displ.data 3-107
xv
3-89
3-113
3-114
OR data 3-115
OR reg 3-116
OR (HLI 3-117
OR (IX + displ 3-117
OR (lY + displ 3-117
OUT (Cl.reg 3-118
OUTD 3-119
OTDR 3-119
OUTI 3-120
OTIR 3-120
OUT (portl.A 3- 121
POP rp 3-122
POP IX 3-122
POP IY 3-122
PUSH rp 3-123
PUSH IX 3-123
PUSH IY 3-123
RES b.reg 3-124
RES B.IHLI 3-125
RES b.(lX + displ 3-125
RES b.(ly + displ 3-125
RET 3-126
RET cond 3-127
RETI 3-128
RETN 3-129
RL reg 3-130
RL (HLI 3-131
RL (IX + displ 3-131
RL (lY + displ 3-131
RLA 3-132
RLC reg 3-133
RLC (HLI 3-133
RLC (IX + displ 3-134
RLC (lY + displ 3-134
RLCA 3-135
RLD 3-136
RR reg 3-137
RR (HLI 3-138
RR (IX + displ 3-138
RR (lY + displ 3-13B
RRA 3-139
RRC reg 3-140
RRC IHLI 3-141
RRC (Ix + displ 3-141
RRC (ly + displ 3-141
RRCA 3-142
RRD 3-143
RST n 3-144
SBC A.data 3-145
SBC A.reg 3-146
SBC A.IHLI 3-147
SBC A.(lX + displ 3-147
SBC A(lY + displ 3-147
SBC HLrp 3-14B
SCF 3-149
SET b.reg 3-150
SET b.(HLI 3-151
SETb.(lX+displ 3-151
SET b.(lY + displ 3-151
SLA reg 3-152
SLA IHLI 3-153
SLA (Ix + displ 3-153
SLA (IY + displ 3-153
SRA reg 3-154
SRA (HLI 3-155
SRA (IX + displ 3-155
SRA (lY + displ 3-155
SRL reg 3-156
SRL (HLI 3-157
SRL (Ix + displ 3-157
SRL (ly + displ 3-157
SUB data 3-158
SUB reg 3-159
SUB (HU 3-160
SUB (Ix + dis pi 3-160
SUB (IY + displ 3-160
XOR
XOR
XOR
XOR
XOR
xvi
data
reg
(HLI
(IX +
(IY +
3-161
3-162
3-163
displ 3-163
displ 3-163
Index
Accumulator. using the. 4-2
Add/Subtract flag. 8-7
.Address field. numbers and characters in. 3-172
Algebraic notation. 1-8
Algorithm
multiplication. 8-8
simple sorting. 9-10
Allocating RAM. 2-7
Arithmetic and Logical Expressions. 2-10
ASCII
characters. 2-10
handling data In. 6-1
Assembler. 1-5
arithmetic and logical operations. 3-172
chOOSing an. 1-6
meta-.2-14
mlcro-. 2-14
one-pass. 2-14
reSident. 2-14
two-pass. 2-14
Assembler directive. 2-4
Assembly language
applications. 1-10
fields. 2-1
program. 1-5
BasIc software delav. 11-8
BCD and blnarv. accuracv In. 8-8
Blanking a leading zero. 16-22
Block I/O instruction. 6-6
use of. 11-21
Block. moving data within. 7-8
Block search instructions. 6-6
Block transfer instructions. 8-4
Binary and BCD. accuracy In. 8-8
Binary instructions. 1-1
rounding. 8-24
Binary numbers. doubling and halving. 8-23
Bootstrap loader. 2-15
Bottom-up design. 13-44
BreakpOint. 14-2
insertion of. 14-3
RST as. 14-2
Buffer
double buffering. 12-7
emptYing with Interrupts. 12-19
filling via Interrupts. 12-16
Buffer. emptYing with Interrupts. 12-19
CalibratIOn table. use of. 16-21
Character format. 11-81
Checklist. what to Include In. 14-10
Coding. 13-3
relative Importance of. 13-1
Commenting
examples. 15-4
gUidelines. 15-2
techniques. 2-13
xvii
Index (Continued)
8080A/Z80 (continued)
compatibility features. 3-164
incompatibilities. 3-164
8085/Z80 Incompatibililies. 3-165
ENDC and COND pseudo-operatIOns. 3-174
Error considerations. 13-5
Errors. common. 14-11
Example format. 4-1
Examples. gUidelines for. 4-1
Execution time. saving. 15-15
External references. 2-8
Flowcharting
advantages of. 13-17
credit verification. 13-22
disadvantages of. 13-18
sections. 13-22
switch and light system. 13-19
sWitch-based memorv loader. 13-20
Flowcharts
data. 13-19
hints for use. 15-7
Format. 2-2
FORTRAN. 1-7
Full-duplex. 11-89
General service routines. tasks for. 12-30
Hand assemblv. 1-5
Hand checking questions. 14-11
Handshake. 11-2
Hashing. 9-4
Hexadecimal loader. 1-3
Hexadecimal or octal. 1-3
High-level language
advantages of. 1-9
applications for. 1-10
disadvantages of. 1-9
inefficiency of. 1-8
machine Independence. 1-7
overhead for. 1-9
portability of. 1-8
syntax of. 1-10
unsUitabilitv of. 1-10
Machine language
applicatIOns for. 1-10
program. 1-2
MACRO and ENDM pseudo-operations. 3-174
xviii
Index (Continued)
Macro-assembler. 2-14
Macros
advantages of. 2-12
disadvantages of. 2-12
Mamtenance and redesign. 13-3
Matrix kevboard. 11-60
Memory dump. 14-7
Memory loader error handling. 13-10
Memory map. typical. 15-8
Meta-assembler. 2-14
Micro-assembler. 2-14
Mnemonics. problems with. 1-4
Modular programmmg
advantages of. 13-26
disadvantages of, 13-27
rules for. 13-30
Modularization
pnnclples of. 13-27
switch and light system. 13-28
switch-based memorv loader. 13-28
verification terminal. 13-28
Multiplication algonthm. 8-8
Names
choice of. 2-6. 15-2
defining. 2-6
use of. 2-6
Number svstems. 2-9
Numbers. self-checkin9. 8-17
Non-maskable interrupt. 12-2. 12-3
Object program. 1-2. 1-5
Octal or hexadeCimal. 1-3
One-pass assembler. 2-14
Operation codes. two-word. 3-164
Operator error connection in memorv loader.
13-10
Operator interaction. 13-6
ORG pseudo-operation. 3-171
Passmg parameters. 10-1
PIO
addresses. 11-11
bidirectional mode. 11-15
control mode. 11-15
daisy cham signals. 12-9
directions m control mode. 11-15
mput mode. 11-15
mterrupts. enabling and disabling. 12-7
modes. 11-15. 11-16
output mode. 11-15
registers and control lines. 11-11
steps in configunng. 11-17
Polling. 12-2. 12-10
Polling interrupt svstems with SIOs. 12-10
Portabil itv. 1-6
Primed registers. saving values in. 12-16
Pnoritv.12-16
Problem definition. 13-3
xix
Index (Continued)
rules for, 14-29
sort program, 14-29
special cases. 14-28
Testing aids, 14-27
Testing, structured, 14-28
Thermometer analog hardware, 16-15
Timing incompatibilities, 3-165
Timing Intervals
methods for producing. 11-8
uses of. 11-8
Timing method. choosing a, 11-8
Top-down design
advantages of, 13-44
disadvantages of. 13-44
format for. 13-49
methods. 13-44
of SWitch and light system. 13-45
of switch-based memory loader. 13-46
of verification terminal. 13-47
Transmission errors
correcting. 13,15
redUCing, 11-5
Transparent delay routine, 11-8
TTL encoder. uSing a. 11-34
TTY
Interface. 11-81
receive mode. 11-81
standard TTY, 11-81
transmit mode. 11-86
Two-pass assembler 2-14
Two-word operation codes. 3-164
SIO (continued)
reset. 11-97
special features of. 11-97
Software developement, stages of. 13-1
Software simulator. 14-8
Source program. 1-5
Special Instructions. 4-3
Standard Interfaces. 11-103
Standard program library forms. 15-10
Standard TTY. 11-81
Start bit Interrupt. 12-28
Status and control transfers. documenting.
11-59
Status changes with instruction execution, 3-22
Status information, separating. 11-58
Stopwatch Input procedure. 16-1
Strobe. 11-5
Structures. examples of. 13-33
terminators for. 13-43
Structured kevboard routine, 13-38
Structured program for credit verification
ter mlnal. 13-38
Structured programming
advantages of. 13-35
basic structures of. 13-31
disadvantages of. 13-35
for sWitch-based memorv loader. 13-36
In SWitch and light system. 13-36
rules for, 13-43
when to use. 13-35
Structured receive routine. 13-40
Structed testing. 14-28
Stubs. 13-44
SUbroutine instructions. 10-1
Subroutine library. 10-1
SUbroutines. documenting. 10-2
SWitch and light error handling. 13-7
SWitch and light input. 13-6
SWitch and light outputs. 13-7
SWitch and light system. defining, 13-6
SWitch-based memory loader. defining. 13-8
SWitch bounce. 11-26
Symbol table. 2-6
Svnchronlzlng with I/O devices. 11-57
UART.11-88
Varrables. local or global. 2-13
Vectorrng. 12-2
Verification terminal
defining a. 13-11
error handling. 13-14
Inputs. 13-13
outputs. 13-13
Z80
delay loop constant. 11-10
Index registers. use of. 7-7
Interrupt Inputs. 12-2
Interrupt InstrucliOn, 12-3
Interrupt response. 12-3
I/O instructions, 11-18
non-maskable interrupt. 12-3
xx
OSBORNE/McGraw-Hili SOFTWARE
Practical Basic Programs
by L. Poole et a!.
Some Common BASIC Programs
by L. Poole and M. Borchers
Payroll with Cost Accounting - CBASIC
by Lon Poole et a!.
Accounts Payable and Accounts Receivable - CBASIC
by Lon Poole et al.
General Ledger - CBASIC
by Lon Poole et al.
Some Common Basic Programs - PET/CBM
edited by Lon Poole et a!.