Re: A85: Square roots


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

Re: A85: Square roots



Erik L Gillespie wrote:
> there's nothing wrong with dividing besides the
> fact that it's slow and there's always the possibility of dividing by
> zero

That's true, except the original poster doesn't really want square
roots, he just wants to know if a number is prime, k?
 
> if your multiply and divide source routines are in assembler i'd be happy
> to take a look

Well, i can give you some routines off the top of my head. These may not
be optimized but they are small and effective:

mult:			;output: hl=b*e 
			;if b=0, hl=e*256...
			;if e=0, hl=0
			;d,b destroyed
	ld	d,0	;de=e
	ld	hl,0	;hl=0
mult2:
	add	hl,de	;adds de to hl for each b
	djnz	mult2
	ret

div:			;output: c=a/b rounded down to nearest integer, a=a%b
			;if b=0 cf is set
	ld	(temp),a;store a
	xor	a	;a=0
	ld	c,a	;c=0
	cp	b	;does b=0?
	scf
	ret	z	;if so return w/cf set
	ld	a,(temp);recall a
div2:
	cp	b	;is a<b?
	ccf		;if so reset cf
	ret	nc	;and ret
	sub	b	;else subtract b from a
	inc	c	;and increment the division counter
	jr	div2	;and goto div2

I'm not sure if these routines work (didn't test them), but they
should...
Make sure you define a temp as an address somewhere in the text memory.

> also i just started doing Z-80 stuff and i am wondering if they have the
> command "div"

Unfortunately, no :`(
There isn't even a mulu command!
You'll have to write your own routine i suppose (or use the ones i just
wrote)

-- 
Terry Peng <tpeng@geocities.com>


References: