# 24-bit square root routine

Started by July 12, 2004
 Searching the web for a squrae root routine i came up with many usefull math routines in the next address http://techref.massmind.org/techref/microchip For square root i found many routines. I used the 32-bit which for many inputs give irrelevant outputs. Next i tried the 24- bit integer sqrt which follows next. I altered some variables name? I can't figure out what test variable is. I can;t make it work. Any idea? Thanks ; brSQRT24 ; ; Calculates the square root of a twentyfour bit number using the ; binary restoring method. ; ; Result in ResultH,ResultL ; Input in InputH,InputM,InputL ; Test TestH,TestL ; Counter in Counter ; ; 40 words long, uses 8 bytes RAM (inc. 3 holding 24-bit input). ; ;-------------------------------- SQRT24 macro InputH,InputM,InputL,ResultH,ResultL,TestH,TestL local ShftUp local SubTest local BitLoop local Set1 local Set0 clrf ResultH ; clrf ResultL ; clrf TestL ; clrf TestH ; movlw .12 ; movwf Counter ; (6 cycle intro, 8 incl. call) ShftUp rlf InputL,f ; Shift input up 2 places. rlf InputM,f ; (33 cycles per loop if bit is 0) rlf InputH,f ; (29 cycles per loop if bit is 1) rlf TestL,f ; rlf TestH,f ; rlf InputL,f ; rlf InputM,f ; rlf InputH,f ; rlf TestL,f ; rlf TestH,f ; rlf ResultL,f ; Shift root-so-far up by two and append rlf ResultH,f ; ... '01'. rlf ResultL,f ; rlf ResultH,f ; bcf ResultL,1 ; bsf ResultL,0 ; SubTest movf ResultL,W ; subwf TestL,f ; movf ResultH,W ; subwf TestH,f ; btfsc STATUS,C ; goto Set1 movf ResultL,W ; Restore the remainder. addwf TestL,f ; ... (the current bit is 0). movf ResultH,W ; addwf TestH,f ; goto Set0 ; Set1 bsf ResultL,1 ; Set0 rrf ResultH,f ; rrf ResultL,f ; bcf ResultH,7 ; BitLoop decfsz Counter,f ; goto ShftUp endm