Program to Subtract two 8-bit numbers with the borrow

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

Flowchart/Algorithm

Program

Address Mnemonics Operand Opcode Comments
2000 LXI H, 3000H 21 Load H-L pair with address 3000H.
2001 00
2002 30
2003 MOV A, M 7E Move the 1st operand from memory to reg. A.
2004 INX H 23 Increment H-L pair.
2005 MOV B, M 46 Move the 2nd operand from memory to reg. B.
2006 MVI C, 00H 0E Initialize reg. C with 00H.
2007 00
2008 SUB B 90 Subtract B from A.
2009 JNC 200D D2 Jump to address 200DH if there is no borrow.
200A 0D
200B 20
200C INR C 0C Increment reg. C.
200D INX H 23 Increment H-L pair.
200E MOV M, A 77 Move the result from reg. A to memory.
200F INX H 23 Increment H-L pair.
2010 MOV M, C 71 Move borrow from reg. C to memory.
2011 HLT 76 Halt

Output

Before Execution:

3000H: 05H
3001H: 02H

After Execution:

3002H: 03H
3003H: 00H

Program Explanation

  1. This program subtracts two operands stored in memory location 3000H and 3001H, along with considering the borrow took (if any).
  2. Let us assume that the operands stored at memory location 3000H is 05H and 3001H is 02H.
  3. Initially, H-L pair is loaded with the address of first memory location.
  4. The first operand is moved to accumulator from memory location 3000H and H-L pair is incremented to point to next memory location.
  5. The second operand is moved to register B from memory location 3001H.
  6. Register C is initialized to 00H. It stores the borrow (if any).
  7. The two operands stored in register A and B are subtracted and the result is stored in the accumulator.
  8. Then, carry flag is checked for borrow. If there is a borrow, C register is incremented.
  9. H-L pair is incremented and the result is moved from the accumulator to memory location 3002H.
  10. H-L pair is again incremented and borrow (either 0 or 1) is moved from register C to memory location 3003H.

4 thoughts on “Program to Subtract two 8-bit numbers with the borrow”

    • First HL increment is used for calling second number. Last 2 HL increments used to store results i.e. one for subtraction result at address 3002H and other for borrow at 3003H.

      Reply

Leave a Comment

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