Re: A85: 8bit division routine
[Prev][Next][Index][Thread]
Re: A85: 8bit division routine
On Sun, 29 Nov 1998, Erik Huizing wrote:
> has anyone got a small 8 bit division routine?
> or something that's hard-coded to divide by 3
>
An 8-bit division routine:
;E = A/D
;A = A%D
Div8:
ld e,a
cp 0
ret z
ld a,e ;Check for divide by zero
ld e,0
ld b,1
Div8Loop1:
bit 7,d
jr z,Div8Set ;if bit 7 = 1 then Ready
inc b
sla d
jr Div8Loop1 ;shift D until bit 7 is 1, keep track of shifts in B
Div8Set:
cp d
jr c,Div8NoSub
sub d
Div8NoSub:
ccf
rr e
srl d
djnz Div8Set
ret
Hard Coded to use only 3.. Same except for setup.. which is a lot smaller
;E = A/3
;A = A%3
DivBy3:
ld e,0
ld d,%11000000
ld b,7
DivBy3Set:
cp d
jr c,NoSub
sub d
NoSub:
ccf
rr e
srl d
djnz DivBy3Set
ret
AS if anyone cares, here's how it works: Check if D =0, if you don't,
you'll get an infinite loop. After that it works the same as long
division in binary, you find the answer starting with the most signifcant
bit which is determined by a comparison ..
00110010
________
00000011|10011000
11000000 Too Big -> Bit 6=0
- 1100000 Smaller -> Bit 5=1 and do a subtraction
= 111000
- 110000 Smaller -> Bit 4=1 and do a subtraction
= 1000
11000 Too Big -> Bit 3=0
1100 Too Big -> Bit 2=0
- 110 Smaller -> Bit 1=1 and do a subtraction
= 10
11 Too Big -> Bit 0=0
So 10011000 / 11 = 110010 and the remainder is 10
Anybody read this far?
If you did I'll be posting a demo of my game (barely playable and ugly) on
the tyrant web page, as a backup to make it easy to look at then throw
away. Have fun.
-Humberto Yeverino Jr.
Official Tyrant Home Page:
http://www.engr.csufresno.edu/~humberto/tyrant.html
References: