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

Re: A85: 16-bit integer Square Root




On Sun, 15 Mar 1998, GrafixxHQ wrote:

> I guess you enter you 16 bit number into the HL register, but which register
> does the square root go into?
> 
> Thanxx

The square root of hl is stored in hl but if you want to change that then
here's how. (changes have **)
>  Anyways here it is:
>  Sqrt16:	

**   push hl ;if you want to save hl 

>    push de
>    push bc
>    ld de,0
>    ld bc,0
>    call CP_HL_DE
>    jr z,Sqrt16Done  ;0 is a special case.
>    ld de,65280
>    ld bc,256
>    sbc hl,de
>    jr nc,Sqrt16done
>    dec bc           ;bc=255
>    scf
>    rr h
>    rr l		   ;shift hl
>    call Sqrt16loop
>  Sqrt16done:

** 	   ;when the routine gets here the square root of hl is stored in
**         ;bc and hl has been destroyed so you can ignore the next two
**         ;lines and pop hl before returning and keep the answer in bc
**	   ;which would mean ignoring pop bc also.

>    ld h,b	;Load the answer from bc into hl	
>    ld l,c
 
>    pop bc
>    pop de
**   pop hl
>    ret	;The routine exits here

;This is just a subroutine that does all the work

>  Sqrt16loop:
>    add hl,bc
>    ret c
>    dec c
>    jp Sqrt16loop  

-Humberto Yeverino Jr.

"Small potatos make the steak look bigger."

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

<font size=3>For browsers:<br>
<a href="http://www.engr.csufresno.edu/~humberto/Home.html">
Have a look.</a></font><br>


Follow-Ups: References: