From: frustum at
pacbell.net
After doing the "fastest" possible implementation, I realized that
Dwight's "smallest" code is far from the smallest. Behold the trivial
solution on the other end of the spectrum:
div10:
ld de,-10 // divisor
xor a // quotient accumulator; clear carry
loop:
inc a
add hl,de
jr c,loop
sbc hl,de ; undo last subtraction; remainder in L
dec a ; we looped one time too many
That is 11 bytes. It is slow as dirt, but if the game is smallest wins,
then this is doing better than your current 16 byte program.
Hi Jim
I like this. If I'm that pressed for space, I'll use this.
It isn't all that bad on average. It is about 1110 clocks
for the average.
Here is my Idea of the fastest code without table lookup
just for fun:
; 0-799 divided by 10
; hl = dividend
; Returns:
; h = quotient
; a = remainder
ld d,h
ld e,l
add hl,hl
add hl,hl
add hl,de
add hl,hl
ld b,h
ld c,l
add hl,hl
add hl,hl
add hl,bc
add hl,de
ld a,h
rra
ld l,a
ld h,#0
ld c,a
ld b,h
add hl,hl
add hl,hl
add hl,bc
add hl,h,
ex de,hl
sbc hl,de
ld c,#-10
ld h,a
ld a,l
add a,c
jr c,d10f1
sub a,c
ret
d10f1:
inc h
ret
Worst case clocks is 239 ( by my count )
Now, if someone can comment the code!!
Dwight
_________________________________________________________________
Connect and share in new ways with Windows Live.
http://www.windowslive.com/share.html?ocid=TXT_TAGHM_Wave2_sharelife_012008