[Coco] Divide by 10 on a Motorola MC6809(E)

Don Barber don at dgb3.net
Wed Mar 20 13:08:58 EDT 2024


A trick I use to divide 8 bit integers by 10 is to MUL by 205, then shift the A register down by 3 bits (ASRA three times). This works out to 205/2048 which is close enough precision to 1/10 for 8 bit integers.

Thanks

Don


Sent from my iPhone

> On Mar 20, 2024, at 1:35 AM, Joel Rees via Coco <coco at maltedmedia.com> wrote:
> 
> The multiply-by-constant as a fixed sequences of adds and shifts is,
> of course, interesting (multiply-and-accumulate).
> 
> I've tried to find a similar inverse method for divide-by-constant, but
> I think the fixed sequence turns out to be per the dividend, not the
> divisor, that is, divide-a-constant-by rather than divide-by-constant.
> 
> I think.
> 
> I have also looked at division as multiply-by-inverse, but convinced
> myself that inverting the divisor ends up being expensive enough
> to negate any advantage over just doing the subtract-shift-count
> binary long division.
> 
> Looking at this again, it occurs to me that, if the divisor is a
> constant, it can be pre-computed. So, in the case of divide-by-ten,
> the inverse of ten in binary is
> 
> .0001100110011001
> 
> to sixteen bits.
> 
> (bc is useful in calculating such things.)
> 
> Giving it a try with an arbitrary dividend of 65000 shows me that
> there will be some tricky business with rounding, but it should
> almost work:
> 
> bc -l<enter>
> obase=2<enter>
> 65000<enter>
> 1111110111101000
> 65000/10<enter>
> 1100101100100.000000000000000000000000000000000000000000000000000000\
> 0000000000000
> 1/10<enter>
> .0001100110011001100110011001100110011001100110011001100110011001100
> ibase=2<enter>
> 1111110111101000*0001100110011001<enter>
> 11001011000110110011110101000
> 
> Divide-by-ten can also be looked at as divide-by-five, then
> divide-by-two, which might be of use in working out the rounding.
> 
> 1/10 and 1/5 are repeating fractions in binary, of course, and that
> could be used to effect in turning the long chain of add-and-shifts
> into a loop, at the cost of adding loop overhead.
> 
> I'm going to come back to this when i have more time.
> 
> --
> Joel Rees
> 
> http://reiisi.blogspot.jp/p/novels-i-am-writing.html
> 
> --
> Coco mailing list
> Coco at maltedmedia.com
> https://pairlist5.pair.net/mailman/listinfo/coco



More information about the Coco mailing list