Swap Two 8-bit numbers
Write an 8085 assembly program that swaps the values stored at two memory locations. After execution, the value at the first address should move to the second, and vice versa.
-
Input:
2500H
= first 8-bit number2501H
= second 8-bit number
-
Output:
2500H
= value originally at 2501H2501H
= value originally at 2500H
This program should perform an in-place swap using only registers — no additional memory locations should be used.
⚡ TL;DR — Final Working Code
Section titled “⚡ TL;DR — Final Working Code” ; Define memory addresses FIRST_ADDR EQU 2500H SECOND_ADDR EQU 2501H
; Step 1: Load values into registers LXI H, FIRST_ADDR MOV A, M ; A ← value at 2500H INX H MOV B, M ; B ← value at 2501H
; Step 2: Write swapped values back DCX H ; HL → back to 2500H MOV M, B ; 2500H ← B INX H MOV M, A ; 2501H ← A
HLT
🧱 Step 1: Define the Interface
Section titled “🧱 Step 1: Define the Interface”We need a clear contract for where the two numbers are read from and where they’ll return after swapping. Let’s assume:
Address | Role |
---|---|
2500H | First byte input |
2501H | Second byte input |
2500H | First byte output (swapped) |
2501H | Second byte output (swapped) |
This means the program should exchange the two memory values in place. A clean interface like this ensures the swap is fully reversible and testable.
🧱 Step 2: Load the Values into Registers
Section titled “🧱 Step 2: Load the Values into Registers”To perform the swap, we need both values in registers so we can manipulate them safely without overwriting either one prematurely.
🧠 What We’re Doing
Section titled “🧠 What We’re Doing”- Use the
HL
register pair to point to the first memory address (2500H
) - Load the first value into register
A
- Increment
HL
to point to the second value (2501H
) - Load the second value into register
B
This setup gives us:
A
→ value at2500H
B
→ value at2501H
🧾 Code (Load Phase Only)
Section titled “🧾 Code (Load Phase Only)”LXI H, 2500H ; HL → first byteMOV A, M ; A ← value at 2500HINX H ; HL → 2501HMOV B, M ; B ← value at 2501HHLT
🧪 Manual Test
Section titled “🧪 Manual Test”Before running, set memory:
2500H = 3CH2501H = 91H
Expected after halt:
- Register A =
3C
- Register B =
91
These values are now ready to be written back in the reverse order in the next step.
🧱 Step 3: Write the Swapped Values Back to Memory
Section titled “🧱 Step 3: Write the Swapped Values Back to Memory”Now that the two values are safely loaded into registers, we’ll write them back to memory — but in reversed order.
🧠 What We’re Doing
Section titled “🧠 What We’re Doing”- Use the
HL
register pair again to point back to2500H
- Store the value in
B
(originally from2501H
) into2500H
- Increment
HL
to2501H
- Store the value in
A
(originally from2500H
) into2501H
This completes the swap directly in memory.
🧾 Code (Store Phase Only)
Section titled “🧾 Code (Store Phase Only)”LXI H, 2500H ; HL → 2500HMOV M, B ; 2500H ← B (originally second value)INX H ; HL → 2501HMOV M, A ; 2501H ← A (originally first value)HLT
🧪 Manual Test
Section titled “🧪 Manual Test”Initial memory:
2500H = 3CH2501H = 91H
Expected memory after program halts:
2500H = 91H2501H = 3CH
This confirms that the swap was successful. No temporary memory location is needed because we used registers to hold both values. Let me know if you’d like to proceed to TL;DR and summary.
📚 Summary
Section titled “📚 Summary”This program demonstrates a basic and clean technique to swap two memory values using registers:
- Data is read into registers
A
andB
to avoid overwriting. - Values are written back in reverse order using the same memory locations.
- The program uses no extra memory — just internal registers.
This simple logic is widely used in sorting, data transformation, and register exchange operations. It also serves as a good exercise in register and memory pointer management in 8085 assembly.