Dudes: Just a quick note... David Knell and I have been discussing the square-root routine that he posted last week, and a couple of interesting points have come up. David points out that all the routines that the rest of us have posted find the highest integer not greater than the square root; his algorithm finds the NEAREST integer. For example, his routine will return 6 as the square root of 35; all the rest return 5. This is a useful property; anyone who can afford the time that his routine requires should consider using it. Ok... A couple other things have come up in our discussion. Here's David's routine as originally posted: MOVLW 0 CLRC RRF INHI RRF INLO INCF INHI LOOP: ADDLW 1 SUBWF INLO SKPC DECF INHI SKPZ GOTO LOOP There's a minor coding error there: Since the "SUBWF" affects the Z flag as well as the "DECF" does, the routine will occasionally give completely-wrong results. Fortunately, the fix is real easy; the "DECF INHI, SKPZ" combination merely has to be replaced with a "DECFSZ INHI". This has the added benefit of speeding up the code by 15%. Also, the routine won't handle inputs above 65280 (FF00 hex), because it tries to round the square roots of those numbers up to 256, which won't fit in 8 bits. Additionally, it gives "1" as the square root of 0. Each of these problems can be corrected with the addition of only a few lines of code; correcting for 0 takes three instructions and correcting for >65280 takes four. The fully-corrected version of the code is as follows: MOVF INHI,W ;CORRECT FOR 0. BZ DONE ; INCF INHI,W ;CORRECT FOR >65280. SWAPF INHI,W ; BZ DONE ; MOVLW 0 CLRC RRF INHI RRF INLO INCF INHI LOOP: ADDLW 1 SUBWF INLO SKPC DECFSZ INHI GOTO LOOP DONE: With these changes, the routine works fine and still only requires 17 words of program space and no RAM other than the two registers which hold the input. It still isn't EXACTLY right when the square root is something like x.49999, but this is a very minor issue; I mean, who really cares whether the square root of 20022 (141.4991166) is rounded to 141 or 142? -Andy Andrew Warren - fastfwd@ix.netcom.com Fast Forward Engineering, Vista, California http://www.geocities.com/SiliconValley/2499