News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Long division

Started by carlottagp, June 29, 2006, 01:24:05 PM

Previous topic - Next topic

carlottagp

I wonder if someone could recommend a textbook covering
digital arithmetic.  Specifically, I want to learn how
to divide a two-word number by a one-word number. Presumably,
the procedure involves shifting the bits in the divisor
to the left a certain number of times, followed by repeated
subtractions from the dividend; this would be analogous to
ordinary long division.  (It's not at all clear how one would
use the DIV instruction for a problem like this.)  Any
suggestions or comments would be appreciated.

Michael

Tedd

The DIV instruction works as a two-word (two 16-bit registers) divided by a one-word number/register.
"div cx" means "dx:ax divided_by cx" (the 'big' number is made up of dx as the high part, and ax as the low)

However, if you want to divide 10-word numbers by 5-word numbers, then you need to play around with 'long' division.
A little step from (a+b)/c = (a/c)+(b/c) should be enough to work it out though :wink
No snowflake in an avalanche feels responsible.

raymond

One thing Tedd forgot to mention about "dx:ax divided_by cx" is that the answer must fit into the AX register, i.e. 16 bits maximum. Otherwise, your program will crash.

One way to avoid that is to verify that CX is greater than DX before the division. If CX is not greater than DX, you must then resort to a long division as suggested.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

Tedd

Yeah, sorry, forgot to mention the result (quotient) is in ax, with the remainder in dx.
The remainder will come in useful for long division :wink
No snowflake in an avalanche feels responsible.