LOGBOOK

HELP

Quiz Entry - updated: 2026.07.06

Why do some functions use mov %edi, %eax as their very first instruction?

It copies the first argument into %eax (the return-value register) so the function can compute and return a result derived from that argument.

func4:
    mov  %edx, %eax
    sub  %esi, %eax
    ...

Common reasons:

  1. Preparing for arithmetic%eax is used for operations like cdq + idiv, or the result will be returned in %eax
  2. Freeing up the argument register%rdi/%rsi are caller-saved, so copying to %eax lets us use %rdi for something else (like a function call)
  3. Direct return — a simple function might just massage the argument and return it

Note: Using %edi/%esi/%edx (32-bit) instead of %rdi/%rsi/%rdx (64-bit) tells you the arguments are int (4 bytes), not long (8 bytes).

Go deeper:

From Quiz: REVE1 / Assembly Patterns & GDB | Updated: Jul 06, 2026