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

• Difficulty Level : Easy
• Last Updated : 14 Aug, 2018

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

Assumption –

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

• 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 –

2003XCHGEXCHANGE H-L PAIR WITH D-E PAIR
2007MVI C, 00C<-00H
2009MOV A, EA<-E
200ASUB LA<-A-L
200BSTA 20542054<-A
200EMOV A, DA<-D
200FSBB HSUBTRACT WITH BORROW
2010STA 20552055<-A
2013HLTTERMINATES THE PROGRAM

Explanation –