×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

predicting a divide overflow.
2

predicting a divide overflow.

predicting a divide overflow.

(OP)
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?

RE: predicting a divide overflow.

2
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

RE: predicting a divide overflow.

(OP)
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.

RE: predicting a divide overflow.

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

RE: predicting a divide overflow.

(OP)
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

RE: predicting a divide overflow.

That's it!

Tessa

RE: predicting a divide overflow.

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?

RE: predicting a divide overflow.

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close