Open In App
Related Articles

8085 program to swap two 16 bit numbers using Direct addressing mode

Improve Article
Save Article
Like Article

Problem – Write a program to swap two 16-bit numbers using direct addressing mode where starting address is 2000 and the first 16-bit number is stored at 3000 and the second 16-bit number is stored at 3002 memory address. Example – Algorithm –

  1. Load a 16-bit number from memory 3000 into a register pair (H-L)
  2. Exchange the register pairs
  3. Load a 16-bit number from memory 3002 into a register pair (H-L)
  4. Exchange both the register pairs
  5. Stop

Program –

Memory Mnemonics Operands Comment
2000 LHLD [3000] [H-L] <- [3000]
2003 XCHG   [H-L] [D-E]
2004 LHLD [3002] [H-L] <- [3002]
2007 XCHG   [H-L] [D-E]
2008 HLT   Stop

Explanation – Registers (H-L) pair, (D-E) pair are used for general purpose.

  1. LHLD is used to load register pair H-L direct using 16-bit address (3 Byte instruction)
  2. XCHG is used to exchange the data of both the register pair (H-L), (D-E) (1 Byte instruction)
  3. HLT is used to halt the program.


  • The program is a useful utility program for swapping two 16-bit numbers using direct addressing mode.
  • It is relatively simple and easy to understand, making it useful for teaching purposes.
  • It can be customized to swap numbers at different memory locations by modifying the source and destination addresses.


  • The program is not optimized for speed, as it uses multiple instructions to move each byte of data.
  • It requires separate code blocks for each number to swap, which can be cumbersome for large data sets.
  • The program does not check for errors or boundary conditions, such as when the source or destination address is outside the valid memory range or when the data size is not a multiple of 16 bits.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 09 Apr, 2023
Like Article
Save Article
Similar Reads