: Divide 10 bits by 10.

Chuck Guzis cclist at sydex.com
Sun Jan 20 13:59:15 CST 2008


> Date: Sat, 19 Jan 2008 13:01:25 -0800
> From: dwight elvey <dkelvey at hotmail.com>


>  I'm not sure how effective that would be in Z80 code. It looks
> like I'd at least need to do 32 bit coding to keep track of things.
> I'd still need to calculate the remainder when done( I need both ).
>  The maximum sector index would be 800 decimal. 

Ah, then it's easy:

;*      Divide 10 bits by 10.
;       ---------------------
;
;       Input in (HL)
;
;       Quotient in (A), remainder in (L).
;


Div10by10:
        ld      de,(10 shl 6)           ; divisor
        ld      bc,0701h                ; iteration count + 1 for xor
        xor     a                       ; quotient
Dtbt2:
        xor     c                       ; assume set
        sbc     hl,de                   ; subtract
        jr      nc,Dbt4                 ; if no carryout
        add     hl,de                   ; add back
        xor     c                       ; clear the bit
Dbt4:
        rr      d							; (carry is clear)
        rr      e							; shift divisor
        add     a,a                     ; shift quotient
        djnz    dbt2                    ; loop
        rra                             ; correct quotient
        ret                             ; a = quotient, l = remainder

That will do it for values of a divident up to 1023.  The algorithm 
can be extended somewhat by shifting the value of 10 left more places 
and increasing the number of iterations.

I haven't tested it out on real hardware, but it should work.  I've 
also got a divide 32-bits by 10 along the same line, if you're 
interested.  It's fairly deterministic in terms of cycles; i.e., 
there's not a lot of difference in timing between a dividends of 0 
and 799.

Cheers,
Chuck


More information about the cctalk mailing list