The MASM Forum Archive 2004 to 2012

Miscellaneous Forums => 16 bit DOS Programming => Topic started by: carlottagp on June 29, 2006, 01:24:05 PM

Title: Long division
Post by: carlottagp on June 29, 2006, 01:24:05 PM
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
Title: Re: Long division
Post by: Tedd on June 29, 2006, 07:03:04 PM
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
Title: Re: Long division
Post by: raymond on June 30, 2006, 02:06:47 AM
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.
Title: Re: Long division
Post by: Tedd on June 30, 2006, 01:46:40 PM
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