Multiplication & Division
Multiplication and division are where x86 shows its age. Unlike add, they have special forms that use rax and rdx implicitly, and there are separate instructions for signed and unsigned numbers.
imul: the easy multiplication
For everyday signed (and unsigned, if you only need the low 64 bits) multiplication, use the two- or three-operand forms of imul:
| Form | Effect |
|---|---|
imul d, s |
d = d * s |
imul d, s, imm |
d = s * imm |
section .text
global _start
_start:
mov rax, 12
imul rax, rax ; 12 * 12 = 144
mov rdi, rax
call print_int
mov rbx, -7
imul rdi, rbx, 6 ; rdi = -7 * 6
call print_int
mov rcx, 1000
imul rcx, rcx, 1000 ; one million
imul rcx, rcx ; one trillion (10^12) fits easily in 64 bits
mov rdi, rcx
call print_int
mov eax, 60
xor edi, edi
syscall
; ---- print_int: print rdi as a signed decimal number and a newline ----
print_int:
test rdi, rdi
jns print_uint ; not negative: print it as it is
neg rdi ; make it positive
push rdi ; keep it (write and syscall clobber registers)
push '-' ; the minus sign, as a byte on the stack
mov eax, 1 ; write(1, rsp, 1)
mov edi, 1
mov rsi, rsp
mov edx, 1
syscall
pop rdi ; drop the '-'
pop rdi ; the positive value again
jmp print_uint ; print_uint's ret returns to our caller
; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
mov rax, rdi ; rax = the number to convert
sub rsp, 32 ; room for up to 20 digits and a newline
lea rsi, [rsp + 31] ; fill the buffer from its end
mov byte [rsi], 10 ; the newline is the last character
mov ecx, 10
.next_digit:
xor edx, edx ; the dividend is rdx:rax, so clear rdx
div rcx ; rax = quotient, rdx = remainder (0..9)
add dl, '0' ; remainder -> ASCII digit
dec rsi
mov [rsi], dl ; store it in front of the previous one
test rax, rax
jnz .next_digit ; repeat until the quotient is 0
lea rdx, [rsp + 32]
sub rdx, rsi ; rdx = number of bytes to print
mov eax, 1 ; write(1, rsi, rdx)
mov edi, 1
syscall
add rsp, 32
ret144
-42
1000000000000
These forms keep only the low 64 bits of the product. Because of two's complement, the low half is the same for signed and unsigned operands, so imul is fine for unsigned numbers too as long as the result fits.
mul: the full 128-bit product
The one-operand forms multiply rax by the operand and store the double-width result in rdx:rax (high half in rdx, low half in rax). mul is unsigned, the one-operand imul signed:
mul rbx ; rdx:rax = rax * rbx (unsigned)
imul rbx ; rdx:rax = rax * rbx (signed)Smaller sizes work the same way: mul ecx gives edx:eax, and mul cl gives ax. Notice that rdx is overwritten even when the product is small — a classic source of bugs when rdx held something else.
section .text
global _start
_start:
mov rax, 0x100000000 ; 2^32
mov rbx, 0x300000000 ; 3 * 2^32
mul rbx ; 3 * 2^64: does not fit in 64 bits
mov r12, rax ; save the low half
mov rdi, rdx ; high half
call print_uint ; 3
mov rdi, r12 ; low half
call print_uint ; 0
mov eax, 60
xor edi, edi
syscall
; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
mov rax, rdi ; rax = the number to convert
sub rsp, 32 ; room for up to 20 digits and a newline
lea rsi, [rsp + 31] ; fill the buffer from its end
mov byte [rsi], 10 ; the newline is the last character
mov ecx, 10
.next_digit:
xor edx, edx ; the dividend is rdx:rax, so clear rdx
div rcx ; rax = quotient, rdx = remainder (0..9)
add dl, '0' ; remainder -> ASCII digit
dec rsi
mov [rsi], dl ; store it in front of the previous one
test rax, rax
jnz .next_digit ; repeat until the quotient is 0
lea rdx, [rsp + 32]
sub rdx, rsi ; rdx = number of bytes to print
mov eax, 1 ; write(1, rsi, rdx)
mov edi, 1
syscall
add rsp, 32
ret3
0
div and idiv: division
Division is the mirror image: the 128-bit dividend is rdx:rax, the divisor is the operand, and you get two results:
| Instruction | Dividend | Quotient | Remainder |
|---|---|---|---|
div r64 (unsigned) |
rdx:rax |
rax |
rdx |
idiv r64 (signed) |
rdx:rax |
rax |
rdx |
div r32 |
edx:eax |
eax |
edx |
There is no immediate form: div 10 doesn't exist, the divisor must be in a register or memory.
Before an unsigned div, clear rdx (xor edx, edx). Before a signed idiv, sign-extend rax into rdx with cqo (it copies the sign bit of rax into every bit of rdx). Forgetting this is the number one division bug.
section .text
global _start
_start:
; 1234 minutes = ? hours and ? minutes
mov eax, 1234
xor edx, edx ; dividend = 0:1234
mov ecx, 60
div rcx ; rax = 20, rdx = 34
mov r12, rdx
mov rdi, rax
call print_uint
mov rdi, r12
call print_uint
; signed: -17 / 5
mov rax, -17
cqo ; rdx = all ones (the sign of rax)
mov ecx, 5
idiv rcx ; rax = -3, rdx = -2 (truncates toward zero)
mov r12, rdx
mov rdi, rax
call print_int
mov rdi, r12
call print_int
mov eax, 60
xor edi, edi
syscall
; ---- print_int: print rdi as a signed decimal number and a newline ----
print_int:
test rdi, rdi
jns print_uint ; not negative: print it as it is
neg rdi ; make it positive
push rdi ; keep it (write and syscall clobber registers)
push '-' ; the minus sign, as a byte on the stack
mov eax, 1 ; write(1, rsp, 1)
mov edi, 1
mov rsi, rsp
mov edx, 1
syscall
pop rdi ; drop the '-'
pop rdi ; the positive value again
jmp print_uint ; print_uint's ret returns to our caller
; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
mov rax, rdi ; rax = the number to convert
sub rsp, 32 ; room for up to 20 digits and a newline
lea rsi, [rsp + 31] ; fill the buffer from its end
mov byte [rsi], 10 ; the newline is the last character
mov ecx, 10
.next_digit:
xor edx, edx ; the dividend is rdx:rax, so clear rdx
div rcx ; rax = quotient, rdx = remainder (0..9)
add dl, '0' ; remainder -> ASCII digit
dec rsi
mov [rsi], dl ; store it in front of the previous one
test rax, rax
jnz .next_digit ; repeat until the quotient is 0
lea rdx, [rsp + 32]
sub rdx, rsi ; rdx = number of bytes to print
mov eax, 1 ; write(1, rsi, rdx)
mov edi, 1
syscall
add rsp, 32
ret20
34
-3
-2
Like C, idiv truncates toward zero, and the remainder has the same sign as the dividend: -17 = 5 × (-3) + (-2).
When division goes wrong
Division by zero does not give an error code: the CPU raises a divide error exception and Linux kills the program with SIGFPE. The same happens if the quotient doesn't fit into rax, which is exactly what you get when rdx contains leftover garbage:
section .text
global _start
_start:
mov rdx, 50 ; leftover value from earlier code...
mov rax, 100
mov ecx, 10
div rcx ; divides 50 * 2^64 + 100 by 10: quotient too big!
mov eax, 60
xor edi, edi
syscallProgram terminated with signal SIGFPE (8)
Choosing the right instruction
| Task | Use |
|---|---|
| multiply, result fits in 64 bits | imul d, s or imul d, s, imm |
| multiply by 2, 3, 4, 5, 8, 9 | lea or shl (faster) |
| need the full 128-bit product | mul / one-operand imul |
| unsigned divide | xor edx, edx then div |
| signed divide | cqo then idiv |
| divide by a power of two | shr (unsigned) / sar (signed, with care) |
Note: Division is slow — tens of cycles, compared with 3 for
imuland 1 foradd. Compilers replace division by a constant with a multiplication by a "magic" reciprocal; you will see this in Reading Compiler Output.
Test yourself
Where does div rcx put the remainder?
- rax
- rcx
- rdx
- rbx
What must you do before a signed idiv rcx with the dividend in rax?
xor edx, edxcqoto sign-extend rax into rdx- Nothing
neg rax
What does imul rdi, rbx, 6 do?
- rdi = rdi * rbx * 6
- rdi = rbx * 6
- rbx = rdi * 6
- rdx:rax = rbx * 6