# SXMicrocontrollerMathMethod

## 16 bit Square Root successive approximation method

Andy David says:

I've added a 16-bit square root routine (again for the 17c43) that uses successive approximation to find the square root, the binary restoring method I'm fairly sure would be quicker. This is the only 16-bit sqrt routine I have specifically for the 17cxx PICs, I've only included it as the original poster requested a 16-bit routine. If you really want speed speed speed, I'd rewrite the 32-bit routine to be 16-bit.

Standard disclaimer applies

```;==========================================================================
; SQRT16
;
; Calculates the square root of a sixteen bit number by successive
; approximation.
;
;
; Input in ACCaHI:ACCaLO
;
; Output in ACCbHI
;
; Also uses ACCbLO and ACCcHI.
;
;--------------------------------------------------------------------------

SQRT16:
mov	W, #\$80	;
movpf WREG,ACCbLO ; set up walking tester bit.

clr	ACCbHI	;

mov	W, #\$08	;
movpf WREG,ACCcHI ;

tstest:	movfp ACCbLO,WREG ; set bit for testing.
or	ACCbHI, W	;
movpf WREG,ACCbHI ;

mulwf ACCbHI ; square the current estimate.

movfp ACCaLO,WREG ; compare PRODH:PRODL with ACCaHI:ACCaLO
sub	PRODL, W	;
movfp ACCaHI,WREG ;
subwfb PRODH,WREG ;

sb	ALUSTA.C	; If the result is <= input,
jmp	nxtbit	; ... keep this bit set.

movfp ACCbLO,WREG ; Result was > input, so reset
xor	ACCbHI, W	; ... bit to zero.

nxtbit:	rrncf ACCbLO,f ; rotate tester bit right.

decsz	ACCcHI	;
jmp	tstest	;

ret

```

 file: /Techref/scenix/lib/math/sqrt/sqrt16sa_sx.htm, 1KB, , updated: 2004/6/10 14:40, local time: 2024/7/24 10:58, TOP NEW HELP FIND:  44.192.44.30:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?Please DO link to this page! Digg it! / MAKE! SX Microcontroller Math Method

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
 Did you find what you needed? "No. I'm looking for: " "No. Take me to the search page." "No. Take me to the top so I can drill down by catagory" "No. I'm willing to pay for help, please refer me to a qualified consultant"

### Welcome to sxlist.com!

Site supported by
& kind contributors
just like you!

(here's why

Copies of the site on CD
are available at minimal cost.

.