MOV Immediate
Part of the Fundamentals section of Coddy's Assembly journey — lesson 15 of 45.
The simplest form of MOV is MOV immediate. "Immediate" means the value is written directly in the instruction itself.
Syntax:
mov register, valueThe value is copied into the register.
Examples:
| Instruction | What it does |
|---|---|
mov rax, 5 | Copies the number 5 into rax |
mov rbx, 100 | Copies the number 100 into rbx |
mov rcx, 0 | Copies the number 0 into rcx |
This code does nothing visible (no output yet), but it successfully puts numbers into registers.
Note: You will see the effect of these MOV instructions in later lessons when you learn to print or debug. For now, just know that the numbers are stored in the registers.
Important rules:
| Rule | Explanation |
|---|---|
| The value must fit in the register | rax can hold very large numbers (up to 2⁶⁴-1) |
| The value is copied, not moved | The original number still exists (it is just a number) |
| Registers on the left | mov rax, 5 is correct. mov 5, rax is wrong |
Challenge
Write one line of assembly code for each task:
- Put the number 5 into the
raxregister. - Put the number 7 into the
rbxregister. - Put the number 3 into the
rcxregister.
Try it yourself
section .data
msg_rax db 'rax = '
msg_rax_len equ $ - msg_rax
msg_rbx db 'rbx = '
msg_rbx_len equ $ - msg_rbx
msg_rcx db 'rcx = '
msg_rcx_len equ $ - msg_rcx
section .bss
digit resb 1
section .text
global _start
_start:
%include "solution.asm"
; Save values
mov r12, rax
mov r13, rbx
mov r14, rcx
; Print "rax = "
mov rax, 1
mov rdi, 1
mov rsi, msg_rax
mov rdx, msg_rax_len
syscall
; Print rax value
mov rax, r12
add rax, 48
mov [digit], al
mov rax, 1
mov rdi, 1
mov rsi, digit
mov rdx, 1
syscall
; Print "rbx = "
mov rax, 1
mov rdi, 1
mov rsi, msg_rbx
mov rdx, msg_rbx_len
syscall
; Print rbx value
mov rax, r13
add rax, 48
mov [digit], al
mov rax, 1
mov rdi, 1
mov rsi, digit
mov rdx, 1
syscall
; Print "rcx = "
mov rax, 1
mov rdi, 1
mov rsi, msg_rcx
mov rdx, msg_rcx_len
syscall
; Print rcx value
mov rax, r14
add rax, 48
mov [digit], al
mov rax, 1
mov rdi, 1
mov rsi, digit
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 Architecture