Re: A85: a div 6


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

Re: A85: a div 6




On Sat, 28 Nov 1998, Kaus wrote:
> can anyone give me some z80 asm for getting the integer division of a
> deivided by 6???
> quick, hackiest thing you can think of.
> im stumped. there should be a way to do this, but i cant find it. the only
> way i can think of would be to take a-6 and inc until a-6<0, then dont inc
> and you are done. this would suck bad, but i may have to use it.
> 
> 
Here's a faster way:

DivBy6:
  ld b,6
  ld c,%00100000
  ld d,%11000000
  ld h,0
DivBy6Loop:
  cp d
  jr c,DB6_AisLess
  sub a,d		;subtract
  ld e,a		;save a
  ld a,c
  or h			;set bit
  ld h,a		;save in h
  ld a,e		;restore a
DB6_AisLess:
  srl c
  srl d
  djnz DivBy6Loop
  ret			;h=a/6 and a=remainder

The loop executes only 6 times, but a routine that could divide by any
number would only execute up to8 loops, but would also require shifting
the denominator.. Just for fun..

DivAbyD:
  ld b,0
  ld c,1
  ld h,b
DivSetup:
  inc b
  sla c
  sla d
  jr nc,DivSetup
  sra c
  scf
  rr d
  dec b
DivLoop:
  cp d
  jr c,DivAisLess
  ... same mess  
  ret

This would divide a by d with the same results as above.  If anyone has a
better way to divide by constants, I'd like to see it.

later,

-Humberto Yeverino Jr.

"I kick ass for the Lord."
-Dead Alive (1992)

***********************************************************
Home Page:                                               
  http://www.engr.csufresno.edu/~humberto/Home.html      

Ti Page:                                                 
  http://www.engr.csufresno.edu/~humberto/tex.html       

z80 Source Page:                                         
  http://www.engr.csufresno.edu/~humberto/z80source.html 

Official Tyrant Home Page:                              
  http://www.engr.csufresno.edu/~humberto/tyrant.html    

E-mail:                                                  
  humberto@engr.csufresno.edu                            
***********************************************************


Follow-Ups: References: