Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 40 ยท Core Instructions

MOV, LEA and Moving Data

Master mov, lea, movzx, movsx and xchg in x86-64: immediate, register and memory operands, effective addresses, and why lea is really arithmetic.

If you open any compiled program and count instructions, mov wins by a wide margin. Programs spend most of their time moving values: from memory into registers, from registers into memory, from one register to another, and from constants into places. Understanding exactly what mov can and cannot do is the foundation for reading everything else.

Its close relative lea ("load effective address") looks like a memory instruction but never touches memory at all; compilers use it as a compact way to do arithmetic. This lesson covers mov's operand forms, the extension variants movzx and movsx, xchg, a first look at push and pop, and a toy interpreter where you can run all of it.

We use Intel syntax throughout (destination first: mov dst, src). If you have seen movl %esi, %eax instead, that is AT&T syntax, covered in Assemblers and Syntax Flavors.

What mov does

mov dst, src copies the value of src into dst. The source is unchanged, and no flags are modified. Despite the name, nothing moves. The legal operand combinations are:

Form Example Meaning
register, register mov rax, rbx copy a register
register, immediate mov ecx, 10 load a constant
register, memory mov eax, [rbx] load from memory
memory, register mov [rbx], eax store to memory
memory, immediate mov dword [rbx], 7 store a constant (size must be given)

Not allowed: memory to memory. To copy a value between two memory locations you load into a register and then store.

The size of the operation comes from the register (eax means 4 bytes, al means 1 byte), or from a size keyword when no register is present: byte, word, dword, qword. In mov dword [rbx], 7 the keyword tells the CPU to write four bytes. Without it the assembler cannot tell and reports an error.

Remember the rule from the x86-64 register lesson: a 32-bit destination zero-extends into the full 64-bit register, while 8- and 16-bit destinations leave the other bits alone.

Memory operands

A memory operand has the general shape

TEXT
[ base + index * scale + displacement ]

where base and index are 64-bit registers, scale is 1, 2, 4 or 8, and displacement is a constant. Every part is optional (but you need at least one). This single mechanism covers pointers ([rdi]), struct fields ([rdi+8]), array elements ([rdi+rcx*4]) and global variables ([rip+counter]). The next lessons dig deeper, in particular Arrays and Addressing Modes. The value computed from these parts is called the effective address.

LEA: address arithmetic without the memory

lea dst, [expression] evaluates the bracketed expression and puts the resulting address in dst. It does not read memory. Compare:

NASM
mov eax, [rbx + rcx*4]   ; read 4 bytes from memory at rbx + rcx*4
lea rax, [rbx + rcx*4]   ; rax = rbx + rcx*4   (no memory access)

This is the natural way to compute the address of an array element or a struct field, which is what C's &array[i] means. But because the CPU's address-generation hardware can add up to three terms and multiply one by 1, 2, 4 or 8 in a single step, lea is also a cheap general-purpose add-and-multiply instruction that does not modify flags:

NASM
lea eax, [rdi + rdi*2]   ; eax = 3 * edi
lea eax, [rdi + rdi*4]   ; eax = 5 * edi
lea eax, [rdi + 7]       ; eax = edi + 7
lea eax, [rdi + rsi]     ; eax = edi + esi, and rdi, rsi untouched (a 3-operand add)

Compilers love these. When you see lea eax, [rdi+rdi*4] in compiler output, read it as rdi * 5, not as a memory access. Because only scales 1, 2, 4 and 8 exist, multiplying by 3, 5 and 9 takes one lea (index plus base), and larger constants combine lea with shl or another lea.

Extending narrow values: movzx, movsx and movsxd

When you load a byte into a bigger register, what should the upper bits be? That depends on whether the value is signed. Two instructions make the choice explicit:

  • movzx dst, src zero-extends: upper bits become 0 (for C unsigned char, unsigned short).
  • movsx dst, src sign-extends: upper bits copy the sign bit (for C signed char, short).
  • movsxd rax, eax sign-extends 32 bits to 64 bits (C int to long). Plain mov eax, eax would zero-extend instead.

For example the byte 0xF0 is 240 as unsigned and -16 as signed. movzx eax, byte [rbx] yields 240 (0x000000F0); movsx eax, byte [rbx] yields -16 (0xFFFFFFF0). Sign extension is covered in more detail in Binary, Hex and Two's Complement. It is the instruction-level reason that C code behaves differently for char and unsigned char.

XCHG and a peek at push and pop

xchg a, b swaps two operands atomically in one instruction. With a memory operand it is automatically locked (it becomes an atomic exchange), which makes it useful in synchronisation code; as a plain register swap it saves writing three movs through a temporary. xchg eax, eax was historically how the one-byte nop was encoded.

push src decrements rsp by 8 and stores src at the new [rsp]; pop dst loads from [rsp] into dst and adds 8 to rsp. They are data movement too, and rsp is the stack pointer. We spend the whole of The Stack on them.

flowchart LR
  subgraph mov
    direction TB
    S1["src (unchanged)"] -->|copy| D1["dst (overwritten)"]
  end
  subgraph lea
    direction TB
    E["base + index*scale + disp"] -->|address value, no memory access| D2["dst"]
  end

mov copies a value (reading memory if the source has brackets); lea copies the computed address itself.

A toy mov machine

Let us run these ideas. The interpreter below has registers, a small byte memory and the instructions mov, lea, movzx, movsx and xchg. Memory operands use the full [base + index*scale + disp] form. Everything is plain JavaScript, so you can edit the program and rerun it.

JS Toy mov, lea, movzx, movsx and xchg

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Step through the output. movzx rax, byte [rbx] gives 0xf0 (240), while movsx rcx, byte [rbx] gives 0xfffffffffffffff0 (-16 in 64 bits). lea rdx, [rbx + 8] leaves the memory alone and produces the address 24, and the following mov rax, [rdx] then really reads memory there. The lea with rcx + rcx*4 + 1 computes 3 * 5 + 2 = 17 without any multiply instruction.

(Our toy mov is always 64-bit to keep it short. A real assembler picks the width from the register name.)

lea as a multiplication cookbook

Because lea handles base + index*scale + displacement, a surprising number of small multiplications fit into one instruction. The table below is generated by evaluating the real formula for several inputs, the way a compiler would think about it.

JS Multiplying with lea

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The check column compares each recipe against ordinary multiplication; nothing prints MISMATCH.

Common mistakes

  • Forgetting the brackets. mov rax, rbx copies a register; mov rax, [rbx] loads from memory. lea is the opposite: brackets are written but memory is never read.
  • Using mov with two memory operands. Not encodable; go through a register.
  • Missing size. mov [rbx], 7 is ambiguous. Write mov dword [rbx], 7 or mov qword [rbx], 7.
  • Expecting mov eax, -1 to give a 64-bit -1. It gives 0x00000000FFFFFFFF because of zero-extension; use mov rax, -1.
  • Treating lea as slow memory access. It is arithmetic. Remember that lea does not set flags, which is sometimes exactly why a compiler uses it between a cmp and its conditional jump.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does lea rax, [rbx + rcx*8 + 16] do?

    lea evaluates the address expression and keeps the result; it never accesses memory and does not modify flags.
  2. 2. Which instruction is NOT valid in x86-64?

    There is no memory-to-memory mov. You must load into a register first, then store.
  3. 3. A byte in memory is 0x80. What does movsx eax, byte [rbx] put in eax?

    movsx copies the sign bit (bit 7, which is 1) into all upper bits, producing 0xFFFFFF80, i.e. -128 as a signed 32-bit number. movzx would give 0x00000080.
  4. 4. Why do compilers often use lea eax, [rdi + rdi*4]?

    Base plus index*4 equals 5 times the register, and the address-generation unit does that in one step.

Summary

  • mov dst, src copies a value and never changes flags; operands can be register, immediate or memory, but never two memory operands.
  • Memory operands have the form [base + index*scale + disp] with scale 1, 2, 4 or 8; the size comes from the register or a byte/word/dword/qword keyword.
  • A write to a 32-bit register zero-extends into the 64-bit register.
  • lea computes an address (or any small sum/product) without touching memory and without changing flags.
  • movzx zero-extends, movsx/movsxd sign-extend; pick them by whether the source is unsigned or signed.
  • xchg swaps two operands; push and pop move data to and from the stack.

Next: Arithmetic: ADD, SUB, MUL, DIV, where flags start to matter.