PIC Microcontroller Math Method

Square 10 bits

;by John Payson

clrf DstH
clrf DstM
clrf DstL
movf SrcL,w
andlw $0F
btfss Src,4
addwf DstM
rrf DstM
rrf DstL
btfss Src,5
addwf DstM
rrf DstM
rrf DstL
btfss Src,6
addwf DstM
rrf DstM
rrf DstL
btfss Src,7
addwf DstM
call Sqr4
addwf DstL
swapf SrcL
andlw $0F
call Sqr4
addwf DstM
; At this point, 16-bit result is in DstM:DstH
; 25 words of code prior to this point (plus a
; 17-word table-lookup). Total execution time:
; 35 cycles up to this point.
btfss SrcH,0
goto NoBit8
movf SrcL,w
btfsc C
incf DstH
addwf DstM
btfsc C
incf DstH
incf DstH
; Another 9 words for bit 8; 3 or 9 cycles to exec.
NoBit8:
btfss SrcH,1
goto NoBit9
movlw 4
btfss SrcH,0
movlw 8
addwf DstH
rlf SrcL,w
btfsc C
incf DstH
btfsc C
incf DstH
addwf DstM
btfsc C
incf DstH
addwf DstM
btfsc C
incf DstH
; Another 17 words for bit 9; 3 or 17 cycles to execute
; Total worst-case time: 35+26 = 61 cycles.
NoBit9:
retlw 0 ; All done!
Sqr4:
addwf PC
db 0,1,4,9,16,25,36,49,64,81,100,121,144,169,196,225

See also: