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:
- Preparing for arithmetic —
%eaxis used for operations likecdq+idiv, or the result will be returned in%eax - Freeing up the argument register —
%rdi/%rsiare caller-saved, so copying to%eaxlets us use%rdifor something else (like a function call) - 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: