RSGaonkar8085 1
RSGaonkar8085 1
One of the primary functions of the microprocessor is copying data, from a register (or
VOor mnemory) called the source, to another register (or I/O or memory) called the des
tination. In technical literature, the copying function is frequently labeled as the data
transfer function, which is somewhat misleading. In fact, the contents of the source re
not transferred, but are copied into the destination register without modifying the contents
of the Source.
Several instructions are used to copy data (as listed in Chapter 2). This section is
concerned with the following operations.
MOV : Move Copy a data byte.
MVI : Move Immediate Load adata byte directly.
OUT :0utput to Port Send a data byte to an output device.
IN :Input from Port Read a data byte from an input device.
The term copy is equally valid for input/output functions because the contents of the
source are not altered, However, the term data transfer is used so commonly to indicate
the data copy function that, in this book, these terms are used interchangeably when the
meaning is not ambiguous.
In addition to data copy instructions, it is necessary to introduce two machine
control operations to execute programs.
HLT: Halt Stop processing and wait.
NOP: No Operation Donot perform any operation.
These operations (opcodes) are explained and illustrated below with examples.
Instructions The data transfer instructions copy data from a source into a destination
without modifying the contents of the source. The previous contents of the destination are
replaced by the contents of the source.
INTRODUCTION TO 8085
INSTRUCTIONS 177
*The Symbols Rd, Rs. and Rare generic terms; they represent any of the 8085 g.hit registers: A, B, C, D, E, H,
and L.
178
PROGRAMMING THE 8085
Example Load the accumulator A with the data byte 82H (the letter H
indicates hexadecimal num.
6.1 ber), and save the data in register B.
Instructions MVIA, 82H,
MOV B,A
The first instruction is a 2-byte instruction that loads the
82H, and the second instruction MOVB,A copies the accumulator with the data byte
ister B without changing the contents of the
contents of the accumulator in reg
accumulator.
Example Write instructions to read eight ON/OFF switches
6.2 connected to the input port with the ad
dress 0OH, and turn on the devices connected to the
output port with the address 01lH, as
shown in Figure 6.1. (I/O port addresses are given in
hexadecimal.)
Solution The input has eight switches that are connected to the data bus
Any one of the switches can be connected to +5 V through the tri-state buffer.
each switch controls the corresponding device at the (logic 1) or to ground (logic 0), and
needs to read the bit pattern on the switches and send theoutput port. The microprocessor
same bit pattern to the output
port to turn on the corresponding devices.
+5 V
S, OFF 0
S ON 1 D,
Ss OFF 0 Data Bus
S4 OFF 0 Tri-State 4F
Buffer A 4F
B
Flags
S3 ON 1
D
Input
S ON Port H
00H
S ON 1 Do
So ON 1
EN Turm Solid State
OFF Relay Air Conditioner
Input Enable ON
Space Heater
OFF
4F Output Coffeepot
Port OFF Radio
01H ON
1 Light I
ON
1 Light 2
ON
Light 3
EN ON
Light 4
Output Enable
FIGURE 6.1
Reading Data at Input Port and Sending Data to Output Port
INTRODUCTION TO
8085
INSTRUCTIONS
Instructions IN 00H
OUT 01H
HLT
FLOWCHART
The thinking process described here and the steps necessary to write the program can be
represented in apictorial format, called a flowchart. Figure 6.2 describes the preceding
steps in a flowchart.
Flowcharting is an art. The flowchart in Figure 6.2 does not include all the steps de
scribed earlier. Although the number of steps that should be represented in a flowchart is
ambiguous, not all of them should be included. That would defeat the purpose of the
flowchart. It should represent a logical approach and sequence of steps in solving the
problem. Afowchart is similar to the block diagram of a hardware system or to the out
line of a chapter. Information in each block of the flowchart should be similar to the head
ing of a paragraph. Generally, a flowchart is used for two purposes: to assist and clarify
the thinking process and to communicate the programmer's thoughts or logic to others.
Symbols commonly used in flowcharting are shown in Figure 6.3. Two types or
symbols-rectangles and ovals- -are already illustrated in Figure 6.2. The diamond is
FIGURE 6.2
Flowchart Start
Enter
Number
in a Register
Output
Number
Stop
Meaning
FIGURE 6.3
Flowcharting Symbols 181
182 PROGRAMMING THE 8085
used with Jump instructions for decision making (see Figure 6.10), and the double-sided
rectangle is used for subroutines (see Chapter 9).
The flowchart in Figure 6.2 includes what steps to do and in what sequence. As a
rule, ageneral flowchart does not include how to perform these steps or what registers are
being used. The steps described in the flowchart are translated into an assembly language
progranm in the next section.
This program has six machine codes and will require six bytes of memory to enter
the program into your system. If your single-board nmicrocomputer has R/W memory
starting at the address 2000H, this program can be entered in the memory locations
2000H to 2005H. The format generally used to write an assembly language program is
shown below.
PROGRAM FORMAT
Memory Machine
Address Code Instruetion
(Hex) (Hex) Opcode Operand Comments
XX00 06 MVI B,37H ;Load register B with data 37H
XX01 37
XX02 78
XX03 MOV A,B
D3 :Copy (B) into (A)
XX04 OUT PORTI :Display accumulator contents
PORT1*
XX05 76 ; (37H) at Portl
HLT
:End of the program
This
programn has five columns:
Operand, and Comments. Each is describedMemory Address, Machine Code,
in the context of a single-board Opcode,
puter. microcom
Memory Address These are 16-bit addresses of the user (R/W)
where the machine code of the program is stored. The memory in the system,
XX00; the symbol XX represents the page number of thebeginning address is shown as
available R/W memory in the
microcomputer, and 00 represents the line number. For example, if the microcomputer has
theuser memory at 2000H, the symbol XX represents page number 20H; if
the user
memory begins at 0300H, the symbol XX represents page 03H. Substitute the appropri
ate page when entering the machine code of a program.
Machine Code These are the hexadecimal numbers (instruction codes) that are entered
(or stored) in the respective memory addresses through the hexadecimal keyboard of the
microcomputer. The monitor program, which is stored in Read-Only memory (ROM) of
the microcomputer, translates the Hex numbers into binary digits and stores the binary
digits in the R/W memory.
If the system has R/W memory with the starting address at 2000H and the output
port address 01H, the program will be stored as follows:
Memory Hex
Address Memory Contents Code
Opcode (Operation Code) An instruction is divided into two parts: Opcode and
Operand, Opcodes are the abbreviated symbols specified by the manufacturer (Intel) to
indicate the type of operation or function that will be performed by the machine code
Operand The operand part of an instruClion speCimes the item to be processed: it can
memory address.
be 8- or 16-bit data, a register, or a
D, D, Do
D, D, D, D, D, 0 0 0 =70H
0 11 1 0
The output port requires 70H, and it can be sent to the port by loading the accumulator
with 70H.
PROGRAM
ADD: Add
Add the contents of a register.*
ADI: Add Immediate Add 8-bit data.
SUB: Subtract
SUI : Subtract Immediate Subtract the contents of a register.
INR : Increment Subtract 8-bit data.
Increase the contents of a register by 1.
DCR: Decrement Decrease the contents of a register by 1.
The arithmetic operations Add and Subtract are performed in relation to the
the accumulator. However, the Increment or the Decrement contents of
operations can be performed
in any register. The instructions for these operations are explained
below.
INSTRUCTIONS
These arithmetic instructions (except INR and DCR)
Example The contents of the accumulator are 93H and the contents of register Care B7H. Add
6.3 both contents.
Instruction ADD C
CY D, D, D, D4 D, D, D, Do
(A) : 93H = 1 00 1 0 0 1 1
(C) B7H = 1 0 1 1 0 1 1 1
Carry
SUM (A): 14AH = 2 0 1 0 0 1 0 1
CY
Flag Status: S =0,Z= 0, CY =1
When the 8085 adds 93H and B7H, the sum is 14AH; it is larger than eight bits.
Therefore, the accumulator will have 4AH in binary, and the CY flag will be set. The re
sult in the accumulator (4AH) is not 0, and bit D, is not 1; therefore, the Zero and the
Sign flags willbe reset.
Add the number 35H directly to the sum in the previous example when the CY filag is set. Exarmple
6.4
Instruction ADI 35H
CY
(A) : 4AH = 1 0 1 00 10 10
+
(Data) 35H = 00 1 1 0 1 0 1
(A) : 7FH = 0 0 1 1 1 11 1 1
Assume the accumulator holds the data byte FFH. Illustrate the differences in the flags set Example
by adding 01H and by incrementing the accumulator contents. 6.5
CY
(A) : FFH = 1 111 11 11
After adding 01H to FFH, the sum in the accumulator is 0 with a carry. Therefore, the CY
and Z flags are set. The Sign flag is reset because D, is 0.
Instruction INR A
The accumulator contents will be 00H, the same as before. However, the instruction INR
will not affect the Carry flag: it will remain in its previous status.
Flag Status: S = 0, Z = 1, CY = NA
Result
Flag Register
CY D, D, Ds D, D, D, D, Do D, D, D, D, D, D, D, Do
1 |0 1 0 0 1 0 1 0
4A
CY
Carry Flag Set to Ibecause the answer is larger than eight bits; there is acarry gener
ated out of the last bit D,. During the addition, bits D, through D, may
but these carries do not affect the CY flag. generate carries,
Misconception #1 Bit D, in the result (4AH) corresponds to the bit position of the CarrY
flag D, in the flag register; therefore, the Carry flag is reset.
Misconception #2 In the addition process, bits Do of 93H and B7H
other bit additions generate carries); therefore the Carry flag is set. generate carry (0
a
INTRODUCTION TO 8085 INSTRUCTIONS 191
Zero Flag Reset to 0 because the answer is not zero. The Zero flag is set only when all
eight bits in the result are 0.
Misconception #3 Bit D, in the result (4AH) is 1, and it corresponds to bit D, (Zero flag
position) in the flag register. Therefore, the Z flag is set.
Sign Flag Reset to 0 because D, in the result is 0. The position of the sign flag in the
flag register is also D,. But it is just a coincidence. The microprocessor designer could
have chosen bit D, for the Sign flag and bit D, for the Zero flag in the flag register. The
Sign flag is relevant only when we are using signed numbers.
Misconception #4 If the Sign flag is set, the result must be negative.
See Questions and Programming Assignments 9 through 19 at the end of this chapter.
PROBLEM STATEMENT
Write a program to perform the following functions, and verify the output.
1. Load the number 8BH in register D.
2. Load the number 6FH in register C.
3. Increment the contents of registerCby one.
4. Add the contents of registers C and D and display the sum at the output PORT1.
PROGRAM
The illustrative program for arithmetic operations using addition and increment is pre
sented as Figure 6.5 to show the register contents during some of the steps.
PROGRAM DESCRIPTION
1. The first four machine codes load 8BH in register D and 6FH in register C (see Figure
6.5). These are Data Copy instructions. Therefore, no flags will be affected; the flags
will remain in their previous status. The status of the flags is shown as X to indicate
no change in their status.
2. Instruction INR Cadds Ito 6FH and changes the contents of C to 70H. The result is
nonzero and bit D, is zero; therefore, the S and Z flags are reset. However, the CY flag
is not affected by the INR instruction.
3. To add (C) to (D), the contents of one of the registers must be transferred to the accu
mulator because the 8085 cannot add two registers directly. Review the ADD instruc
tion. The instruction MØV A,Ccopies 70H from register C into the accumulator with
out affecting (C). See the register contents.
192 PROGRAMMING THE 8085
03 6F
A 70 SZ CY LL
00
05 79 MOV A,C B 70 C
D 8B E
06 82 ADD D A FB
SZ CY P
I0 0
07 D3 OUT PORTI B 70 C
08 PORT # PORTI D 8B E
FIGURE 6.5
Ilustrative Prograrm for Arithmetic Operations-Using Addition and Increment
4. Instruction ADD Dadds (D) to (A), stores the sum in A, and sets the Sign fiag as
shown below:
By examining the contents of the registers, the following points can be confirmed:
1. The sum is stored in the accumulator.
2. The contents of the source registers are not changed.
3. The Sign (S) flag is set.
Even though the Sign (S)flag is set, this is not a negative sum. The microprocessor
sets the Sign flag whenever an operation results in D, = 1. The microprocessor cannot rec
ognize whether FBH is a sum, a negative number, or a bit pattern. It is your responsibil
ity to interpret and use the flags. (See "Flag Concepts and Cautions" in Section 6.2.1.) In
this example, the addition is not concerned with the signed numbers. With the signed
numbers, bit D, is reserved for a sign by the programmer (not by the microprocessor), and
no number larger than +12710 can be entered.
6.2.3 Subtraction
The 8085 performs subtraction by using the method of 2's comnplement. (If you are not
familiar with the method of 2's complement, review Appendix A2.)
Subtraction can be performed by using either the instruction SUB to subtract the
from the
contents of a source register or the instruction SUI to subtract an 8-bit number
the
contents of the accumulator. In either case, the accumulator contents are regarded as
minuend (the number from which to subtract).
(or
The 8085 performs the following steps internally to execute the instruction SUB
SUI).
complement.
Step 1: Converts subtrahend (the number to be subtracted) into its l's
subtrahend.
Step 2: Adds 1 to 1's complement to obtain 2's complement of the
accumulator).
Step 3: Add 2's complement to the minuend (the contents of the
Step 4: Complements the Carry flag.
These steps are illustrated in the following example.
B from
Register B has 65H and the accumulator has 97H. Subtract the contents of register Example
accumulator. 6.6
the contents of the
Instruction SUB B
Subtrahend(B): 6SH = 0 I 10 I 01
Step 1: I'scomplement of 6SH = I 0 0 I I0 10
(Substitute 0 for Iand 1for 0)
Add 01 to obtain 00 0 0 0 0 0 1
Step 2:
2's complement of 65H = I0 0 1 10 1 1
+
PROGRAMMING THE 8083
194
To subtract:97H- 65H,
01 I J
Add 97H to 2's complement of 65H = 100 1
hSary
Step 3: 0 0 11001 0
Step 4: Complement Carry 0 011 0010
Result (A): 32H
Flag Status: S = 0, Z= 0, CY = 0
If the answer is negative, it willbe shown in the 2's complement of the actual magnitude.
For example, if the above subtraction is performed as 65H 97H, the answer will be the
2's complement of 32H with the Carry (Borrow) flag set.
PROGRAM DESCRIPTION
1. Registers B and Care loaded with 30H and 39H, respectively. The
instruction MOV
A,B copies 30H into the accumulator (shown as register contents). This is an
essenttal
step because the contents of a register can be subtracted only from the contents of the
accumulator and not from any other register.
2. Toexecute the instruction SUB Cthe
ternally:
microprocessor performs the following steps in
Step 1: 39H = 0 0 11 I00 I
T's complement of 39H = 1 I00 0 110
Step 2: Add 01 = 0 00 0 0 00 1
2's complement of 39H = 1|00 011 1
+
Step 3: Add 30H to 2's complement of 39H = 0 0
| 0 0 00
CY 0 1I|1 0 1 1 1
INTRODUCTION TO 8085 INSTRUCTIONS 195
HI-LO
XX00 06 MVI B,30H Load the minuend in register B
01 30 Load the subtrahend in register C
02 OE MVI C,39H
The register contents:
03 39 A 30
04 78 MOV A,B
R 30 39
A F7
SZ N
05 91 SUB
B 30 39
06 D3 OUT PORTI
07 PORT#
08 76 HLT
FIGURE 6.6
Ilustrative Program for Subtraction of Two Unsigned Numbers
2. The results of the arithmetic operations are stored in the accumulator; thus, the previ.
ous contents of the accumulator are altered.
3. The flags are modified to reflect the data conditions of an operation.
4. The contents of the source register are not changed as a result of an arithmetic opera
tion.
5. In the Add operation, if the sum is larger than 8-bit, CY is set.
6. The Subtract operation is performed by using the 2's complement method.
7. If a subtraction results in a negative number, the answer is in 2's complement and CY
(the BorrOw flag) is set.
8. In unsigned arithmetic operations, the Sign flag (S) should be ignored.
9. The instructions INR (Increment) and DCR (Decrement) are special cases of the arith
metic operations. These instructions can be used for any one of the registers, and they
do not affect CY, even if the result is larger than 8-bit. All other flags are affected by
the result in the register used (not by the contents of the accumulator).
A microprocessor is basically a programmable logic chip. It can perform all the logic
functions of the hard-wired logic through its instruction set. The 8085 instruction set in
cludes such logic functions as AND, OR, Ex OR, and NOT (complement). The opcodes
of these operations are as follows:*
ANA: AND Logically AND the contents of a register.
ANI : AND Immediate Logically AND 8-bit data.
ORA: OR
Logically OR the contents of a register.
ORI : OR Immediate Logically OR 8-bit data.
XRA: X-OR Exclusive-OR the contents of a register.
XRI : X-OR Immediate Exclusive-OR 8-bit data.
All logic operations are performed in relation to the contents of the accumulator. The in
structions of these logic operations are described below.
INSTRUCTIONS
The logic instructions
1. implicitly assume that the accumulator is one of the operands.
2. reset (clear) the CY flag. The instruction CMA is an exception; it does not atfect any
flags.
3. modify the Z, P, and S flags according to the data conditions of the result.
4. place the result in the accumulator.
5. do not affect the contents of the operand register.
*Memory-related logic operations are excluded here; they will be discussed in the next chapter.
INTRODUCTION TO 8085 INSTRUCTIONS 197
1 1
(B) =
L0
FHeDl 0 0
D
(A) = 1
Input - Output
(a) ANA B
(A) = 0 0 0 D4 (b)
1
FIGURE 6.7
AND Gate (a) and a Simulated ANA Instruction (b)
1. turn on the air conditioner if switch S, of the input port 00H is on.
2. ignore all other switches of the input port even if someone attempts to turn on other
appliances.
(To perform this experiment on your single-board microcomputer, simulate the reading of
the input port 00H with the instruction MVIA, 8-bit data.)
PROBLEM ANALYSIS
In this problem you are interested in only one switch position, S, which is connected to
data line D,.Assume that various persons in the family have turned onthe switches of the
air conditioner (S;), the radio (S4), and the lights (S3, Sz. S1, So).
If the microprocessor reads the input port (IN 00H), the accumulator will have data
byte 9FH. This can be simulatedby using the instruction MVIA,9FH. However, if you
are interested in knowing only whether switch S, is on, you can mask bits D, through Do
by ANDing the input data with a byte that has 0 in bit positions D, through D, and I in
bit position D,.
D, D, Ds D4 D, D, D, Do
1 0 0 0 0 0 0 0 = 80H
After bits D, through D,have been masked, the remaining byte can be sent to the output
port to simulate turning on the air conditioner.
INTRODUCTION TO 8085 INSTRUCTIONS 199
PROGRAM
PROGRAM OUTPUT
The instruction ANI 80H ANDs the accumulator data as follows:
The ANDing operation always resets the CY flag. The result (80H) will be placed in the
accumulator and then sent to the ouptput port, and logic 1 of data bit D, turns on the air
conditioner. Inthis example, the output (80H) is the same as the masking data byte (80H)
because switch S, (or data bit D) is on. If S, is off, the output will be zero.
The masking is a commonly used technique to eliminate unwanted bits in a byte.
The masking byte to be logically ANDed is determined by placing Os in bit positions that
are to be masked and by placing ls in the remaining bit positions.
Assume register B holds 93H and the accumulator holds 15H. Illustrate the results of the Example
instructions ORA B, XRA B, and CMA. 6.
(B) = | 0 0 | 0 0 | | (93H)
OR
(A) = 0 00 | 0 I0 | 1SH)
(A) =| 0 0 1 0 L 9H)
Flag Status: S = 1,Z=0, CY = 0
The esult 97H will be placed in the accumulator, the CY lag will be reset, and the
other flags will be modificd to reflect the data conditions in the accumulator.
2. The instruction XRA B will perform the following
operation.
(B) = | 0 0 1 00 1 1 (93H)
X-OR
(A) =0 00 1 0 I 0 1 (15H)
(A) = | 000 0 1 |0 (86H)
Flag Status: S = 1,Z=0, CY = 0
The result 86H will be placed in the
shown.
accumulator, and the flags will be modified as
3. The instruction CMA will result in
ON: S, D
OFF: S
OFF: S.
Data Bus B085
ON: S Input Port
00H System
ON: S,
ON: S,
ON: S
Do
ON: So EN
Output Enable
FIGURE 6.8
Input Port to Control Appliances
The instruction IN reads the switch positions shown as D,-Do and the instruction ORI
sets the bit D, without affecting any other bits.
In Figure 6.8, assume it is winter, and turn off the air conditioner without affecting the Example
other appliances. 6.9
Solution
To turn off the air conditioner, reset bit D, by ANDing the reading of the input port with
the data byte 7FH as follows:
IN 00H: (A)= D, D, Ds D, D, D, D, Do
ANI 7FH: = 0
0 D, Ds D, D, D, D, D
Flag Status: CY = 0; others will depend onthe data bits.
The ANI instruction resets bit D, without affecting theother bits.
204
PROGRAMMING THE 8085
5. The logic operations cannot be performed dircctly with the contents of
6. The individual bits in the two registers.
accumulator can be set or reset using logic instructions
See Qucstions and Progranming Assignments 20-29 at the end of this chanter