Re: A83: Divide HL by DE


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

Re: A83: Divide HL by DE




>Does anyone have a simple routine to divide HL by DE on the 83+?  Speed is
>more important than size in this case.
>
>
>Thanks,
>Asm83P

Signed or unsigned?

If you just want a simple unsigned 16-bit division, this routine will work:

;DivHLbyDE will divide an unsigned HL by an unsigned DE
;INPUT:  HL = 16 bit number to be divided
;        DE = 16 bit number to divide by
;OUTPUT  BC = answer
;        HL = remainder
;DESTROYED  HL,BC,DE, and any flags
DivideHLbyDE:
or a           ;1 byte , 4 cycles
ld bc,0        ;3 bytes, 10 cycles
DivhlbyDE
sbc hl,de      ;2 bytes, 15 cycles
jp c,nomore    ;3 bytes, 10 cycles
inc bc         ;1 byte , 6 cycles
jp z,nomore    ;3 bytes, 10 cycles
jp DivhlbyDE   ;3 bytes, 10 cycles
nomore:
add hl,de      ;1 byte , 11 cycles
ret            ;1 byte , 10 cycles
;TOTAL = 18 bytes, 86 cycles


  If you don't care about the remainder, you should use this one:

;DivHLbyDE will divide an unsigned HL by an unsigned DE
;INPUT:  HL = 16 bit number to be divided
;        DE = 16 bit number to divide by
;OUTPUT  BC = answer
;DESTROYED  HL,BC,DE, and any flags
DivideHLbyDE:
or a           ;1 byte , 4 cycles
ld bc,0        ;3 bytes, 10 cycles
DivhlbyDE
sbc hl,de      ;2 bytes, 15 cycles
ret c          ;1 bytes, 10 cycles
inc bc         ;1 byte , 6 cycles
ret z          ;1 bytes, 10 cycles
jp DivhlbyDE   ;3 bytes, 10 cycles
;TOTAL = 12 bytes, 65 cycles


If you don't care about speed, change the jp's to jr's to save a byte or 
two.
   Sam


______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com