Hello everyone,
I am trying to divide a DWORD value by a BYTE or WORD value. But I don't understand what DX:AX means.

Usage: IDIV src
Modifies flags: (AF,CF,OF,PF,SF,ZF undefined)
Signed binary division of accumulator by source. If source is a byte value, AX is divided by "src" and the quotient is stored in AL and the remainder in AH. If source is a word value, DX:AX is divided by "src", and the quotient is stored in AL and the remainder in DX.


Will this code work?


mov eax, MyDWORDValue
mov bx, 64
div bx


Can someone explain to me how to use DIV/IDIV properly?
Thanks in advance.
Posted on 2002-02-08 22:11:03 by dilau
Sounds like you're reading old 16bit documentation. If you work
in 32bit, div will divide EDX:EAX by your source. The EDX:EAX notation
means 64bit value, high dword in edx, low dword in eax. Which
also means you have to clear/signextend edx before doing a division.
And you must make sure the result of the division fits within a 32bit
register, otherwise you'll get an overflow exception.
Posted on 2002-02-08 22:39:50 by f0dder
First, forget about DX:AX. It's a leftover from the 16 bit days. In general, you don't want to mix 16 and 32 bit code, for performance reasons. Yea, sometimes you need to mix code, but it's not good. Think EDX:EAX.

On the x86, the dividend is always 2 times the size of the divisor. In 32 bit land, where the divisor is 32 bits, this means the dividend is 64 bits, contained in the register pair EDX:EAX. EAX contains the low 32 bits, and EDX contains the high 32 bits, before the divide. After the divide, EAX contains the quotient, and EDX the remainder. If you have a dividend that is only 32 bits in EAX, you need to sign extend EDX before the divide.

The same holds true for the old 16 bit code. DA:AX contains the 32 bit dividend, when the divisor is 16 bits.

:)
Posted on 2002-02-08 22:42:48 by S/390
If you really want to do the 16-bit way:
mov dx, WORD PTR MyDWORDValue + 2 ; high order word

mov ax, WORD PTR MyDWORDValue ; low order word
mov bx, 64
div bx
; [b]ax[/b] has quotient
; [b]dx[/b] has remainder
See how you need to break the DWORD value up - only a good idea if you need this in the loader for your OS. Or maybe on some homework. ;)
Posted on 2002-02-08 23:18:45 by bitRAKE
Thanks guys, it worked perfectly!! :)
I have a question, though. What's sign-extend?
Reading the OPCODES help, I figured out that it has something to do with converting a WORD to a DWORD for example. Is this correct?
Posted on 2002-02-09 10:29:01 by dilau
The highest bit is the sign:
    [*]byte = bit 7[*]word = bit 15[*]dword = bit 31Sign extension is just filling the rest of a larger size data with the value of the sign bit. For example, if you had a byte 0CCh and you wanted to sign extend that value to a dword, the result would be 0FFFFFFCCh. The three significant bytes are set because the highest bit of the byte was set. It can also be looked at as signed conversion from a smaller operand type to a larger type. There are many instructions to help you do this:
      [*]CWD[*]CDQ[*]MOVSX
Posted on 2002-02-09 12:23:59 by bitRAKE