Problem: Write an assembly language program to swap two 8-bit numbers stored in an 8085 microprocessor.
Assumption: Suppose there are two 8-bit numbers. One 8-bit number is stored at location 2500 memory address and another is stored at location 2501 memory address. Let 05 be stored at location 2500 and 06 be stored at location 2501 (not necessarily, can be any two 8-bit numbers).
Example –

Algorithm –
- Load accumulator with the content of anyone location (either 2500 or 2501 or any given location).
- Move the contents of the accumulator to any register (say B) so that another location’s content can be loaded to the accumulator and the previous data of the accumulator gets saved in the register.
- Store the content of the accumulator in another location (data of 2501 to 2500).
- Load accumulator with the content of register and then store it to another address location.
Program –
Address |
Mnemonics |
Comments |
2000 |
LDA 2500 |
A<-[2500] |
2003 |
MOV B, A |
B<-A |
2004 |
LDA 2501 |
A<-[2501] |
2007 |
STA 2500 |
2500<-[A] |
200A |
MOV A, B |
A<-B |
200B |
STA 2501 |
2501<-[A] |
200E |
HLT |
Terminates the program |
Explanation –
- LDA 2500 – Load accumulator with the content of location 2500
- MOV B, A – Copy content of accumulator to register B
- LDA 2501 – Load accumulator with the content of location 2501
- STA 2500 – Store content of accumulator to location 2500
- MOV A, B – Copy content of register B to accumulator
- STA 2501 – Store content of accumulator to location 2501
- HLT – Terminates the program
Approach: 2 Using XRA Instruction :
We can use the XOR operation to swap 2 numbers
without using extra variables we know that we can swap x and y by using :
x = x ^ y
y = x ^ y
x = x ^ y
In the same way we can use registers to find solutions for the same. Implementation is provided below :
Address |
Mnemonics |
Comments/ Explanation |
2000 |
LXI H,2050H |
Load HL register pair as 2050H Which will acts as Memory Pointer. |
2003 |
MOV A, M |
Move content of [2050H] memory location into Accumulator A. |
2004 |
INX H |
Increment memory pointer by 1 . So [HL] = 2051H, For fetching next data. |
2005 |
MOV C, M |
Again move the content of memory pointer 2051H into Register C. So we have two data registers A and C |
2006 |
XRA C |
XOR A and C i.e. { A <—-A ^ C} |
2007 |
MOV D, A |
Move the value of the XORed Accumulator in register D for further calculation. |
2008 |
XRA C |
Again do XOR operation with C register. Please note that here content of the accumulator is copied in the D register it does not vanish from A. |
2009 |
MOV E, A |
Move content of 2nd time XORed Accumulator in E register. which will acts as { y <—– x ^ y} here y = Register E. |
200A |
XRA D |
Again XOR Accumulator with D register. D register contains the value of the 1st XOR operation which is the x variable in general programming. |
200B |
STA 2050H |
Accumulator now contains the value of C register which was initially the 2nd value in the memory and store it in 2050H location. |
200E |
MOV A, E |
Now move Register E value into Accumulator A, Which will be the value of the original accumulator which was on location 2050H initially. |
200F |
STA 2051H |
Store accumulator value of swapped data in memory location 2051H. Hence our swapping operation is done successfully. |
2012 |
HLT |
Halt/Stop program execution |
Initial Code + Location Counter as : 2050H and 2051H . [2050H] = 4C and [2051H] =3E

Before Execution Data
Code after Assembling and Execution:
Swapping is done and location counters sets as [2050H] =3E and [2051H] = 4C

After execution Swapped Data
In this way, with the use of XOR instruction available in the 8085 microprocessor, we can swap two 8-Bit numbers.
Approach : 3>> Using XCHG instruction :
Address |
Mnemonics |
Comments/Explanation |
2000 |
LDA 2500H |
Load accumulator from memory location 2500H |
2003 |
MOV H, A |
Move loaded accumulator data into register H |
2004 |
LDA 2501H |
Now move one location further and fetch data from the 2501H memory location into Accumulator |
2007 |
MOV D, A |
Again move Accumulator data into register D. Now we have two 8-bit data into registers H and D. |
2008 |
XCHG |
XCHG instruction will exchange contents of HL register pair with DE Register pair. |
2009 |
MOV A, H |
Now because of exchange, we have H <- D and D <- H. So now move the H register value in Accumulator A. |
200A |
STA 2500H |
Store value of Accumulator in location 2500H which previously contained original H register value but now it contains the D register value. |
200D |
MOV A, D |
Again move the contents of the D register into Accumulator A. This will now contain exchanged value of the H register. |
200E |
STA 2501H |
Store exchanged Accumulator value at location 2501H. which will now have exchanged value of the H register. So the swapping process is done. |
2011 |
HLT |
Halt/ Stop Program Execution |
- Before Exchange (Input Data Phase) : [2500H] = 1F , [2501H] = 9E
- After Assembled and loaded program : Exchanged Data : [2500H] = 9E , [2501H] = 1F
Hence, In this way, we can swap any two 8-bit data using the above 3 approaches and by using different instructions.
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 :
02 Jun, 2022
Like Article
Save Article