8085 program to swap two 8-bit numbers

In this post, we will write one 8085 program to swap two 8-bit numbers. We will take a problem statement first and then find out the solution methods, algorithm and then will write the 8085 assembly programs to solve the problem.

Program Statement: Swap the contents of memory locations 2000H and 4000H using 8085 assembly language.

Solution methods:

To solve this problem two different methods are used. In the first method (program) direct addressing instructions are used. In the second method indirect addressing instructions are used.

Algorithm to swap two 8-bit numbers

  1. Load accumulator with the content of any one location (either 2000 or 4000 or any given location).
  2. Move the contents of accumulator to any register (say B) so that another location’s content can be loaded to accumulator and the previous data of accumulator get saved in register (say register B).
  3. Store the content of accumulator to another location (data of 4000H to 2000H).
  4. Load accumulator with content of register (here register B) and then store it to another address location (here address 4000H).

8085 program to swap two 8-bit numbers using direct addressing instructions

LDA 2000H // Get the contents of memory location 2000H into accumulator
MOV B, A // Save the contents into B register
LDA 4000H // Get the contents of memory location 4000Hinto accumulator
STA 2000H // Store the contents of accumulator at address 2000H
MOV A, B // Get the saved contents back into A register
STA 4000H // Store the contents of accumulator at address 4000H

INPUT

Memory Address(H)Data(H)
200032
400048

OUTPUT

Memory Address(H)Data(H)
200048
400032

8085 program to swap two 8-bit numbers using indirect addressing instructions

LXI H 2000H // Initialize HL register pair as a pointer to memory location 2000H.
LXI D 4000H // Initialize DE register pair as a pointer to memory location 4000H.
MOV B, M // Get the contents of memory location 2000H into B register.
LDAX D // Get the contents of memory location 4000H into A register.
MOV M, A // Store the contents of A register into memory location 2000H.
MOV A, B // Copy the contents of B register into accumulator.
STAX D // Store the contents of A register into memory location 4000H.
HLT // Terminate program execution.

INPUT

Memory Address(H)Data(H)
200032
400048

OUTPUT

Memory Address(H)Data(H)
200048
400032

Functions of instructions used in the above program

  1. LDA 2000H – Load accumulator with content of location 2000H (Direct addressing mode instruction).
  2. MOV B, A – Copy content of accumulator to register B.
  3. STA 2000H – Store content of accumulator to location 2000H (Direct addressing mode instruction).
  4. MOV A,B – Copy content of register B to accumulator
  5. LDAX D – Get the contents of memory location 4000H into A register (address 4000H is specified by the register pair DE. So it is an indirect addressing mode instruction.)
  6. STAX D – Store content of accumulator to location 4000H (address 4000H is specified by the register pair DE. So it is an indirect addressing mode instruction.)
  7. MOV M, A – Store the contents of A register into memory location 2000H (address 2000H is specified by the register pair HL. So it is an indirect addressing mode instruction.)
  8. LXI H 2000H – Load HL register pair with 16-bit value i.e. 2000H.
  9. LXI D 4000H – Load DE register pair with 16-bit value i.e. 4000H.
  10. HLT – Terminates the program
8085 program to swap two 8-bit numbers
Scroll to top
error: physicsTeacher.in