| <<< CBW, CWD, CDQ Instructions | Index | Divide Overflow >>> |
The IDIV (signed divide) instruction performs signed integer division, using the same operands as the DIV instruction.
For both DIV and IDIV, all of the arithmetic status flags are undefined after the operation.
When doing 8-bit division, you must sign-extend the dividend into AH before using IDIV.
For example, (-48/5):
.DATA
byte_val SBYTE -48
.CODE
mov al, byte_val ; dividend
cbw ; sign-extend AL into AH
mov bl, 5 ; divisor
idiv bl ; quotient AL = - 9 , remainder AH = -3
Similarly, l6-bit division requires that AX be sign-extended into DX.
For example, (-5000/256):
.DATA
word_val SWORD -5000
.CODE
mov ax, word_val ; dividend, low part
cwd ; sign-extend AX into DX
mov bx, 256 ; divisor
idiv bx ; quotient AX = -19, remainder DX = -136
Similarly, 32-bit division requires that EAX be sign-extended into EDX. The next example
For example, (-50000/256):
.DATA
dword_val SDWORD -50000
.CODE
mov eax, dword_val ; dividend, low
cdq ; sign-extend EAX into EDX
mov ebx, 256 ; divisor
idiv ebx ; quotient EAX = -195, remainder EDX = -80
| <<< CBW, CWD, CDQ Instructions | Index | Divide Overflow >>> |