Hi =)
Some time ago, Cas (from Parallax) mailed me the
following code (maybe this is useful...):
> I am not going to present a streamlined piece of code here, but
> division is done in almost the same way as multiplication,
> i.e. it is also bit-wise done with powers of two.
>
> If I remember correctly you can just "try" to substract some
> power of two (highest first) you can set the bits correctly every
> time, and thus you will need a loop for all the bits in the result.
> Let us say that the result will be an 8-bit value, rounded down.
>
> HL=the value that has to be divided
> A=the value that you are dividing by
> (Out: HL=what is left after the division, the "rest",
> A=the result.
> This implies that HL(in)=HL(out)+A(out)*A(in), with HL(out)<>(in)
> )
>
> ld d,a
> ld e,0 ; DE=2^8
> ld b,8
>
> loop: srl d ; divide DE by two
> rr e ; carry flag ends up reset (E was zero)
> sbc hl,de ; try to substract
> jr nc,loop2
> add hl,de ; carry flag will end up set
>
> loop2: rl c ; rotate the carry flag "into" the result
> djnz loop
>
> ld a,c
> cpl ; result was inverted, I wanted to avoid a "CCF" in the loop
> ret
>
> This procedure can still be streamlined a lot, but this is basically what
> it is supposed to be doing.
>
greets
wolfgang
(chief-gavaman from programmers' affair)
****
MSX Mailinglist. To unsubscribe, send an email to [EMAIL PROTECTED] and put
in the body (not subject) "unsubscribe msx [EMAIL PROTECTED]" (without the
quotes :-) Problems? contact [EMAIL PROTECTED] (www.stack.nl/~wiebe/mailinglist/)
****