;_______________________________________________________________________________ ; Routine to Square 12 bit unsigned integer (by Antonie Daas) ; Input: Src<11:0> (12 bits), Output: Dst<23:0> (24 bits) ; 77 instructions, 76 steps incl. return Sqr12 clrf Dst ;required to prevent unwanted carry clrf Dst+1 clrf Dst+2 clrc ;make sure C=0 ;Dst<15:0>= Src<7:0>^2 movf Src,W btfsc Src,0 addwf Dst+1,F ; no carry out rrf Dst+1,F rrf Dst,F ; no carry out btfsc Src,1 addwf Dst+1,F ; no carry out rrf Dst+1,F rrf Dst,F ; no carry out btfsc Src,2 addwf Dst+1,F ; no carry out rrf Dst+1,F rrf Dst,F ; no carry out btfsc Src,3 addwf Dst+1,F ; no carry out rrf Dst+1,F rrf Dst,F ; no carry out btfsc Src,4 addwf Dst+1,F ; no carry out rrf Dst+1,F rrf Dst,F ; no carry out btfsc Src,5 addwf Dst+1,F ; no carry out rrf Dst+1,F rrf Dst,F ; no carry out btfsc Src,6 addwf Dst+1,F ; no carry out rrf Dst+1,F rrf Dst,F ; no carry out btfsc Src,7 addwf Dst+1,F ; no carry out rrf Dst+1,F rrf Dst,F ; no carry out ;+Src<15:8>*Src<7:0>*2^9 (W still has Src) Dst+2= 00000000 Dst+1= ponmlkji C= 0 ;;;;;;;;rrf Dst+2,F ;shift bit q into C (can be omitted as both are 0) C= q rrf Dst+1,F ;align Dst+1 at bit 9, Dst+1= qponmlkj C= i rrf Dst+2,F ;and save bit i Dst+2= i0000000 C= 0 btfsc Src+1,0 ;if Src+1<0> bit is set, then: addwf Dst+1,F ;add Src*2, may give carry r Dst+1= qponmlkj C= r rrf Dst+1,F ;save carry bit r Dst+1= rqponmlk C= j rrf Dst+2,F ;save bit j in Dst+2 Dst+2= ji000000 C= 0 btfsc Src+1,1 ;if Src+1<1> bit is set, then: addwf Dst+1,F ;add Src*4, may give carry s C= s rrf Dst+1,F ;save carry bit s Dst+1= srqponml C= k rrf Dst+2,F ;save bit k in Dst+2 Dst+2= kji00000 C= 0 btfsc Src+1,2 ;if Src+1<2> bit is set, then: addwf Dst+1,F ;add Src*5, may give carry t C= t rrf Dst+1,F ;save carry bit t Dst+1= tsrqponm C= l rrf Dst+2,F ;save bit l in Dst+2 Dst+2= lkji0000 C= 0 btfsc Src+1,3 ;if Src+1<3> bit is set, then: addwf Dst+1,F ;add Src*6, may give carry u C= u rlf Dst+2,F ;carry t into Dst+2 Dst+2= kji0000u C= l rlf Dst+1,F ;restore Dst+1 low bit l Dst+1= srqponml C= t rlf Dst+2,F ;restore Dst+2 bits Dst+2= ji0000ut C= k rlf Dst+1,F ;restore Dst+1 bits Dst+1= rqponmlk C= s rlf Dst+2,F ;restore Dst+2 Dst+2= i0000uts C= j rlf Dst+1,F ;restore Dst+1 low bit j Dst+1= qponmlkj C= r rlf Dst+2,F ;restore Dst+2 bits Dst+2= 0000utsr C= i rlf Dst+1,F ;restore Dst+1 low bit i, Dst+1= ponmlkji C= q rlf Dst+2,W ;Dst+2 in place into W Dst+2= 000utsrq C= 0 ;+Src<15:8>^2*2^16 btfsc Src+1,0 ;if Src+1<0> bit is set, then: addwf Src+1,W ;add Src+1 to W, no carry rlf Src+1,F ;align with bit 1 btfsc Src+1,2 ;if original Src+1<1> bit is set, then: addwf Src+1,W ;add to W, no carry rlf Src+1,F ;align with bit 2 btfsc Src+1,4 ;if original Src+1<2> bit is set, then: addwf Src+1,W ;add to W, no carry rlf Src+1,F ;align with bit 3 btfsc Src+1,6 ;if original Src+1<3> bit is set, then: addwf Src+1,W ;add to W, no carry movwf Dst+2 ;save result in Dst+2 ;following 2 instructions are only needed if Src needs to be returned to its original rlf Src+1,F ;align with bit 4 swapf Src+1,F ;restore Src+1 return