Exact match. Not showing close matches.
PICList
Thread
'[PIC]: Multiply 32x8, carry optional  Need Help'
2003\05\20@103118
by
Werner Soekoe

Hi
I have search the PICList website and archive, but cannot seem to find
working 32x8 bit multiplication code. All I need is the 32bit resulting
product, and the carry bits are optional.
I wrote the following from an example on the PICList website by Byron A Jeff
(http://www.piclist.com/techref/microchip/math/mul/23x8bbaj.htm), but cannot
seem to get it to work, and I don't know where I went wrong. Could someone
please help? The answer I keep getting for 123456 x 81 is 17036928 instead
of 9999936
cblock 0x73
Multiplier
Multiplicant:4
Product:4
endc
org 0x10
main
; Setup Multiplicant and Multiplier for d'123456' x d'81' multiplication,
; which should result in d'9999935'
movlw 0x40
movwf Multiplicant+0
movlw 0xE2
movwf Multiplicant+1
movlw 0x01
movwf Multiplicant+2
clrf Multiplicant+3
movlw 0x51
movwf Multiplier
call Multiply32x8
goto $
Multiply32x8
; Clear Product
clrf Product
clrf Product+1
clrf Product+2
clrf Product+3
MultiplyLoop
movf Multiplier, W
btfsc STATUS, Z
return
bcf STATUS, C
rrf Multiplier, F
btfss STATUS, C
goto ShiftLoop
movf Multiplicant+0, W
addwf Product+0, F
btfsc STATUS, C
call CarryByte1
movf Multiplicant+1, W
addwf Product+1, F
btfsc STATUS, C
call CarryByte2
movf Multiplicant+2, W
addwf Product+2, F
btfsc STATUS, C
call CarryByte3
movf Multiplicant+3, W
addwf Product+3, F
ShiftLoop
bcf STATUS, C
rlf Product+0, F
rlf Product+1, F
rlf Product+2, F
rlf Product+3, F
goto MultiplyLoop
CarryByte1
incfsz Product+1, F
return
CarryByte2
incfsz Product+2, F
return
CarryByte3
incf Product+3, F
return

http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email spam_OUTlistservTakeThisOuTmitvma.mit.edu with SET PICList DIGEST in the body
2003\05\20@125220
by
Andrew Warren
2003\05\20@174436
by
James Newton, webhost
2003\05\20@181813
by
Andrew Warren
Earlier today, I wrote:
> Try shifting your multiplier left, instead of right (change "rrf
> Multiplier,F" to "rlf Multiplier,F").
I just realized that once you do that, you'll also need to add a
counter to ensure that you shift your Product enough times when
Multiplier has trailing zeroes. In other words, your existing
code
movf Multiplier, W
btfsc STATUS, Z
return
should be replaced by something like
decfsz loop_counter
goto $+2
return
Andy
=== Andrew Warren  @spam@aiwKILLspamcypress.com
=== Principal Design Engineer
=== Cypress Semiconductor Corporation
===
=== Opinions expressed above do not
=== necessarily represent those of
=== Cypress Semiconductor Corporation

http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email KILLspamlistservKILLspammitvma.mit.edu with SET PICList DIGEST in the body
2003\05\21@030529
by
Werner Soekoe

Thanks to everyone that helped with this problem. The code is now working,
and I wish to share it with everyone. I also posted it on the PICList.com
website in reply to http://www.piclist.com/math/mul/23x8bbaj.
Here is what I posted:
I worked with this code of Bryon A Jeff to create a 32x8 bit multiplier that
gives a 32 bit result, without any carry. The solution is as follow:
Variable definitions are done as follow:
cblock 0x73
Multiplier
Multiplicant:4
Product:4
MultiCount
endc
Please note that the 0x73 address of the variables should be changed to suit
your application. The variables take a total of 10 bytes of RAM.
The code now looks like this:
;
Multiply32x8
; Clear Product
clrf Product
clrf Product+1
clrf Product+2
clrf Product+3
; Test for an 0 multiplier
movf Multiplier, W
btfsc STATUS, Z
return
; Setup the counter for 8 bits
movlw 0x08
movwf MultiCount
MultiplyLoop
rlf Multiplier, F
btfss STATUS, C
goto ShiftLoop
movf Multiplicant+0, W
addwf Product+0, F
btfsc STATUS, C
call CarryByte1
movf Multiplicant+1, W
addwf Product+1, F
btfsc STATUS, C
call CarryByte2
movf Multiplicant+2, W
addwf Product+2, F
btfsc STATUS, C
call CarryByte3
movf Multiplicant+3, W
addwf Product+3, F
ShiftLoop
decfsz MultiCount, F
goto $+2
return
bcf STATUS, C
rlf Product+0, F
rlf Product+1, F
rlf Product+2, F
rlf Product+3, F
goto MultiplyLoop
CarryByte1
incfsz Product+1, F
return
CarryByte2
incfsz Product+2, F
return
CarryByte3
incf Product+3, F
return
;
To test the multiplication, you can use the following code:
movlw 0x7D
movwf Multiplicant+0
movlw 0x8E
movwf Multiplicant+1
movlw 0xAB
movwf Multiplicant+2
movlw 0x7A
movwf Multiplicant+3
movlw 0x8F
movwf Multiplier
call Multiply32x8
This does 0x7AAB8E7D times 0x8F, which should result in 0x85D497D3. Please
note it does not return 0x4485D497D3, since the 5th byte in the result would
have been the carry, which is disgarded. This routine can be used as it is
for 24x8 bit multiplication, which will result in a full 32 bit product,
with no carry needed. The total instruction cycles to perform the above
multiplication is 221. This will vary, according to the complexity of the
numbers, but should always be below 1ms when running the PIC at 4Mhz or
higher.
Hope this code helps anyone.
Cheers
Werner

http://www.piclist.com hint: To leave the PICList
RemoveMEpiclistunsubscriberequestTakeThisOuTmitvma.mit.edu
>
2003\05\21@100854
by
Scott Dattalo

On Wed, 21 May 2003, Werner Soekoe wrote:
> Thanks to everyone that helped with this problem. The code is now working,
> and I wish to share it with everyone. I also posted it on the PICList.com
> website in reply to http://www.piclist.com/math/mul/23x8bbaj.
Now that you've got it working, I guess it's time to optimize it! The
32bit + 32bit operation can be implemented more efficiently by this:
; 32bit + 32bit addition:
; This is just an extension of 16bit + 16bit addition
movf Multiplicant+0, W
addwf Product+0, F
movf Multiplicant+1, W
skpnc
incfsz Multiplicant+1, W
addwf Product+1,F
movf Multiplicant+2, W
skpnc
incfsz Multiplicant+2, W
addwf Product+1,F
movf Multiplicant+3, W
skpnc
incfsz Multiplicant+3, W
addwf Product+1,F
; note that the carry out is valid, but we don't need it.
For the "loop counter", you can change it to this:
; Setup the counter for 8 bits
movlw 1
movwf MultiCount
and then at the ShiftLoop label:
ShiftLoop:
rlf MultiCount,F ; Clear carry and check if we're done
skpnc
return
rlf Product+0, F
rlf Product+1, F
rlf Product+2, F
rlf Product+3, F
goto MultiplyLoop
BTW, I'm almost positive that I've seen these snippets implement in a
multiplication routine before.
Scott

http://www.piclist.com hint: To leave the PICList
spamBeGonepiclistunsubscriberequestspamBeGonemitvma.mit.edu
>
2003\05\21@103453
by
Werner Soekoe
2003\05\21@105718
by
Scott Dattalo
More... (looser matching)
 Last day of these posts
 In 2003
, 2004 only
 Today
 New search...