ON 20100327@10:41:23 AM at page: On a web page you were interested in at: http://www.piclist.com/techref/microchip/math/sq/index.htm#40264.4454050926 Martin Sturm[MDS-gmail-IE8] Code:
; UNTESTED;
; Square 10bit unsigned
;  by Martin Sturm  2010
;
;   a*a --> r
;   a = aH:aL		(10bit, right justified) (not modified)
;	r = rH:rM:rL	(20bit result)
;
; algorithm
;  r = 2^16*(aH*aH) + 2^8*(2*aH*aL) + aH*aL
;             2x2            2x8       8x8
;  r =        rH      :      rM     :  rL
;
; The 2x2 and 2x8 mult done inline, 8x8 mult algo is your choice
; if using a standard inline 8x8 multiplication (36 instr, 36 cycles)
;   65 instructions, 51-65 cycles, 58 avg
;
; incorrect result if A has non-zero bits above the 10th
;  use optional ANDLW 0x03 to correct for this if necessary
;
SQR_10x10 MACRO aH,aL, rH,rM,rL
 LOCAL g1, g2

	MULT_8x8 aL, aL, rM,rL		; rM:rL <-- aL*aL
		; 36 instr, 36 cycles

	; rH = aH*aH   [2b x 2b square] (8 instr, 8 cyc)
	CLRF	rH
	MOVFW	aH	; multiplicand in W
;	ANDLW	0x03	; prevent errors if aH non-zero above 10th bit
	CLRC
	BTFSC	aH,1
	ADDWF	rH,F	; never sets carry
	RLF	rH,F
	BTFSC	aH,0
	ADDWF	rH,F	; never sets carry

	; rH:rM += 2*aH*aL  [2b x 8b mult] (21 instr, 7-21 cyc, avg. 14)
	MOVFW	aL		; multiplicand in W
	BTFSS	aH,0
	GOTO	g1
	ADDWF   rM,F	; add bH
	SKPNC
	INCF	rH,F
	ADDWF   rM,F	; add bH
	SKPNC
	INCF	rH,F	; rH small so this always clear carry
g1
	BTFSS	aH,1
	GOTO	g2
	RLF     aL,W	; carry guaranteed clear before here
			;  W now holds (2*aL & 0xFF)
	ADDWF   rM,F	; add W to rM
	SKPNC
	INCF	rH,F
	ADDWF   rM,F	; add W to rM
	SKPNC
	INCF	rH,F
	MOVLW	0x02
	BTFSC	aL,7	;
	ADDWF	rH,F	; add twice the upper bit of 2*aL
g2
	ENDM
ON 20100330@3:37:46 PM at page: On a web page you were interested in at: http://www.piclist.com/techref/microchip/math/sq/10b-ms.htm# Martin Sturm[MDS-gmail-IE8] edited the page. Difference: http://www.piclist.com/techref/diff.asp?url=H:\techref\microchip\math\sq\10b-ms.htm&version=0 ON 20100330@5:06:28 PM at page: On a web page you were interested in at: http://www.piclist.com/techref/microchip/math/sq/10b-ms.htm# Martin Sturm[MDS-gmail-IE8] edited the page. Difference: http://www.piclist.com/techref/diff.asp?url=H:\techref\microchip\math\sq\10b-ms.htm&version=1 ON 20100330@5:10:46 PM at page: On a web page you were interested in at: http://www.piclist.com/techref/microchip/math/sq/10b-ms.htm# Martin Sturm[MDS-gmail-IE8] edited the page. Difference: http://www.piclist.com/techref/diff.asp?url=H:\techref\microchip\math\sq\10b-ms.htm&version=2