Subject: Re: div by 10 on Z80 was RE: Reading Polymorphic

Chuck Guzis cclist at sydex.com
Mon Jan 21 12:29:45 CST 2008


> Date: Sun, 20 Jan 2008 20:57:27 -0800
> From: Brent Hilpert <hilpert at cs.ubc.ca>
> Subject: Re: div by 10 on Z80 was RE: Reading Polymorphic ---
 
> (I expect you may have noticed already..) the subroutine (div10s) is
> only called once now, you could inline it to save another 4 bytes.

You can do a bit better with that and by using the carryout to form 
the quotient bits:

 div10:
  xor a
  ld de,0fd80h  ; largest power of 2 times 10 less then 800d
  ld b,07        ; only seven loops needed to finish divide 80
  divloop:
  add hl,de       ; trial subtract
  jr c,div10s1    ; carry means trial passed
  sbc hl,de       ; undo previous add, carry is clear
  div10s1:
  rl a
  add hl,hl ; shift number to reuse same constant, 640 decimal
  djnz divloop
  add hl,hl        ; push full remainder in HL
  ret

Again, I haven't tried it out, but it should work.

Cheers,
Chuck




More information about the cctalk mailing list