Practice: Swap Registers
Part of the Fundamentals section of Coddy's Assembly journey — lesson 21 of 45.
Sometimes you need to swap the values between two registers. For example, if rax contains 5 and rbx contains 10, after swapping rax should be 10 and rbx should be 5.
You cannot do this with one or two MOV instructions. You need a third register as temporary storage.
Why you cannot do it directly:
; This does NOT work
mov rax, rbx ; Now rax = 10, but the original 5 is gone forever
mov rbx, rax ; Now rbx also = 10 (not 5)The correct method — using a temporary register:
mov rcx, rax ; Step 1: Save original rax into rcx (rcx = 5)
mov rax, rbx ; Step 2: Copy rbx into rax (rax = 10)
mov rbx, rcx ; Step 3: Copy saved value from rcx into rbx (rbx = 5)Step by step:
| Step | Instruction | rax | rbx | rcx |
|---|---|---|---|---|
| Start | (before any code) | 5 | 10 | ? |
| 1 | mov rcx, rax | 5 | 10 | 5 |
| 2 | mov rax, rbx | 10 | 10 | 5 |
| 3 | mov rbx, rcx | 10 | 5 | 5 |
After these three instructions, the values are swapped. The temporary register rcx still contains the original value (5), but that is fine.
The temporary register can be any register you are not using:
- You can use
rcx,rdx,r8, or any available register - Just make sure you do not need the original value in that register
Why this matters:
Swapping values is a common operation in sorting algorithms and data manipulation. It teaches you to think carefully about the order of MOV instructions.
Challenge
Write three lines of assembly to swap the values between r8 and r9.
Initial values (provided by test harness):
r8starts with 6r9starts with 2
After your code:
r8should be 2r9should be 6
Use r10 as your temporary register.
Try it yourself
section .data
msg_r8 db 'r8 = '
msg_r8_len equ $ - msg_r8
msg_r9 db 'r9 = '
msg_r9_len equ $ - msg_r9
newline db 0xa
section .bss
digit resb 1
section .text
global _start
_start:
; Set initial values
mov r8, 6
mov r9, 2
%include "solution.asm" ; Student's swap code goes here
; Save values for printing
mov r12, r8
mov r13, r9
; Print "r8 = "
mov rax, 1
mov rdi, 1
mov rsi, msg_r8
mov rdx, msg_r8_len
syscall
; Print r8 value
mov rax, r12
add rax, 48
mov [digit], al
mov rax, 1
mov rdi, 1
mov rsi, digit
mov rdx, 1
syscall
; Print newline
mov rax, 1
mov rdi, 1
mov rsi, newline
mov rdx, 1
syscall
; Print "r9 = "
mov rax, 1
mov rdi, 1
mov rsi, msg_r9
mov rdx, msg_r9_len
syscall
; Print r9 value
mov rax, r13
add rax, 48
mov [digit], al
mov rax, 1
mov rdi, 1
mov rsi, digit
mov rdx, 1
syscall
; Print newline
mov rax, 1
mov rdi, 1
mov rsi, newline
mov rdx, 1
syscall
; Exit
mov rax, 60
mov rdi, 0
syscallThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
1The Machine
What is CPUMemoryRegisters vs MemoryHow instructions workTwo-File PatternThe x86-64 Architecture3First instruction - MOV
What is MOV?MOV ImmediateMOV Register to RegisterMOV Memory to RegisterMOV Register to MemoryLEA - Load AddressMOV with Different SizesPractice: Swap RegistersRecap: MOV Challenge