Microprocessor & Interfacing
Microprocessor & Interfacing
Hall
MICROPROCESSOR.
ANDINTERFACING
programming
andHardware
\ m
m2222*2222222 22-2-2."
Digitized by the Internet Archive
in 2010
http://www.archive.org/details/microprocessorsiOOhall
MICROPROCESSORS
AND INTERFACING
Programming and Hardware
Douglas V. Hall
Gregg Division
McGRAW-HILL BOOK COMPANY
New York Atlanta Dallas St. Louis San Francisco
Auckland Bogota Guatemala Hamburg Lisbon
London Madrid Mexico Montreal New Delhi Panama Paris
San )uan Sao Paulo Singapore Sydney Tokyo Toronto
Sponsoring Editor: Paul Berk
Editing Supervisor: James Fields
Design and Art Supervisor/Cover Designer: Frances Conte Saraeco
Production Supervisor: Priscilla Taguer
Text Designer: Susan Brorein
5 6 7 8 9 0 SEMBKP 8 9 3 2 10 9
ISBN Q-07-0255Eb-l
CONTENTS
Preface v More Practice with Simple Sequence Pro-
grams 72
Flags, .Jumps, and WHILE-DO Implementa-
CHAPTER tion 77
REPEAT- UNTIL Implementation and Exam-
Computer Number Systems, Codes, and Digital ples 87
Debugging Assembly Language Programs l 00
Devices 1
Review Questions and Problems 102
Objectives 1
Computer Number Systems and Codes 1
Adding and Subtracting Binary. Oetal, Hex, and
BCD Numbers 10 CHAPTER
Basic Logic Gates 15
Review Questions and Problems 22 IF-THEN -ELSE Structures, Procedures, and
Macros 104
Objectives 104
CHAPTER IF-THEN, IF-THEN-ELSE. and Multiple IF-
THEN - ELSE Programs 104
Computers, Microcomputers, and Micro- Writing and Using Procedures 1 10
processors—
An Introduction 24 Writing and Using Assembler Macros 139
Review Questions and Problems 141
Objectives 24
Computers 24
The 8086. 8088. 80186. 80188, 80286 Micro-
processors—
Introduction 33 C H A P T E " 6
8086 Internal Architecture 33
Introduction to Programming the 8086 37 8086 Instruction Descriptions and Assembler Di-
Review Questions and Problems 41 rectives 144
Instruction Descriptions 144
Assembler Directives 174
CHAPTER
CHAPTER
CHAP T ER 8
8086 Assembly Language Programming Tech-
niques—
Part 1 72 Interrupts and Interrupt Service Proce-
Objectives 72 dures 221
Objectives 221
8086 Interrupts and Interrupt Responses 221 CHAPTER 12
Hardware Interrupt Applications 232
Review Questions and Problems 258 Microcomputer System Peripherals 406
Objectives 406
Microcomputer Displays 406
CHAPTER .7 Raster Scan CRT Graphics Displays 413
CHAPTER 10
Analog Interfacing and Industrial Control 311 CHAPTER 13
Objectives 311
Data Communication and Networks 442
Review of Operational-Amplifier Characteristics
and Circuits 311 Objectives 442
Sensors and Transducers 316 Asynchronous Serial Data Communication 442
D/A Converter Operation, Interfacing, and Appli- Serial Data Transmission Methods and Stan-
cations 321 dards 448
A/D Converter Types, Specifications, and Inter- Asynchronous Communication Software on the
facing 324 IBM PC 461
A Microcomputer-Based Scale 328 Synchronous Serial Data Communication and
A Microcomputer-Based Industrial Process-Con- Protocols 477
trol System 340 Local Area Networks 481
An 8086-Based Process-Control System 342 Review Questions and Problems 487
Developing the Prototype of a Microcomputer-
Based Instrument 355
Digital Filters
Review Questions
357
and Problems 359
CHAPTER 14
Operating Systems, the 80286 Microprocessor,
and the Future 490
CHAPTER 11 Objectives 490
Multiple Microprocessor Systems and Operating System Concepts and Terms 490
The Unix Operating System 496
Buses 361 The INTEL RMX 86™ Operating System 499
Objectives 361 The INTEL 80286 Microprocessor 502
The 8086 Maximum Mode 361 New Directions 513
Direct Memory Access (DMA) Data Transfer 363 Epilogue 516
Interfacing and Refreshing Dynamic RAM 370 Review Questions and Problems 5 17
Processors With Integrated Peripherals — The
80186 and 80188 374
A Coprocessor — The 8087 Math Coproces- Bibliography 519
sor 377 Appendix A 521
Multiple Bus Microcomputer Systems 397 Appendix B 533
Review Questions and Problems 404 Index 547
CONTENTS
PREFACE
For the most part. Microprocessors and Interfac write, lest . ;ind debug. Experience hasshownthat
tng: Programming and Hardware is based on a the most successful approach to writing a pro
throe-quarter series of microprocessor courses gram is to solve the problem fust . and then simply
that my colleagues and I teach. The book is in- implement the solution in the desired program-
tended
students
tor in electrical engineering pro- ming language.
grams, students
in electronic engineering techni- The 8086 instructions are introduced in Chap
cian trainingprograms, and people working in ters 2 through 5, as they are needed to solve sim-
industry who want to upgrade their knowledge of ple programming problems. Chapter 6 contains a
microprocessors. dictionary of all the 8086 and 80 186 instructions.
Before reading this book, you should have some You can refer to this chapter to find further details
basic knowledge of diodes, transistors, and digital about an instruction you want to use to do a par-
circuitry. One of its aims is to teach you how to ticular operation
in a program.
decipher and use manufacturer's literature; ac- Chapter 7 discusses the hardware signals, tim-
cordingly,
relevant
many parts of data sheets are ing, and
system connections of a simple micro-
shown. Because of the large number of actual de- computer. Chapter
7 also teaches a systematic
vices discussedhere, it was impossible to put the approach to troubleshooting a malfunctioning
complete data sheets for all these devices in the 8086-based system. The remaining chapters
appendixes. Therefore, I strongly suggest that you show how the hardware and the programs work
acquire or gain access to the latest edition of the together. Troubleshooting a microprocessor-
Intel Microsystem Components Handbook so based system, for example, usually requires
that, as you work your way through this book, you knowledge of both its hardware and program-
can refer to it if you need further information ming,I discuss
so here how diagnostic routines
about a particular device. The bibliography lists are written and used to find a problem.
other materials I have found useful. Chapter 8 discusses how the 8086 responds to
I have chosen here to teach the programming, interrupts and how interrupt service procedures
system connections, and interfacing of 16-bit mi- are written and used. Chapters 9 through 13 de-
croprocessors,
function
which as the "brains" of scribe
detail
in how a microcomputer is interfaced
microcomputers such as the IBM PC. My experi- with a wide variety of devices and systems. Also
encean as engineer and as a teacher indicates these chapters describe how the hardware and
that it is more productive to learn one micropro- programs for microprocessor-based products are
cessor familyvery thoroughly, and from that developed. Finally, Chapter 14 discusses operat-
strong base learn other families as needed. There- ing systemprograms, and the 80286 and 80386
fore, thisbook concentrates on the Intel microprocessors that are designed to be used as
8086/8088/80 186/80 188/80286/80386 family the brains of multiuser microcomputer systems.
of microprocessors, rather than superficially cov- Program development for 16-bit microproces-
eringmicroprocessor
the families of several man- sorssomewhat
is tedious on hex-keypad-type de-
ufacturers. velopmentsuchboardsas we used for 8-bit pro-
I came into the world of electronics through the cessors. Furthermore, industry does not usually
route of vacuum tubes. Therefore, my first tend- develop microprocessor-based products in this
ency was to approach microprocessors from a manner. Therefore, for working with 16-bit pro-
hardware orientation. However, the more I de- cessors
recommend
I a systems approach. A mi-
signed with
microprocessors and taught micro- croprocessor development
system, an IBM PC, or
processor classes,
the more I became aware that IBM PC-compatible computer, can be used to edit,
the real essence of a microprocessor is what you assemble, link/locate, run, and debug 8086 as-
can program it to do. For this reason the book sembly language programs. For programs that re-
begins with just a brief overview of the hardware quire external hardware, the object code for these
of a computer. The next five chapters show how a programs can be downloaded to some prototype
microprocessor-based microcomputer can be pro- hardware such as an Intel SDK-86 development
grammed
do someto real tasks. board. Chapter 13 contains a program that allows
The emphasis throughout is on writing assem- you to download object code programs from an
bly languageprograms in a top-down structured IBM or IBM-compatible computer to an SDK-86
manner. The idea is to make programs easy to board. An available laboratory manual, written to
accompany this book, shows you how to use the merous
mention.
to Thanks to Lee Campbell of
SDK-86 board and an IBM PC-compatible com- Spokane Community College in Spokane, Wash-
puterassembly
for language programming and ington,meticulously
who worked his way through
interfacing. the manuscript and made many valuable sugges-
In the interfacing sections of this book I have tions. Thanks
to Wayne J. Vyrostek of Westark
tried to show as many circuits as possible that you College in Fort Smith, Arkansas, who reviewed
can build, add to your microcomputer, and exper- the manuscript and contributed several valuable
iment with.
Building and experimenting with real suggestions. Thanks to Intel Corporation for let-
circuits will help you become fluent with micro- ting use
me many drawings from their data books,
processors.
circuits
The in this book are intended so that this book could lead readers into the mate-
just as starters. Hopefully you will grow far rial they
can use to continue their learning. Fi-
beyond what is shown here, if you have sugges- nally, thanks
to my family and friends for their
tionsimproving
for this book or ideas that might patience and support during the long effort of
clarify a point for someone else, please communi- writing this book.
cate with
me.
I wish to express my profound thanks to the Douglas V. Hall
people who helped make this book a reality.
Thanks to Pat Hunter, without whose cheerful en-
couragement
might not I have made it through the
book. She proofread and coded the manuscript, DEDICATION
Before starting our discussion ol microprocessors and The number ol symbols needed in any base numbei sys
microcomputers we need to make sure that some key tern is equal to the base number. In the decimal numbei
concepts ol the number systems, codes, and digital de- system then, there are 10 symbols, 0 through 9. When
vires used in microcomputers are fresh in your mind. If the count in any digit position passes that ol the highest
the short summaries of these concepts in this chapter value symbol, a carry of 1 is added to the next digit posi-
are not enough to refresh your memory, then it is a good tion and
the other digit rolls back to zero. A car odome-
idea to review them in a current digital text before going ter ais good example of this.
on in this book. A number system can be built using powers of any
number as place holders or digits, but some bases are
more useful than others. It is difficult to build electronic
OBJECTIVES circuits which can store and manipulate 10 different
voltage levels but relatively easy to build circuits which
At the conclusion of this chapter you should be able to:
can handle two levels. Therefore, a binary or base -2
number system is used.
1. Convert numbers between the following codes: bi-
nary, octal,
hexadecimal, and BCD.
2. Define the terms bit. nibble, byte, word, most signif- The Binary Number System
icant bit.
and least significant bit. Figure lib shows the value of each digit in a binary
number. Each binary digit represents a power of 2. A
3. Use a table to find the ASCII or EBCDIC code for a
binary digit is often called a bit. Note that digits to the
given alphanumeric character.
right of the binary point represent fractions used for
4. Perform addition and subtraction of binary, octal, numbers less than one. The binary system uses only two
hexadecimal, and BCD numbers. symbols, zero (0) and one (1). Therefore, in binary you
count as follows: 0, 1, 10. 11. 100. 101. 110. Ill, 1000.
5. Describe the operation of gates, nip-flops, latches,
ri(
registers, ROMs, dynamic RAMs, static RAMs. and Binary numbers are often called binary words or just
buses. ifords. Binary words of certain numbers of bits have
6. Describe how an arithemtic logic unit can be in- also acquired special names. A 4-bit binary word is
structed
perform
to arithmetic or logical operations
on binary words.
5 3 4 6 7 2
103 102 10' 10° 10 ' 10 2
COMPUTER NUMBER SYSTEMS AND
CODES
CHAPTER ONE
4096 512 64 8 1 which groups the binary digits in groups ol four rathei
than three Hexadecimal oi base 16 code does this. Fig-
ureIdI shows the digil values foi hexadecimal, which
Is often | ust called lt<-\ Sim e hex is base M>. you have to
have 16 possible symbols for each digit. The tabli of 1 '
in i' I l/) shows the symbols foi hex code. Aftei the deci
327
J*- ' Decimal = "> 327D = 5078 111.11 symbols 0 through 9 air used up. you use the let! cis
A through F foi values l<> through 15.
As mentioned above, each hex digit is equal to foui
8J327 = 40 binary digits. To convert the binary number 1 10101 10
8j~40 = 5
16' 16-' 16' 16°. 16 ' 16 ' 16 '
8j 5 = 0
4096 256 16 1 ,'„ ,,',
(a)
Dec Hex
Binary 101 011 111 .
0 0
Octal
Binary Point
1
1 1
2 = 2
FIGURE 1-3 Octal numbers, (a) Value of place holders. 3 = 3
(b) Conversion of decimal to octal, (c) Conversion of
binary to o( tal. 4 = 4
5 = 5
16pc4 = 0 RE x 16 = 224
Hexadecimal 22 7
O
-1
a
(0 u. i- o o o t- t- i- o ,- t- O r- O O t- r-
5
i- UJ T- O T- O O O t- o ,- o t- i-
z
UJ
Q r- O t- i- O i- t- o ,- O *- t- T- T- T- O
S
o
LU O t- O o
CO u
z
LU CO T- O O T- ,- ,- t- O O r- r- O
>
UJ
CO ,- O T- 1- O T- T- T" T- T- T- O O O t- i-
REFLECTED
CODE
GRAY
O
O
o
o
i-
T-
O
o
o
O
T~
t-
o
o
T-
O
i-
o
o
O
O
*-
o
T-
t-
o
T-
o
o
O
o
o
O
O
i-
O
*-
T-
o
T-
O
1-
o
i-
T-
o
i-
O
o
p
P
o
CO
CO
CO
T-
O
o
O
T-
o
O
T-
o
T-
T-
o
1000
0111 1010
1001 1100
1011 )0100
)0011 1000
0100
0100
0110
0100
0111
0100
0101
UJ
u
X o o o o
UJ
o o o o p p t- t-
o o o o o o o o o o o o
O 1- O T" O O t- o
CO
CM
O
o
o
O
o
o
T"
o
o
T-
o
o
o
i-
o
o
o
o
i-
o 1100
1011 0000 1000
0001
0010
0001
000110001
0100
0001
001
0001
UJ
a in
o
o
-i
< O i- O 1-
s
o CM
o
o
o
p
o
o
t-
o
o
i-
o
o
0010
OOLL
LLOL 1110
LOLL1111 0010
0001
0000
0001
00010100
0001
0011
0001
1011
0001
UJ
t
Q CM
O i- O i- O t- O i-
a
o
m
O
o
o
O
o
o
i-
o
o
T-
o
o
O
o
O
o
T-
o
T~
o
10000000
0001
1001
00010100
0001
0010
0001
0101
0001
0011
0001
CM
CO
<1
X s O «- CM CO t 111 (O N co a> < cd O O LU LI-
UJ u
X UJ
a
_i
LU O f- CM CO 'S- id co r^
Q O t- CM CO « K) (O s
0 o
o
u
LU >
CO E O i- O 1- O i- O T- Q i- O i- O i- O i-
< O O t- t- O O t- •>- S p i- r- O O i- i-
o o o o o o o o
z o o o o
Zj o o o o
CD
r- Z
r-0
uuS is O f- CM CO m- lo to r~- CO en O r- CM CO %* LO
II 3 «
CHAPTER ONE
id hex, mark off groups ol four, moving to the left from r I 1/
HEX
HEX HEX HEX CODE
CODE CODE CODE SELEC- FOR HOL- HOLES
ASCII FOR 7-BIT BCDIC FOR EP EBCDIC FOR TRIC SELEC- LERITH PUNCHED CODE
SYMBOL ASCII SYMBOL BCDIC SYMBOL EBCDIC SYMBOL TRIC SYMBOL FOR HOLLERITH
N U L 0 0 N U L 0 0 N U L 12 0 9 8 1
S 0 H 0 1 S 0 H 0 1 S C H 12 9 1
S T X 0 2 S T X 0 2 S T X 12 9 2
E T X 0 3 E T X 0 3 E T X 12 9 3
EOT 0 4 EOT 3 7 E C T 9 7
E N Q 0 5 E N Q 2 D E N Q 0 9 8 5
A C K 0 6 A C K 2 E A C K 0 9 8 6
BEL 0 7 BEL 2 F BEL 0 9 8 7
B S 0 8 B S 1 6 B S 119 6
H T 0 9 H T 0 5 H T 12 9 5
L F 0 A L F 2 5 L F 0 9 5
V T 0 B X 9 A V T 0 B V T 12 9 8 3
F F 0 C F F 0 C F F 12 9 8 4
C R 0 D * F F C R 0 D C R 12 9 8 5
S 0 0 E S 0 0 E S 0 12 9 8 6
S 1 0 F S 1 0 F S 1 12 9 8 7
D L E 1 0 D L E 1 0 D L E 12 119 8 1
D C 1 1 1 D C 1 1 1 D C 1 11 9 1
D C 2 1 2 D C 2 1 2 D C 2 11 9 2
D C 3 1 3 D C 3 1 3 D C 3 119 3
D C 4 1 4 D C 4 3 5 D C 4 9 8 4
N A K 1 5 N A K 3 D N A K 9 8 5
S Y N 1 6 S Y N 3 2 S Y N 9 2
E T B 1 7 E 0 B 2 6 E T B 0 9 6
F S 1 C F L S 1 C F S 119 8 4
G S 1 D G S 1 D G S 119 8 5
R S 1 E R D S 1 E R S 119 8 6
U S 1 F U S 1 F U S 119 8 7
S P 2 0 S P 0 0 S P 4 0 S P NO PNCH
I 2 1 1 6 A i 5 A \\ 2 7 i 12 8 7
2 2 4+f 5 F 7 F 2 D 8 7
# 2 3 # 4 B # 7 B # 7 E # 8 3
$ 2 4 $ 2 B $ 5 B $ 7 9 $ 11 8 3
% 2 5 % 5 C % 6 C % 3 D % 0 8 4
) 2 9 6 F ) 5 D ) 3 9 ) 11 8 5
2 A 6 C 5 C 7 C 118 4
(c ontinued)
CHAPTFK ONI
TABLE 1-2
COMMON ALPHANUMERIC CODES (CONTINUED)
HEX
HEX HEX HEX CODE
CODE CODE CODE SELEC FOR HOL- HOLES
ASCII FOR 7-BIT BCDIC FOR EP EBCDIC FOR TRIC SELEC LERITH PUNCHED CODE
SYMBOL ASCII SYMBOL BCDIC SYMBOL EBCDIC SYMBOL TRIC SYMBOL FOR HOLLERITH
t 2 B I 4 E i 0 E i 12 8 6
2 C 1 B 6 B 4 4 0 8 3
2 D 6 0 0 0 11
2 E 7 B 4 B 2 6 12 8 3
1 2 F 1 1 6 1 / 4 1 / 0 1
0 3 0 0 0 A 0 F 0 0 3 1 0 0
1 3 1 1 4 1 1 F 1 1 7 7 1 1
2 3 2 2 4 2 2 F 2 2 3 6 2 2
3 3 3 3 0 3 3 F 3 3 7 6 3 3
4 3 4 4 4 4 4 F 4 4 7 1 4 4
5 3 5 5 0 5 5 F 5 5 3 5 5 r)
6 3 6 6 0 6 6 F 6 6 3 4 6 6
7 3 7 7 4 7 7 F 7 7 7 5 7 7
8 3 8 8 4 8 8 F 8 8 7 4 8 8
9 3 9 9 0 9 9 F 9 9 3 0 9 9
3 A 4 D 7 A 4 D 8 2
3 B 2 E 5 E 4 5 118 6
< 3 C < 7 E < 4 C < 12 8 4
= 3 D V 0 F = 7 E = 0 6 = 8 6
> 3 E > 4 E > 6 E > 0 8 6
? 3 F ? 3 A ? 6 F ? 4 9 ? 0 8 7
,, 4 0 @ 0 C (a 7 C @ 3 E (1 8 4
A 4 1 A 7 1 A C 1 A 6 C A 12 1
B 4 2 B 7 2 B C 2 B 1 8 B 12 2
C 4 3 C 3 3 C C 3 C 5 C C 12 3
D 4 4 D 7 4 D C 4 D 5 D D 12 4
E 4 5 E 3 5 E C 5 E 1 D E 12 5
F 4 6 F 3 6 F C 6 F 4 E F 12 6
G 4 7 G 7 7 G C 7 G 4 F G 12 7
H 4 8 H 7 8 H C 8 H 1 9 H 12 8
I 4 9 I 3 9 I C 9 I 2 C 1 12 9
J 4 A J 2 1 J D 1 J 0 7 J 11 1
K 4 B K 2 2 K D 2 K 1 C K 11 2
L 4 C L 6 3 L D 3 L 5 9 L 11 3
M 4 D M 2 4 M D 4 M 6 F M 11 4
N 4 E N 6 5 N D 5 N 1 E N 11 5
0 4 F 0 6 6 0 D 6 0 6 9 0 11 6
P 5 0 P 2 7 P D 7 P 0 D P 11 7
Q 5 1 Q 2 8 Q D 8 Q 0 C Q 11 8
R 5 2 R 6 9 R D 9 R 6 D R 11 9
S 5 3 S 1 2 S E 2 S 2 9 S 0 2
T 5 4 T 5 3 T E 3 T 1 F T 0 3
[continued)
HEX
HEX HEX HEX CODE
CODE CODE CODE SELEC- FOR HOL- HOLES
ASCII FOR 7 BIT BCDIC FOR EP EBCDIC FOR TRIC SELEC- LERITH PUNCHED CODE
SYMBOL ASCII SYMBOL BCDIC SYMBOL EBCDIC SYMBOL TRIC SYMBOL FOR HOLLERITH
U 5 5 U 1 4 U E 4 U 5 E U 0 4
V 5 6 V 5 5 V E 5 V 6 E V 0 5
w 5 7 w 5 6 w E 6 w 2 8 w 0 6
X 5 8 X 1 7 X E 7 X 5 F X 0 7
Y 5 9 Y 1 8 Y E 8 Y 0 9 Y 0 8
z 5 A z 5 9 z E 9 z 3 F z 0 9
[ 5 B [ 7 D I A D [ 7 F I 12 8 2
\ 5 C \ 1 E N L 1 5 \ 0 8 2
I 5 D ] 2 D ] D D ] 11 8 2
A 5 E D 3 C "I 5 F A 11 8 7
5 F 6 0 6 D 0 8 0 8 5
6 0 RES 1 4 8 1
a 6 1 a 8 1 a 6 4 a 12 0 1
b 6 2 b 8 2 b 1 0 I. 12 0 2
t: 6 3 c 8 3 c 5 4 c 12 0 3
d 6 4 d 8 4 d 5 5 d 12 0 4
e 6 5 e 8 5 e 1 5 e 12 0 5
f 6 6 f 8 6 f 4 6 f 12 0 6
<J 6 7 g 8 7 g 4 7 g 12 0 7
h 6 8 h 8 8 h 1 1 h 12 0 8
i 6 9 i 8 9 i 2 4 i 12 0 9
I 6 A 1 9 1 i 0 7 1 12 111
k 6 B k 9 2 k 1 4 k 12 112
I 6 C 1 9 3 I 5 1 1 12 113
in 6 D m 9 4 m 6 7 m 12 11 4
n 6 E n 9 5 n 1 6 n 12 115
(i 6 F o 9 6 o 6 1 o 12 116
P 7 0 P 9 7 P 0 5 P 12 117
q 7 1 q 9 8 q 0 4 9 12 118
r 7 2 r 9 9 r 6 5 r 12 119
s 7 3 S A 2 s 2 1 s 11 0 2
t 7 4 t A 3 t 1 7 t 11 0 3
u 7 5 u A 4 u 5 6 u 110 4
V 7 6 V A 5 V 6 6 V 11 0 5
w 7 7 w A 6 w 2 0 w 110 6
X 7 8 X A 7 X 5 7 X 11 0 7
y 7 9 y A 8 y 0 1 y 110 8
z 7 A z A 9 z 3 7 z 110 9
1 7 B { 8 B ( 12 0
1 7 C 4 F 1 12 11
} 7 D } 9 B 1 11 0
~
7 E c 4 A - 11 0 1
DEL 7 F DEL 0 7 DEL 12 9 7
BCDIC SELECTRIC
HEX DIGIT HEX DIGIT R,,T,T, SR,4R,R,
PCBA 2 ' 2 • 2 ' 2° HEX DIGIT HEX DIGIT
( HAN Ik ONI
these codes, an additional bit. called a parity bit. is often TABLE 1-3
added as the mosl significanl bit. DEFINITIONS OF CONTROL CHARACTERS
/'(irin/ is a term used i<> Identifj whethei .1 data word
has .111 odd or even number of l's. II .1 data word con NUL NULL DC2 DIRECT CONTROL 2
tains an odd number ol l's. the word is said to have odd SOH START OF HEADING DC3 DIRECT CONTROL 3
parity rhe binary woi d 01 l 0 I l I with five l's has odd STX START TEXT DC4 DIRECT CONTROL 4
parity. The binary word 01 10000 has an even number ol ETX END TEXT NAK NEGATIVE
• •• • • • • ••••
•••• • • • • • • ••••• • ••••••
• •••
• •••• • • • • ••• • • ••
•••• •• •• • •• • ••••••• • ••
ZONE
PUNCHES
iiiiiiiii i
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 II IJ 0 '.
docoooooooooooggooooIIIIIIIIooooooIIooooooooogoooooooo OGC
1 I I II I I I I I I l| I I I Ill i 11 n ii i 1 1 11 1 i i i i i i i i i i i i n i i i i i ii i i i i i i i i i i i i i i i i i i i i I I 1
I 3 ] 3 31 3 3 3 1 3 3 3 3 1 3 3 3 3 3 3 3 1 3 3 3 3 3 3 3 3 3 3 3 1 1 3 1 1 3 3 3 3 3 3 3 3 3 3 3 1 3 3 3 3 3 3 3
33 33 3 3 3 3 3 33 3 3 3|3 3 3 3:
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4| 1 4 4 I 144444|4444444<|4444444|4444444444444444444444444444444 4 l 4
DIGIT
555555555555555 5|555 i 55
5 5 51 5 5 5 51 5 5 5 5 5 5 5 5| 5 5 5 5 5 5 51 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 PUNCHES
6 E G 6 6 6 G 6 6 6 6 6 E 6 6 6 E | B 6 6 6 6 I,56b666|86666666|6666666|6666666S68666|666666666666666
j j ) 7 7 j 7 7 7 ; 7 7 7 7 7 7 7 7 1 J J; 7 7 7 7 7 7 7 J 7 1 7 7 7 7 7 7 7 7 1 7 7 7 7 7 7 7 1 7 ) 7 7 7 7 J 7 7 1 1 7 7 7 7 7 7 7 7 7 7 J 7 7 7 7 7 J
! 8 8 8 » 3 E ! 3 t 3 3 S 8 8 8 S 8 6 | 8 8 8 3 8 8 8 8 8 ' 3 1 1 8 8 8 8 8 I S 8 1 8 ! 8 1 8 8 ! | J 8 8 I 8 8 1 1 1 1 1 1 ! M I ! ! ! 6 5 8 I 8 It I
FIGURE 1-7 (a) ASCII punched paper tape; (b) Hollerith punched card.
10 CHAPTER ONE
lis. some way must be established to represenl the sign %13 00001101
ol the number with .1 1 oi a 0 + 9 00001001
I he waj to 1In 1Ins is to reserve the mosl significant t22 00010110
bit oi the data word .is ,1 sign bit and to use the resl ol
Sign bit is 0
the bits ol the data word to represenl the size (magni so result is positive
tude) ot the quantity. A computer thai works with 8 bil
(a)
words will use the MSB tbit 7) as the siiju hit and the
lowei seven bits to represenl the magnitude for the t 13 00001101
numbers. The usual convention is to represenl a posi-
- 9 11110111 2's complement for 9 with sign bit
tive number with a o sign bil and a negative number
with a 1 sign bit.
f 4 1J 00000100
1 Sign bit is 0
To make computations with signed numbers easier,
so result is positive
tin- magnitude ol negative numbers is represented m a
Ignore carry
special form called 2's complement. The 2's complement
(b)
ol a binary number is formed by inverting each bit ol the
data word and adding one to (he result. Some examples
+ 9 00001001
should help clarify all of this.
-13 11110011 2's complement lor 13 with sign bit
The number ' 7,,, is represented 1118-bil sign-and-
magnitude form as 0000 0111. The sign bit is zero, - 4 11111100 Sign bit is 1
which indicates a positive number. The magnitude of 0000001 1 So invert each bit
icant
bits7 represent the magnitude directly in binary. (a) +() and 1 I % !.(b) 9 and ' I 1. (t 1 +9 and I I
If the number is negative, as indicated by the sign bit id) -') and -13.
FIGURE 1-9 Positive and negative numbers represented The final example in Figure l-10d shows the results of
with a sign bit and 2's complement. adding two negative numbers. The sign bit of the result
represented with 8 binary bits. There are two common methods for doing binary sub-
traction. These
are the pencil method and the 2's com-
plementmethod.
add Figure l-12a shows the truth
is a 1. and the result is negative and in 2's complement table for binary subtraction of two binary digits A and B.
form. Again, inverting each bit. adding 1. and prefixing Also included in the truth table is the effect of a borrow
a minus sign will put the result in a more recognizable in, B|N. from subtracting previous digits. Figure l-12b
form. shows an example of the "pencil" method of subtracting
Now let's consider the range of numbers that can be two 8-bit numbers. Using the truth table, this method is
represented with eight bits in sign-and-magnitude done the same way that you do decimal subtraction.
form. Eight bits can represent a maximum of 2s or 256 A second method of performing binary subtraction is
numbers. Since we are representing both positive and by adding the 2's complement representation of the bot-
negative numbers, half of this range will be positive and tom number(subtrahend) to the top number (minu-
half negative. Therefore, the range then is () to • 127 end). Figurel-12c shows how this is done. First repre-
and from 1 to 128. Figure 1-1 1 shows the sign-and- sent the
top number in sign-and-magnitude form. Then
magnitude binary representations for these values. It form the 2's complement sign-and-magnitude represen-
INPUTS OUTPUTS
0 1 0 1 1
01001101 01001101
38,o 01011000 Complement + 10101000 Complement
00001010
^. 10100111
Qj] 11110101
Add one v 1
Indicates
1011
result negative
Two's comp 10101000 and in two's
Carry complement form
FIGURE 1-12 Binary subtraction, (a) Truth table for 1 bits and borrow, (b) Pencil
method. (C) 2's complement positive result, id) 2's complement negative result.
12 ( MAI' 1 1 K ONI
tation for the negative ol the bottom number. Flnallj 01100 Quotient
.hUI the two parts formed For the example In Figure
I- 12c, the sign of the result Is a zero which Indicates the
Divisor 110)1001000 Dividend 1.'
6)72
resull is posihvt' and in true form. The final carry pro 110
(lined by the addition can be ignored. Figure I 12d 110
BINARY MULTIPLICATION
There are several methods of doing binary multiplica- nets arc added as they are produced and the sum ol the
tion. Figure1-13 shows what is called the pencil method partial products is shifted right rather than each partial
because it is the same as the way you learned to multiply product being shifted left.
decimal numbers. The top number or multiplicand is A point to note about multiplying numbers is the
multiplied by the least-significant digit of the bottom number of bits the product requires. For example, mul-
number or multiplier. The partial product is written tiplying
4-bit
two numbers can give a product with as
down. The top number is multiplied by the next disj.il ol many as 8 bits, and two 8-bit numbers can give a 16-bit
the multiplier. The resultant partial product is written product.
down under the last, but shifted one place to the left.
Adding all the partial products gives the total product. BINARY DIVISION
This method works well when doing multiplication by
Binary division can also be performed in several ways
hand, but it is not practical lor a computer because the
Figure 1-14 shows two examples of the pencil method.
type of shifts required make it awkward to implement.
This is the same process as decimal long division. How-
One of the multiplication methods used by computers
ever,
is itmuch simpler than decimal long division be-
is repeated addition. To multiply 7 %55. for example,
causedibits
the of the result (quotient) can only be 0 or
the computer can just add up seven 55's. For large num-
1. A division is attempted on part of the dividend. If this
bers, however,this method is slow. To multiply 786 x
is not possible because the divisor is larger than that
253. for example, requires 252 add operations
part of the dividend, a 0 is entered into the quotient.
Most computers use an add-and-shift-right method.
Another attempt is then made to divide using one more
This method takes advantage of the fact that, for binary
digit of the dividend. When a division is possible, a 1 is
multiplication, the partial product can only be either the
entered in the quotient. The divisor is then subtracted
top number exactly if the multiplier digit is a 1. or a 0 if
from the portion of the dividend used. The process is
the multiplier digit is a 0. The method does tin same
continued as with standard long division until all the
thing as the pencil method except that the partial prod-
dividend is used. As shown in Figure l-14b. O's can be
added to the right of the binary point and division con-
tinued
convert
to a remainder to a binary equivalent.
11 1011 Multiplicand Another method of division that is easier for com-
100 111 '4/ a carry with a remainder of 9. The 9 is written down and
47e
the carry is added to the next digit column. Then 7 plus
t3G:; + 011 110 + 368 3 plus a carry gives a decimal 1 1 , or B in hex.
1 000 101 8,o 13K, You may use whichever method seems easier to you
and gives you consistently right answers. If you are
1 0 58 1 0 5„
doing a great deal of octal or hexadecimal arithmetic you
(a) (b) might buy an electronic calculator specifically designed
to do decimal, octal, and hexadecimal arithmetic.
FIGURE 1-15 Octal addition, (a) Adding binary
equivalents, (b) Direct octal addition. OCTAL SUBTRACTION
!4 CHAPTER ONE
BCD addition is greatei than loni or 9. Figure l 19 17 1 0111
shows three examples ol BCD addition. The first, In Fig
0 1001
urc l-19a, is very straightforward because the sum is
less than 9. rhe resull is the same as il would be foi 8 i HID Illegal BCD
si. inil. ii d Inn. ii \ 110 Subtract 6
leu the second example, In Figure I 19b, adding BCD
1000 8,,
7 in BCD 5 produces I 100. This is ,i correcl binary re
suli nl 12 bin il is an Illegal BCD code. Io converl Ihe IGURI I .Ml IK 1) m 11111, 11 (ion
resull io BCD format, .i correction factor ol 6 is added.
The rcsuli ol adding 6 is 0001 0010, which is the legal
BCD rude for 12.
BCD SUBTRACTION
Figure l-19c shows anothei case where .i correction
factoi must be added The initial addition ol 9 and 8 Figure 1 20 shows a subtraction <>i B< Dli (0001 01 1 1)
produces 0001 0001. Even though the lower lour digits minus BCD 9 (0000 1001). The initial result, 1 0
are less than 9, this is an incorrecl BOD resull because a 1110. is not a legal BCD number. Whenever this occurs
carry out of bit 3 of the BCD digit-word was produced. ill BCD sill ill, id ion. 0 must he subtracted from Ihe ini-
Tins carry out ol hit 3 is often called an auxiliary cany. tial resull Io produce Ihe correct BCD result. For the
Adding the correction factor of 6 gives the correct BCD example shown in Figure 1-20, subtracting 0 gives a
result of 0001 0111 or 17. correct BCD resull ol 0000 100(1 or 8.
To summarize, a correction factor of 6 must be added The correction factor of 0 must he subtracted from
to the result if the resull in the lower 4 bits is greatei any BCD digit-word il that digit-word is greater than
than 9 or if the initial addition produces a carry out of IO0I . or if a borrow from the next higher digil occurred
bit 3 of any BCD digit-word. This correction is some- during the subtraction.
times called
,i decimal adjust operation.
The reason lor the correction factor of 6 is that in BCD
we want a carry into the next digit after 1001 or 9. but in
binary a cany out of the lower four bits does not occur BASIC LOGIC GATES
until after 1111 or 15. which is 6 more than 9.
Microcomputers such as those we discuss throughoul
this book often contain basic logic gates as "glue" be-
BCD
tween(large
LSI scale integration) devices. For trouble-
35 0011 0101
shootingsystems
these it is important to be able to pre-
+ 23 -t 0010 0011 dict logic
levels at any point directly from Ihe schematic,
rather than having to work your way through a truth
58 0101 1000
table for each gate. This section should help refresh
( a) your memory of basic logic functions and help you re-
memberto how
quickly analyze logic gale circuits.
BCD
7 0111
Inverting and Non-inverting Buffers
+ 5 t 0101
Figure 1-21 shows the schematic symbols and truth
12 1100 Incorrect BCD tables for simple buffers and logic gates. The first tiling
to remember about these symbols is that the shape of
+ 110 Add 6
the symbol indicates the logic function performed by the
0U0 100 If) Correct BCD 12 device. The second thing Io remember about these sym-
bolsthat
is a bubble or no bubble indicates the asser-
(6)
tion level
for an input or output signal. Let's review how
modern logic designers use these symbols.
BCD The first symbol lor a buffer in Figure l-21a has no
1001 bubbles on the input or output. Therefore, the input is
+ 8
active high and the output is active high. We read this
+ 1000
symbol as follows. If the input. A. is asserted high, then
17 00010001 Incorrect BCD the output. Y, will be asserted high. The rest of the truth
110 Add 6 table is covered by the assumption that if the A input is
not asserted high, then the Y output will not be asserted
00010111 Correct BCD 17 high.
The next two symbols lor a hi i Her each contain a huh
(c)
ble. the bubble on the output of the first of these indi-
FIGURE 1-19 BCD addition, la) No correction needed. cates thatthe output is active low. The input has no
(b) Correction needed because of illegal BCD result. bubble so it is active high. You can read the function of
(c) Correction needed because of carry out of BCD the device directly from the schematic symbol as follows.
digit. If the A input is asserted high, then the Y output will be
;=0* :=x> 1) i) ii 1
Look at the truth table in Figure 1-2 lb to see if you agree
with this.
1) i ii 1
Figure l-21c shows the other two possible cases for
Y = A %B Y = A +B 1 0 0 1
the AND symbol. The first of these has bubbles on the
:=C>' %:n>-< 1 i 1 II
inputs
schematic,
and on the outputs.
you should
If you see this symbol
immediately see that the output
in a
1
I
II
1
1
II
16 CHAPTER ONE
complement ol the logic state on (.). When the enable The I) Hip nop in Figure I 22b also has direct set IS|
Inpul is made low again, the state on Q al that time will ami reset (R) inputs a ihp Hup is considered set ii its (}
be latched there Anj changes on D will have no effecl on output is :. It is reset if its Q output is a zero. I In
Q until the enable Input is made high again. When the bubbles on the set and reset inputs tell you that these
enable inpui Lines low, then, the state present on I) insi inputs are active low. The truth table I or the I ) nip Hop
before the enable goes low will be stored on the Q out in Figure l 221) Indicates that the set and reset inputs
put. Keep ilus operation In mind as you read about the are asynchoriOUS. This means thai if the set input is
I) flip Hop In the next section. asserted low, the output will be set. regardless ol the
stale on the I) and the clock inputs. Likewise. II the
THI I) imp-hop reset input is asserted low. the Q output Will be reset,
regardless of the state of the D and clock inputs rheXs
The first type o\Jlip-lh>i> to review is the I) type. Figure
in the /' and CK columns ol the truth table remind you
l-22fa shows the schematic symbol and the truth table
that these inputs are "don't cares' it set Ol reset is ,is-
for ,i typical I) flip-flop. Note that this device has a clock
sei led. The condition indicated by the asterisks (*) is a
input. CK, in place of the enable input on the I) latch.
nonstable condition; that is. it will not persist when
Also note the up arrows in the clock column of the truth
reset or clear inputs return to their inacl ive (high) level.
table. These arrows are used to indicate that a one or
zero on the D input will be copied to the Q output at the
instant the clock input goes from low to high. In other THE )K FLIP-FLOP
words, the D flip-Hop takes a snapshot of whatever state Figure l-22c shows the schematic symbol and the truth
is on the D input when the clock goes high, and displays table for a common Ik Hip-Hop such as the 74LS76. The
the photo on the Q output. If the clock input is low, a two data inputs. ) and K make this device more versatile
change on D will have no effect on the output. Likewise. than a D Hip-Hop. The bubble on the clock input of the
if the clock input is high, a change on D will have no symbol and the downward arrows in the truth table in-
effect on the Q output. Contrast this operation with dicate that
the Q and Q outputs will only change when
that of the D latch to make sure you understand the the clock inpul goes from a high to a low. Changes on )
difference between the two devices. or K will have no effect on the output if the clock input is
low or if the clock input is high.
D t Q 0
If | and K are both low when the CK input goes low. the
outputs will remain the same as they were before the
X u QN On
clock edge. This is indicated by QN and QN in the truth
0 i 0 i
table. If ) is low and K is high at the time of the clock
1 l 1 0
edge. Q will become a zero. If | is high and K is low at the
time of the clock edge, Q will become a one. If | and K are
both high at the time of the clock edge, the Q output will
toggle. This means that it will change to the opposite
s R u CK a Q
state of what it was before the clock edge. The Ik Hip-Hop
1 1 1 t 1 0
also has asynchronous set and reset inputs which func-
D Q
1 0 1 n 1
1
tion the
same as those of the D Hip-Hop described previ-
>CK I 1 X II 0N (.',. ously.
1 1 X 1 oN On
II 1 X X t 0 REGISTERS
1 ii X X i) i
Flip-Hops can be used individually or in groups to store
0 v X binary data. A register is a group of D Hip-Hops con-
nected
parallel
in as shown in Figure l-23a. A binary
word applied to the data inputs of this register will be
transferred to the Q outputs when the clock input is
A fl CK Q Q made high. The binary word will remain stored on the Q
1) II 1 1 1 °N °N outputs until a new binary word is applied to the D in-
I.I 1 1 1 0 1 puts and
a low-to-high signal applied to the clock input.
1 0 1 1 1 0 Other circuitry can read the stored binary word from the
1 1 1 1 TOGGLE
Q outputs at any time without changing its value
'CK ,
CK> *-
DATA
OUT
FIGURE 1-23 Registers, (a) Simple data storage, (b) Shift register.
word left or right when the register is clocked. As we will table counter functions, so there is no need to go into
show later in this chapter, the ability to shift binary the internal circuitry of the device. If the reset input is
numbers is very useful. asserted, the Q outputs will all be made zeros. After the
reset signal is unasserted, each clock pulse will cause
COUNTERS the binary count on the outputs to be incremented by
one. As shown in Figure l-24b. the count sequence will
Flip-Hops can also be connected in parallel to make
go from 0000 to 1111. If the outputs are at 1111, then
counters. Figure 1-24 shows a schematic symbol and
the next clock pulse will cause the outputs to "roll over"
count sequence for a presettablc 4-bit binary counter.
to 0000 and a carry pulse to be sent out the carry out-
The main point we want to review here is how a preset-
put. This
carry pulse can be used as the clock input tor
another counter.
Now. suppose that we want the counter to start count-
O, O, O, Q„ ing fromsome number other than 0000. We can do this
0 0 0 0 by applying the desired number to the four data inputs
and asserting the load input. For example if we apply a
binary 6, 01 10, to the data inputs and assert the load
> CLOCK input, this value will be transferred to the Q outputs.
D0 After the load signal is unasserted, the next clock signal
will increment the Q outputs to 0111 or 7. Counters
D,
such as this can be connected in series (cascaded! to
D-, 0.
produce counters of any desired number of bits.
D 0
LOAD
ROMs
18 CHAPTFK ONt
,\ i mm ss ii
NCI l is
3DRESS
BUS
A ,
1, ",,i 4Q ^M
D0
•
DATA
BUS
O, i < %º
that the information stored in them is not lost when the lines connect to each device to allow us to address one of
power is removed from them. the 32,768 words in each. A set of parallel lines used to
Figure l-25a shows the schematic symbol of a com- send addresses or data to several devices in this way is
mon ROM. As indicated by the eight data outputs. DO- called a bus. The data outputs of the ROMs are likewise
D7. this ROM stores 8-bit data words. The data outputs connected in parallel so that any one of the ROMs can
are three-state outputs. This means that each output output data on the common data bus. If these ROMs
can be at a logic low state, a logic high state, or a high- had standard two state outputs, a serious problem
impedance, floating state. In the high-impedance state would occur because each device would be trying to out-
an output is essentially disconnected from anything put an
addressed word onto the data bus. The resulting
connected to it. If the CE input of the ROM is not as- argument between data outputs would probably destroy
serted, all
thenof the outputs will be in the high-imped- some of the outputs and give meaningless information
ance state.
Also, most ROMs switch to a lower-power- on the data bus. Since the ROMs have three-state out-
consumption condition if CE is not asserted. If the CE puts, however,we can use external circuitry to make
input is asserted, the device will be powered up. and the sure that only one ROM at a time has its outputs en-
output buffers will be enabled. Therefore, the outputs abled. very
The important principle here is that when-
will be at a normal logic low or logic high state. You will ever several
outputs are connected on a bus. the outputs
soon see why this is important if you don't happen to should all be three-state, and only one set of outputs
remember. should be enabled at a time.
You can think of the binary words stored in the ROM At the beginning of this section we mentioned that
as being in a long, numbered list. The number that cor- some ROMs can be erased and rewritten or repro-
responds
each to stored word is called its address. In grammed with new data. Here's a summary of the differ-
order to get a particular word onto the outputs of the ent types of ROM.
ROM you have to do two things. You have to apply the
Mask-programmed ROM — Programmed during
address of that word to the address inputs, A0-A14, and
manufacture; cannot be altered.
you have to assert the CE input to turn on the outputs.
Incidentally, you can tell the number of binary words PROM — User programs by blowing fuses: cannot be
stored in the ROM by the number of address inputs. The altered except to blow additional fuses.
number of words is equal to 2N where N is the number of
EPROM — Electrically programmable by user: erased
address lines. The device in Figure l-25a has 15 ad-
by shining ultraviolet light on quartz window in
dress lines,A0-A14. so the number of words is 215 or
package.
32.768. In a data sheet this device would be referred to
as a 32K x 8 ROM. This means 32K addresses by 8 bits EEPROM — Electrically programmable by user; erased
per address.
with electrical signals instead of ultraviolet light.
Now. let's see why we want three-state outputs on this
STATIC AND DYNAMIC RAMs
ROM. Suppose that we want to store more than 32K
data words. We can do this by connecting two or more The name RAM stands for random-access memory, but
ROMs in parallel as shown in Figure l-25b. The address since ROMs are also random access, the name probably
20 CHAPTER ONE
ACTIVE-HIGH DATA
SELECTION
M H M 1 , AMI 1 lir.1l III i >l'l H "%
LOGIC
S3 S2 SI SO FUNCTIONS H (no carry) ( rj 1 (with carry)
L L L L / A F = A F = A PLUS 1
1 L L H ' A i B F = A * B F ~ (4 ) S) PLUS 1
L 1 H L I At; F = A i li Z7 WiS] PLUS 1
1 L II H I (1 F MINUS 1 (2'sCOMPLI F - ZERO
1 H 1 L F = AB F - A PLUS AB f = 4 PLUS4« PLUS 1
L M 1 H F = B F • {A + B) PLUS4S F = (4 + S) PLUS 40 PLUS 1
1 H H L F = A®B F = A MINUSff MINUS 1 f = 4 MINUSS
1 M H H F = AB F - AB MINUS 1 f = AB
M 1 L L r a i /; F = A PLUS AB F = 4 PLUS4S PLUS 1
H L 1 H F = 4©B F = A PLUSS F = 4 PLUS ff PLUS 1
M L M L F = B F= [A +fi) PLUSES F= (4 +B) PLUS 4S PLUS 1
H 1 H H F = AB F = 4flMINUS 1 F = 4S
n I 1 0
^ / F F0
n II 1 0
^3 / F, F0
1 1 1 0
4©e
FIGURE 1-27 Arithmetic logic unit (ALU), (a) Schematic symbol, (fa) Truth table.
(c) Sample AND, OR, XOR operations.
data inpLits. In other words, instead of having to build a Seven-segment display code
different circuit to perform each of these functions, we
Alphanumeric codes: ASCII, BCDIC, EBCDIC.
have one programmable device. We can perform any of
Selectric, Hollerith
the operations that we want in a computer with a se-
quence
simple
of operations such as those of the Parity bit, odd parity, even parity
74LS181. Therefore, an ALU is a very important part of
the microprocessors and microcomputers which we dis- Converting between binary, octal, hexadecimal, BCD
cussthe
in next chapter. Arithmetic with binary, octal, hexadecimal. BCD
ROM: address lines, data lines, bus lines RAM: static, dynamic
nonvolatile volatile
three-state READ/WRITE input
cascaded outputs ALU
enable input
c. 500 b. -7
c. -26
2. Convert the following binary numbers to decimal: d. -125
a. 1011
b. 11010001 12. Show the subtraction, in binary, of the following
c. 1110111001011001 decimal numbers using both the pencil method
and the 2's complement addition method:
3. ('(invert to following numbers to octal:
a. 7-4
a. 110101001 binary b. 37 - 26
b. 1 1 decimal
c. 1 25 - 93
c. 111011101100 binary
13. Show the multiplication of 1001 and 011 by the
4. Convert the following octal numbers to decimal:
pencil method. Do the same for 11010 and 101.
a. 314
b. 74 14. Show the division of 1 100100 by 1010 using the
c. 43 pencil method.
b. 62
c 33
0101 1001 BCD
S. The L key is depressed on an ASCII-encoded key- 0010 0110 BCD
board. What
pattern of 1 's and 0's would you expect
to find on the seven parallel data lines coming from
the keyboard? What pattern would a carriage re- 0110 01 11 BCD
turn, CR,
give? 001 1 1001 BCD
10. Show addil inn of: 16. For the circuit in Figure 1-28
a. 1001 I , and 101 I , m binary a. Is the Y output active high or active low?
I). 37|0 and 2510 in BCD b. Is the C signal active high or active low?
c. 37H and 25R in octal c. What input conditions on A. B. and C will
22 CHAPT1K ONI
19. Why do most ROMs and RAMs have three stale out
puts?
20. Using Figure I 27, show the programming "I th<
select and modi- Inputs the 74181 requires to pi i
form the following arithmeti< functions:
a. A i B
I). A MINUS li MINUS I
c. A PLUS li
Computers, Microcomputers,
and Microprocessors —An
Introduction
We live in a computer oriented society and we are con- parallel lines called buses. The three buses are the ad-
stantly bombardedwith a multitude of terms relating to dress bus.the data bus. and the control bus.
computers. Before getting started with the main flow of
the book we will t ry to clarify some of these terms and to MEMORY
give an overview of computers and computer systems.
The memory section usually consists of a mixture of
RAM and ROM. It may also have magnetic floppy disks,
OBJECTIVES magnetic hard disks, or laser optical disks. Memory has
two purposes. The first purpose is to store the binary
At the conclusion of this chapter you should be able to: codes for the sequence of instructions you want the
computer to carry out. When you write a computer pro-
1. Define the terms: microcomputer, microprocessor, gram, what
you are really doing is just writing a sequen-
hardware, software, firmware, time share, multi- tial list
of instructions for the computer. The second
tasking, distributed
processing, and multiprocess- purpose of the memory is to store the binary-coded data
ing. with which the computer is going to be working. This
2. Describe how a microcomputer fetches and executes data might be the inventory records of a supermarket,
an instruction.
for example.
INPUT
DEVICE
I
CONTROL CENTRAL CONTROL
1 0 BUS BUS MEMORY
PROCESSING
PORTS UNIT
(RAM AND
ROM)
(CPUI
OUTPUT
DEVICE '
ADDRESS BUS
24
such as a video display terminal, a printer, 01 .1 digital HARDWARE, SOFTWARE, AND FIRMWARI
to-analog il ' A) 1onvertei Physically, an input 01 outpul When working around computers you hear the terms
port is often just .1 set ol parallel l> Hip Hops which let hardware, software, and firmware almost constantly.
data pass through when they arc enabled or clocked by a Hardware is the name given to the physical devices and
control signal from the CPU. * in iiiiiv ol the computer. Software refers 10 the pro-
grams written
foi the computer. Firmware is the term
CENTRAL PROCESSING UNIT
given i<> programs stored in ROMs or in oilier devices
riic central processing unit or CPU controls the opera which keep theii stored Information when the power is
tion of (lie computer, li fetches binary coded instruc- turned nil
tions from
memory, decodes the instructions into a se
1 irs 0! simple actions, and carries out these actions. The Execution of a Three-Instruction Program
CPU contains an arithmetic logic unit, or ALU. which
can perform add, subtract. OR, AND. invert, or exclu EXECUTION SEQUENCE
sue t )R operations on binary words when instructed to
do so. The CPU also contains an address countei which To line you a better idea ol how the pai is 1ii .; i omputer
function together, we will now describe the aiiions .1
is used to hold the address ol the next instruction or
data to be fetched from memory, general-purpose regis- simple computer might go through to tarn- out [exe
ters whichare used for temporary storage of binary data, cute) .1 simple program. The three instructions of the
and circuitry which generates the control bus signals. program are:
6A 5A 4A 3A 2A 1A IB 2B 3B 4B 5B 1C 2C 3C 4C 5C 6C
i i i i i i
L CONTROL BUS J
3 on
m 3
£ CPU <
s <
<
r CONTROL
1
BUS
1 n
6D 2D 2E 6F
\ I
L
I/O
PORT 05
PROGRAM '
: 6
1. Input a value from port 05. •
SEQUENCE
26 CHAPTER TWO
AEMi >R> CONTENTS CONTENTS ( )PI RATION
\DDRESS (BINARY) illl X)
control bus (line 4B). The memory will then put the con- be written on the data bus. and sending out a mem-
tentsthe
of addressed byte (in this case the number ory write signal on the control bus.
07H) on the data bus (line 4C). The CPU will read in the
3. To read data from a port, the CPU sends the port
byte on the data bus and add it to the contents of
address out on the address bus and sends an I/O
the accumulator as instructed. Assume the result of the read signal on the control bus. Data from the port
addition is left in the accumulator. This completes the
comes into the CPU on the data bus.
second instruction.
The CPU must now fetch its next instruction. To do 4. To write data to a port, the CPU sends out the port
this it sends out the next sequential address (00104H) address on the address bus, sends the data to be
on the address bus (line 5A). sends out a memory read written to the port out on the data bus, and sends
signal on the control bus (line 5B). and reads in the ad- an I/O write signal out on the control bus.
dressed (E6H)
byte from the data bus (line 5C). From
5. A microcomputer fetches each program instruction
this byte the CPU determines that it is now supposed to
in sequence, decodes the instruction, and executes
do an output operation to a port. The CPU also deter-
mines that
it must go to memory again to get the ad- it.
dress
theof port that it is supposed to output to. To do
this it sends out the next sequential address (00105H)
Types of Computers
on the address bus (line 6A), sends out a memory read
signal on the control bus (line 6B). and reads in the byte
MAINFRAMES
(02HI put on the data bus by the memory (line 6C\. The
CPU now has all the information that it needs to execute Computers come in a wide variety of sizes and capabili-
the instruction. To output a data byte to a port, the CPU ties. The
largest and most powerful are often called
first sends out the address of the desired port on the mainframes. Mainframe computers may fill an entire
address bus (line 6D), Next it puts the data byte from the room. They are designed to work at very high speeds
accumulator onto the data bus (line 6E). The CPU then with large data words, typically 64 bits or greater, and
sends out an I/O write signal on the control bus (line they have massive amounts of memory. Computers of
6F1. This signal enables the addressed output port de- this type are used for military defense control, business
2H CHAPTER TWO
f±± ±±ri
%¡
i[ ii
i i i II
wn
MASS COMPUTER
DATA (MAINFRAME
STORAGE OR MINI)
VIDEO VIDEO
PRINTER LOW-COST
VIDEO TERMINAL TERMINAL
VIDEO PRINTER
ERMINAL TERMINAL
30 CHAPTFR TWO
MA If: I RAMI Hll ,11 Mi I :
STORAGE COMPUTER PRINTER
FLOPPY DISK
DRIVE
%¡ \Q_\Q
PRINTER MICROCOMPUTER
memory is often referred to as a data base. For a small means that a person can do many tasks locally on the
company a system such as this might be adequate. microcomputer without having to use the large com-
However, there are at least two potential problems. puter
all.at Since the microcomputers are connected to
The first potential problem is "What happens if the the large computer with a network, however, a user can
computer is not working?" The answer to this question access the computing power, memory, or other re-
is that everything grinds to a halt. In a situation where sources
the oflarge computer when needed.
people have become dependent upon the computer, not Distributing the processing around to multiple com-
much gets done until the computer is up and running puters
processors
or in a system has several advantages.
again. The old saying about putting all your eggs in one First, if the large computer goes down, the local micro-
basket comes to mind here. computers
continue
can working until they need to ac-
The second potential problem of the simple time cess the
large computer for something. Second, the bur-
share system is saturation. As the number of users in- den on
the large computer is reduced greatly, because
creases,
timetheit takes the computer to do each user's much of the computing is done by the local microcom-
task increases also. Eventually the computer's response puters. Finally,
the distributed processor approach al-
time to each user becomes unreasonably long. People lows system
the designer to use a local microcomputer
get very upset about the time they have to wait. best suited to the task it has to do.
A partial solution for the two potential problems of a Distributed processing seems to be the best way to go
simple time-share system is to use a distributed proces- about computerizing our electronics factory. Engineers
sor system.Figure 2-6 shows a block diagram for such a can each have a personal computer on their desk. With
system. The system has a powerful central computer this they can use available programs to design and test
with a large memory and a high-speed printer as does circuits. They can access the large computer if they need
the simple time-share system decribed previously. How- data from its memory. Through the telephone lines, the
ever,this
in system each user or group of users has a engineer with a personal computer can access data in
microcomputer instead of simply a video display termi- the memory of other computers all over the world. The
nal.other
In words, each user station is an independent drafting people can have personal computers for simple
functioning microcomputer with a CPU, ROM, RAM, work, or large computer-aided design systems for more
and probably magnetic or optical disk memory'. This complex work. Completed work can be stored in the
have personal computers tied into the network. They required 20 or more additional devices to form a func-
then can interact with any of the other systems on the tional CPU.
In 1974 Intel announced the 8080, which
network. Sales people can have portable personal com- had a much larger instruction set than the 8008 and
puters they
that can carry with them in the field. They only required two additional devices to form a functional
can communicate with the main computer over the tele- CPU. Also, the 8080 used NMOS transistors, so it oper-
phone lines
using a modem. Secretaries doing word pro- ated muchfaster than the 8008. The 8080 is referred to
cessing
usecanindividual word processing units or per- as a second-generation microprocessor.
sonal computers.Since word processing is not a high Soon after Intel produced the 8080. Motorola came
intensity use for a computer, several video display ter- out with the MC6800, another 8-bit general-purpose
minals
wordfor processing can be connected to a local CPU. The 6800 had the advantage that it required only a
microcomputer, and this local microcomputer can be + 5 V supply rather than the -5 V, +5 V, and +12 V
connected to the large computer through the network. supplies required by the 8080. For several years the
Users can also send messages to each other over the net- 8080 and the 6800 were the top-selling 8-bit microproc-
work. specifics
The of a computer system such as this essors. Some
of their competitors were the MOS Tech-
will obviously depend on the needs of the individual nology 6502
used as the CPU in the Apple II microcom-
company for which the system is designed. puter, and
the Zilog Z80 used as the CPU in the Radio
Shack TRS-80 microcomputer.
SUMMARY AND DIRECTION FROM HERE As designers found more and more applications for
microprocessors, they pressured microprocessor manu-
The main concepts that you should take with you from
facturers
develop
to devices with architectures and fea-
this section are multiprogramming, time-sharing or
tures optimizedfor doing certain types of tasks. In re-
multitasking, and distributed processing or multiproc-
sponse
the toexpressed needs, microprocessors have
essing.
youAswork your way through the rest of this
evolved in three major directions during the last 10
book, keep an overview of the computerized electronics
company in the back of your mind. The goal of this book years.
is to teach you how all the parts of a system such as this
DEDICATED CONTROLLERS
work, how the parts are connected together, and how
the system is programmed at different levels. One direction has been dedicated controllers. These
The first step toward this goal will be a quick look at devices are used to control "smart" machines such as
the different types of microprocessors available. We then microwave ovens, clothes washers, sewing machines,
discuss a specific microprocessor, the Intel 8086. and auto ignition systems, and metal lathes. Texas Instru-
the programming of a microcomputer built around a ments producedmillions of their TMS-1000 family of
member of this microprocessor family, the IBM PC. Next 4-bit microprocessors for this type of application. In
we discuss the hardware connections and timing of this 1976 Intel introduced the 8048. which contains an 8-bit
microcomputer. From there we show how the micro- CPU, RAM. ROM, and some I/O ports all in one 40-pin
computer
interfaced
is to a wide variety of peripheral package. Other manufacturers have followed with simi-
devices. And finally we cycle back to our computerized lar products. These devices are often referred to as
electronics company, the networks it uses, and the sys- microcontrollers. Some currently available devices in
tem programs it requires. this category, the Intel 8051 and the Motorola MC6801.
for example, contain programmable counters, a serial
port (UART) as well as a CPU, ROM, RAM, and parallel
I/O ports. A more recently introduced single-chip
Common Microprocessor Types
microcontroller, the Intel 8096, contains a 16-bit CPU.
MICROPROCESSOR EVOLUTION
ROM, RAM, a UART, ports, timers, and a 10-bit analog-
to-digital converter.
A common way of categorizing microprocessors is by the
number of bits that their ALU can work with at a time.
BIT-SLICE PROCESSORS
In other words, a microprocessor with a 4-bit ALU will
be referred to as a 4-bit microprocessor, regardless of A second direction of microprocessor evolution has been
the number of address lines or the number of data bus bit-slice processors. For some applications general-
lines that it has. The first microprocessor was the Intel purpose CPUs such as the 8080 and 6800 are not fast
4004 produced in 1971. It contained 2300 PMOS tran- enough or their instruction sets are not suitable. For
sistors. The 4004 was a 4-bit device intended to be used these applications several manufacturers produce de-
with some other devices in making a calculator. Some vices which
can be used to build a custom CPU. An ex-
logic designers, however, saw that this device could be ample
theis Advanced Micro Devices 2900 family of
used to replace PC boards full of combinational and se- devices. This family includes 4-bit ALUs, multiplexers.
32 CHAPTER TWO
sequencers, and other parts needed foi custom building M. I hi I hi i. ii v wind'. I in 8086 has ,1 16-bit data bus, so
.1 CPU. The term slice comes from the fact that these ii ran i en I (1. 11, i iii Mi, mi m hi i lata io memory and ports
pails can be connected in parallel u> work with 8 bil either 16 bits or 8 bits at a time The 8086 lias ., '.'n bil
winds. 16 Imi words, or 32 bit words. In other words, a aililnss bus, so il ran address any one ol
designei can add as many slices as needed for a particu- 1,048,576 memory locations. Each ol the 1,048,576
lar application. the designer not only custom designs i .nlilirsscs ol i he 8086 represents a byte wide
ilic hardware oi the CPU, but also custom makes the location. Words will be stored in two conse< utive mem
instruction sel for it using "microcode." orv locations. II (he lirst byte ol a word is ,il an even
address, the 8086 can read the entire wind in one opera
CENERAL-PURPOSI ( PUs lion. II I he I list byte of the word is at an odd address, the
8086 will read the first byte in one operation, and the
The third major direction ol microprocessor evolution
second byte 111 another operation, hater we will discuss
has been toward general-purpose CPUs which give a
this in del ail. the main point here is that if the In si byte
microcomputer most or all of the computing power ol
ol a 16-bit word is at an even address, the 8086 i an read
earlier minicomputers. After Motorola came out with
the word in one opei it Ion
the MC6N00. Intel produced the 8085. an upgrade of the
The Intel 8088 has the same arithmetic logic unit, the
8080 requiring only a +5 V supply. Motorola then pro-
same registers, and the same instruction set as the
ducedMC6809
the which has a few 16-bit instructions,
8086. The 8088 also has a 20-bit address bus so it can
but is still basically an 8-bit processor. In 1978 Intel
address any one of 1,048,576 bytes in memory. The
came out with the 8086 which is a full 16-bit processor.
8088, however, has an 8-bit data bus so it can only read
Some 16-bit microprocessors, such as the National
data from or write data to memory and ports 8 bits at a
PACE and the Texas Instruments 9900 family of devices,
time. The 8086, remember, can read or write either 8 or
were available previously, but the market apparently
16 bits at a time. To read a 16-bit word from two succes-
wasn't ready. Soon after Intel came out with the 8086.
sive memory locations, the 8088 will always have to do
Motorola came out with the 16-bit MC68000, and the
two read operations. Since the 8086 and the 8088 are
16-bit race was off and running. The 8086 and the
almost identical, any reference we make to the 8086 in
68000 work directly with 16-bit words instead of with
the rest of the book will also pertain to the 808K unless
8-bit words, they can address a million or more bytes of
we specifically indicate otherwise. This is done to make
memory instead of the 64 Kbytes addressable by the
reading easier. The Intel 8088. incidentally, is used as
8-bit processors, and they execute instructions much
the CPU in the IBM Personal Computer and several com-
faster than the 8-bit processors. Also these 16-bit proc-
patible personalcomputers.
essors have
single instructions for functions that re-
The Intel 80186 is an improved version of the 8086.
quired
lengthy
a sequence of instructions on the 8-bit
and the 80188 is an improved version of the 8088. In
processors. addition to a 16-bit CPU the 80186 and 80188 each have
The evolution along this last path has continued on to programmable peripheral devices integrated in the
32-bit processors that work with giga ( 109) bytes or tera
same package. In a later chapter we will discuss these
(1012) bytes of memory. Examples of these devices are
integrated peripherals. The instruction set of the 80186
the Intel 80386. the Motorola MC68020, and the Na-
and the 80188 is a superset of the instruction set of the
tional 32032. 8086. The term superset means that all of the 8086 and
Since we could not possibly describe in this book the
8088 instructions will execute properly on an 80186 or
operation and programming of even a few of the avail-
on an 80188. but the 80186 and the 80188 have a few
able processors,we confine our discussions to primarily
additional instructions. In other words, a program writ-
one group of related microprocessors. The family we
ten loran 8086 or for an 8088 is upward-compatible to
have chosen is the Intel 8086. 8088, 80186. 80188,
an 80186 or to an 80188, but a program written for an
80286 family. Members of this family are very widely
80186 or for an 80188 may not execute correctly on an
used in personal computers, business computer sys-
8086 or an 8088. In the instruction set descriptions in
tems, and
industrial control systems. Our experience
Chapter 6. we specifically indicate which instructions
has shown that learning the programming and opera-
only work with the 80186 or 80188. The 80186 is used
tionone
of family of microcomputers very thoroughly is
as the CPU in several personal computers.
much more useful than looking at many processors su-
The Intel 80286 is an advanced version of the 8086
perficially.
you learnIf one processor family well, you will
specifically designed for use as the CPU in a multiuser or
most likely find it quite easy to learn another when you
multitasking microcomputer. Programs written for an
have to.
8086 can be run on an 80286 operating in its real ad-
dress mode.
We discuss in Chapter 14 the operation
THE 8086, 8088, 80186, 80188, AND and use of the 80286. The 80286 is the CPU used in the
80286 MICROPROCESSORS- IBM PC/AT personal computer.
INTRODUCTION
The Intel 8086 is a 16-bit microprocessor intended to be 8086 INTERNAL ARCHITECTURE
used as the CPU in a microcomputer. The term "16-bit"
means that its arithmetic logic unit, internal registers. The three-instruction program section of this chapter
and most of its instructions are designed to work with describes how a CPU sends out addresses, sends out
control signals, reads in instructions and data to inter- which does not require use of the buses. When the EU is
nal registers, and sends out data to ports or memory. ready for its next instruction, it simply reads the in-
Before we can talk about how to write programs for the struction
thefrom
queue in the BIU. This is much faster
8086. we need to discuss its specific internal features than sending out an address to the system memory and
such as registers, instruction byte queue, and flags. waiting for memory to send back the next instruction
As shown by the block diagram in Figure 2-7, the 8086 byte or bytes. The process is analogous to the way a
CPU is divided into two independent functional parts, bricklayer's assistant fetches bricks ahead of time and
the bus interface unit or BIU. and the execution unit or keeps a queue of bricks lined up so that the bricklayer
EU. Dividing the work between these two units speeds can just reach out and grab a brick when necessary.
up processing. Except in the cases of |UMP and CALL instructions
where the queue must be dumped and then reloaded
starting from a new address, this prefetch-and-queue
The Bus Interface Unit scheme greatly speeds up processing. Fetching the next
The BIU sends out addresses, fetches instructions from instruction while the current instruction executes is
memory, reads data from ports and memory, and writes called pipelining.
data to ports and memory. In other words the BIU han-
dles all
transfers of data and addresses on the buses for SEGMENT REGISTERS
the execution unit. The following sections describe the The BIU contains four 16-bit segment registers. They
functional parts of the BIU. are: the code segment (CS) register, the stack segment
(SS) register, the extra segment (ES) register, and the
data segment IDS) register. These segment registers are
THE QUEUE used to hold the upper 16 bits of the starting addresses
To speed up program execution, the BIU fetches as many of four memory segments that the 8086 is working with
as six instruction bytes ahead of time from memory. The at a particular time. The 8086 BIU sends out 20-bit ad-
prefetched instruction bytes are held for the EU in a dresses,
it can
so address any of 220 or 1.048.576 bytes
first-in-first-out group of registers called a queue. The in memory. However, at any given time the 8086 only
BIU can be fetching instruction bytes while the EU is works with four. 65.536-byte (64 Kbyte) segments
decoding an instruction or executing an instruction within this 1.048,576-byte II Mbyte) range. Figure 2-8
a CHAPTER TWO
PHYSICAL A stack is .i scci urn n! memor) i el aside in sti
ADDRESS
dresses and data while .i subprogram executi
FFFFFH si.uk segment register is used for the upper 16 bits <>i
I II 1 M the starting address foi the program stack. We will dis-
cuss the use ,ind operation ol ,i si, irk in detail later.
1 he extra segment ir^isin and tin data
istei in- used in hold the uppei l<> bits ol the st.uim^
addresses nl two memory segments thai an- used lor
data.
EXTRA SEGMFNT BASE
ES - 7000H
INSTRUCTION POINTER
5FFFFH — TOPOF STACK i
The nexl lea tore lo look a i in i he I ill i is i he instruction
pointei III'] register. As discussed previously, the code
segment register holds the upper 16 bits ol the starting
address ol the segment from which the BIW is fetching
instruction code bytes. The instruction pointer registei
STACK SEGMENT BASE
holds the 16-bit address of Hie next code byte within
SS - 5000H
this code segment. The value con lamed in the IP is often
me i K i. hi i m 'in referred lo as an offset, because iliis value musl be offset
from (added to) the segment base address in CS to pro-
duce therequired 20-bit physical address. Figure 2-9a
shows in diagram form how this works. The CS register
points to the base or start of the current code segment
The IP contains the distance or offset from this base
CODE SEGMENT BASE
CS = 348AH address to the next instruction byte to be fetched. Fig-
ure 2-9bshows how the 16-bit offset in IP is added to the
TOPOF DATA SEGMENT
16-bit segment base address in CS to produce the 20-bit
physical address. Notice that the two 16-bit numbers
are not added directly in line. One way to describe this
process is to say that the contents of the CS register are
shifted left four bit positions before the contents of the
BOTTOM OF DATA SEGMENT IP are added to it. CS contains 348AH. When shifted left
by four bit positions this produces 348A0H as the start-
FIGURE 2-8 One way that four 64 Kbyte segments ing address of the code segment. The offset of 4214H in
might be positioned within 1 Mbyte address space of the IP is added to this base to give a 20-bit physical ad-
808b. dress 38AB4H.
of
ratedshown,
as or. lor small programs which do not TOPOF CODE SEGMENT
need all 64 Kbytes in each segment, they can overlap. A 1J89F II
1 .' 1 1
the lowest 4 bits. This constraint was put on the loca-
PH .Ml AL AHURFSS 3 8 A R 1
tionsegments
of so that it is only necessary to store and
manipulate 16-bit numbers when working with the
starting address of a segment. The part of a segment
starting address stored in a segment register is often FIGURE 2-1) Addition of IP to CS to produce physical
called the segment base. address of code byte, (a) Diagram, (b) Computation.
15 14 1! 12 11 10 9 ;-; 7 6 5 4 3 2 1 0
U U U OF DF IF TF SF ZF U AF U PF U CF
U= UNDEFINED
L CARRY FLAG - SET BY CARRY OUT OF MSB
PARITY FLAG - SET IF RESULT HAS EVEN PARITY
AUXILIARY CARRY FLAG FOR BCD
ZERO FLAG - SET IF RESULT = 0
SIGN FLAG = MSB OF RESULT
SINGLE STEP TRAP FLAG
INTERRUPT ENABLE FLAG
STRING DIRECTION FLAG
OVERFLOW FLAG
(6 CHAPTER TWO
and the DI1 DL pair is referred to .is the DX regtstei Foi rhe operation and use of the stack will be discussed in
16-bit operations, AX is called the accumulator. detail lain as need arises
lh, 8086 reglstei set is very similai to those ol the
earlier generation 8080 and 8085 microprocessors. Ii OTHER POINTER AND INDEX REGISTERS
was designed tins way so thai the many programs wrl I In addll Km in the stack p tei registei . SP, the EI i con
ten for the 8080 and 8085 could easily be translated to la ins a 16-bil base polntei (BP) register It also contains
run on the 8086 oi the 8088. The advantage ol using a 16-bil source index (SI) registei and a 16-bit destina-
Internal registers for the temporary storage "i data Is tion indexIDli register. These three registers can be
that, since the data is already In the EU, n can be ac used for temporary storage ol data jusl as the gi neral
cessed much more quickly than it could be accessed in purpose registers described above I lowevei . theii mam
external memory. use is to hold the 16 bit offset ol a data word in one ol
the segments. SI. foi example, can be used to hold the
STACK POINTER REGISTER
offset ol a daia word in the data segment The physical
A stack, remember, is a section ol memory set aside In address ol die data in memory will be generated in this
store addresses and data while a subprogram is execut- case by shilling the contents of the data segment regis-
ing. The
8086 allows you to set aside an entire 64 Kbyte ter fourlui positions to the lefl and adding the contents
segment as a stack. The upper 16 bits of the starting of SI to the result. A later section on addressing modes
address for this segment is kept in the stack segment will discuss and show mam- examples of the use ol these
register. The stack pointer (SP) register contains the base and index registers.
16-bit offset from the start of the segment to the mem-
ory location where a word was most recently stored on
the stack. The memory location where a word was most INTRODUCTION TO PROGRAMMING
recently stored is called the top of stack. Figure 2-1 la THE 8086
shows this in diagram form.
The physical address for a stack read or for a stack Programming Languages
write is produced by adding the contents of the stack Now that you have an overview of the 8086 CPU. it is time
pointer register to the segment base address in SS. To to start you thinking about how it is programmed. To
do this the contents of the stack segment register are run a program, a microcomputer must have the pro-
shifted four bit positions left and the contents of SP are gram stored
in binary form in successive memory loca-
added to the shifted result. Figure 2-1 lb shows an ex- tions. There
are three language levels that can be used
ample.5000H
The in SS is shifted left four bit positions to write a program for a microcomputer.
to give 50000H. When FFE0H in the SP is added to this.
the resultant physical address for the top of the stack MACHINE LANGUAGE
will be 5FFE0H. The physical address can be repre- You can write programs as simply a sequence ot the bi-
sented either
as a single number, 5FFE0H, or it can be nary codes
for the instructions you want the microcom-
represented in SS:SP form as 5000:FFE0H. puter
execute.
to The three-instruction program in Fig-
ure 2-2b
is an example. This binary form of the program
is referred to as machine language because it is the
PHYSICAL ADDRESSES form required by the machine. However, it is difficult, if
END OF STACK 5FFFFH not impossible, for a programmer to memorize the thou-
SEGMENT TOP OF STACK sands
binary
of instruction codes for a CPU such as the
5FFE0H 8086. Also, it is very easy for an error to occur when
working with long series of Is and 0's. Using hexadeci-
mal representation lor the binary codes might help
some, but there are still thousands of instruction codes
to cope with.
ASSEMBLY LANGUAGE
START OF STACK SEGMENT
To make programming easier many programmers write
50000 H
programs in assembly language. They then translate
the assembly language program to machine language so
it can be loaded into memory and run. Assembly lan-
guage uses
two-, three-, or four-letter mnemotiics to
h ii 0 ii ii
represent each instruction type. A mnemonic is just a
F t t 0 device to help you remember something. The letters in
PHYSICAL ADDRESS h F r E u an assembly language mnemonic are usually initials or
(TOP OF STACK) a shortened form of the English word(s) for the opera-
tion performedby the instruction. For example, the
mnemonic for subtract is SUB, the mnemonic for exclu-
FIGURE 2-11 Addition of SS and SP to produce physical sive OR
is XOR, and the mnemonic lor the instruction to
address ot top of stack, (a) Diagram, tbi Computation. copy data from one location to another is MOV.
ADD AL. 07H ADD CORRECTION FACTOR A later section on 8086 addressing modes will show all
NEXT:
of the ways in which the source of an operand and the
destination of the result can be specified. The point here
FIGURE 2-12 Assembly language program statement
is that the single mnemonic. ADD. together with a spec-
format.
ified source
and a specified destination can represent a
great many 8086 instructions in an easily understanda-
Assembly language statements are usually written in ble form.
a standard form having four fields. Figure 2-12 shows The question that may occur to you at this point is. "If
an assembly language statement with the four fields I write a program in assembly language, how do I get it
indicated. The first field in an assembly language state- translated into machine language which can be loaded
ment
theis label field. A label is a symbol or group of into the microcomputer and executed?" There are two
symbols used to represent an address which is not spe- answers to this question. The first method of doing the
cifically known
at the time the statement is written. translation is by working out the binary- code for each
Labels are usually followed by a colon. Labels are not instruction a bit at a time using the templates given in
required in a statement, they are just inserted where the manufacturer's data books. We will show you how to
they are needed. We will show later many uses of labels. do this in the next chapter. It is a tedious and error-
The opcode field of the instruction contains the mne- prone task. The second method of doing the translation
monicthe
for instruction to be performed. Instruction is with an assembler. An assembler is a program which
mnemonics are sometimes called operation codes or op- can be run on a personal computer or microcomputer
codes. ADD
The mnemonic in the example statement in development system. It reads the assembly language
Figure 2-12 indicates that we want the instruction to do instructions and generates the correct binary code for
an addition. Chapter 6 describes the function of each each. For developing all but the simplest assembly lan-
8086 instruction type and gives the opcodes for each. guage programs,an assembler and other program devel-
The operandfield of the statement contains the data, opment are
tools essential. We will introduce you to
the memory address, the port address, or the name of these program development tools in the next chapter
the register on which the instruction is to be performed. and describe their use throughout the rest of this book.
Operand is just another name for the data item(s) acted
on by an instruction. In the example instruction in Fig- HIGH LEVEL LANGUAGES
ure 2-12there are two operands. AL and 07H. specified Another way of writing a program for a microcomputer
in the operand field. AL represents the AL register, and is with a high level language such as BASIC, FOR-
07H represents the number 07H. This assembly lan- TFiAN. or Pascal. These languages use program state-
guage statementthen says add the number 07H to the ments which are even more English-like than those of
contents of the AL register. By Intel convention the re- assembly language. Each high level statement may rep-
sult the
of addition will be put in the register or the resent many
machine code instructions. An interpreter
memory location specified before the comma in the op- program or a compiler program is used to translate
erand field.
For the example statement in Figure 2-12 higher level language statements to machine codes
then, the result will be left in the AL register. As another which can be loaded into memory and executed. Pro-
example, the assembly language statement. ADD BH. gramsusually
can be written faster in high level lan-
AL, when converted to machine language and run. will guages than
in assembly language because the high
add the contents of the AL register to the contents of the level language works with bigger building blocks. How-
BH register. The results will be left in the BH register. ever, programs written in a high level language and in-
Looking back at the example assembly language state- terpreted
compiled
or execute slower than the same pro-
ment Figure
in 2-12. observe the comment field which grams written in assembly language. Programs that
starts with a semicolon. This field is very important. involve a lot of hardware control, such as robots and
Comments do not become part of the machine language factory control systems, or programs that must run as
program. You write comments in a program to remind quickly as possible are usually best written in assembly
you of the function that this instruction or group of in- language. Programs that manipulate massive amounts
structions performs
in the program. of data, such as insurance company records, are usually
To summarize why we use assembly language, let's best written in a high level language. The decision of
look a little more closely at the assembly language ADD which language to use has recently been made more dif-
statement. The general format of the 8086 ADD instruc- ficult
theby fact that current assemblers allow the use of
tion is: manv high level language features, and the fact that
some current high level languages provide assembly lan-
ADD destination, source
guage features.
W CHAP 11
ware Interfacing. Before we start teaching you assembly her that the destination location is spe< Ified In the In
language programming in the nexl chapter, however, struction before the source Also note that the contents
we wanl to give you an introduction to how the 8086 ol AX are |iist i opied In CX, not actually moved. In oilier
.in esses data. words, the previous contents ol < x are written over, hut
the contents of AX are not changed. Foi example, If CX
contains 2A84H and AX contains 4971H before the
How the HOW) Accesses Immediate and Register MOV CX, AX instruction executes, then alter the in-
Data struction executes
CX will contain 4971H and AX will
hi a previous discussion ol the 8086 Bin we described si ill coi i lam 197 1H. You can MOV any 16-bil registei to
how the 8086 ai cesses code bytes using CS and IP. We any l «> bit register, or sou can MOV any 8 hn register to
also described how the 8086 accesses the stack using any 8 hit register. However, you cannot use an instruc-
ss ami SP. Before we can teach you assembly language tion suchas \H)V ( X, AI because this is an attempt to
copy a byte-type operand (Al.l into a word-type destina
programming techniques, we need to discuss some ol
the different ways that an 8086 can access tin- data that
tion (CX). The' byte in AI. would fit in CX. hut the 8086
When executed, this instruction copies a word or a byte OVERVIEW OF MEMORY ADDRESSING MODES
from the specified source location to the specified desti-
The addressing modes described in the following sec-
nation location.
The source can be a number written
tions used
are to specify the location of an operand in
direi tlv in the instruction, a specified register, or a
memory location specified in one of 24 different ways.
memory. A previous section described how the 8086
produces the physical address for instruction codes by
The destination can be a specified register or a memory
location specified in any one of 24 different ways. The
adding an offset in the instruction pointer to the code
source and the destination cannot both be memory loca-
segment base in the CS register. Remember that the
contents of CS are shifted four bit positions left before
tionsanin instruction.
the contents of IP are added. Another previous section
IMMEDIATE ADDRESSING MODE
described how the 8086 accesses stack locations by add-
ing anoffset in the stack pointer register to the stack
Suppose that in a program you need to put the number segment base in the stack segment register. Here again
437BH in the CX register. The MOV CX, 437BH instruc- the contents of the stack segment register are shifted
tion can
be used to do this. WLien it executes, this in- four bit positions left before the contents of the stack
struction
putwall
the immediate hexadecimal number pointer are added.
437BH in the 16-bit CX register. This is referred to as To access data in memory the 8086 must also produce
immediate addressing mode because the number to be a 20-bit physical address. It does this by adding a 16-bit
loaded into the CX register will be put in two memory value called the effective address to one of the four seg-
locations immediately following the code for the MOV ment bases.
The effective address (EA) represents the
instruction. This is similar to the way the port address displacement or offset of the desired operand from the
was put in memory immediately after the code for the segment base. In most cases, any of the segment bases
input instruction in the three-instruction program in can be specified, but the data segment is the one most
Figure 2-2b. often used. Figure 2- 13a shows in graphic form how the
A similar instruction, MOV CL, 48H could be used to
EA is added to the data segment base to point to an
load the 8-bit immediate number 48H into the 8-bit CL operand in memory. Figure 2- 13b shows how the 20-bit
register. You can also write instructions to load an 8-bit
physical address is generated by the BIU. The starting
immediate number into an 8-bit memory location or to
address for the data segment in Figure 2-135 is 20000H
load a 16-bit number into two consecutive memory loca-
so the data segment register will contain 2000H. The
tions, but
we are not yet ready to show you how to spec- BIU shifts the 2000H four bit positions left and adds the
ify these. effective address. 437AH. to the result. The 20-bit physi-
cal addresssent out to memory by the BIU will then be
REGISTER ADDRESSING MODE 2437AH. The physical address can be represented either
Register addressing mode means that a register is the as a single number. 2437AH, or in the segment base:
source of an operand for an instruction. The instruction offset form as 2000:437AH.
MOV CX, AX. for example, copies the contents of the The execution unit calculates the effective address for
16-bit AX register into the 16-bit CX register. Remem- an operand using information you specify in the in-
40 I MAIM IK TWO
wiih the segment base addresses assigned u> the set ond Addi ess. data, and I ontrol buses
user's program. In othei words, segmentation makes II
i ontrol bus signals
easy to keep users' programs and data separate from
each other, and segmentation makes ii easy to switch ALU
from one user's program to anothei user's program
Segmentation
Mill
IMPORTANT TERMS AND CONCEPTS Instruction byte queue, pipelining
EROM THIS CHAPTER ES, CS, SS. DS registers, IP register
EU
II yon do noi remember any ol the terms oi concepts in
AX, MX, CX, D.\ registers, flag register
the following list, use the index to find them In the
ALU. SI'. MP. SI. 1)1 registers
chaptei
Machine language
Microcomputer, mici oprocessoi
Assembly language
Hardware, software, firmware Mnemonic, opcode, operand, label, comment,
Time-share Assembler
Describe the main advantages of a distributed pro- 8. What physical address is represented by:
cessing computer
system over a simple time-share a. 4370:561EH
system. b. 7A32:0028H
42 CHAPTERTWO
CHAPTER
43
2. Add 7 to value read in. FLOWCHARTS
3. Store eorrected value in memory location. If you have done any previous programming in BASIC or
in FORTRAN, you are probably familiar with flowcharts.
4. Wait one hour.
Flowcharts use graphic shapes to represent different
5. Read next sample from temperature sensor. types of program operations. The specific operation de-
sired
written
is in the graphic symbol. Figure 3-1 shows
6. Add 7 to value read in.
some of the common flowchart symbols. Plastic tem-
7. Store eorrected value in next memory location. platesavailable
are to help you draw these symbols if vou
decide to use them for your programs.
Figure 3-2 shows a flowchart for a program to read in
24 data samples from a temperature sensor at 1-hour
intervals, add 7 to each, and store each result in a mem-
ory location. A racetrack-shaped symbol labeled START
97. Read last data sample from temperature sensor. is used to indicate the beginning of the program. A par-
allelogram
used to is represent input or output opera-
98. Add 7 to value read in. tions.
theIn example we use it to indicate reading data
99. Store corrected value in next memory location.
from the temperature sensor. A rectangular box symbol
is used to represent simple operations other than input
and output operations. The box containing "add 7" in
As you can see. this direct form is not a very compact or
Figure 3-2 is an example.
efficient way of representing the operation of the pro-
A rectangular box with double lines at each end is
gram.
more
A efficient way of writing the sequential task
often used to represent a subroutine or procedure that
list for this program is:
will be written separately from the main program. When
a set of operations must be done several times through-
Read a data sample from temperature sensor.
outprogram,
a it is usually more efficient to write the
Add 7 to the value read in. series of operations once as a separate subprogram and
then just use or "call" this subprogram as it is needed.
Store corrected value in memory location. For example, suppose that there are several times in a
Wait one hour. program where you need to compute the square root of a
number. Instead of writing the series of instructions for
24 samples yet? computing a square root each time you need it in the
No, read next sample and process.
program, you can write the instruction sequence once
Yes. done.
as a subprogram and set it aside in some location in
memory. Vou can then call this subprogram each time
The last three lines indicate that we want the program you need to compute a square root. In the flowchart in
to do the read. add. store, and wait operations 24 times. Figure 3-2 we use the double-ended box to indicate that
Carefully written sequential task lists are often quite the "wait 1 hour" operation will be programmed as a
close to the assembly language statements that will im- subroutine. Incidentally, the terms subprogram, sub-
plement them,
so you may find them useful. As you de- routine,procedure
and all have the same meaning.
termine hardware
details, such as port addresses for the Chapter 5 shows how procedures are written and used.
system on which the program is to run. you can add this A diamond-shaped box is used in flowcharts to repre-
information to the appropriate task statement. The next sentdecision
a point or crossroad. Usually it indicates
section show-, you a more graphic way of representing that some condition is to be checked at this point in the
ilu algorithm of a program or program segment. program and. if the condition is found to be true, one set
44 ( HAPTER lllkl t
f STAR! J however, have several disadvantages First, yo
write much Informal in the little boxes Second,
flowcharts do nol present information In a verj <ompai i
form. For mine complex problems, flowcharts lend to
become spread out over many pages I hej are verj hard
in follow back and forth between pages Ftiird and most
important, with flowcharts the overall structure ol the
program tends to get losl in the details. The following
section describes a more clearly structured and compact
method ol representing the algorithm of a program or
pi ogi .mi segment.
make programs clearer. If you have previously written Eat supper out.
these structures are probably already familiar to you. Take a taxi home.
46 i M 'NIK THRFt
-r
1 %:
u /
>% -'
-
,' 5 * :% m
I „ ^ (- O O
< tn O * 5-
:. LI.
<
2 -
5>o
- <
z <
LU (J
g 2
5 —- 1
< < --' ~
-
1
Q L_
—
T
TO
—
§
C Z
•a 0
X
s
h-
-,-
/
:
—
OS C
_ i
"5
u
— E
8086 FAMILYASSEMBLY
LANGUAGEPROGRAMMING- INTRODUCTION 47
Another form of the repetition operation that you more complex programming problems, a sequential list
might see in high level language programs is the FOR- may become very messy because it has little real struc-
DO loop.
This structure has the form: ture standardization.
or Another way of representing
program operations is with flowcharts. Flowcharts are a
FOR count = 1 TO n DO very graphic representation, and they are useful for
statement short program segments, especially those that deal di-
statement rectly with
hardware. However, flowcharts use a great
deal of space. Consequently, the flowchart for even a
moderately complex program may take up several pages.
In assembly language we usually implement this type of
It often becomes difficult to follow program flow back
operation with a REPEAT-UNTIL structure, so we have
and forth between pages. Also, since there are no
not included a sample of it.
agreed-upon structures, a poor programmer can write a
The CASE structure shown in Figure 3-3/ is a com-
flowchart which jumps all over the place and is even
pact way
of representing a choice among several alterna-
more difficult to follow. The term "logical spaghetti"
tive actions.The choice is determined by testing some
comes to mind here.
quantity. The example in Figure 3-3/ best shows how
A third way of representing the operations you want
this is used. This everyday example describes the de-
in a program is with a top-down design approach and
sired actions
for a cook in a restaurant. The pseudocode
standard program structures. The overall program
is just a summary of the thinking the cook might go
problem is first broken down into major functional
through. The cook or the computer checks the value of
modules. Each of these modules is broken down into
the variable called "day" and selects the appropriate ac-
smaller and smaller modules until the steps in each
tionsthat
for day. Each of the indicated actions, such as
module are obvious. The algorithms for the whole pro-
"Make celery soup." is itself a sequence of actions which
gram andfor each module are each expressed with a
could be represented by the structures we have de-
standard structure. Only three basic structures.
scribed.
SEQUENCE, IF-THEN-ELSE. and WHILE-DO. are
The CASE structure is really just a compact way to
needed to represent any needed program action or series
represent a complex IF-THEN-ELSE structure. To il-
of actions. However, other useful structures such as IF-
lustrateFigure
this, 3-3g also shows how the soup cook
THEN, REPEAT-UNTIL. FOR-DO. and CASE can be
example can be represented as a series of IF-THEN-
derived from these basic three. A structure can contain
ELSE structures. Note that, in this example, the last
another structure of the same type or one of the other
IF-THEN has no ELSE after it because all of the possible
types. Each structure has only one entry point and one
days have been checked. You can, if you want, add the
exit point. These programing structures may seem re-
final ELSE to the IF-THEN-ELSE chain to send an
strictive,
using
but them usually results in program rep-
error message if the data does not match any of the
resentationsarewhich
easy to understand and for
choices. The CASE structure does contain the final
which it is easy to write the programs. A program writ-
ELSE, however. The CASE form is more compact for
ten ina structured manner is easier to debug and
documentation purposes and some high-level languages
much more understandable to someone else who has to
such as Pascal allow you to implement it directly. How-
work on it. Furthermore, a program representation de-
ever, the
IF-THEN-ELSE structure gives you a much
velopedstructured
with programming techniques can
better idea of how you write an assembly language pro-
be implemented easily in assembly language or in a
gram sectionto choose between several alternative ac-
high-level language such as Modula II or C.
tions.
Throughout the rest of this book we show you how to
use these structures to represent program actions and Finding the Right Instruction
how to implement these structures in assembly lan-
After you get the structure of a program worked out and
guage.
written down, the next step is to determine the instruc-
tion statements required to do each part of the program.
SUMMARY OF PROGRAM STRUCTURE
Since the examples in this book are based on the 8086
REPRESENTATION FORMS
family of microprocessors, now is a good time to give you
Writing a successful program does not consist of just an overview of the instructions the 8086 has for you to
writing down a series of instructions. You must first use.
think carefully about what you want the program to do You do not usually learn a new language by studying
and how you want the program to do it. Then you must its dictionary from cover to cover. It is more productive
represent the structure of the program in some way that to first learn a few very useful words and learn how to
is very clear to you and to anyone else who might have to put together simple sentences. You can then learn more
work on the program. If the structure is well developed, words as you need them to express more complex
it is usually not a difficult step to write the actual pro- thoughts. Chapter 6 contains a dictionary of all of the
gramming language
statements that implement it. 8086 instructions with detailed descriptions and exam-
One way of representing program operations is with a ples for
each. You can use this as a reference as you
sequential lask list. For initial thinking and simple pro- write programs. Here we simply list the 8086 instruc-
gramming problems
this technique works well. For tions
Junctional
in groups with single-sentence descrip-
48 CHAPTER IHKft
lions so thai you can see the types ol Instructions thai SAH1 Store (copy) All registei to low byte ol flag
are available to you. As you read through this set tlon, do registei .
not expect to understand all ol the Instructions. When
PUSHF Copy 11«iLi,register to top ol stack.
you si. u i writing programs, you will probably use ihis
section to determine the type of Instruction and Chapter POPF Copy word at top ol stack to flag registi i
6 to get the Instruction details .is you need them. Aftei
you have written a few programs, you will remember ARITHMI IK INSIRIK IIONS
most oi the basic Instruction types and will be able to
Addition instructions:
|ust look up an instruction in Chapter 6 to get any addl
tional del. ills you need. Chaptei I shows you in detail
ADD Add specified byte to byte, oi specified word
how to use the move, arithmetic, logical, jump, and to word.
string instructions. Chapter 5 shows how to use the call
Instructions and the stack. ADC Add byte * byte • carry flag oi word
As von skmi through the following overview ol the word • (airy flag.
8086 instructions, see II you can find the instructions
INC Increment specified byte or specified word by
needed to do the "read temperature sensor value from a
one.
port, add I 7. and store result in memory" example pro-
gram. AAA ASCII adjust alter addition
LAHF Load (copy to) AH with the low byte of the CWD Fill upper word of double word with sign bit
of lower word.
flag register.
50 CHAPTER THRU
JBE JNA Jump il below or equal Jump il nol INTO Interrupt program execution it overflow
above. flag I.
,ic Jump il carry flag \CV) 1. [RET Return from interrupt service procedure
to main program
JE JZ Jump il equal Jump il zero flag (ZF) I
JG JNLE Jump il greater Jump it nol less than //n//i level language interface instructions
noi equal
ENTER (80186 80188 ( INLY) Enter procedure.
JGE JNL Jump il greater than or equal Jump il
noi less than. I 1. \\i (80186/80188 ONLY) Leave procedure.
JNE.JNZ Jump il not equal/Jump il nol zero (zero CLC ( le.11 carry flag (CF) to 0.
flag = 0).
CMC Complement the state ol the earn' flag [< I
JNO Jump if no overflow (Jump if overflow
STD Set direct ion flag (DPI to 1 (decrement
flag = 0).
string pointers).
JNP/JPO Jump if not parity/Jump it parity odd
CLD Clear direction flati (DF) to 0.
(PF = 0).
STI Set interrupt enable flag to 1 (enable 1NTR
JNS Jump if not sign (Jump it sign flag = 0).
input I.
JO Jump if overflow flag = 1.
CL1 Clear interrupt enable flag to 0 (disable
JP/JPE Jump if parity/Jump if parity even (PF - INTR input).
1).
External hardware synchronization instructions:
JS Jump if sign flag = 1.
Iteration control instructions: HLT Halt (do nothing! until interrupt or reset.
the one that best fits the specific application. 8087 or 8089.
Data segment register result. This location must be in available RAM so that
we can write to it. Address 00100H is an available RAM
Stack segment register location on an SDK-86 prototyping board, for example.
Extra segment register Next, we decide where in memory we want to start put-
ting the
code bytes for the instructions of the program.
Stack pointer register Again, on an SDK-86 prototyping board, address
00200H and above is available RAM. so we chose to start
Base pointer register
the program at address 00200H.
Source index register The first operation we want to do in the program is to
initialize the data segment register. As discussed previ-
Destination index register
ously, MOV
two instructions are used to do this. The
8255 programmable ports MOV AX, 0010H instruction, when executed, will load
52 ( HAIMIR IHRU
PROGRAMMER 2> Mill I
DATA
ADDRESS 1 ABI 1 S MNEM. OPERAND(S) COMMENTS
or
CODE
00/03 Z connected
luf Ike pnoanam.
00/04 XX meanA- dan t cane amid
00/05 contenti ol location.
00/06
00/07
00/08
00/09
00/OA
00/08
00)00.
00/OD
Oi sS MOV 2>£, AX
04 2>8
OS .:'• M AI, 05Jl Readtempenatune
[nom
Ob 05 pont 05Jl
07 04 A2>2> AZ 07Jl Add connection
[acton
(98 07 o[ +07
09 A2 MOV [oooo], Alt Stale leinlt in leie-Mied
OB 00
Of
BYTE 3 BYTE 4
1 | 0 | 0 | 0 | 1| 0 1 1 LOWDISPLACEMENT
j HIGHDISPLACEMENT
OPCODE D w MOD REG R/M
I
OPERATION CODE
FIGURE 3-6 Coding template for 8086 instructions which MOV data between
registers or between a register and a memory location.
BH 1 1 1
MOV Instruction Coding Examples
All of the examples in this section use the MOV instruc-
..„„,- r-.,^ „„ tion template in Figure 3-6. As you read through these
SbbREG CODE , .. , „ , . . . , t , c .. ." . ,.t
examples, it is a good idea to keep track of the bit-by-bit
development on a separate paper for practice.
ES 00
56 ( HAPTER IMKII
R M 00 01 in 11
W i) U 1
a 1. and put 100 in the REG field to represent SP. The the table. Read the required MOD-bit pattern from the
MOD field will be 11 to represent register addressing top of the column. In this case. MOD is 00. Then read
mode. Make the R/M field 01 1 to represent the other reg- the required R/M-bit pattern at the left of the box. For
ister, BX.
The resultant code for the instruction MOV this instruction you should find R/M to be 111. Assem-
SP,BX will be 10001011 11100011. Figure 3-9a shows blingofallthese bits together should give you 10001010
the meaning of all of these bits. 00001 1 1 1 as the binary code for the instruction MOV
If you change the D bit to a 0 and swap the codes in CL, [BX], Figure 3-9c summarizes the meaning of all the
the REG and R/M fields, you will get 10001001 bits in this result.
11011 100. which is another equally valid code for the
instruction. Figure 3-9b shows the meaning of the bits MOV 43H[SI], DH
in this form. This second form, incidentally, is the form
This instruction will copy a byte from the DH register to
that the Intel 8086 Macroassembler produces.
a memory location. The effective address of the memory
location will be computed by adding the indicated dis-
CODING MOV CL, [BX]
placement
43H toof the contents of the SI register. The
This instruction will copy a byte to CL from the memory actual physical address will be produced by shifting the
location whose effective address is contained in BX. The contents of the data segment base in DS 4 bits left and
effective address will be added to the data segment base adding this effective address to the result.
in DS to produce the physical address. The 6-bit opcode for this instruction is again 100010.
To find the 6-bit opcode for byte one of the instruc- Make the D bit a 0 because you are moving/rom a regis-
tion, consult the table in Appendix A. You should find ter. Makethe W bit a 0 because you are moving a byte.
that this code is 100010. Make the D bit a 1 because Put 110 in the REG field to represent the DH register.
data is being moved to register CL. Make the W bit a 0 The R/M field will be 100 because SI contains part of the
because the instruction is moving a byte into CL. Next effective address. Make the MOD field 01 because the
you need to put the 3-bit code which represents register displacement contained in the instruction. 43H, will fit
CL in the REG field of the second byte of the instruction in one bvte. If the specified displacement had been a
code. The codes for each register are shown in Figure number larger than FFH, then MOD would have been
3-7. In this figure you should find that the code for CL is 10. Putting all these pieces together gives 10001000
001. Now, all you need to determine is the bit patterns 01 1 10100 for the first two bytes of the instruction code.
for the MOD and R/M fields. Again use the table in Fig- The specified displacement, 43H or 0100001 1 binary is
ure 3-8to do this. To use the table, first find the box put after these two as a third instruction byte. Figure
containing the desired addressing mode. The box con- 3-9d shows this. If an instruction specifies a 16-bit dis-
taining |BX].
for example, is in the lower left corner of placement,
the then
low byte of the displacement is put
'TO' REG -
L EG SP 'FROM' REG - REG BX
BYTE 1 BYTE 2 1
|,|o 0 | 0 | 1| 0 ii 0 0 | 1 '!'!"! 1" 1
MOV CL, (BX] CODE
OPCOC R/M = [SI]
1 000101 000001 1 1
ROM' REG — I ;
'FROM' REG = DH
I IP I HI)!
VIOV RYTF
MOV BYTE 1 MEMORY, ONE BYTE
DISPLACEMENT
'TO' REG
1 BYTE 3
MOV BYTE MEMORY,
NO DISPLACEMENT n^ |o|o|o|o|i|i|
DISPLACEMENT = 43H
BYTE I BYTE 2
SEGMENT OVERRIDE PREFIX
H" li il 1 0| 1 1 0 0 0 | 0 | 1| 1| %
l"l I BYTE 1 I
001011
OPCODE
DIRECT
'TO' REG ' ADDRESSING
MOV WORD
BYTE 3
10001000000101 MOV CS [BX], DL
0 j 1| I | I | 1 | 0 | 1| II| 0 | I | " I '"'I n | II| I | I | MOV
CX,[437AH]
OP CODE
'
DIRECT ADDRESS DIRECT ADDRESS
'FROM' REG REG = DL
LOW BYTE HIGH BYTE
7AH 43H MOV BYTE MEMORY, NO DISPLACEMENT
(el
FIGURE 3-9 MOV instruction coding examples, la) MOV SP, BX. lb) MOV SP,
BX alternative. Id MOV CL, [BX]. Id) MOV 4?H [SI], DH. (e) MOV CX,
[437AH], If) MOV CS:[BX], DL.
in as byte three of the instruction code, and the high which is the mode specified by this instruction. For di-
byte of the displacement is put in as byte four of the rect addressing you should find MOD to be 00 and R/M
instruction code. to be 110. The first two code bytes for the instruction
then are 1000101 1 00001 1 10. These two bytes will be
CODING MOV CX, [437AH] followed by the low byte of the direct address, 7AH
(01 1 1 1010 binary) The high byte of the direct address
Tins instruction copies the contents of two memory lo-
43H (01000011 binary) will be put after that. The in-
cationsCX.
into The direct address or displacement of
struction
be willcoded into four successive memory
the first memory location from the start of the data seg-
addresses as 8BH, OEH. 7AH, and 43H. Figure 3-9e
ment
437AH.
is The physical memory address will be
spells this out in detail.
produced by shifting the contents of the data segment
register, DS, 4 bit positions left and adding this direct
CODING MOV CS:[BX], DL
address to the result.
The 6-bit opcode for this instruction is again lOOOlO. This instruction copies a byte from the DL register to a
Make the D bit a 1 and the W bit a 1 because you are memory location. The effective address for the memory
moving a word to CX. Put OOl in the REG field to repre- location is contained in the BX register. Normally an ef-
sent the
CX register, and then consult Figure 3-8 to find fective address
in BX will be added to the data segment
the MOD and R/M codes. In the first column of the fig- base in DS to produce the physical memory address. In
ure youshould find a b<>\ labeled "direct addressing." this instruction, CS: indicates that we want the BIU to
58 CHAPTER THREE
.idd the effective address to the code segmenl basi In < S \ni ) \i , on i
in produce the physical address ("he CS Is called a seg This instruction adds the immediate number 07H to the
ment ovei ride pn /i * AI. register and puts the resull in the Al. registi
When .in Instruction containing a segmenl override
simplest template to use foi coding this instruction is
prefix Is coded, an 8 bil code foi the segmenl override
found in the table in Appendix A undei the heading
prefix Is put in memorj before the code for the resl ol the
ADD Immediate to accumulator." the format is
Instruction. The code byte foi the segmenl ovei ride pre
10W, data byte, data byte. Since we are adding a
fix has the formal 001XX1 10. You Inserl a 2 bil code in
byte, the W hit should he a (I. the immediate data byte
pl.uc ol the X"s to Indicate which segment base you wanl
wi are adding will be put in the second code byte The
the effective address to be added to. I lie i tides for these
third code byte will not be needed because we are only
2 bus are .is follows 00 ES. 01 CS, 10 SS, and
adding a byte, the code then will be 00000100
I l DS. The segmenl override prefix byte foi CS then
00000111.
is 00101 l 10. For practice, code out the resl ol ihis in-
struction. Figure
3 9/ shows the result you should get
and how the code for the segment override prefix is put MOV [00001, AL
before the other code bytes for the Instruction.
This instruction copies the contents of the AL register to
a memory location. The direct address or displacement
ol the memory location fr the start of the data seg
ment is 0000H. The code template for this instruction is
Coding the Example Program
found in the table in Appendix A under the heading
Again, as you read through tins section follow the bit- "MOV — Accumulatoi to memory." The format ior the
by-bit development of the instruction codes on a sepa- instruction is 1010001W, address low byte, address
rate paper
for practice high bvte. Since the instruction moves a byte, the W bit
should be a 0. The low byte of the direct address is writ
MOV AX, 00 I OH ten in as the second instruction code byte, and the high
byte of the direct address is written in as the third in-
This instruction will move the immediate word 0010H
structionbyte.
code The codes for these 3 bytes then will
into the AX accumulator. The simplest code template to
be 10100010 00000000 00000000.
use for this instruction is listed in the table in Appendix
A under the MOV "immediate to register" heading. The
format for it is 1011 VV REG. data byte low. data byte INT 3
high. Make the W bit a 1 because you want to move a
word. Consult Figure 3-7 to find the code for the AX reg-
In most 8086 systems this instruction causes the 8086
to stop executing instructions and do nothing but wait
ister. You
should find this to be 000. Put this 3-bit code
for the user to tell it what to do next. According to the
in the REG field of the instruction code. The completed
format table in the appendix, the code for this instruc-
instruction code byte is 101 1 1000. Put the low byte of
the immediate number, 10H. in as the second code byte.
tionthe
is single byte 1 1001 100 or CCH.
Then put the high byte of the immediate data. 00H. in
as the third code byte. SUMMARY OF HAND CODING THE EXAMPLE
PROGRAM
MOV DS, AX Figure 3-4 shows the example program with all the in-
struction codes
in sequential order as you would write
This instruction copies the contents of the AX register
them so that you could load the program into memory
into the data segment register. The template to use for
and run it. Codes are in HEX to save space.
coding this instruction is found in the table in Appendix
A under the heading "Register/memory to segment reg-
ister." format
The for this template is 10001 1 10 MOD 0
segreg R/M . Segreg represents the 2-bit code for the de- A Look at Another Coding Template Format
sired segmentregister. These codes are also found in As we mentioned previously. Intel literature shows the
the table at the end of Appendix A. The segreg code for 8086 instruction coding templates in two different
the DS register is 1 1 . Since the other operand is a regis- forms. The preceeding sections have shown you how to
ter. MODshould be 11. Put the 3-bit code for the AX use the templates found in the 8086 data sheet in Ap-
register. 000, in the R/M field. The resultant codes for pendix
NowA. let's take a brief look at the second form
the two code bytes should then be 10001 110 1 101 1000. shown along with the instruction clock cycles in Appen-
dix B.
The only difference between the second form for the
IN AL, 05H templates and the form we discussed previously is that
This instruction copies a byte of data from port 05H to the D and W bits are not individually identified. Instead,
the AL register. The coding for this instruction was de- the complete opcode bytes are shown for each version of
scribed
a previous
in section. The code for the instruc- an instruction. For example, in Appendix B the opcode
tion11100100
Is 00000101. byte for the MOV memory, register 8 instruction is
dentallyoutleavean instruction in your program, you reference a large number of the available assembler di-
can replace the NOPs with the needed instruction. This rectives. we
Herewill discuss the basic assembler direc-
way you don't have to rewrite the entire program after tives you
need to get started writing programs. We will
the missing instruction. introduce more of these directives as we need them in
After you have written down the instruction state- the next two chapters.
ments, recheck
very carefully to make sure you have the
right instructions to implement your algorithm. Then,
work out the binary codes for each instruction and write SEGMENT and ENDS Directives
them in the appropriate places on the coding form.
The SEGMENT and ENDS directives are used to identify
Hand coding is laborious for long programs. When
a group of data items or a group of instructions that you
writing long programs, it is much more efficient to use
want to be put together in a particular segment. These
an assembler. The next section of this chapter shows
directives are used in the same way that parentheses are
you how to write your programs so you can use an as-
used to group like terms in algebra. A group of data
sembler
produce
to the machine codes for the instruc-
tions. statements or a group of instruction statements con-
tained between
SEGMENT and ENDS directives is called
a logical segment. When you set up a logical segment.
you give it a name of your choosing. In the example
WRITING PROGRAMS FOR USE WITH program the statements DATA HERE SEGMENT and
AN ASSEMBLER DATA HERE ENDS set up a logical segment named
DATA HERE. There is nothing sacred about the name
If you have an 8086 assembler available, you should DATA HERE. We simply chose this name to help us re-
learn to use it as soon as possible. Besides doing the memberthis
that logical segment contains data state-
tedious task of producing the binary codes for vour in- ments. The
statements CODE HERE SEGMENT and
struction statements,an assembler also allows you to CODE HERE ENDS in the example program set up a logi-
refer to data items by name rather than by numerical cal segment named CODE HERE which contains in-
addresses. As you should soon see. this greatlv reduces struction statements.
The Intel and the IBM 8086 macro
the work you have to do and makes your programs assemblers, incidentally, allow you to use names and
much more readable. In this section we show you how to labels of up to 31 characters. You can't use spaces in a
write your programs so that you can use an assembler name, but you can use an underscore as shown to sepa-
on them. The assemblers used for the programs in this rate wordsin a name. Also, you can't use instruction
book were the Intel 8086 8088 80186 80188 M mnemonics as segment names or labels. Throughout
sembler and the Microsoft Macro Assembler for the IBM the rest of the program you will refer to a logical segment
Personal Computer. If you are using another assembler. bv the name that vou give it when vou define it.
60 CHAPTER THRFE
PAGI ,132 M ,i e listing file line! ' : ers w i do
18036 pi %¡g i .mi
; ABSTRACT I Ii I • I 'I i ii M 'Hi ffll I I. t 1 I %] 1 i " till' I W( ' 16 I'll t'i it il I
the memi <\ . Location: called Ml II (ill l CAI ID
MU1 TIPLIER. I In • resul 1 i • stored Ln thi n
i di at ion cal led PRODUCT
;P0R1 B USED : None
;PROCI DI IRI S IS! D: None
;ri listers u 3ED : CS , Di ;., DX and AX
DATA_HERE SEGMf N 1
FIGURE 3-10 Assembly language source program to multiply two 16-bit binary
numbers to give a 32-bit result.
A logical segment is not usually given a physical start- them by name rather than having to remember or calcu-
ing address when it is declared. After the program is late their value each time you refer to them in an in-
assembled, and perhaps linked with other assembled struction.
otherIn words, if you give names to con-
program modules, it is then assigned the physical ad- stants, variables, and addresses the assembler can use
dress where
it will be loaded in memory to be run. these names to find the desired data item or address
when you refer to it in an instruction. Specific directives
are used to give names to constants and variables in
Data and Addresses Naming Directives — EQU, your programs. Labels are used to give names to ad-
DB, DW, and DD dresses
yourin programs.
hi CHAPTER THREE
bin \ka structions
the m CODE HERE section ol the 11
111 Figure 3 10, find the instruction MOV \\
Foi example, when you use a binary numbei In .1 state
VII I riPLK AND llns ins tion, when executed, will
ment, you put a B aftei the string oi 1'sandO's to let the
copy a word hum memory to the AX registei When the
assemble) know that you want the number to be treated
assemblei reads through this program the lust time, it
as a binary number. The statemenl 11 Ml' MAX DB
will automatically calculate the displacement ol -
01 1 1 1001 B is an example. II you want to put In a negative
the named data items from the segment base
binary number, write the number in its 2 s complement
DATA HERE. Referring to Figure 3 I I you can sec thai
si^ii and-magnitude form.
the displacemenl ol MULTIPLICAND from the segment
( X IAI base Is 0000. This is because MULTIPLICAND is the firsl
data Hem dec Tared in the segment. The assembler, then,
To Indicate that you want a number to be evaluated as
will find that the displacement ol MULTIPLICAND is
base 8 or octal, put a Q after the string oi octal dibits.
00001 1. When the assembler reads the program the sec
rhe statemenl OLD COMPUTER DW 7341 Q is an example.
ond lime to produce the binary codes tor the instruc
lions. 11 will insert Ibis displacement as part ol the bi
DECIMAL
nary code tor the instruction MOV AX, MUI Ill'l K AND.
ITie assemble! Heats a 111mil mi with no identifying lei Since we know that the displacement of MULTIPLK AM )
tei after it as a decimal number. In other words, if you is 0000. we could have written the instruction as MOV
forget to put an H alter a number that you want the AX, |0000|. However, there would be a problem if we latei
assembler to treat as hexadecimal, the assembler will changed the program by adding another data item be-
treat it as a decimal number. The assembler automati- fore MULTIPLICAND in DATA HERE. The displacemenl
cally converts a decimal number in a statement to bi- of MULTIPLICAND would be changed. Therefore, we
nary the
so value can be loaded into memory. The state- would have to remember to go through the entire pro-
ment TEMPERATURE MAX DB 49 is an example. If you gram and
correct the displacement in all instructions
indicate a negative number in a data declaration state- that access MULTIPLICAND. If you use a name to refer to
ment, the
assembler will convert the number to its 2's each data item as shown, the assembler will automati-
complement sign-and-magnitude form. For example, cally calculate the correct displacement of that data item
given the statement TEMP MIN DB -20. the assembler for you and insert this displacement each time you refer
will insert the value 11101 100. which is the 2's comple- to it in an instruction.
ment representation for -20 decimal. To summarize how this works, then, the instruction
NOTE You can put a D after decimal values if you want MOV AX, MULTIPLICAND is an example of direct ad-
to more clearly indicate that the value is decimal. dressing where
the direct address or displacement
within a segment is represented by a name. For instruc-
HEXADECIMAL tions such
as this, the assembler will automatically cal-
culatedisplacement
the of the named data item from the
As shown in several previous examples, a hexadecimal
start of the segment and insert this value as part of the
number is indicated by an H after the hexadecimal dig-
binary code for the instruction. When the instruction
its. The
statement MULTIPLIER DW 3B2AH is an example.
executes, the BIU will add the displacement contained
in the instruction to the data segment base in DS. (Re-
ASCII
member,
contents
the of DS are shifted 4 bit positions
ASCII characters can be put in data declaration state- left before the displacement is added.) This addition
mentsenclosing
by them in single quotation marks. produces the 20-bit physical address needed to address
The statement BOY 1 DB 'ALBERT, for example, tells the the data named MULTIPLICAND in memory.
assembler to set aside six memory locations named The next instruction in the program in Figure 3-10 is
BOY 1 . It also tells the assembler to put the ASCII code another example of direct addressing using a named
for A in the first memory location, the ASCII code for L in data item. The instruction MUL MULTIPLIER multiplies
the second, the ASCII code for B in the third, etc. The the word named MULTIPLIER in DATAJTERE times the
assembler will automatically determine the ASCII codes word in the AX register. The low word of the result is left
for the letters or numbers within the quotes. in the AX register, and the high word of the result is left
NOTE ASCII can only be used with the DB directive. in the DX register. When the assembler reads through
this program the first time, it will find the displacement
DECIMAL REAL AND HEXADECIMAL REAL of MULTIPLIER in DATA HERE is 0002H. When it reads
through the program the second time it inserts this dis-
These two types are used to represent noninteger num-
placement
part as of the binary code for the MUL in-
bers suchas 3.14159. We will discuss how these are
struction. When
the MUL MULTIPLIER instruction exe-
used in Chapter 11.
cutes, the
BIU will add the displacement contained in
the instruction to the data segment base in DS to ad-
Accessing Named Data with Program dress MULTIPLIER in memory.
Instructions The next instruction, MOV PRODUCT, AX. in the pro-
Now that we have shown you how the data structure is gramFigure
in 3-10 copies the low word of the result
set up, let's look at how program instructions access from AX to memory. The low bvte of AX will be copied to
this data. Temporarily skipping over the first two in- a memory location named PRODUCT. The high byte of
64 ("HAI'UR IHKtl
segment reglstei rhis is the same operation we de
scribed for hand coding the example program In Figure
3 \. except that here we use the segment name instead
ol .1 number to refei to the segment base addi ess In this
example we used the AX registei to pass the value, but
an) 16 bit register othei than a segment register can be
used it you in hand coding youi programs, yi
just insert the uppei 16 bits ol the 20 bit segment stai t
Ing address in place ol DATA 1IERE in the instru< tion
For example, u in your pai t icular system you decide to
locate DATA HERE at address 00300H, DS should be
loaded with 0030H It you are using an assemble] . you
can use the segment name to refer to its base address as
shown in the example.
If you use the stack segment and the extra segment in
.i program, the stack segmenl register and the extra seg
ment register must also be initialized by program in-
structions
the same in way
\V1ifii the assembler reads through your assembly Ian
guage program, it calculates the displacement ol each
named variable from the start ol the logical segment
that contains it. The assembler also keeps track ol the FIGURE Mi Applied Microsystems is 1800 16-bit
displacement of each instruction code byte from the emulator. (Applied Microsystems ( orp.)
start of a logical segment. The CS:CODE HERE part of
the ASSUMED statement in Figure 3-10 tells the assem- ment tools to make your work easier These systems
blercalculate
to the displacements of the following in- usually contain several hundred Kbytes of RAM. a key-
structionsthe from start of the logical segment board video
and display, floppy and or hard disk drives,
CODE HERE. In other words, it tells the assembler a printer, and an emulator. Figure 3 12 shows an Ap-
that, when this program is run. the code segment regis- plied MicrosystemsES 1800 16-bit emulator which can
ter willcontain the upper 16-bits of the address where be added to an IBM PC/AT or compatible computer to
the logical segment CODE HERE was located in mem- produce a complete 8086/80186/80286 development
ory. Theinstruction byte displacements that the assem- system. The following sections give you an introduction
blerkeeping
is track of are the values that the 8086 will to several common program development tools which
put in the instruction pointer, IP. to fetch each instruc- you use with these systems. Most of these tools are pro-
tion byte. grams which
you run to perform some function on the
There are several ways that the CS register can be program you are writing. You will have to consult the
loaded with the code segment base address and the in- manuals foi your system to get the specific details for it.
struction pointer
can be loaded with the displacement of but this section should give you an overview of the steps
the instruction byte to be fetched next. The first way is involved in developing an assembly language microcom-
with the command you give your system to execute a puter programusing a system. An accompanying lab
program starting at a given address. A typical command manual steps you through the use of all these tools with
ol this sort is G = 0010:0000 <CR > . (<CR > means the SDK-86 board and the IBM Personal Computer.
"press the return key.") This command will load CS with
0010 and load IP with 0000. The 8086 will then fetch
Editor
and execute instructions starting from address 00100, An editor is a program which, when run on a system,
the address produced when the BIU shifts CS and adds lets you type in the assembly language statements for
IP. The other ways of loading CS and IP will be discussed your program. Examples of editors are ALTER which
in later sections. runs on Intel systems. EDLIN which runs on IBM PCs.
and Wordstar which runs on most systems. The main
The END Directive function of an editor is to help you construct your as-
sembly language
program in just the right format so
The END directive, as the name implies, tells the assem-
that the assembler will translate it correctly to machine
bler stop
to reading. Any instructions or statements
language. Figure 3-10 shows an example of the format
that you write after an END directive will be ignored
you should use when typing in your program. This form
of your program is called t he source program. The actual
ASSEMBLY LANGUAGE PROGRAM position of each field on a line is not important, but you
DEVELOPMENT TOOLS must put the fields of each statement in the correct
order, and you must leave at least one blank between
Introduction fields. Whenever possible, we like to line the fields up in
For all but the very simplest assembly language pro- columns so that it is easier to read the program.
gramswill
youprobably want to use some type of micro- As you type in your program, the editor stores the
computer development
system and program develop ASCII codes for the letters and numbers in successive
66 CHAPTER THREE
The IBM Personal Computer MACROAssembler 03-06-85 PA6E 1-1
PAGE ,132 ; Makes listing file lines 13? characters wide
;8086 program
".ABSTRACT : This program multiplies the two 16-bit words in
; the memory locations called MULTIPLICAND and
! MULTIPLIER. The result is stored in the memory
; location called PRODUCT
i PORTS USED : None
! PROCEDURES USED: None
REGISTERS USED : CS. DS, DX and AX
Symbols:
Name Type Value Attr
Warning Severe
Errors Errors
0 0
FIGURE 3-13 Assembler listing lor example program in Figure 5-10
Emulator
Another way to run your program is with an emulator.
An emulator is a mixture of hardware and software. It is
usually used to test and debug the hardware and soft-
wareanof external system such as the prototype of a
microprocessor-based instrument. Part of the hardware
of an emulator is a multiwire cable which connects the
host system to the system being developed. A plug at the
end of the cable is plugged into the prototype in place of
its microprocessor. Through this connection the soft-
waretheof emulator allows you to download your object-
code program into RAM in the system being tested and
run it. As with a debugger, an emulator allows you to
load and run programs, examine and change the con-
tentsregisters,
of examine and change the contents of
memory locations, and insert breakpoints in the pro-
gram. emulator
The also takes a "snapshot" of the con-
1 registers, activity on the address and data bus.
and the state of the flags as each instruction executes. The
emulator stores this trace data, as it is called, in a large
RAM. You can do a printout of the trace data to see the re-
sults that
your program produced on a step by-step basis.
Another powerful feature of an emulator is the ability
to use either system memory or the memory on the pro-
totype lor the program you are debugging. In a later C STOP J
chapter we diseuss in detail the use ol an emulator in
developing a microprocessor-based producl FIGURE 3-14 Program development algorithm.
loop until the assembler uils you on the listing thai it Mnemonics
found nn errors, li your program consists oi several
modules, then use i he linker to join their object modules Initializal ion list
Algorithm
tiongreater
is than 25H.
DH DL
7. Given the register contents in Figure 3-15. answer
DX 33 02
the following questions:
a. What physical address will the next instruction
be fetched from?
b. What is the physical address for the top of the
stack?
SP 0000
8. Describe the operation and results of each of the
BP 2468
following instructions, given the register contents
SI 4C00
shown in Figure 3-15. Include in your answer the
DI 7D00
physical address or register that each instruction
will get its operands from and the physical address FIGURE 3-15 8086 register and memory contents tor
or register that each instruction will put the result. Problems 7, 8, and 10.
Use the instruction descriptions in Chapter 6 to
help you. Assume that the instructions below are
independent, not sequential unless listed together
under a letter. Write the 8086 instruction which will perform the
a. MOV AX, BX k. OR CL. BL
indicated operation. Use the instruction overview
b. MOV CL, 37H L NOT AH
in this chapter and the detailed descriptions in
c. INC BX m. ROL BX, 1
Chapter 6 to help you.
d. MOV CX, [246BH] n. AND AL. CH
MOV DS. AX
a. Copies AL to BL
e. MOV CX. 246BH a
b. Loads 43H into CL
/. ADD AL, DH P- ROR BX. CL
c. Increments the contents of CX by one
9- MUL BX <l AND AL. OFH
d. Copies SP to BP
h. DEC BP r. MOV AX, [BX]
e. Adds 07H to DL
i. DIV BL S. MOVIBX1ISI1. CL
f. Multiplies AL times BL
j- SUB AX. DX
g. Copies AX to a memory location at offset
See if you can spot the grammatical (SYNTAX) er- 245AH in the data segment
rorsthe
in following instructions (use Chapter 6 to h. Decrements SP by one
help you): i. Rotates the most significant bit of AL into the
a. MOV BH. AX d. MOV 7632H, CX least-significant bit position
b. MOV DX, CL e. IN BL, 04H j. Copies DL to a memory location whose offset is
c. ADD AL, 2073H in BX
k. Masks the lower 4 bits of BL
1(1. Show the results that will be in the affected regis- /. Sets the most significant bit of AX to a one but
ters memory
or locations after each of the following
does not affect the other bits
groups of instructions execute. Assume that each m. Inverts the lower 4 bits of BL but does not af-
group of instructions starts with the register and
fect theother bits.
memory contents shown in Figure 3-15. (Use
Chapter 6.) 2. Construct the binary code for each of the following
a. ADD BL. AL SUB AL, CL 8086
808R instructions.
instructions.
MOV [0004], BL INC BX a. MOV BL. AL J- ROR AX. 1
b. MOV CL. 04 MOV [BX], AL b. MOV [BX], CX 9- OUT DX. AL
ROR DI. CL d ADD AL. BH c. ADD BX. 59H[DI] h. AND AL. OFH
c. MOV BX, 000AH DAA d. SUB [2048], DH i. NOP
70 CHAPTER THREE
13. Describe the function of each assembler directive 1?. Write the pseudocode representation for tli<- flow
and Instruction statement in the shorl program chart In Figure 3-14,
shown below these review problems.
i pressure read p r o 3 r a m
DATA.HERE SEGMENT
PRESSURE DB 0 istorase for pressure
DATA_HERE ENDS
PRESSURE-PORT EOU 04H ipressure sensor connected