PICMicrocontollerMathMethod

Square 10 bits

Nikolai Golovchenko says:

[The] debugged version: {ed: with correction from Vladyslav Borodavka Thank you!}
```
;***********************************************
; Square 10 bits
;
; 6 Aug 2000 by Nikolai Golovchenko
; Based on the original version of John Payson
;
; Input:
;  SrcH:SrcL
; Output:
;  DstH:DstM:DstL
;
; Instructions: 68
; Execution time(including return): 51+5*3+1=67
;
; Description:
;  The goal is calculation of Dst = Src^2
;
;  Src may be rewritten as:
;  	Src = SrcH*256 + SrcL
;  Let's introduce variables a, ah, al, and b:
;  	a = ah*16 + al = SrcL
;  	b = SrcH
;  Then
;	Src^2 = (256*b+a)^2=65536*b^2+512*b*a+a^2
;
;  Sqr4 routine calculates squares for 4 bit data
;  (b, al, and ah) using look-up table.
;
;  Now we can find b^2 by Sqr4 and multiply it by 65536,
;  and calculate 512*b*a.
;
;  To calculate a^2 let's expand it in byte halves:
;  a^2=(16*ah+al)^2=256*ah^2+32*ah*al+al^2
;
;  So,
;	Src^2=65536*b^2+512*b*a+256*ah^2+32*ah*al+al^2
;
;  This is the algorithm of how to square a 10 bit number
;  using Sqr4 look-up table.
;
;***********************************************
Sqr10
clrf DstH	;clear result
clrf DstM
clrf DstL
clrc		;clear carry
;find 32*ah*al
movf SrcL, w	;w = SrcL
andlw 0x0F
btfsc SrcL, 4
rrf DstM, f	;use carry after addition
rrf DstL, f
;DstM:DstL=ah<0>*al*256/2
btfsc SrcL, 5
rrf DstM, f
rrf DstL, f
;DstM:DstL=(ah<0>*al*256/2 + ah<1>*al*256)/2=64*al(ah<0>+2*ah<1>)
btfsc SrcL, 6
rrf DstM, f
rrf DstL, f
;DstM:DstL=32*al(ah<0>+2*ah<1>+4*ah<2>)
btfsc SrcL, 7
;DstM:DstL=32*al(ah<0>+2*ah<1>+4*ah<2>+8*ah<3>)=32*ah*al
;(maximum value = 0x1C20)

;Now add squared al and ah
call Sqr4
skpnc
incf DstM, f	;propagate carry to DstM

swapf SrcL, w
andlw 0x0F	;w = ah
call Sqr4
addwf DstM, f	;carry is reset

;At this point DstM:DstH contains a^2

;512*b*a=512*b*a=512*(2*b<1>+b<0>)*<128*a<7>+a<0:6>)=
;=65536*b*a<7>+256*(2*a<0:6>*b<0>+2*a<0:6>*b<1>+2*a<0:6>*b<0>)

movf SrcH, w	;Dst += 512*b*(128*a<7>) = 65536*b*a<7>
btfsc SrcL, 7
addwf DstH, f  ;carry is reset

rlf SrcL, w	;w = a<0:6>*2 (a<7> is already used)
clrc
btfsc SrcH, 0
addwf DstM, f	;Dst += 512*b<0>*a<0:6>
skpnc
incf DstH, f

clrc		;Dst += 512*b<1>*a<0:6>
btfsc SrcH, 1
skpnc
incf DstH, f

clrc		;Dst += 512*b<1>*a<0:6>
btfsc SrcH, 1
skpnc
incf DstH, f

movf SrcH, w
call Sqr4
retlw 0		; All done!
Sqr4:			;Look-up table for 4 bit squares
DT 0,1,4,9,16,25,36,49,64,81,100,121,144,169,196,225

;***********************************************

```

The original version:

```	clrf DstH
clrf DstM
clrf DstL
movf SrcL,w
andlw \$0F
btfss Src,4
rrf DstM
rrf DstL
btfss Src,5
rrf DstM
rrf DstL
btfss Src,6
rrf DstM
rrf DstL
btfss Src,7
call Sqr4
swapf SrcL
andlw \$0F
call Sqr4
; 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
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
rlf SrcL,w
btfsc C
incf DstH
btfsc C
incf DstH
btfsc C
incf DstH
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:
db 0,1,4,9,16,25,36,49,64,81,100,121,144,169,196,225
```

Questions:

• Hi guys, I'm not sure if i'm the one doing something wrong here, but the method for squaring a 10 bit number that is displayed on this page doesn't seem to work properly. The following function call:

movlw 0x03
movwf SrcH
movlw 0xFF
movwf SrcL
call Sqr10

gives me F3B45 as the square of 3FF... which is obviously wrong. (it should be FF801). Any help/comments ?+

 file: /Techref/microchip/math/sq/10b.htm, 5KB, , updated: 2010/3/30 08:47, local time: 2018/10/23 10:26, TOP NEW HELP FIND:  54.224.247.42:LOG IN

 ©2018 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?Please DO link to this page! Digg it! / MAKE! /  PIC Microcontoller Math Method Square 10 bits

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
 Did you find what you needed? "No. I'm looking for: " "No. Take me to the search page." "No. Take me to the top so I can drill down by catagory" "No. I'm willing to pay for help, please refer me to a qualified consultant" "No. But I'm interested. me at when this page is expanded."

 PICList 2018 contributors: o List host: MIT, Site host massmind.org, Top posters @20181023 RussellMc, Van Horn, David, Sean Breheny, David C Brown, Isaac M. Bavaresco, Neil, Bob Blick, John Gardner, Harold Hallikainen, AB Pearce - UKRI STFC, * Page Editors: James Newton, David Cary, and YOU! * Roman Black of Black Robotics donates from sales of Linistep stepper controller kits. * Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters. * Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated! * Contributors: Richard Seriani, Sr.

.