# PICMicrocontollerMathMethod

## Multiply 4 x 4 bits

From John Payson, Ray Gardiner, Dmitry Kiryashov

See further discussion in Square

```; 4x4 bit multiply
; result in W
; by  John Payson
movlw   0
bcf     C
btfss   n1,0
rlf     n2,f
btfss   n1,1
rlf     n2,f
btfss   n1,2
rlf     n2,f
btfss   n1,3
rlf     n2,f
swapf   n2,f
; 15 cycles, putting the result in W and
; leaving n1 and n2 untouched at the end.
; Striking the requirement that n2 be untouched would save the
; last two cycles.
; Note that if n1<=15 and n1*n2<=255 this routine will ; produce a correct result even if n2>15;
; the value in n2 will be trashed in such a case, though.

; 4x4 bit multiply
; by  John Payson
; result in n2...
swapf   n1,w    ; Assume top 4 bits zero
btfss   n2,0
rrf     n2,f
```

If we have Cy=1 before entering this code fragment and we skip first addition we'll got error.

```
btfss   n2,0
rrf     n2,f
btfss   n2,0
rrf     n2,f
btfss   n2,0
rrf     n2,f
; 13 cycles, leaving the result in n2.
; This one won't work if either factor is oversized.

```

```; 4x4 bit multiply
; by Ray Gardiner
;  X*Y ; result left in W
;  X and Y are 4 bits
;
;
CLRW            ; Clear Result
CLRC            ; Clear Carry for RLF later                     ;
BTFSC   Y,0     ; ?*1
ADDWF   X,W     ; W = ?X
RLF     X,F     ; X = 2X
;
BTFSC   Y,1     ; ?*2
ADDWF   X,W     ; W = ?X + ?2X
RLF     X,F     ; W = 4X

BTFSC   Y,2     ; ?*4
ADDWF   X,W     ; W = ?X + ?2X + ?4X
RLF     X,F     ; W = 8X

BTFSC   Y,3     ; ?*8
ADDWF   X,W     ; ?X + ?2X + ?4X + ?8X
;;;;;;;;;;;;;;;;;;;;;;;;;

13 cycles result in W  13 bytes of code space

Looking back, I now realise that is what John Payson has already
posted. Albeit in a different form. Sigh(!)

```

```
;       4 X 4 multiplication
; by Dmitry Kiryashov
movfw   x       ;0000abcd
btfss   y,0     ;0000efgh
movlw   0
btfsc   y,1
rlf     x,F
btfsc   y,2
rlf     x,F
btfsc   y,3
;       12 clocks/words

```

 file: /Techref/microchip/math/mul/4x4.htm, 3KB, , updated: 2016/9/27 18:44, local time: 2024/6/24 10:34, TOP NEW HELP FIND:  3.236.142.143: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 4 x 4 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 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"

.