Difference between revisions of "Z80 Routines:Math:Division"

From WikiTI
Jump to: navigation, search
(added categorization)
m (correct some typos)
Line 4: Line 4:
 
== Introduction ==
 
== Introduction ==
  
All these routines use the restoring divison algorithm, adapted to the z80 architecture to maximize speed.
+
All these routines use the restoring division algorithm, adapted to the z80 architecture to maximize speed.
 
They can easily be unrolled to gain some speed.
 
They can easily be unrolled to gain some speed.
  
 
== 8/8 division ==
 
== 8/8 division ==
  
The following routine multiplies d by e and places the quotient in d and the remainder in a
+
The following routine divides d by e and places the quotient in d and the remainder in a
  
 
  <nowiki>
 
  <nowiki>
Line 31: Line 31:
 
== 16/8 division ==
 
== 16/8 division ==
  
The following routine multiplies hl by c and places the quotient in hl and the remainder in a
+
The following routine divides hl by c and places the quotient in hl and the remainder in a
  
 
  <nowiki>
 
  <nowiki>

Revision as of 01:07, 14 May 2010


Introduction

All these routines use the restoring division algorithm, adapted to the z80 architecture to maximize speed. They can easily be unrolled to gain some speed.

8/8 division

The following routine divides d by e and places the quotient in d and the remainder in a

div_d_e:
   xor	a
   ld	b, 8

_loop:
   sla	d
   rla
   cp	e
   jr	c, $+4
   sub	e
   inc	d
   
   djnz	_loop
   
   ret
 

16/8 division

The following routine divides hl by c and places the quotient in hl and the remainder in a

div_hl_c:
   xor	a
   ld	b, 16

_loop:
   add	hl, hl
   rla
   cp	c
   jr	c, $+4
   sub	c
   inc	l
   
   djnz	_loop
   
   ret
 

16/16 division

The following routine divides ac by de and places the quotient in ac and the remainder in hl

div_ac_de:
   ld	hl, 0
   ld	b, 16

_loop:
   sll	c
   rla
   adc	hl, hl
   sbc	hl, de
   jr	nc, $+4
   add	hl, de
   dec	c
   
   djnz	_loop
   
   ret
 

24/8 division

The following routine divides ehl by d and places the quotient in ehl and the remainder in a

div_ehl_d:
   xor	a
   ld	b, 24

_loop:
   add	hl, hl
   rl	e
   rla
   cp	d
   jr	c, $+4
   sub	d
   inc	l
   
   djnz	_loop
   
   ret
 

32/8 division

The following routine divides dehl by c and places the quotient in dehl and the remainder in a

div_dehl_c:
   xor	a
   ld	b, 32

_loop:
   add	hl, hl
   rl	e
   rl	d
   rla
   cp	c
   jr	c, $+4
   sub	c
   inc	l
   
   djnz	_loop
   
   ret