Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

predicting a divide overflow. 2

Status
Not open for further replies.

denc4

Programmer
Feb 17, 2005
107
0
0
NL
let's say you got this code:

; 20002h \ 2.

mov ax,2
mov dx,ax
mov bx,ax
div bx

result would be 10001h, which ofcourse does not fit in ax:
a divide overflow error is generated.

Is it possible to predict a divide overflow before it
occurs?
 
Yes, do a compare on dx with bx and if ae then its an
overflow.

mov ax,2
mov dx,ax
mov bx,2
cmp dx,bx
jae error
div bx
ok:

error:

succes, Tessa
 
That makes sense.
I'm not a math person myself, so I am strugling with the
logic behind it. But I have executed some experimental code
based on your example and it seems to work fine.

thank you.
 
Look it like this:

100.000/10 gives 10.000 if you now look at the
three zeros afther the dot then that is the ax register
and the 100 is the dx register.

by dividing you see that afther the division ax can't hold the whole 10.000.
so when you divide the 100.000 by 200 it gives .500 and that can be holded in ax.

try it with dividing by 100 and the result will be 1.000
with is just 1 to much to hold since the maximum in this
exxample is 999

I hope this explaince the arithmetic behind it a bit.

succes, Tessa
 
it's a bit clearer now, I could not understand what the
relationship between dx and bx was, and why bx should be
greater than dx in order to divide without an overflow.

But it can be explained by dividing the high order word
of a 32 bit dividend (dx:ax) with a 16 bit value (bx).
So let's leave out ax and concentrate on dx only:

; dx div bx

2 div 1 = 1 ;overflow.
2 div 2 = 1 ;overflow.
2 div 3 = 0 ;no overflow (quotient fits in 16 bit).

btw, I think this error checking code (for me at least) is
a bit clearer:

cmp bx,dx
jbe error
 
I never understood why Mr Intel thought that you could divide a 32-bit number by a 16-bit and come up with a 16-bit result. Did he think that division is the inverse of multpiplication and 16-bit x 16-bit yields 32 bit, or did he just run out of registers?
 
It is rather common to use this method, that stems from
the time computers had a huged lag of memory.
If you want to calculate the mean value of a lot of
numbers, as for administration purposes, you add up the
numbers in the double register set and then divide it by
the number count.
In this way you can add 64k numbers of a maximum of 64k
and it will still fit into the final calculation.
Then you can also use the remainder the refine your
outcom to as mutch decimal digits as needed.

So 100 times 30.000 will fit in the dx:ax pair and the result of 30.000 times 100 divide by 100 will still represent the correct answer.

100*30000 3.000.000
--------- = --------- = 3.000
100 100

Hopefully this clears things up a bit.

Tessa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top