Re: A85: Square roots


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

Re: A85: Square roots



;Unsigned 8 bit division.  Source adapted from Andreas Ess's UDIV16
;Input:
;  l is numerator
;  a is denominator
;Output:
;  a is quotient
;  h is remainder
;i.e.  l / a = a remainder h

UDiv8:
   push bc
   push de
   ld d,l               ;save the original l
   ld c,a
   ld b,8               ;number of bits in division
   or a                 ;clear the carry flag
   ld a,0
UDivLoop8:
   rl l
   rla

   ld h,a
   sbc a,c
   ccf

   jr c,Drop8
   ld a,h
Drop8:
   djnz UDivLoop8

   rl l
   ld h,a
   ld a,l
   ld l,d

   pop de
   pop bc
   ret

;Signed Multiplication de = d * e
SMul8:
   push bc
   ld c,0                       ;assume both numbers are positive
   bit 7,d
   jr z,Check2ND
   ld a,d
   neg
   ld d,a
   inc c                        ;c is now 1
Check2ND:
   bit 7,e
   jr z,DoUMul8
   ld a,e
   neg
   ld e,a
   inc c                        ;c is 1 if only multiplier (?) is
negative or
                                ; is 2 if both multiplicand and
multiplier
                                ; are negative
DoUMul8:
   ld b,8
   xor a
DoUMul8Loop:
   bit 0,e
   jr z,DoUMul8Skip
   add a,d
DoUMul8Skip:
   rra
   rr e
   or a
   djnz DoUMul8Loop
   ld d,a

   dec c                        ;sets z bit when c is 1 (result is
negative)
   pop bc
   ret nz                       ;the answer is positive so return
   push hl
   xor a                        ;\
   ld h,a                       ; |
   ld l,a                       ; |negate de
   sbc hl,de                    ; |
   ld d,h                       ; |
   ld e,l                       ;/
   pop hl                       
   ret


The div is unsigned, but can be changed to signed quite easily (in the
same manner as the multiplication).

>You can raise that number to the power of -1 (i'm not sure how the math
>goes to do that), plus you can divide, but remember you can't take the
>square root of negative numbers so your best bet would do some error
>checking through the algorithm and if neccessary, take the absolute value
>of any negative numbers.  there's nothing wrong with dividing besides the
>fact that it's slow and there's always the possibility of dividing by
>zero
>
>if your multiply and divide source routines are in assembler i'd be happy
>to take a look
>
>another thing i just thought of that might enhance performance, i believe
>you could use the built in tolerance in the TI-85/86 you just have to
>find the memory address to where the tolerance is stored
>
>also i just started doing Z-80 stuff and i am wondering if they have the
>command "div"
>
>Kakos
>egillespie@juno.com
>


References: