; Max number to root is 131071 movlw b'00000000' ; 17-bit maximum as the divide by 2 movwf Word3 ; will result in 16-bit which is the movwf S3 ; maximum denominator for the division movlw b'00000001' ; section of program. movwf Word2 movwf S2 movlw b'10100011' movwf Word1 movwf S1 movlw b'10010110' movwf Word0 movwf S0 ; 107414 is the number I want to square root Squareroot clrf denomH movlw d'2' movwf denomL call divide ; Divide by 2 ; Our approximation label_4 movf Word3, w movwf T3 movf Word2, w movwf T2 movf Word1, w movwf T1 movf Word0, w movwf T0 ; Move 32-bit result to temporary movf S3, w movwf Word3 movf S2, w movwf Word2 movf S1, w movwf Word1 movf S0, w movwf Word0 ; Move 107414 back to division registers movf T1, w movwf denomH movf T0, w movwf denomL ; Divide by approximation or result from last ; loop call divide movf T0, w addwf Word0, f call check1 movf T1, w addwf Word1, f call check2 movf T2, w addwf Word2, f call check3 movf T3, w addwf Word3, f goto label_3 ; Add contents of division to approximation or ; result from last loop. check1 btfss STATUS, C return movlw d'1' addwf Word1, f check2 btfss STATUS, C return movlw d'1' addwf Word2, f check3 btfss STATUS, C return incf Word3, f return label_3 clrf denomH movlw d'2' movwf denomL call divide ; Divide by 2 movf T0, w ; Compare previous calculation to current xorwf Word0, w ; calculation. btfss STATUS, Z goto label_4 ; Do not match then loop again. movf T1, w ; If first 8-bits match them check next eight. xorwf Word1, w btfss STATUS, Z goto label_4 movf T2, w xorwf Word2, w btfss STATUS, Z goto label_4 movf T3, w xorwf Word3, w btfss STATUS, Z goto label_4 return ; Square Root Finished divide movlw d'32' ; 32-bit divide by 16-bit movwf temp clrf remH clrf remL label_1 clrc rlf Word0, f ; LSB rlf Word1, f rlf Word2, f rlf Word3, f ; MSB rlf remL, f rlf remH, f ; 16-bit Remainder btfsc STATUS, C ; Check if there is a remainder goto rem ; Deal with remainder movf denomH, w ; Compare partial remainder and denominator subwf remH, w btfss STATUS, Z goto testrem ; Not equal so test if remH is greater movf denomL, w ; High bytes are equal, compare low bytes subwf remL, w testrem btfss STATUS, C ; Carry set if remainder more than or equal to goto decloop ; denominator. rem movf denomL, w ; Subtract denominator from remainder. subwf remL, f btfss STATUS, C ; Do we need a borrow? decf remH, f ; If so take the borrow. movf denomH, w subwf remH, f bsf Word0, 0 decloop decfsz temp, f ; Skip when looped 32 times goto label_1 return