mov & Addressing Modes
mov is the most common instruction in almost every program. It copies a value from a source to a destination: mov dest, src. The source is unchanged, flags are not affected, and both operands must have the same size.
The forms of mov
mov rax, 42 ; immediate -> register
mov rax, rbx ; register -> register
mov rax, [value] ; memory -> register (load)
mov [value], rax ; register -> memory (store)
mov qword [value], 42 ; immediate -> memory (size needed)What is not possible: memory to memory (mov [a], [b]), mixing sizes (mov rax, ebx) and writing an immediate into a segment or the instruction pointer.
Addressing modes
The part in square brackets is the effective address. x86-64 lets you combine up to four parts:
[ base + index * scale + displacement ]
| | | |
register register 1,2,4,8 constant (or label)| Mode | Example | Typical use |
|---|---|---|
| direct | [total] |
a global variable |
| register indirect | [rbx] |
a pointer |
| base + displacement | [rbx + 16] |
a field in a structure |
| base + index × scale | [rbx + rcx*8] |
element rcx of an array of 8-byte items |
| everything | [rbx + rcx*4 + 12] |
a field inside an array of structures |
| RIP-relative | [rel total] |
a global, encoded relative to the instruction |
section .data
primes: dq 2, 3, 5, 7, 11, 13
section .text
global _start
_start:
mov rdi, [primes] ; direct: element 0
call print_uint
mov rbx, primes ; rbx = address of the array (a pointer)
mov rdi, [rbx + 8] ; base + displacement: element 1
call print_uint
mov ecx, 4
mov rdi, [rbx + rcx*8] ; base + index*scale: element 4
call print_uint
mov ecx, 2
mov rdi, [rbx + rcx*8 + 8] ; all parts: element 2 + 1 = 3
call print_uint
mov eax, 60
xor edi, edi
syscall
; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
mov rax, rdi ; rax = the number to convert
sub rsp, 32 ; room for up to 20 digits and a newline
lea rsi, [rsp + 31] ; fill the buffer from its end
mov byte [rsi], 10 ; the newline is the last character
mov ecx, 10
.next_digit:
xor edx, edx ; the dividend is rdx:rax, so clear rdx
div rcx ; rax = quotient, rdx = remainder (0..9)
add dl, '0' ; remainder -> ASCII digit
dec rsi
mov [rsi], dl ; store it in front of the previous one
test rax, rax
jnz .next_digit ; repeat until the quotient is 0
lea rdx, [rsp + 32]
sub rdx, rsi ; rdx = number of bytes to print
mov eax, 1 ; write(1, rsi, rdx)
mov edi, 1
syscall
add rsp, 32
ret2
3
11
7
rbx was chosen as the pointer because print_uint (like any function following the standard convention) does not change it. rcx is changed by print_uint, so we set it again before each use.
Loads and stores of different sizes
The register name determines how many bytes move. To load a small value into a big register, choose how the upper bits are filled:
| Instruction | Effect |
|---|---|
mov al, [x] |
load 1 byte, the rest of rax unchanged |
movzx eax, byte [x] |
load 1 byte, zero-extend (unsigned) |
movsx rax, byte [x] |
load 1 byte, sign-extend (signed) |
movsx rax, word [x] |
load 2 bytes, sign-extend |
movsxd rax, dword [x] |
load 4 bytes, sign-extend to 64 bits |
mov eax, [x] |
load 4 bytes, zero-extend (the 32-bit rule) |
section .data
temperature: db -12 ; stored as 0xF4
delta: dd -300000
section .text
global _start
_start:
movzx edi, byte [temperature] ; as unsigned: 244
call print_int
movsx rdi, byte [temperature] ; as signed: -12
call print_int
mov edi, [delta] ; 32-bit load, zero-extended: wrong for negatives
call print_int
movsxd rdi, dword [delta] ; sign-extended: right
call print_int
mov eax, 60
xor edi, edi
syscall
; ---- print_int: print rdi as a signed decimal number and a newline ----
print_int:
test rdi, rdi
jns print_uint ; not negative: print it as it is
neg rdi ; make it positive
push rdi ; keep it (write and syscall clobber registers)
push '-' ; the minus sign, as a byte on the stack
mov eax, 1 ; write(1, rsp, 1)
mov edi, 1
mov rsi, rsp
mov edx, 1
syscall
pop rdi ; drop the '-'
pop rdi ; the positive value again
jmp print_uint ; print_uint's ret returns to our caller
; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
mov rax, rdi ; rax = the number to convert
sub rsp, 32 ; room for up to 20 digits and a newline
lea rsi, [rsp + 31] ; fill the buffer from its end
mov byte [rsi], 10 ; the newline is the last character
mov ecx, 10
.next_digit:
xor edx, edx ; the dividend is rdx:rax, so clear rdx
div rcx ; rax = quotient, rdx = remainder (0..9)
add dl, '0' ; remainder -> ASCII digit
dec rsi
mov [rsi], dl ; store it in front of the previous one
test rax, rax
jnz .next_digit ; repeat until the quotient is 0
lea rdx, [rsp + 32]
sub rdx, rsi ; rdx = number of bytes to print
mov eax, 1 ; write(1, rsi, rdx)
mov edi, 1
syscall
add rsp, 32
ret244
-12
4294667296
-300000
Swapping with xchg
xchg swaps two operands. With a memory operand it is also an atomic operation, which is used for locks in multithreaded code.
section .text
global _start
_start:
mov r12, 111
mov r13, 222
xchg r12, r13 ; swap the two registers
mov rdi, r12
call print_uint
mov rdi, r13
call print_uint
mov eax, 60
xor edi, edi
syscall
; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
mov rax, rdi ; rax = the number to convert
sub rsp, 32 ; room for up to 20 digits and a newline
lea rsi, [rsp + 31] ; fill the buffer from its end
mov byte [rsi], 10 ; the newline is the last character
mov ecx, 10
.next_digit:
xor edx, edx ; the dividend is rdx:rax, so clear rdx
div rcx ; rax = quotient, rdx = remainder (0..9)
add dl, '0' ; remainder -> ASCII digit
dec rsi
mov [rsi], dl ; store it in front of the previous one
test rax, rax
jnz .next_digit ; repeat until the quotient is 0
lea rdx, [rsp + 32]
sub rdx, rsi ; rdx = number of bytes to print
mov eax, 1 ; write(1, rsi, rdx)
mov edi, 1
syscall
add rsp, 32
ret222
111
Absolute vs RIP-relative addresses
mov rax, [total] encodes the absolute 32-bit address of total inside the instruction. That works for the simple static executables built in this course, whose code and data live at low addresses like 0x402000. Position-independent executables (PIE) — the default for gcc on modern Linux — and shared libraries can be loaded anywhere, so they address globals relative to rip:
mov rax, [rel total] ; RIP-relative: works anywhere
lea rsi, [rel message] ; address of message, RIP-relative
default rel ; or: make [label] RIP-relative for the whole fileRIP-relative addressing cannot be combined with an index register, so for arrays you first load the base address into a register (lea rbx, [rel primes]) and then index from it.
mov with 64-bit immediates
Most instructions accept at most a 32-bit immediate (sign-extended to 64 bits). mov is special: mov rax, 0x1122334455667788 takes a full 64-bit constant. NASM automatically picks the shortest encoding that gives the same result, so mov rax, 1 assembles as the 5-byte mov eax, 1.
Test yourself
Which address does [rbx + rcx*8 + 16] refer to when rbx = 1000 and rcx = 2?
- 1016
- 1032
- 1018
- 8016
How do you load a signed byte into rax so that −5 stays −5?
mov al, [x]movzx rax, byte [x]movsx rax, byte [x]mov rax, [x]
Which scale factors are allowed for the index register?
- Any number
- 1, 2, 4 or 8
- Only powers of 10
- 1 and 16