Course on Microprocessor Theory and Interfacing. This is 4th lecture on 8085 microprocessor which include arithmetic instructions like add, subtract, BCD addition in detail.
8085 Microprocessor Addition and Subtraction Instructions
1. Presented by:
Engr Zeeshan Ahmed Lodro
Lecturer-Department of Electronics Engineering Technology
BBS University of Technology and Skill Development Khairpur Mirs
1
Lecture-03
3. ❑Instructions to perform addition
▪ Instruction type ADD R
▪ Flag register
▪ Instruction type ADI d8
▪ Instruction type INR R
▪ Instruction type ADC R
▪ Instruction type ACI d8
3
4. ❑Instructions to perform subtraction
▪ Instruction type SUB R
▪ Instruction type SUI d8
▪ Instruction type DCR R
▪ Instruction type SBB R
▪ Instruction type SBI d8
▪ Instruction type INX rp
▪ Instruction type DCX rp
▪ Instruction type DAD rp
4
6. ❑8085 imposes the restriction that one of the
operands must be in the accumulator.The
other operand can be one of the following
▪ Contents of an 8-bit register.
▪ Contents of memory location pointed by HL
register pair.
▪ Eight bit immediate data.
6
7. ❑ADD is a mnemonic that stands for Add
contents of R to accumulator. In this case R
stands for any of the following register pairs.
Or memory location M pointed by HL pair.
❑R= A,B,C,D,E,H,L or M
❑This instruction is used to add contents of R
and accumulator .
❑The result of addition is stored in
accumulator.
7
8. ❑As R can have any of eight values, there are
eight opcodes for this type of instruction. It
occupies only 1-byte in memory.
❑ADD E is an example instruction of this type.
It is 1-byte instruction.
8
9. ❑The result of execution is shown below.
Summary:
ADD R( 1-byte;ADD E; 8 opcode)
9
Register Before After
E F3H
A 45H 38H
10. In the previous example, the result of
addition of 45 H and F3H turns out to be 38H,
which is less than the original value of 45 H
present in the accumulator.
It is very clear that user will not be happy with
this because the correct answer is 38H with
carry of 1.
This carry information is stored in special 8-
bit register called the flag register.
10
11. The flag register can be represented by 1 bit
of information.
There are 5 types of flag registers.
Carry flag(CY)
Auxiliary carry flag(AC)
Sign flag(S)
Parity flag(P)
Zero flag(Z)
11
12. Their position in flag register is shown below.
The positions marked with x are don’t care
bits.
These individual flags are either set to 1 or
reset to 0 depending on the result of
execution of an arithmetic or logical
instructions.
12
13. Notice that in the addition of any two 8-bit
numbers, the carry generated can be either 1
or 0.Thus to store the carry information 1-bit
storage is enough.
The Cy bit is stored in the LS bit position in
the flag register.
13
15. In the addition of any two 8-bit numbers, a
carry may be generated when we add the LS
hex digits of the two numbers. Such carry is
called intermediate carry, half carry or
auxiliary carry.
Intel prefers to call it AC. In example 1 AC in
not generated however in example 2AC is
generated.
15
16. As this is intermediated carry we are not
interested in storing the information. But
8085 still stores this AC information in bit
position 4 of the flag register.
16
17. The S flag is set to 1, if the result of an
arithmetic operation is negative. Indicated by
MS bit of 8 bit result being 1.
The S flag is set to 0, if the result of an
arithmetic operation is positive. Indicated by
MS bit of 8 bit result being 0.
Thus the value of S flag is essentially the
value of the MS bit of the 8-bit result.
17
18. The P flag is set to 1, if the 8–bit result of an
arithmetic operation has an even number of
1s in it.
The P flag is set to 0, if the 8–bit result of an
arithmetic operation has an odd number of 1s
in it.
In Example 1.The number of 1s are odd so the
parity flag is set to 0.
In Example 2.The number of 1s are even so
the parity flag is set 1.
18
19. The Z flag is set to 1, if the 8–bit result of an
arithmetic operation is 00H.
The Z flag is set to 0, if the 8–bit result of an
arithmetic operation is not equal to 00H.
In Example 1.The 8-bit result is 38H and is
non zero so the Z flag is reset to zero.
What about Example 2?
19
20. ADI is a mnemonic , which stands for ADd
Immediate to Accumulator and d8 stands for
any 8-bit data.
This instruction is used to add 8-bit
immediate data to the accumulator.
The result of addition will be store in
accumulator.
The flags are affected based on the results.
20
21. ADI F3H.
▪ The result of execution is shown below
Summary:
ADI d8( 2-bytes; ADI F3H; 1 opcode)
21
Register Before After
A 45H 38H
F Any values CY=1, AC=0 S=0,
P=0 Z=0
22. INR is a mnemonic that stands for INcRement
and R stands for any of the following registers or
memory location M pointed by HL pair.
R=A,B,C,D,E,H,L or M
This instruction is used to add 1 to the contents
of R.
The result of increment will be stored in R.
All flags, except CY, flag are affected depending
on the results.
22
23. INR M.
▪ The result of execution is shown below
Summary:
INR R( 1-byte; INR M; 8 opcodes)
23
Register Before After
HL F850H
F850H 45H 46H
F Any values Cy= no change,
AC=0, S=0,P=0,Z=0
24. There are times when a user is required to
add tow numbers each of which is several
bytes in size.
For example , we are adding 16 bit numbers.
In this example the addition of 56H and F2H
results in a sum of 48H with carry of 1.
24
25. Next we have to add 34H andA2H along with
this carry value of 1.
To facilitate such operations 8085 provides
instructions to add two numbers along with
carry.
ADC is mnemonic that stands for Add with
carry and R stands for any of the following
registers or memory location M pointed by
HL pair.
25
26. R=A,B,C,D,E,H,L, or M
This instruction is used to add contents of R
and accumulator along with the carry value.
The result of addition will be store in the
accumulator.
26
27. ADC E
▪ The result of execution is shown below
Summary:
ADC R( 1-byte; ADC E; 8 opcodes)
27
Register Before After
E 45H
A 33H 79H
F Cy=1
Others= any values
Cy= 0AC=0,
S=0,P=0,Z=0
28. ACI is a mnemonic that stands for Add with
Carry Immediate to accumulator and d8
stands for any 8-bit dat.
This instruction is used to add 8 bit
immediate data to the accumulator along
with the carry value.
The result of addition will be stored in the
accumulator.
28
29. ACI F3H
▪ The result of execution is shown below
Summary:
ACI d8( 2-bytes; ACI F3H; 1 opcode)
29
Register Before After
A 45H 38H
F Cy=0
Others= any values
Cy= 1, AC=0,
S=0,P=0,Z=0
30. In the subtraction of two numbers 8085
imposes the restriction that accumulator will
have one operand from which the other
operand specified by one of the following will
be subtracted.
▪ Contents of 8-bit register
▪ Contents of memory location pointed by HL pair.
▪ 8-bit immediate data.
30
31. SUB is a mnemonic that stands for SUBtract
contents of R from Accumulator.
R=A,B,C,D,E,H,L, or M
The result of the subtraction will be stored in
theAccumulator.
31
32. SUB E
▪ The result of execution is shown below
Summary:
Sub R( 1-byte; SUB E; 8 opcodes)
▪ Why S=1? Check next slide
32
Register Before After
A F3H AEH
E 45H
F any values Cy= 0AC=0,
S=1,P=0,Z=0
33. Why S=1?
Because the number is negative.
How ?
We check in two ways if result is in hex or binary. Look
at the left digit and deduce the result as given.
▪ 0-7 <-> 0000-0111 <-> pos
8-F <-> 1000-1111 <-> neg
▪ In previous example result is AE so left digit is A which
is >7 hence number comes in range of 8-F the result is
negative. Similarly convert whole number AE into
binary and check the MSB(Left most digit) if it is 1
number is negative and if it is 0 number positive.
33
34. Internally , 8085 performs this subtraction by
adding the 2’s complement of 45 H to F3H.
Carry generated in this addition will be
complemented.Thus the Cy flag after the
subtraction is zero.
34
45
0100 0101
1011 1010
+1
1011 1011
8+2+1 8+2+1
11 11
B B
B=11
11+3=14=E
F=15
B=11
Total = 26
Subtract
16 that becomes 10=A
16 means carry
35. SUB E
▪ The result of execution is shown below
Summary:
Sub R( 1-byte; SUB E; 8 opcodes)
35
Register Before After
A 30H 10H
E 20H
F any values Cy= 0AC=1,
S=1,P=0,Z=0
36. How 8085 will perform this operation
internally?
36
37. SUI stands for Subtract Immediate from
Accumulator and d8 stands for any 8-bot
data.
This instruction is used to subtract 8-bit
immediate data from the accumulator.
The result of the subtraction will be stored in
the accumulator.
The flags are affected based on the results.
37
38. SUI F3H
▪ The result of execution is shown below
Summary:
SUI d8( 2-bytes; SUI F3H; 1 opcode) 38
Register Before After
A 45H 52H
F any values Cy= 1, AC=1,
S=0,P=0,Z=0
F3
1111 0011
0000 1100
+1
0000 1101
0 D
D=13
13+5=18-16=2
4+1=5 Subtraction
of 16 from 18 means
addition of carry to next hex value
39. DCR is a mnemonic that stands for
DeCRement and R stands for following
registers or memory location pointed b HL
pair.
This instruction is used to subtract 1 from the
contents of R.The result of the increment will
be stored in R. All flags, except Cy flag are
affected depending on the result.
39
40. DCR M
▪ The result of execution is shown below
Summary:
DCR R( 1-byte; DCR M; 8 opcodes)
40
Register Before After
HL F850H
F850 45H 44H
F any values Cy= no change
41. There are times when user is required to
subtract two numbers each of which is
several bytes in size.
For example, let us say it is needed to
perform the following subtraction.
41
42. In this example the subtraction of 56H and
F2H results in 64H with a borrow of 1.
Next we have to subtract 34H and 12H along
with this borrow.
To facilitate such an operation, 8085 provides
instruction to subtract two numbers along
with borrow value.
42
43. SBB is mnemonic that stands for SuBtract
with Borrow and R stands for any of the
following registers or memory location M
pointed by HL pair.
This instruction is used to subtract contents
of R from accumulator along with carry value.
The result of subtraction will be stored in
accumulator.
43
44. SBB E
▪ The result of execution is shown below
Summary:
SBB R( 1-byte; SBB E; 8 opcodes)
44
Register Before After
E 45H
A 33H 44H
F Cy=1
Other=any values
Cy=1, AC=0, Z=0,
P=1, S=1
Borrow 1 from 3 that is equal
to=16+3=19-5=14, 1 is
Auxiliary carry=16+2=18-
4=14=4 1 is carry
3-1=2+16=18 3
4 5
4 4
45. SBI is a mnemonic that stands for ‘Subtract
with Borrow Immediate from Accumulator’
and d8 stands for any 8-bit data.
This instruction is used to subtract 8-bit
immediate data from the accumulator along
with the carry value.
The result of subtraction will be stored in
accumulator.
45
46. SBI F3H
▪ The result of execution is shown below
Summary:
SBI R( 2-bytes; SBI F3H; 1 opcode)
WHY C,P,S=1? 46
Register Before After
A 45H 52H
F Cy=0
Other=any values
Cy=1, AC=0, Z=0,
P=1, S=1
45
F3
-----
52
47. INX is a mnemonic that stands for Increment
eXtended register and rp stands for any of
the following register pair
rp=BC, DE, or HL
This instruction is used to add 1 to the
contents of rp.The result of increment will be
stored in rp.
A register pair is generally used to store a
memory address.
47
48. INX B
▪ The result of execution is shown below
Flags are not affected in this instruction by default
because it causes problems in many cases.
Summary:
INX rp( 1-byte; INX B; 3 opcode)
48
Register Before After
BC F850H F851H
49. DCX is a mnemonic that stands for
DeCrement eXtended register and rp stands
for any of following register pair.
rp= BC, DE or HL
This instruction is used to subtract 1 from the
contents of rp.The result of decrement will
be stored in rp.
Flags are not affected in this type of
instruction.
49
50. DCX B
▪ The result of execution is shown below
It may appear that DCX B is same as DCR C, But if content
of BC was F900H, it becomes F8FFH after execution, thus
DCX decrements 16-bit quantity while DCR decrements
8-bit quantity.
Summary:
DCX rp( 1-byte; DCX B; 3 opcode)
50
Register Before After
BC F850H F84FH
51. Intel 8085 is basically an 8-bit
microprocessor. But the designers have
provided instructions to perform 16-bit
additions also.
As the internal architecture is only 8 bit.This
instruction easily takes double the time
needed to add two 8-bit numbers.
Dad is a mnemonic which stands for Double
Add and rp stands for any of the following
register pair.
51
52. Rp= BC, DE, or HL
This instruction is used to add contents of rp
to HL.
The result of addition will be stored in HL.
Thus in this instruction , HL is used as a 16-bit
accumulator.
Only Cy flag is affected depending on the
result.
52
53. DAD B
▪ The result of execution is shown below
Summary:
DAD rp( 1-byte; DAD B; 3 opcodes)
53
Register Before After
HL 1234H 0A84H
BC F850H
F any values Cy=1, no change in
other flags
54. In digital computer every thing is represented
by 0s and 1s.
For example an instruction will have a code
using only 0s and 1s. Data is also represented
using 0s and 1s.
Data can be of different types like unsigned
numbers, signed numbers, floating point
numbers, binary coded decimal(BCD)
numbers.
54
55. Many time we are required to represent
decimal numbers in a computer to perform
arithmetic of these numbers.
For example we may be required to total
marks a student has obtained in five different
subjects where obviously the marks are
awarded in decimal notation.
For this purpose BCD code is extensively
used.
55
56. In BCD notation 4 bits are used to code a digit
and so two digits of information is stored in a
byte.
For example decimal 39 is represented in BCD
so two digit of information is stored in a byte.
Decimal 39 is represented in BCD as
0011, 1001
Codes 1010,1011,1100,1101,1110 and 1111 are
illegal in BCD notation.
56
57. Similarly decimal 1024 is represented in BCD
as 0001 0000 0010 0100
Same decimal is represented in binary in
binary as 0000 o100 0000 0000
57
58. Suppose we want to add two decimal
numbers 38 and 45.They will be represented
in BCD as 0011 1000 and 0100 0101.The
addition will be 0111 1101. if we interpret this
result as BCD number the answer is incorrect
as well as illegal.
This is where DAA instruction proves its
usefulness.
58
59. The working of DAA instruction depends on
AL register, Cy and AC flags.
In effect , it adds 00H, 06H, 60H or 66H to
accumulator as to get the correct BCD
answer in the accumulator with given
conditions.
Condition #1
If the LS hex digit in A is <=9 andAC flag is 0,
the LS hex digit value will not be altered.
59
60. Condition#2
If the LS hex digit is >9 or ifAC flag is set to 1,
it adds 6 to the LS hex digit of A. Further it
increments the MS hex digit if this addition
resulted in carry to the MS digit position.
In this process, the Cy flag will be set to 1 if
the MS hex digit was incremented from F to
0.
60
61. Condition#3
If the MS hex digit is <=9 and Cy flag is 0.The
MS hex digit will not be altered.And CY flag
will be reset to 0.
Condition#4
If the MS hex digit is>9 or if Cy flag is set to 1,
it adds 6 to the MS hex digit of A and sets Cy
flag to 1.
Note: DAA instruction can not be used for
decimal subtraction.
61
62. Add 45 BCD and 38 BCD and store the result
7DH inA.
In this case, Cy flag=0 and AC flag=0, But a D
is an invalid BCD code,The DAA instruction
adds 06 to A.Thus we get 83H in A which will
now be interpreted as 83 BCD.
62
63. Add 63 BCD and 88 BCD and store the result
EBH in A.
Add 53 BCD and 36 BCD and store the result
89H in A.
Add 99 BCD and 88BCD and store the result
21H in A.
Add 63 BCD and 42 BCD and store the result
A5H in A.
63