Quiz Entry - updated: 2026.07.14
What does imul with 3 operands do and when does it appear?
imul $const, %src, %dst computes %dst = %src * const — a one-instruction multiply by any constant, not just the 1/2/4/8 that lea can handle.
# rax = rdi * 12
imul $12, %rdi, %rax
This computes %rdi * 12 and stores in %rax. Unlike lea tricks, imul works with any constant, not just 1/2/4/8 + offset combinations.
The 3 forms of imul:
| Form | Example | Meaning |
|---|---|---|
| 1-operand | imul %ecx |
edx:eax = eax * ecx (full 64-bit result) |
| 2-operand | imul %ecx, %eax |
eax = eax * ecx (truncated) |
| 3-operand | imul $12, %edi, %eax |
eax = edi * 12 (truncated) |
Common context:
- Array index calculations for 2D arrays:
imul $ROW_SIZE, %rdi, %rax - Computing struct sizes:
imul $STRUCT_SIZE, %rdi, %rax - Any constant multiplication too complex for
lea
Go deeper: