# PICMicrocontollerMathMethod

## Multiply 16x8 bit from Dingbat

```;--------------------------------------------------------------------+
;   Function: Math : Multiply 16bits by 8bits                        |
;--------------------------------------------------------------------|
;   Filename: M_MUL16x8.inc       |   Environment    : MPLAB         |
;   Version : 1                   |   Microcontroller: PIC16F8x*     |
;   Date    : 21/07/2003          |   OSC MHz/MIPS   : xx / xxx      |
;--------------------------------------------------------------------|
;   Author         : Dingbat                                         |
;   Company        : -                                               |
;   Files required : none                                            |
;--------------------------------------------------------------------|

; Unsigned 16*8 bit Multiply AA(16) * BB(8), Result (CC) is 24bit
; adapted from code found on www.piclist.com
;
; params
;	AA 0:1 (MSB:LSB) 	= number to multiply (16bit)
;	BB					= multiplier (8bit)
;   CC 0:3 (MSB:LSB)	= Product (24bit)
;
; Taking out the loops and inlining the code would save about
; another 20-30 instructions

MUL16x8
clrf	CC+0		;clear result
clrf	CC+1		;
clrf	CC+2		;
movlw	.8
movwf	bitCount	;bit counter set @ number of bits in multiplier

mul_L1					;main loop here
rlf		CC+2,f			;rotate result left (*2)
rlf		CC+1,f			;
rlf		CC+0,f			;
bcf		CC+2,0			;clear LSB of result after rotation
btfss	BB,7			;test msb of multipier
goto	dontAdd			;if bit is clear then dont add
movfw   AA+1      		;Add the 16 bits of AA to product
addwf   CC+2,f      	;
skpnc					;
incf	CC+1,f			;
movfw   AA				;
addwf   CC+1,f      	;
skpnc					;
incf	CC+0,f			;
dontAdd
rlf		BB,f			;rotate multiplier left
decfsz	bitCount,f		;chk if finished all bits in multiplier
goto	mul_L1

return

```
+

Comments:

• The problem is worse than the previous comment suggested. Only AA+1 (the LSByte) has to be zero for the routine to fail, which is a much bigger problem than if the whole AA register were zero+
• The modification suggested by Jordi Agust causes the multiply routine to fail if the AA register set is zero. The zero flag would only be valid if the increment above it was used. In the case of AA being zero, the zero flag is still set from the previous line "addwf CC+2,f", meaning CC+0 will get incremented, returning a non-zero result.+
• Someone said:
I think it must be

skpnz
incf CC+0,f

after the

skpnc
incf CC+1,f

because the instruction incf does not affect the C flag (only the Z flag), so when you make the incf CC+1,f the carry flag doesn't become 0 when CC+1 isn't 00000000. But un CC+1 become 00000000 then the Z flag will become 1 so now I can detect the carry from CC+1 and increment CC+0.

• After the code

skpnc
incf CC+1,f

should continue

skpnc
incf CC+0,f

because the addition may be FF+1... and overflow the register.

David Salgado+

See also:

 file: /Techref/microchip/math/mul/16x8-d.htm, 3KB, , updated: 2013/4/9 07:39, local time: 2022/1/18 23:56, TOP NEW HELP FIND:  54.80.249.22:LOG IN

 ©2022 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

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" "No. But I'm interested. me at when this page is expanded."

.