what is a divide overflow?
Posted on 2006-04-17 09:20:03 by para
The "Divide Overflow" or "Divide by Zero" is an exception that occurs when you try to preform division by zero. This is an illegal operation and causes an exception.
Posted on 2006-04-17 09:36:25 by Synfire
From "Art of Assembly"

Division by zero and Division Overflow (they're NOT the same thing):
You cannot, on the 80x86, simply divide one eight bit value by another. If the denominator is an eight bit value, the numerator must be a sixteen bit value. If you need to divide one unsigned eight bit value by another, you must zero extend the numerator to sixteen bits. You can accomplish this by loading the numerator into the al register and then moving zero into the ah register. Then you can divide ax by the denominator operand to produce the correct result. Failing to zero extend al before executing div may cause the 80x86 to produce incorrect results! When you need to divide two 16 bit unsigned values, you must zero extend the ax register (which contains the numerator) into the dx register. Just load the immediate value zero into the dx register12. If you need to divide one 32 bit value by another, you must zero extend the eax register into edx (by loading a zero into edx) before the division. When dealing with signed integer values, you will need to sign extend al to ax, ax to dx or eax into edx before executing idiv. To do so, use the cbw, cwd, cdq, or movsx instructions. If the H.O. byte or word does not already contain significant bits, then you must sign extend the value in the accumulator (al/ax/eax) before doing the idiv operation. Failure to do so may produce incorrect results. There is one other catch to the 80x86’s divide instructions: you can get a fatal error when using this instruction. First, of course, you can attempt to divide a value by zero. Furthermore, the quotient may be too large to fit into the eax, ax, or al register. For example, the 16/8 division “8000h / 2??? produces the quotient 4000h with a remainder of zero. 4000h will not fit into eight bits. If this happens, or you attempt to divide by zero, the 80x86 will generate an int 0 trap. This usually means BIOS will print “division by zero??? or “divide error??? and abort your program. If this happens to you, chances are you didn’t sign or zero extend your numerator before executing the division operation. Since this error will cause your program to crash, you should be very careful about the values you select when using division.
Posted on 2006-04-17 23:48:27 by ti_mo_n

The "Divide Overflow" or "Divide by Zero" is an exception that occurs when you try to preform division by zero. This is an illegal operation and causes an exception.

Divide Overflow is not Divide by Zero.
In div operation divident size is twice of divider.
For example in
div ecx
Divider is 32 bits. Divident is 64 bits (edx:eax edx high dword eax low dword) Result is placed in the same registers as served for divident (in the example edx:eax). Part that was served as high dword now holds remainder, low - quontient.
If in divident high dword > divider then quontien bigger then 232-1 and thus can not be placed in 32 bit and you get overflow. In other words - you get overflow when half size of
divident (or size of divider) is not enough to hold quontien.
Posted on 2006-04-18 00:34:43 by Yan
Divide Overflow is not Divide by Zero.

Same difference really. Yes the cause is different but it's the same exception (INT 0). Both are handled the same way using the same "Division Error" interrupt and, in more cases than not, people refer to them interchangable and only show distinction when associated with an instruction, AAM/DIV/IDIV versus just DIV/IDIV. Even the IA-32 Architecture Exception Reference doesn't differentiate the two, calling all Division exceptions "#DE (divide error / by zero)". I'm sure para appreciates you posting a detailed explaination and honestly I had to make my post in haste as I wasn't on my own computer atm. Thanks for clearification.

Regards,
Bryant Keller
Posted on 2006-04-18 04:45:54 by Synfire
Yes, they indeed have same #DE exception, but para asked "what is a divide overflow?". To me it looks like he saw "division by zero" and "division overflow" somewhere. Apparently, he knew what "division by zero" was :)
Posted on 2006-04-18 12:09:23 by ti_mo_n
Yea, I took it as though he just saw "division overflow" occur in one of his applications. I've seen many debuggers report "Division Overflow" on any INT 0, even when an explicit call to int 0 is used or even when div 0 is used. Which is why I was explaining that it was the same error. Though looking back I agree, I should have probably been more thorough in my reply.
Posted on 2006-04-18 12:48:18 by Synfire
the division overflow message occured when I ran a program that I had made that used the div operation
I wasn't debbuging it I was running it

I know what a division by zero is
Posted on 2006-04-18 18:44:43 by para
Hi para,

Just be sure to use CDQ before a divide or in the case of positive numbers zero out EDX as an unwelcome value in EDX is the most common cause of a divide (integer) overflow, that is where the result is greater than can be stored in a single register (64 or 32 bits depending on mode and processor).

Donkey
Posted on 2006-04-18 20:10:57 by donkey
Signed integers
IDIV with 32 bit dividend (fits in EAX) do CDQ

Unsigned integers
DIV with 32 bit dividend XOR EDX,EDX  ; same as MOV EDX, 0

Reason CDQ wont work for both
If your using unsigned integers then
CDQ will fill EDX with 0FFFFFFFFh
if the unsigned value in
EAX is greater or equal to 2^31.

Code like this can prevent division overflow and division by zero
if dividing a 64 bit number by 32 bit.
If the divisor is small, the quotient
may need to be stored in two dwords.
``.data  dvdd dd 3, 9  ; 900000002h  dvsr dd 2,  0  quo  dd 0,  0  rem  dd 0,  0.code  mov ecx, dvsr  mov edx, dvdd+4  mov eax, dvdd  .if ecx == 0         ; would be division by zero     ; indicate it however you want  .elseif ecx <= edx   ; would overflow eax, result >= 2^32     mov eax, edx     mov edx, 0     div ecx     mov quo+4, eax     ; edx has the remainder so already set     mov eax, dvdd     div ecx     mov quo, eax     mov rem, edx  .else     div ecx     mov quo+4, 0     mov quo, eax     mov rem, edx  .endif``
Posted on 2006-04-19 08:01:15 by dsouza123