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
- Load accumulator with the content of any one location (either 2000 or 4000 or any given location).
- 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).
- Store the content of accumulator to another location (data of 4000H to 2000H).
- 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) |
2000 | 32 |
4000 | 48 |
OUTPUT
Memory Address(H) | Data(H) |
2000 | 48 |
4000 | 32 |
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) |
2000 | 32 |
4000 | 48 |
OUTPUT
Memory Address(H) | Data(H) |
2000 | 48 |
4000 | 32 |
Functions of instructions used in the above program
- LDA 2000H – Load accumulator with content of location 2000H (Direct addressing mode instruction).
- MOV B, A – Copy content of accumulator to register B.
- STA 2000H – Store content of accumulator to location 2000H (Direct addressing mode instruction).
- MOV A,B – Copy content of register B to accumulator
- 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.)
- 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.)
- 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.)
- LXI H 2000H – Load HL register pair with 16-bit value i.e. 2000H.
- LXI D 4000H – Load DE register pair with 16-bit value i.e. 4000H.
- HLT – Terminates the program