dwight elvey wrote:
I think my routine may be extended a few bytes by
changing the
tweak value. It has greater effect for small values but could get one
or two numbers increased if optimized.
Such tweaks are there to make up for the way we truncate instead
of rounding. It means the value is always too small after a large number
of operation.
Dwight, if my test-code version of your routine is correct, the constant
correction can be reduced to 1 from 16 and the routine then fails at 1280
instead of 1210. The addition can now be changed to an increment and some other
register optimisation done to take another two bytes and some cycles off (no
need to use A any longer, quotient is in C):
------------------------------
Segment: MAIN
base=$0000 end=$001D bytes=30
* 0-799 divided by 10
* implements: quo = ( 51*x + (51*x/256+1) ) / 512
* rem = x - (q*4+q)*2
* hl = dividend
* Returns:
* c = quotient
* l = remainder
machine z80
0000 54 Div10fast LD D,H de = hl
0001 5D LD E,L
0002 29 ADD HL,HL
0003 29 ADD HL,HL
0004 19 ADD HL,DE x * 5
0005 29 ADD HL,HL x * 10
0006 44 LD B,H bc = 10x
0007 4D LD C,L
0008 29 ADD HL,HL
0009 29 ADD HL,HL
000A 09 ADD HL,BC x * 50
000B 19 ADD HL,DE hl = x*50 + x
000C 06 00 LD B,0 bc = hl/256
000E 4C LD C,H
000F 03 INC BC + 1
0010 09 ADD HL,BC ADD little more accuracy
0011 4C LD C,H c = hl / 256 / 2
0012 CB 19 RR C quo = c, carry cleared in ADD
0014 60 LD H,B hl = quo
0015 69 LD L,C
0016 29 ADD HL,HL quo*10 to calc RemainDEr
0017 29 ADD HL,HL
0018 09 ADD HL,BC
0019 29 ADD HL,HL
001A EB EX DE,HL
001B ED 52 SBC HL,DE rem = l
001D C9 RET
------------------------------