[net.micro] Z80 divide by 10

don (03/24/83)

 In response to:

      Can anyone tell me a fast assembly language divide routine
      which works under the following assumptions and constraints:

      Z80 micro,
      16 bit dividend which is evenly divisible by 10 (i.e. no remainder)
      divisor is the constant "10" decimal
      routine must perform its miracle in under 200 microseconds
      (about 65 instr.)

 Assuming the dividend is unsigned, the following
 somewhat perverse algorithm should work, and it's FAST!

    A=dividend
    B=A/16  (integer divide, remainder discarded)
    C=A/256
    D=B+C
    E=D*3
    F=E/256
    G=E+F
    H=A-G
    I=H/8
    quotient=I

 This is exact when the dividend is evenly divisible by 10 and accurate
 to +/- 1 otherwise (for a dividend of 65535 or less).
 Assuming the dividend is in BC in the Z80, the following routine
 should leave the quotient in HL.  I don't have access to a Z80, so
 you'll have to test this yourself.

                Time (T-cycles)

    LD    D,B     ;  4   DE=BC
    LD    E,C     ;  4

    SRL   B       ;  8   BC=BC/16
    RR    C       ;  8
    SRL   B       ;  8
    RR    C       ;  8
    SRL   B       ;  8
    RR    C       ;  8
    SRL   B       ;  8
    RR    C       ;  8

    LD    H,0     ;  7   HL=DE/256+BC
    LD    L,D     ;  4
    ADD   HL,BC   ; 11

    LD    B,H     ;  4   HL=HL*3
    LD    C,L     ;  4
    ADD   HL,BC   ; 11
    ADD   HL,BC   ; 11

    LD    B,0     ;  7   HL=HL/256+HL
    LD    C,H     ;  4
    ADD   HL,BC   ; 11

    EX    DE,HL   ;  4   HL=DE-HL
    SBC   HL,DE   ; 15

    SRL   H       ;  8   HL=HL/8
    RR    L       ;  8
    SRL   H       ;  8
    RR    L       ;  8
    SRL   H       ;  8
    RR    L       ;  8

 Quotient is now in HL
 BC, DE, and F have all been altered
 Total time 213 T cycles   (53.25 microseconds for a 4 MHz machine)

                                   Good luck!

                                   Don Winsor
                                   University of Michigan
                                   Ann Arbor

swanson (04/10/83)

#R:uofm-cv:-17100:uiucdcs:10400059:000:80
uiucdcs!swanson    Apr  9 21:13:00 1983

 Thank you for the routine. It works in just under 56 msecs.
 Robert Swanson