Wednesday, October 14, 2015

8085 Program To Subtract Two 8 Bit Numbers

Let us suppose that we store the two 8 bit numbers that are to be subtracted in the memory location 9000H and 9001H. Now the difference of the numbers is to be stored in 9100H and borrow, if present, is to be stored in 9101H.

Algorithm:

  1. Start
  2. Initialize one register (suppose C) to 00 to store the borrow value.
  3. Load HL register pair with content of memory location 9000H.
  4. Move the content of memory into accumulator.
  5. Increment the register pair HL.
  6. Move the content of memory into register B.
  7. Subtract the content of register B from the content of accumulator and store the result in accumulator.
  8. Since the carry flag acts as the borrow flag during subtraction so if carry is present go to step 9 else go to step 10
  9. Increment register C by 1 which is borrow.
  10. Store the contents of the accumulator (difference value) in memory location 9100H.
  11. Move the content of register C into accumulator.
  12. Store the contents of the accumulator (carry value) into memory location 9101H.
  13. Terminate the program.

Program Code:

MVI C, 00
HLXI H, 9000H
MOV A, M
INX H
MOV B, M
SUB B
JNC DOWN
INR C
DOWN:
STA 9100H
MOV A, C
STA 9101H
HLT


The register C is initialized with value 00 at the beginning to store the value of borrow. Register pair HL is loaded with the contents of memory location 9000H. The content of memory is moved into accumulator meaning that the content of accumulator is the minuend. The register pair HL in then incremented. The content of memory is moved into register B meaning that the content of register B is the subtrahend. Now the content of register B is subtracted from the accumulator and then stored in the accumulator. Since the carry flag acts as the borrow flag during subtraction so the status of the carry flag is now checked. If carry(borrow) in present register C is incremented by 1 but if no carry(borrow) is present then the statement
INR C
is skipped and the content of accumulator is stored in memory location 9100H which is the difference value. The content of register C is moved to accumulator. The content of the accumulator is now stored into memory location 9101H which is the borrow value.

Output:


Example: 1
Here, 05 is the first 8 bit number which is the minuend (i.e. the number to be subtracted from) and 03 is the second 8 bit number which is the subtrahend (i.e. the number to be subtracted). Here,
 05
-03
-----
 02

So the difference value (i.e. 2) is stored in memory location 9100H and since borrowing does not take place so the value of borrow is 0 and is stored in memory location 9101H.


Example: 2
Here, 03 is the first 8 bit number which is the minuend (i.e. the number to be subtracted from) and 05 is the second 8 bit number which is the subtrahend (i.e. the number to be subtracted). Here,
  03
-05
----
1FE

Here, FE is the difference value so is stored in memory location 9100H and a 1 is borrowed for the subtraction so 01 is stored in memory location 9101H.

2 comments: