Program to Subtract two 16-bit numbers without borrow

Q. Write an 8085 program and draw a flowchart to Subtract two 16-bit numbers without considering the borrow.(8085 Microprocessor Program)

Flowchart/Algorithm

Program

Address Mnemonics Operand Opcode Comments
2000 LHLD 3000H 2A Load H-L pair with 1st operand from 3000H.
2001 00
2002 30
2003 XCHG EB Exchange H-L pair with D-E pair.
2004 LHLD 3002H 2A Load H-L pair with 2nd operand from 3002H.
2005 02
2006 30
2007 MOV A, E 7B Move the lower-order of 1st number from reg. E to reg. A.
2008 SUB L 95 Subtract the lower-order of 2nd number from lower-order of 1st number.
2009 MOV L, A 6F Move the result from reg. A to register L.
200A MOV A, D 7A Move the higher-order of 1st number from reg. D to reg. A.
200B SBB H 9C Subtract the higher-order of 2nd number from higher-order of 1st number with borrow from the previous subtraction.
200C MOV H, A 67 Move the result from reg. A to reg. H.
200D SHLD 3004H 22 Store the 16-bit result from H-L pair to memory.
200E 04
200F 30
2010 HLT 76 Halt

Output

Before Execution:

3000H: 08H
3001H: 06H
3002H: 05H
3003H: 04H

After Execution:

3004H: 03H
3005H: 02H

Program Explanation

  1. This program subtracts two 16-bit operands stored in memory locations 3000H-3001H and 3002H-3003H, without considering the borrow taken (if any).
  2. Let us assume that the operands stored at memory locations 3000H-3001H is 08H-06H and 3002H-3003H is 05H-04H.
  3. The H-L pair is loaded with the first 16-bit operand 0806H from memory locations 3000H- 3001H.
  4. Then, the first 16-bit operand is moved to D-E pair.
  5. The second 16-bit operand 0504H is loaded to H-L pair from memory locations 3002H- 3003H.
  6. The lower-order of the first number is moved from register E to accumulator.
  7. The lower-order of 2nd number in register L is subtracted from lower-order of 1st number in the accumulator.
  8. The result of the subtraction is moved from the accumulator to register L.
  9. Then, the higher-order of 1st number is moved from register D to accumulator.
  10. The higher-order of 2nd number in register H is subtracted from higher-order of the first number in the accumulator, along with the borrow from the previous subtraction.
  11. The result of the subtraction is moved from the accumulator to register H.
  12. Now, the final result is in H-L pair.
  13. The result is stored from H-L pair to memory locations 3004H-3005H.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.