# PICMicrocontollerMathMethod

## Multiply 16x16 bit unsigned from Martin Sturm

Code:

• Martin Sturm shares this code:
• ```
; 16bit by 16bit unsigned multiply
;  by Martin Sturm 2010
; tested
;
; aH:aL * bH:bL --> r4:r3:r2:r1
;
; t1 is a temporary variable
; if modifying a or b is ok, aL, bL, or bH can be used as t1
;
; 140 instructions, 140 cycles
;

; helper macro
mmac MACRO A,bit, u2,u1
BTFSC	A,bit
RRF	u2,F
RRF	u1,F
ENDM

MULT_16x16_FASTEST MACRO aH,aL, bH,bL, r4,r3,r2,r1, t1

; 16x8 multiply  aH:aL * bL -> r3:r4:r1
CLRF	r3
CLRF	r1
CLRC
MOVFW	bL
mmac	aL,0, r3,r1
mmac	aL,1, r3,r1
mmac	aL,2, r3,r1
mmac	aL,3, r3,r1
mmac	aL,4, r3,r1
mmac	aL,5, r3,r1
mmac	aL,6, r3,r1
mmac	aL,7, r3,r1
CLRF	r4
; carry already clear from last RRF of mmac above
; bL still in W
mmac	aH,0, r3,r4
mmac	aH,1, r3,r4
mmac	aH,2, r3,r4
mmac	aH,3, r3,r4
mmac	aH,4, r3,r4
mmac	aH,5, r3,r4
mmac	aH,6, r3,r4
mmac	aH,7, r3,r4

; 16x8 multiply  aH:aL * bH -> r4:t1:r2
;  middle byte from previous 16x8 multiplication starts in r4
;  but ends in r2
CLRF	r2
MOVFW	bH
; carry already clear from last RRF of mmac above
mmac	aL,0, r4,r2
mmac	aL,1, r4,r2
mmac	aL,2, r4,r2
mmac	aL,3, r4,r2
mmac	aL,4, r4,r2
mmac	aL,5, r4,r2
mmac	aL,6, r4,r2
mmac	aL,7, r4,r2
; W still holds bH
CLRF	t1	; bH,bL, or aL may be used for t1
; carry already clear from last RRF of mmac above
mmac	aH,0, r4,t1
mmac	aH,1, r4,t1
mmac	aH,2, r4,t1
mmac	aH,3, r4,t1
mmac	aH,4, r4,t1
mmac	aH,5, r4,t1
mmac	aH,6, r4,t1
mmac	aH,7, r4,t1

; add middle byte aH:aL * bH to upper byte of aH:aL * bL
MOVFW	t1
SKPNC
INCF	r4,F

ENDM
```

Questions:

• Hi, have never used macros, so how would I insert this code into my own and call it from within my code? where do I put my two numbers to be multiplied and where do I look for the answer?
Thanks for your help & please excuse my ignorance, I am starting from a point of very little knowlege. James Newton of Massmind replies: You just need to define your 9 byte wide variables, which you can call anything you like, but here we called them aH,aL, bH,bL, r4,r3,r2,r1, and t1, initialize aH to the high 8 bits of the 16 bit multiplicand, aL to the low 8 bits, bH to the high 8 bits of the multiplier and bL to the low 8 bits, you don't have to initialize the rest, and then call the macro like this:
MULT_16x16_FASTEST aH,aL, bH,bL, r4,r3,r2,r1, t1
The answer will be in the r variables.
+

+

 file: /Techref/microchip/math/mul/m16x16mds.htm, 3KB, , updated: 2010/7/13 15:15, local time: 2024/2/27 05:43, owner: MDS-gmail-IE8, TOP NEW HELP FIND:  3.236.145.153: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 16x16 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.

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"

 PICList 2024 contributors: o List host: MIT, Site host massmind.org, Top posters @none found - 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.

.