Sunday, October 25, 2015

8085 Program To Divide Two 8 Bit Numbers

Let us suppose that we store the two 8 bit numbers that are to be divided in the memory location 9000H and 9001H. Now the remainder of these numbers is to be stored in 9002H and the quotient is to be stored in 9003H.

Algorithm:

  1. Start
  2. Initialize register C with 00.
  3. Load into accumulator directly from location 9000H.
  4. Move content of A into register B.
  5. Load into accumulator directly from 9001H.
  6. Compare register B with accumulator A.
  7. If no carry is present go to 8 else go to 11.
  8. Subtract contents of register B from accumulator A.
  9. Increment the value of register c.
  10. Go to 6.
  11. Store data present in accumulator into memory location 9002H.
  12. Move contents of register C into accumulator A.
  13. Store data present in accumulator A into memory location 9003H.
  14. Terminate the program.

Program Code:

MVI C, 00H
LDA 9000H
MOV B, A
LDA 9001H
UP:
CMP B
JC LAST
SUB B
INR C
JMP UP
LAST:
STA 9002H
MOV A, C
STA 9003H
HLT

Register C is initialized with 00 to store the quotient value. The first 8 bit number is loaded into the accumulator A and is then moved into register B which is the divisor (meaning that the 8 bit number stored in 9000H is the divisor). And the second 8 bit number is then loaded into the accumulator which is the dividend. The content of the accumulator is compared with the contents of B register. If the content of accumulator A is less than content of register B then the carry flag is set. If the carry flag is not set, then the content of accumulator A is subtracted with the content of register B and after subtraction, the quotient (i.e. register C) is incremented by one. Again, the content of the accumulator is compared with the contents of B register and these steps are repeated as long as the content of accumulator A is less than the content of register B. If the carry flag is set, then the instructions:
SUB B
INR C

are skipped and the content of accumulator which is the remainder is stored into memory location 9002H and the content of register C is now moved into accumulator A and is stored into memory location 9003H which is the quotient.

Output:


Example 1

Here, the data in address 9000 i.e. 04 is the divisor. And the data in address 9001H i.e. 12 is the dividend. The data of location 9002H is the remainder and the data in 9003 is the quotient.

Example 2

Here, 15 is the divisor and 03 is the dividend and 03 is the remainder since division cannot take place so 00 is the quotient. 

4 comments: