# PICMicrocontollerMathMethod

## Multiply 10x10 bit unsigned from Martin Sturm

If you're looking for a 10bit squaring routine it can be done faster than using this multiply, see SQR_10

• Code:
• Martin Sturm shares this code:
• ```; Multiply 10bit by 10bit unsigned
;  by Martin Sturm  2010
; Tested over full range of 10bit input combinations
;
;   a (10bit) = aH:aL   (not modified)
;   b (10bit) = bH:bL   (not modified)
;   aH:aL * bH:bL --> rH:rM:rL
;
; incorrect result if a or b has non-zero bits above the 10th
;  unless optional ANDLW 0x03 is used
;
; Algorithm
;  r = a*b
;  r = 2^16*(aH*bH) + 2^8*(aH*bL + bH*aL) + aL*bL
;             2x2           2x8     2x8      8x8
; all multiplications are unrolled
;
;  73 instructions, 57-73 cycles, 65 avg
;

; helper macro
mmac MACRO A,bit, uH,uL
BTFSC	A,bit
RRF	uH,F
RRF	uL,F
ENDM

MULT_10x10 MACRO aH,aL, bH,bL, rH,rM,rL
LOCAL g1, g2, g3, g4

; rM:rL = aL*bL   [8x8 bit multiply] (36 cycles)
CLRF	rM
CLRF	rL
CLRC
MOVFW	bL
mmac	aL,0, rM,rL
mmac	aL,1, rM,rL
mmac	aL,2, rM,rL
mmac	aL,3, rM,rL
mmac	aL,4, rM,rL
mmac	aL,5, rM,rL
mmac	aL,6, rM,rL
mmac	aL,7, rM,rL

; rH = aH*bH   [2x2 bit multiply] (8 cycles)
CLRF	rH
MOVFW	bH	; multiplicand in W
;	ANDLW	0x03	; prevent errors if bH non-zero above 10th bit
BTFSC	aH,1	; carry always clear by here
ADDWF	rH,F	; never sets carry
RLF	rH,F
BTFSC	aH,0

; rH:rM += aH*bL  [2-bit x 8-bit] (7-15 cycles, avg 11)
MOVFW	bL	; multiplicand in W
BTFSS	aH,0
GOTO	g1
SKPNC
INCF	rH,F
g1
BTFSS	aH,1
GOTO	g2
CLRC
RLF     bL,W	;  W now holds (2*bL & 0xFF)
ADDWF   rM,F	; add W to rM
SKPNC
INCF	rH,F
BTFSC	bL,7	;
INCF	rH,F	; add the upper bit of 2*bL
g2

; rH:rM += bH*aL  [2-bit x 8-bit] (7-15 cycles, avg 11)
MOVFW	aL	; multiplicand in W
BTFSS	bH,0
GOTO	g3
SKPNC
INCF	rH,F
g3
BTFSS	bH,1
GOTO	g4
CLRC
RLF     aL,W	;  W now holds (2*aL & 0xFF)
ADDWF   rM,F	; add W to rM
SKPNC
INCF	rH,F
BTFSC	aL,7	;
INCF	rH,F	; add the upper bit of 2*aL
g4

ENDM
```

 file: /Techref/microchip/math/mul/10x10ms.htm, 3KB, , updated: 2010/4/14 19:57, local time: 2024/2/23 04:59, owner: MDS-gmail-IE8, TOP NEW HELP FIND:  3.235.145.108:LOG IN

 ©2024 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 Multiply 10x10 bit from Martin Sturm

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 a nice message (short messages are blocked as spam) 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.

Link? Put it here:
if you want a response, please enter your email address:
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"

.