Lesson 6 of 15
Multiplication & Division
Multiplication
x86_64 has two multiply instructions:
IMUL (Signed Multiply)
The most common form:
imul rax, rbx ; rax = rax * rbx
imul rax, rbx, 5 ; rax = rbx * 5
MUL (Unsigned Multiply)
One-operand form that uses rax implicitly:
mul rbx ; rdx:rax = rax * rbx
The result is 128 bits, split across rdx (high) and rax (low).
Division
IDIV (Signed Divide) and DIV (Unsigned Divide)
Division also uses implicit registers:
; Before dividing, sign-extend rax into rdx:rax
cqo ; sign-extend rax -> rdx:rax
idiv rbx ; rax = rdx:rax / rbx, rdx = remainder
For unsigned division:
xor rdx, rdx ; zero rdx (unsigned, no sign extension)
div rbx ; rax = rdx:rax / rbx, rdx = remainder
Important: You must set up
rdxbefore dividing! Usecqofor signed orxor rdx, rdxfor unsigned.
Your Task
Compute and print:
- 3 * 3 = 9
- 8 / 2 = 4
- 7 % 3 = 1 (remainder)
Print: "9\n4\n1\n"
x86_64 runtime loading...
Loading...
Click "Run" to execute your code.