Re: 'Divide by A' procedure in 90x0 ROM

From: Rhialto <>
Date: Sat, 19 Feb 2011 01:31:33 +0100
Message-ID: <>
On Fri 18 Feb 2011 at 15:03:31 +0100, Baltissen, GJPAA (Ruud) wrote:
> Hallo Groepaz,
> > think of how you divide on paper, 

It is just like a long division on paper. Except, if you do it on paper,
you shift your divisor to the right as you progress, 1 digit at a time.
Here, instead, the dividend is shifted to the left (the asl/rol block of
code).  That is the same, relative to each other. And the starting
position of the divisor is just a bit to the left of the dividend.

[NL: dividend = teller, divisor = noemer ]

Then a check is made if the divisor can be subtracted from the BIG end
(as stored, by now, in the Accumulator) (the usual lda/sec/sbc/... part)
and if so, a 1 is appended to the result (and the subtraction, as stored
in Y and A is done), otherwise a 0.

The funny part of the code is that the Result bytes are used both for
input of the dividend and output of the result. That is possible because
as bits are shifted left out of the input into the Accumulator, the
rightmost bits become unused and are re-used by shifting in bits from
the result (quotient).

I don't actually think that Accumulator+1 is really needed, unless the
dividend would be divided by values over 0xFF. After all, the
Accumulator is where the high bits of Result are shifted into, and when
the divisor can be subtracted from it, it will be. So this value will
never be bigger than the divisor, which (here) is 1 byte.

So the code could have been simpler. Perhaps the extra flexibility could
be used by jumping into it from somewhere else, except that for the high
byte of the divisor, #00 is always used.

Also, with the 18 x going through the loop instead of 16 x, it looks
like it does it too often, which results in the wrong answer by a factor
of 4...

>    / /  |_/     Groetjes, Ruud
___ Olaf 'Rhialto' Seibert  -- There's no point being grown-up if you 
\X/ rhialto/at/    -- can't be childish sometimes. -The 4th Doctor

       Message was sent through the cbm-hackers mailing list
Received on 2011-02-19 01:00:13

Archive generated by hypermail 2.2.0.