"Use of mnemonics" demonstrations
Use of IDIV

The IDIV instruction takes only one operand. It can be used for byte, word or dword operation. IDIV only works with signed numbers. The result is the correct sign to suit the signs of the dividend and the divisor.
The dividend is always implicit and is either AX, DX:AX or EDX:EAX (depending on the data size used for the divisor). Note that DX:AX is 32 bits but made up of two 16 bit registers. EDX:EAX is 64 bits made up of two 32 bit registers. When using the word or dword form of IDIV it is essential to ensure that the correct value is in DX or EDX (as appropriate). If using only AX or EAX as a dividend, ensure that DX or EDX (as appropriate) is set to zero. Failure to do this will give an inaccurate result and could cause an exception.
The divisor is the instruction's operand. It can be a byte, word or dword register or a byte, word or dword-sized memory label.
The result of the divide goes into AL for byte operations (with the remainder in AH), into AX for word operations (with the remainder in DX) and into EAX for dword operations (with the remainder in EDX).
A divide by zero will cause the processor to halt (in Windows an exception code 0C0000094h occurs). To avoid this at run-time if there is any chance that the divisor is zero always test it for that condition before doing the divide.
If the result of the divide is larger than the data size of the destination again the processor will halt (in Windows an exception code 0C0000095h occurs). This would happen for example if you try to divide -2,000 by 5 using only a byte-sized IDIV instruction, for example IDIV BL. Here the result of -400 would not fit in the destination AL. For that calculation, therefore, you should use the word or dword-sized versions of the instruction (eg. IDIV BX or IDIV EBX). For this reason in 32 bit programming in most cases it is prudent only to use the 32 bit versions of IDIV, and if there is any chance of an overflow test for that condition before carrying out the division.

To run the IDIV test set the breakpoint to USEOF_IDIV then single-step through the code.
In this example results cannot be compared with the results which would be obtained using the unsigned version of the division instruction (DIV) because of the exceptions which result (the processor believes the dividend to be a large positive number).

;********************* IDIV demonstration
;*********** divide -21 by 5 result should be -4 remainder -1
;****** byte operation
MOV AX,-21             ;put -21 into AX for dividend
MOV BH,5               ;put 5 into BH for divisor
IDIV BH                ;divide AX by BH: AL=-4, AH=-1 - correct
;****** word operation
MOV DX,-1              ;sign extend -21 into higher order dividend
MOV AX,-21             ;put -21 into AX for lower order dividend
MOV BX,5               ;put 5 into BX for divisor
IDIV BX                ;divide DX:AX by BX: AX=-4, DX=-1 - correct
;****** dword operation
MOV EDX,-1             ;sign extend -21 into higher order dividend
MOV EAX,-21            ;put -21 into EAX
MOV EBX,5              ;put 5 into EBX for divisor
IDIV EBX               ;divide EDX:EAX by EBX: EAX=-4, EDX=-1 - correct
;*********** divide -21 by -5 result should be +4 remainder -1
;****** byte operation
MOV AX,-21             ;put -21 into AX for dividend
MOV BH,-5              ;put -5 into BH for divisor
IDIV BH                ;divide AX by BH: AL=4, AH=-1 - correct
;****** word operation
MOV DX,-1              ;sign extend -21 into higher order dividend
MOV AX,-21             ;put -21 into AX for lower order dividend
MOV BX,-5              ;put -5 into BX for divisor
IDIV BX                ;divide DX:AX by BX: AX=4, DX=-1 - correct
;****** dword operation
MOV EDX,-1             ;sign extend -21 into higher order dividend
MOV EAX,-21            ;put -21 into EAX
MOV EBX,-5             ;put -5 into EBX for divisor
IDIV EBX               ;divide EDX:EAX by EBX: EAX=4, EDX=-1 - correct
;*********** divide +21 by -5 result should be -4 remainder +1
;****** byte operation
MOV AX,21              ;put 21 into AX for dividend
MOV BH,-5              ;put -5 into BH for divisor
IDIV BH                ;divide AX by BH: AL=-4, AH=1 - correct
;****** word operation
MOV DX,0               ;sign extend 21 into higher order dividend
MOV AX,21              ;put 21 into AX for lower order dividend
MOV BX,-5              ;put -5 into BX for divisor
IDIV BX                ;divide DX:AX by BX: AX=-4, DX=1 - correct
;****** dword operation
MOV EDX,0              ;sign extend 21 into higher order dividend
MOV EAX,21             ;put 21 into EAX
MOV EBX,-5             ;put -5 into EBX for divisor
IDIV EBX               ;divide EDX:EAX by EBX: EAX=-4, EDX=1 - correct