MOV, LEA, and XCHG
Data movement dominates every assembly program. Before you can compute a thing, you have to drag data into registers, shuffle it between them, and write results back out to memory. Three instructions cover most of it: MOV, LEA, and XCHG.
MOV - The Universal Data Mover
MOV is the most-issued instruction in x86-64, hands down. The name is a slight lie: it copies data from source to destination. Nothing gets erased on the source side.
The general form:
mov destination, sourceMOV accepts a handful of operand shapes:
; Register to registermov rax, rbx ; copy rbx into rax
; Immediate to registermov rax, 42 ; load the constant 42 into raxmov rcx, 0xDEADBEEF ; load a hex constant
; Memory to registermov rax, [rbx] ; load 8 bytes from the address in rbxmov eax, [rsi + 4] ; load 4 bytes from address rsi+4mov al, [rdi] ; load 1 byte from address rdi
; Register to memorymov [rbx], rax ; store rax at the address in rbxmov [rsp + 8], rdx ; store rdx at stack offset +8mov byte [rdi], 0 ; store a zero byte at address rdi
; Immediate to memorymov qword [rbx], 0 ; store 8 zero bytes at address rbxmov dword [rsi], 100 ; store the 32-bit value 100x86 does not allow mov [addr1], [addr2]. You cannot move data directly from one memory location to another in a single instruction. You must load into a register first, then store: mov rax, [addr1] followed by mov [addr2], rax. This is a hardware limitation - the instruction encoding only supports one memory operand.
Memory Addressing Modes
Square brackets [...] mean "the memory at this address". x86-64 gives you a few addressing modes:
; Direct (absolute address -- rare in 64-bit code)mov rax, [0x400000]
; Register indirectmov rax, [rbx] ; address is the value in rbx
; Register + offset (displacement)mov rax, [rbx + 8] ; base + constant offsetmov rax, [rbp - 16] ; common for local variables on the stack
; Register + register (index)mov rax, [rbx + rcx] ; base + index
; Register + register * scale + offset (full SIB addressing)mov rax, [rbx + rcx*8 + 16] ; base + index*scale + displacement; scale can be 1, 2, 4, or 8The full addressing mode is: [base + index*scale + displacement]
It's powerful because it lines up neatly with everyday data-structure access:
[rbx + 8]- accessing a struct field at offset 8[rbx + rcx*4]- indexing into an array of 32-bit integers[rbx + rcx*8 + 16]- indexing into an array of 64-bit values at an offset within a struct
LEA - Load Effective Address
LEA runs the same addressing-mode arithmetic as MOV, but it doesn't dereference. It hands you back the computed address itself, in the destination register.
; LEA computes the address, MOV loads the value at the addresslea rax, [rbx + rcx*4 + 8] ; rax = rbx + rcx*4 + 8 (the ADDRESS)mov rax, [rbx + rcx*4 + 8] ; rax = value AT address rbx+rcx*4+8From a distance, LEA looks like nothing more than rbx + rcx*4 + 8. That's exactly what it is, which is why compilers love it as a stealthy arithmetic instruction:
; Multiply rcx by 5 and store in raxlea rax, [rcx + rcx*4] ; rax = rcx + rcx*4 = rcx*5
; Multiply rax by 3 and add 7lea rax, [rax + rax*2 + 7] ; rax = rax*3 + 7
; Add two registers and a constantlea rdx, [rdi + rsi + 1] ; rdx = rdi + rsi + 1Unlike ADD and other arithmetic instructions, LEA does not modify the flags register (rflags). This makes it invaluable when you need to compute an address or value without disturbing flags that a subsequent conditional jump depends on. Compilers frequently use LEA for this reason, and you will see it constantly in disassembled code.
LEA for Position-Independent Code
In 64-bit mode, the way to grab a data address is LEA together with RIP-relative addressing:
section .data msg db "Hello", 10 msg_len equ $ - msg
section .text global _start
_start: lea rsi, [rel msg] ; RIP-relative address of msg ; equivalent to: mov rsi, msg (but position-independent)RIP-relative addressing encodes the address as an offset from the current instruction pointer rather than as an absolute. That's what position-independent code (PIC) needs to be loadable at any address — a hard requirement for shared libraries, and the foundation ASLR (Address Space Layout Randomization) is built on.
Shellcode (injected machine code) must be position-independent because the attacker rarely knows the exact memory address where it will land. The classic trick is to use a call/pop sequence or LEA with RIP-relative addressing to discover the shellcode's own location at runtime. Understanding LEA is directly relevant to writing and analyzing shellcode.
XCHG - Atomic Exchange
XCHG swaps the values of two operands:
xchg rax, rbx ; swap rax and rbx atomicallyxchg [rdi], rax ; swap rax with the value at memory address rdiWhen one operand is a memory location, XCHG quietly grabs a lock prefix and makes the swap atomic. No other core can squeeze in between the read and the write.
; Without XCHG, swapping requires a temporary register:mov rcx, rax ; temp = amov rax, rbx ; a = bmov rbx, rcx ; b = temp
; XCHG does it in one instruction:xchg rax, rbx ; swap a and bA wrinkle worth remembering: xchg rax, rax (or any register with itself) is a no-op that some assemblers actually encode as NOP. And the implicit lock on memory operands means XCHG runs slower than the three-instruction swap when you don't need the atomicity.
MOVZX and MOVSX - Extending Moves
Loading a smaller value into a wider register, you pick one of two flavours:
; Zero-extend: fill upper bits with zeros (unsigned interpretation)movzx rax, byte [rdi] ; load 1 byte, zero-extend to 64 bitsmovzx eax, word [rsi] ; load 2 bytes, zero-extend to 32 bits
; Sign-extend: fill upper bits with the sign bit (signed interpretation)movsx rax, byte [rdi] ; load 1 byte, sign-extend to 64 bitsmovsx eax, word [rsi] ; load 2 bytes, sign-extend to 32 bitsmovsxd rax, dword [rdi] ; load 4 bytes, sign-extend to 64 bitsIf the byte at [rdi] is 0xFF (255 unsigned, -1 signed):
movzx rax, byte [rdi]producesrax = 0x00000000000000FF(255)movsx rax, byte [rdi]producesrax = 0xFFFFFFFFFFFFFFFF(-1)
Confusing MOVZX and MOVSX is a real vulnerability class. If a program reads a user-supplied byte as signed and sign-extends it, the value 0x80 (128 unsigned) becomes 0xFFFFFFFFFFFFFF80 (a huge number or -128 signed). This can cause bounds checks to pass when they should fail, leading to buffer overflows. Always be explicit about whether you are treating data as signed or unsigned.
Try it before reading on. The byte at [len] comes from the user. Give rax in hex after each line, for a byte of 0xFF and then 0x80:
movzx eax, byte [len] ; (a)movsx rax, byte [len] ; (b)Now the bounds check that follows the sign-extending load, with a buffer of 64 bytes and a byte of 0x80:
movsx rax, byte [len]cmp rax, 64jg .too_big ; signed: is rax > 64?; ... copy rax bytes into a 64-byte bufferAnswers
- (a)
0xFFgives0x00000000000000FFand0x80gives0x0000000000000080.movzxintoeaxalso clears the top half ofrax. - (b)
0xFFgives0xFFFFFFFFFFFFFFFF(-1) and0x80gives0xFFFFFFFFFFFFFF80(-128). - The check passes. As a signed number
raxis -128, which isn't greater than 64, sojgfalls through. Hand that same register to a copy routine that treats it as asize_tand it asks for 18,446,744,073,709,551,488 bytes. Swapmovsxformovzx(the length can only be 0-255) orjgfor the unsignedjaand the check catches it.
Summary
| Instruction | Operation | Flags affected? |
|---|---|---|
MOV dst, src | Copy src to dst | No |
LEA dst, [addr] | Compute address, store in dst | No |
XCHG a, b | Swap a and b | No |
MOVZX dst, src | Copy with zero extension | No |
MOVSX dst, src | Copy with sign extension | No |
What is Next
Data movement squared away, the next lesson tackles arithmetic: addition, subtraction, multiplication, division, and the quietly treacherous behaviour of signed versus unsigned operations.