Skip to content

Instantly share code, notes, and snippets.

@DmitrySoshnikov
Last active January 19, 2017 03:07
Show Gist options
  • Save DmitrySoshnikov/e67af89df229b7329828 to your computer and use it in GitHub Desktop.
Save DmitrySoshnikov/e67af89df229b7329828 to your computer and use it in GitHub Desktop.
Addressing modes in `mov`, and their C version
Addressing modes in mov, and their C version

Below are addressing modes (immediate, register, memory) on the examples of the mov assembly instruction. Here we use movl ("move long") for a 32-bit word.

Note: cannot move memory-to-memory, need two instructions.

movl Src Dest Src, Dest C version
Imm Reg movl $0x4,%eax x = 0x4;
Mem movl $-5,(%eax) *px = -5;
Reg Reg movl %eax,%edx x = y;
Mem movl %eax,(%edx) *px = y;
Mem Reg movl (%eax),%edx y = *px;
General form of memory access

Also, the most general form of memory access is:

D(Rb, Ri, S)

That corresponds to the following memory address calculation:

Mem[Reg[Rb] + S * Reg[Ri] + D]

Where:

  • D: Constant "displacement" (offset), 1, 2, or 4 bytes
  • Rb: Base register: Any of the 8/16 integer registers
  • Ri: Index register: Any, except for %esp/%rsp
  • S: Scale: 1, 2, 4, 8 (numbers are to support arrays of values of these sizes)

Besides, special cases can use any combination of D, Rb, Ri, and S:

(Rb, Ri)        Mem[Reg[Rb] + Reg[Ri]]
D(Rb, Ri)       Mem[Reg[Rb] + Reg[Ri] + D]
(Rb, Ri, S)     Mem[Reg[Rb] + S * Reg[Ri]]

Examples:

  • Parameters access on the stack:
movl 8(%ebp), %eax       ; parameter with offset 8 from the base pointer
movl 12(%ebp), %edx      ; parameter with offset 12 from the base pointer
  • Address computation instruction: leal Src, Dest, "load effective address", where Src is address mode expression. The result is stored in the Dest.
leal (%edx, %ecx, 4), %eax

The leal can be used for:

  • Computing addresses without a memory reference. E.g. translation of p = &x[i];
  • Computing arithmetic expressions of the form x + k * i, where k is 1, 2, 4, or 8.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment