Prev: ascii to tword - help
Next: Is hex an ascii thing?
From: Wolfgang Kern on 31 Mar 2007 13:38 Hello Brian, [...] > so is the sign extension necessary with unsigned division? Also, in > the above, you say "sign extension" but have "MOVZX"... you meant > "MOVSX", right? No, sorry for the confusion. Unsigned DIV: MOVZX ebx, word divisor ;if you want a "16-bit Unsigned" divisor MOV eax, dword dividend XOR edx,edx ;edx=0 DIV ebx ;cannot overflow Signed DIV: MOVSX ebx, word divisor ;if you want a "16-bit Signed" divisor MOV eax, dword dividend CDQ ;edx= 0 or -1 depending on MSbit of eax IDIV ebx ;cannot overflow You can also use MOV ebx, dword divisor in both instead of MOVSx/Zx IF "you want a 32 bit divisor". __ wolfgang
From: Rick Hodgin on 1 Apr 2007 19:25 > What are good solutions to preventing DIV overflow? The only solution for never having divide overflow is to upsize your dividend to a size equal to the divisor. > Is it best just to pick a large size register combination for all > division to avoid overflow? The best solution, if you're concerned about occasional overflow conditions using 16-bit math, with the desire to return only a 16-bit quantity, would be to write your code like this: #1 - do normal divide (div 32 bits by 16 bits, result will be 16 bits or overflow) #2 - check if overflow, if so, go to 4 #3 - go to 5 #4 - either recompute divide using larger operands and return 32-bit value, or signal overflow by returning saturated value (all 16 bits set to 1) #5 - return result The best way to avoid overflows is to always use a dividend that is the same size as your divisor. In that way, even if you divide by 1, it will always be at least the same size value (a max 32-bit quantity of the source value). - Rick C. Hodgin
From: Frank Kotler on 1 Apr 2007 19:01 Rick Hodgin wrote: .... > #1 - do normal divide (div 32 bits by 16 bits, result will be 16 bits or > overflow) > #2 - check if overflow, if so, go to 4 Sounds good if you say it quick. Problem is, my program has been terminated by the exception - or are you hooking the exception? Best, Frank
From: Charles Crayne on 1 Apr 2007 19:36 On Sun, 01 Apr 2007 23:01:41 GMT Frank Kotler <fbkotler(a)verizon.net> wrote: > Problem is, my program has been > terminated by the exception Reverse steps 1 & 2. If, and only if, dx is greater than or equal to the divisor, then overflow will occur.
From: Rick Hodgin on 1 Apr 2007 23:13
>> #1 - do normal divide (div 32 bits by 16 bits, result will be 16 bits or >> overflow) >> #2 - check if overflow, if so, go to 4 > > Sounds good if you say it quick. Problem is, my program has been > terminated by the exception - or are you hooking the exception? WOW! In all of my years of assembly programming I never knew the DIV instruction threw a divide error exception for overflow...until tonight. After your response, which had me wondering if we were talking FPU and not 16-bit regs and masked/unmasked exceptions, I looked it up in an Intel IA-32 Architecture programming manual. I have always thought that was one of the purposes for INTO instruction, and that the overflow was just FLAG'd and not thrown. Twenty years of x86 assembly programming (this year) and I never knew that. WOW! :) I was even all prepared to write some sample code: ; Load dx:ax div bx jo fixup ret fixup: ; adjust return value by whatever method ret :) Thanks, Frank! Now I'm wondering what else I don't know about something I thought I had pretty-much nailed. - Rick C. Hodgin |