See also:
Archive:
; 4x4 bit multiply
; result in W
; by John Payson
movlw 0
bcf C
btfss n1,0
addwf n2,w
rlf n2,f
btfss n1,1
addwf n2,w
rlf n2,f
btfss n1,2
addwf n2,w
rlf n2,f
btfss n1,3
addwf n2,w
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
addwf n2,f
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
addwf n2,f
rrf n2,f
btfss n2,0
addwf n2,f
rrf n2,f
btfss n2,0
addwf n2,f
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
addwf x,F
btfss y,0 ;0000efgh
movlw 0
btfsc y,1
addwf x,W
rlf x,F
btfsc y,2
addwf x,W
rlf x,F
btfsc y,3
addwf x,W ;result in W
; 12 clocks/words
Interested:
See:
32-bit signed integer math routines. add, subtract, multiply, divide, round, sqrt, bin2dec, dec2bin. By Peter Hemsley. +
Questions:
Comments:
;As a thank you for all the code, here is a 32x16 bit Mult.
;Unsigned 32 bit by 16 bit multiplication
;This routine will take a4:a3:a2:a1*b2:b1 -> a6:a5:a4:a3:a2:a1
mult_32_16:
banksel EECON1
; Begin rearrange code
nop
movf a4,w
movwf a6
movf a3,w
movwf a5
movf a2,w
movwf a4
movf a1,w
movwf a3
; End rearrange code
CLRF a2 ; clear partial product
CLRF a1
MOVF a6,W
MOVWF c4
MOVF a5,W
MOVWF c3
MOVF a4,W
MOVWF c2
MOVF a3,W
MOVWF c1
MOVLW 0x08
MOVWF bitcnt
LOOPUM3216A:
RRF b1, F
BTFSC STATUS, C
GOTO ALUM3216NAP
DECFSZ bitcnt, F
GOTO LOOPUM3216A
MOVWF bitcnt
LOOPUM3216B:
RRF b2, F
BTFSC STATUS, C
GOTO BLUM3216NAP
DECFSZ bitcnt, F
GOTO LOOPUM3216B
CLRF a6
CLRF a5
CLRF a4
CLRF a3
RETLW 0x00
BLUM3216NAP:
BCF STATUS, C
GOTO BLUM3216NA
ALUM3216NAP:
BCF STATUS, C
GOTO ALUM3216NA
ALOOPUM3216:
RRF b1, F
BTFSS STATUS, C
GOTO ALUM3216NA
MOVF c1,W
ADDWF a3, F
MOVF c2,W
BTFSC STATUS, C
INCFSZ c2,W
ADDWF a4, F
MOVF c3,W
BTFSC STATUS, C
INCFSZ c3,W
ADDWF a5, F
MOVF c4,W
BTFSC STATUS, C
INCFSZ c4,W
ADDWF a6, F
ALUM3216NA:
RRF a6, F
RRF a5, F
RRF a4, F
RRF a3, F
RRF a2, F
DECFSZ bitcnt, f
GOTO ALOOPUM3216
MOVLW 0x08
MOVWF bitcnt
BLOOPUM3216:
RRF b2, F
BTFSS STATUS, C
GOTO BLUM3216NA
MOVF c1,W
ADDWF a3, F
MOVF c2,W
BTFSC STATUS, C
INCFSZ c2,W
ADDWF a4, F
MOVF c3,W
BTFSC STATUS, C
INCFSZ c3,W
ADDWF a5, F
MOVF c4,W
BTFSC STATUS, C
INCFSZ c4,W
ADDWF a6, F
BLUM3216NA
RRF a6, F
RRF a5, F
RRF a4, F
RRF a3, F
RRF a2, F
RRF a1, F
DECFSZ bitcnt, F
GOTO BLOOPUM3216
nop
return
+
| file: /techref/microchip/math/mul/index.htm, 10KB, , updated: 2008/9/1 12:50, local time: 2008/10/11 19:12,
38.103.63.61:LOG IN
|
| ©2008 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! <A HREF="http://piclist.com/techref/microchip/math/mul/index.htm"> PIC Microcontoller Basic Math Multiplication Methods</A> |
| Did you find what you needed? |
|
o List host: MIT, Site host massmind.org, Top posters @20081011 Apptech, Jinx, Xiaofan Chen, olin piclist, Vitaliy, Alan B. Pearce, William \Chops\ Westfield, JonnyMac, Tamas Rudnai, Gerhard Fiedler, * 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: Shultz Electronics, Timothy Weber, on-going support is MOST appreciated! * Contributors: Richard Seriani, Sr. |
|
.