IDIV-DIV
By Vesper Vei
1 minute read
Table of Contents
IDIV-DIV (idiv / div)
Basic purpose
IDIV → signed division
DIV → unsigned division
Result layout:
- Quotient → eax
- Remainder → edx
(or rax / rdx)
IDIV (signed) format
idiv r/m32; edx:eax ÷ r/m32; 有符号除法Before execution:
- Fill EDX with the sign extension of EAX (
cdqinstruction)
DIV instruction format (unsigned)
div r/m8; ax ÷ r/m8; al = 商; ah = 余数
div r/m32; edx:eax ÷ r/m32; eax = 商; edx = 余数Note: before execution, edx must be cleared to zero (if the dividend is an unsigned dword).
Behavioral characteristics
- Divisor is 0 → divide-by-zero exception
- Quotient or remainder out of range → overflow exception
- Implicitly uses registers (AL/AX/EAX/RAX and AH/EDX/RDX)
Examples
mov eax, 100mov ecx, 7xor edx, edxdiv ecx; eax = 14, edx = 2Signed:
mov eax, -30mov ecx, 4cdqidiv ecx; eax = -7, edx = -2