idiv
Signed divide
Divides the implicit signed dividend selected by the operand width, producing a quotient and remainder.
8086 and later
- Arithmetic, logic and bitsx86-64 assembly
- Multiply and divide, with the remainderx86-64 assembly
- ? undefined afterwards
- – not changed
Forms
Operands
r/m- A general-purpose register or memory location matching the operation’s size, for example
raxorqword [rbp-8]. The byte registersah,bh,chanddhcannot be used with a REX prefix, including when the memory address usesr8–r15.
The divisor width selects the implicit dividend and outputs: 8 bits divides ax into quotient al and remainder ah; 16 bits divides dx:ax into ax and dx; 32 bits divides edx:eax into eax and edx; 64 bits divides rdx:rax into rax and rdx.
The high half is part of the signed dividend. To divide a signed value held only in the low half, extend it first with cbw, cwd, cdq or cqo respectively. For example, cqo then idiv rcx divides signed rax. The quotient rounds toward zero; a nonzero remainder has the dividend's sign. A zero divisor or a quotient outside the destination's signed range causes a divide error. Flags are undefined.
global _start
section .text
_start:
mov rax, -100 ; the dividend is rdx:rax
cqo ; sign extends rax into rdx
mov rbx, 7
idiv rbx ; quotient in rax, remainder in rdx
mov rax, 60 ; syscall 60: exit
xor rdi, rdi
syscall