Skip to main content
Be Bitwise
x86 AssemblyData Movement & Arithmetic

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:

nasm
mov destination, source

MOV accepts a handful of operand shapes:

nasm
; Register to register
mov rax, rbx ; copy rbx into rax
; Immediate to register
mov rax, 42 ; load the constant 42 into rax
mov rcx, 0xDEADBEEF ; load a hex constant
; Memory to register
mov rax, [rbx] ; load 8 bytes from the address in rbx
mov eax, [rsi + 4] ; load 4 bytes from address rsi+4
mov al, [rdi] ; load 1 byte from address rdi
; Register to memory
mov [rbx], rax ; store rax at the address in rbx
mov [rsp + 8], rdx ; store rdx at stack offset +8
mov byte [rdi], 0 ; store a zero byte at address rdi
; Immediate to memory
mov qword [rbx], 0 ; store 8 zero bytes at address rbx
mov dword [rsi], 100 ; store the 32-bit value 100
Critical Rule: No Memory-to-Memory MOV

x86 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:

nasm
; Direct (absolute address -- rare in 64-bit code)
mov rax, [0x400000]
; Register indirect
mov rax, [rbx] ; address is the value in rbx
; Register + offset (displacement)
mov rax, [rbx + 8] ; base + constant offset
mov 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 8

The 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.

nasm
; LEA computes the address, MOV loads the value at the address
lea 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+8

From 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:

nasm
; Multiply rcx by 5 and store in rax
lea rax, [rcx + rcx*4] ; rax = rcx + rcx*4 = rcx*5
; Multiply rax by 3 and add 7
lea rax, [rax + rax*2 + 7] ; rax = rax*3 + 7
; Add two registers and a constant
lea rdx, [rdi + rsi + 1] ; rdx = rdi + rsi + 1
Key Concept: LEA Does Not Touch Flags

Unlike 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:

nasm
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.

Security Note: LEA in Shellcode

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:

nasm
xchg rax, rbx ; swap rax and rbx atomically
xchg [rdi], rax ; swap rax with the value at memory address rdi

When 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.

nasm
; Without XCHG, swapping requires a temporary register:
mov rcx, rax ; temp = a
mov rax, rbx ; a = b
mov rbx, rcx ; b = temp
; XCHG does it in one instruction:
xchg rax, rbx ; swap a and b

A 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:

nasm
; Zero-extend: fill upper bits with zeros (unsigned interpretation)
movzx rax, byte [rdi] ; load 1 byte, zero-extend to 64 bits
movzx 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 bits
movsx eax, word [rsi] ; load 2 bytes, sign-extend to 32 bits
movsxd rax, dword [rdi] ; load 4 bytes, sign-extend to 64 bits

If the byte at [rdi] is 0xFF (255 unsigned, -1 signed):

  • movzx rax, byte [rdi] produces rax = 0x00000000000000FF (255)
  • movsx rax, byte [rdi] produces rax = 0xFFFFFFFFFFFFFFFF (-1)
Security Note: Sign Extension Bugs

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:

nasm
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:

nasm
movsx rax, byte [len]
cmp rax, 64
jg .too_big ; signed: is rax > 64?
; ... copy rax bytes into a 64-byte buffer
Answers
  • (a) 0xFF gives 0x00000000000000FF and 0x80 gives 0x0000000000000080. movzx into eax also clears the top half of rax.
  • (b) 0xFF gives 0xFFFFFFFFFFFFFFFF (-1) and 0x80 gives 0xFFFFFFFFFFFFFF80 (-128).
  • The check passes. As a signed number rax is -128, which isn't greater than 64, so jg falls through. Hand that same register to a copy routine that treats it as a size_t and it asks for 18,446,744,073,709,551,488 bytes. Swap movsx for movzx (the length can only be 0-255) or jg for the unsigned ja and the check catches it.

Summary

InstructionOperationFlags affected?
MOV dst, srcCopy src to dstNo
LEA dst, [addr]Compute address, store in dstNo
XCHG a, bSwap a and bNo
MOVZX dst, srcCopy with zero extensionNo
MOVSX dst, srcCopy with sign extensionNo

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.