Open In App

8085 program to subtract two 16-bit numbers with or without borrow

Improve
Improve
Like Article
Like
Save
Share
Report

Problem – Write an assembly language program in 8085 microprocessor to subtract two 16 bit numbers. Assumption –

  • Starting address of program: 2000
  • Input memory location: 2050, 2051, 2052, 2053
  • Output memory location: 2054, 2055

Example –

INPUT:
       (2050H) = 19H
       (2051H) = 6AH 
       (2052H) = 15H 
       (2053H) = 5CH

OUTPUT:
        (2054H) = 04H
        (2055H) = OEH

RESULT: Hence we have subtracted two 16 bit numbers. Algorithm –

  1. Get the LSB in L register and MSB in H register of 16 Bit number.
  2. Exchange the content of HL register with DE register.
  3. Again Get the LSB in L register and MSB in H register of 16 Bit number.
  4. Subtract the content of L register from the content of E register.
  5. Subtract the content of H register from the content of D register and borrow from previous step.
  6. Store the result in memory location.

Program –

MEMORY ADDRESS MNEMONICS COMMENTS
2000 LHLD 2050 Load H-L pair with address 2050
2003 XCHG EXCHANGE H-L PAIR WITH D-E PAIR
2004 LHLD 2052 Load H-L pair with address 2052
2007 MVI C, 00 C<-00H
2009 MOV A, E A<-E
200A SUB L A<-A-L
200B STA 2054 2054<-A
200E MOV A, D A<-D
200F SBB H SUBTRACT WITH BORROW
2010 STA 2055 2055<-A
2013 HLT TERMINATES THE PROGRAM

Explanation –

  1. LHLD 2050: load HL pair with address 2050.
  2. XCHG: exchange the content of HL pair with DE.
  3. LHLD 2052: load HL pair with address 2050.
  4. MOV A, E: move the content of register E to A.
  5. SUB L: subtract the content of A with the content of register L.
  6. STA 2054: store the result from accumulator to memory address 2054.
  7. MOV A, D: move the content of register D to A.
  8. SBB H: subtract the content of A with the content of register H with borrow.
  9. STA 2055: store the result from accumulator to memory address 2055.
  10. HLT: stops executing the program and halts any further execution.

Advantages:

  • This program can subtract two 16-bit numbers with or without borrow, making it flexible for different use cases.
     
  • The use of SBB instruction allows for efficient subtraction with borrow, reducing the need for additional calculations.
     
  • The program is simple and easy to understand, making it accessible for programmers with varying levels of experience.
     

Disadvantages:

  • This program does not handle cases where the result of the subtraction operation is negative, as the 8085 architecture does not have a dedicated negative flag.
     
  • The program does not handle overflow situations, where the result of the subtraction operation is larger than 16 bits.
     
  • The program assumes that the memory locations of the 16-bit numbers are known and fixed, which may not always be the case in a dynamic computing environment.

Last Updated : 11 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads