Let us suppose that the first 16 bit number is stored in
memory location 9000H and the second number is stored in memory location 9002H.
These two 16 numbers are to be subtracted and the result is to be stored in memory location 9004H.
Algorithm:
- Start.
- Load first data from 9000H directly into register pair HL.
- Exchange contents of register pair DE and HL.
- Load second data from 9002H directly into register pair HL.
- Move contents of register L into accumulator A.
- Subtract content of register E from A.
- Move contents of accumulator A into register L.
- Move contents of register H into accumulator A.
- Subtract with borrow contents of register D from accumulator A.
- Move contents of accumulator A into register H.
- Store data contained in HL register pair into 9004H.
- Terminate the program.
Program Code:
LHLD 9000H
XCHG
LHLD 9002H
MOV A, E
SUB L
MOV L, A
MOV A, D
SBB H
MOV H, A
SHLD 9004H
HLT
The first 16 bit number is loaded into
HL register pair. The 16 bit number is then moved into DE register pair. The
second 16 bit number is then loaded into HL register pair. Since, in 8085 there
is no instruction for subtracting two 16 bit numbers (DAD is instruction for 16
bit addition) so we subtract two 8 bit numbers. The content of register E is
moved into accumulator A and is the content of accumulator A is subtracted with
contents of register L. The difference obtained is again moved from accumulator
to L register. The content of D register is now moved into accumulator A and
then the contents of register H is subtracted with borrow from the contents of
accumulator. The difference thus obtained is moved into the H register. Now HL
register pair contains the difference of 16 bit numbers. And the difference is
stored in memory location 9004H.
Output:
Example: 1 |
Here, the first 16 bit number is
stored in 9000H and 9001H and the second 16 but number is stored in 9002H and
9003H. Now, since 8085 does not have direct subtraction instruction of 16 bit
numbers so we subtract two 8 bit numbers.i.e.
At first,
34
-34
-----
00
This result is stored in memory
location 9004H
Then,
12
-12
-----
00
This result is stored in memory
location 9005H
So, we obtain the subtraction of 16
bit numbers.
Example: 2 |
Here, At first,
08
-05
-----
03
This result is stored in 9004H.
Then,
06
-04
-----
02
This result is then stored in 9005H.
Thus, we obtain the result of subtraction of two 16 bit
numbers.
This comment has been removed by the author.
ReplyDeletethanks very helpfull
ReplyDeleteVery nice
ReplyDeleteHow can we go for negative results?
ReplyDeleteWrite an 8085 program in assembly language to add two 8-bit numbers (99)h, (98)h. and store the result in B register. For the second step, Store the result in memory location (1000, 3030, 1FAB). And move the result from memory address (3030) to register. The last step, Add the data which is located in memory location (1000, 1FAB) and store the result in the second register of memory
ReplyDelete