Re: A86: ASM Programming Test - div 3


[Prev][Next][Index][Thread]

Re: A86: ASM Programming Test - div 3




>Awsome!  That routine is definitely going in my toolbox of useful routines.
>You forgot the <sub c> with the <inc d>, but I figured that out from the
>comments.  Do you think you could explain how it works?
>
OK, think about the process of long division. That's what this is. You work
your way down the bit string from the MSB. At each position, if the divisor
goes into the first x bits of the dividend, you make that digit in the
quotient a 1, if not, you make the digit a 0.

Since this is binary, the number is only going to go in once, because if
the number doesn't go into X, it's only going to go into 2X + 1 at most one
times. For base 10, each time you have to check if the divisor goes in 0 to
9 times, with binary you have to check if it goes in 0 to 1 times.

Cool, huh?

BTW, while I did come up with this on my own originally, I later discovered
there's a built-in one on the calc that divides hl by a and returns the
quotient in hl, remainder in a. It's used on the calc to convert numbers to
strings for printing, as there's also a routine to divide by 10.

Addresses are:
_HLdiv10 equ $4044
_HLdivA equ $4048
_AHLdiv10 equ $404C

_AHLdiv10 also returns quotient in hl, remainder in a, so it can only be
used once. That's why _dispAHL can only display numbers up to 655350 (or
whatever) rather than all the way up to the full 24 bit capacity.

--Joshua



References: