Re: A82: Dividing and remainders


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

Re: A82: Dividing and remainders



Funny you should ask that, ilya... Just today I asked the same question of
Dines.  He gave me an excerpt from Programming the Z80 by Rodney Zaks..
Here is the routine, it seems that is what you are looking for.
<<
(E is devidend, C is divisor, A is quotient, B is remeinder)
DIV88:
  XOR A
  LD B,8
Loop88:
  RL E
  RLA
  SUB C
  JR NC,$+3
  ADD A,C
  DJNZ Loop88
  LD B,A
  LD A,E
  RLA
  CPL
  RET
>>

So if you want to do an integer division, the answer is in A.
If you wanted to do a modulo, the answer would be b.  
There was another routine that would do division between two numbers in
memory.  It looks like this is an integer division, with the result stored
in hl.
<<
(Divide DVAD by DVSAD)
DIV168:
  LD A,(DVSAD)
  LD D,A
  LD E,0
  LD HL,(DVAD)
  LD B;8
DIV:
  XOR A
  SBC HL,DE
  INC HL
  JP P,NOADD
  ADD HL,DE
  DEC HL
NOADD:
  ADD HL,HL
  DJNZ DIV
  RET
>>

						Best Regards,
						Rob Bonstein
						unguent on the net
At 06:42 PM 11/23/97 -0600, you wrote:
>There is a routine included with the Ark v1.1 source that divides a by b 
>(a/b). My question is when you divide say 8/3 what happens to the 
>remainder and if I just wanted the interger value what would I do. And 
>if anyboby has another way to divide numbers please post this 
>routine.(isn't there something in your z80 books dines?).
>I tried to attach the routines below to look at. If it isn't there oh 
>well!
>; Math routines for the Z80 microprocessor
>; by Randy Gluvna
>; gluvna@home.com
>; http://members.home.com/gluvna
>
>MULT:
>        PUSH BC
>        DEC B
>        LD C,A
>MULT_LOOP:
>        ADD A,C
>        DJNZ MULT_LOOP
>        POP BC
>        RET                  
>DIV:
>        PUSH DE
>        LD D,0
>DIV_LOOP:
>        INC D
>        SUB B
>        OR A
>        JP P,DIV_LOOP
>        LD A,D
>        POP DE
>        DEC A
>        RET
>


References: