Let us suppose that the two 16 bit numbers are stored in
9000H and 9002H. These two 16 but numbers are to be added and the result is to
be stored in memory location 9004 and
the carry (if generated) is to be stored in memory location 9006H.
Algorithm:
- Start.
- Initialize register C for using it as a counter for storing carry value.
- Load data into HL register pair from one memory address (9000H).
- Exchange contents of register pair HL with DE.
- Load second data into HL register pair (from 9002H).
- Add register pair DE with HL and store the result in HL.
- If carry is present, go to 8 else go to 9.
- Increment register C by 1.
- Store value present in register pair HL to 9004H.
- Move content of register C to accumulator A.
- Store value present in accumulator(carry) into 9006H.
- Terminate the program.
Program Code:
MVI C, 00H
LHLD 9000H
XCHG
LHLD 9002H
DAD D
JNC AHEAD
INR C
AHEAD:
SHLD 9004H
MOV A, C
STA 9006H
HLT
The register
C is initialized to 00 to store the carry value. The first 16 bit number is
loaded into HL register pair. 9000H stores the lower significant 8 bits and
9001H stores the upper significant 8 bits meaning that if 1234 is the 16 bit number stored from memory
location 9000H then the 16 bit numbers are stored in memory as:
9000H - 34
9001H - 12
The first 16 bit number is then
stored in DE register pair with the help of XCHG instruction. The second 16 bit
number (stored is 9002H and 9003H) is then loaded into the HL register pair.
The contents of DE register pair is then added with the contents of HL register
pair and the result is stored in HL register pair. If carry is generated during
the addition then register C is incremented by 1. But if no carry is present
then the program skips the INR C instruction. The contents of HL register pair
is then stored in memory location 9004H which is the sum value. Then the
contents of register C is moved into the accumulator and then stored in memory
location 9006H which is the carry value.
Output:
Example: 1 |
Here the first 16 bit number is stored in 9000H and 9001H
and the second 16 bit number is stored in 9002H and 9003H. The addition takes
place as:
1234
+1234
--------
2468
The sum value is stored in memory location 9004H and 9005H
with lower 2 significant bits at 9004H and the upper 2 significant bits at 9005H.
Since no carry is generated so 00 is stored in 9006H.
Example: 2 |
Here, the addition takes place as:
FFFF
+1111
--------
1 1110
Here, since carry is generated so 01 is stored in memory
location 9006H.
No comments:
Post a Comment