DIV divides an unsigned dividend that is twice as wide as its single operand, the divisor. The registers are fixed:
| Divisor | Dividend | Quotient | Remainder |
|---|---|---|---|
| byte | AX | AL | AH |
| word | DX:AX | AX | DX |
The quotient is rounded down and the remainder is what is left over, so 200 ÷ 7 gives 28 remainder 4. There is no immediate form: DIV 10 does not exist, load the divisor into a register. A memory divisor needs BYTE PTR or WORD PTR when its size is not known.
MOV AX, 200XOR DX, DX ; DX:AX = 200: the high word must be 0MOV BX, 7DIV BX ; AX = 28, DX = 4MOV CX, DX ; keep the remainderAAM ; AH = tens, AL = onesOR AX, 3030h ; both digits -> charactersMOV BX, AXMOV DL, BHMOV AH, 02h ; DOS: print DLINT 21hMOV DL, BLMOV AH, 02hINT 21hMOV DL, ' 'MOV AH, 02hINT 21hMOV AL, CLADD AL, '0' ; digit -> characterMOV DL, ALMOV AH, 02h ; DOS: print DLINT 21h
Divide overflow
If the divisor is 0, or the quotient does not fit in AL (more than FFh) or AX (more than FFFFh), the CPU does not divide but raises interrupt 0. Under DOS this usually ends the program with a "Divide overflow" message. The classic cause is forgetting DX: a word DIV always divides the 32-bit DX:AX, so whatever was left in DX becomes part of the dividend. Clear it with XOR DX, DX before dividing a 16-bit number. The same goes for AH before a byte divide.
On the 8086 and 8088 the return address pushed for interrupt 0 points to the instruction after the DIV; from the 286 on it points at the DIV itself.
Flags
All six arithmetic flags are undefined afterwards. Test the quotient or remainder explicitly (OR AX, AX, CMP DX, 0) rather than reading ZF.
Printing a number in decimal
Repeated division by 10 yields the digits from the last one up. Push them, then pop them to print in the right order. This prints 12345:
MOV AX, 12345MOV BX, 10XOR CX, CX ; digit countNEXT1:XOR DX, DXDIV BX ; AX = AX / 10, DX = last digitPUSH DXINC CXOR AX, AXJNZ NEXT1PUT1:POP DXADD DL, '0'MOV AH, 02hINT 21hLOOP PUT1
Later CPUs
The 386 adds DIV ECX, dividing EDX:EAX. Dividing by a power of two is cheaper as a right shift (SHR).