PICMicrocontollerMathMethod

Multiply 8 x 8 bits

From Andrew Warren

Posted to the PICList

```; Enter with multiplier in W-Reg, multiplicand in "PRODLO".
; Exits with product in PRODHI:PRODLO.

MPY8X8:

CLRF PRODHI

CLRF COUNT
BSF COUNT,3

RRF PRODLO,F

LOOP:

SKPNC

RRF PRODHI,F
RRF PRODLO,F

DECFSZ COUNT
GOTO LOOP
```

This can also be unrolled for an even faster (and not much larger version)

```;***************************************************************************
;**  time efficient multiplication 8 bit x 8 bit = 16 bit (unsigned)
;**
;**  company:       elektronik 21 GmbH
;**  programmer:            Martin Schaefer (idea from Andrew Warren)
;**
;**  execution time:  fixed 38 cycles  (with jump in and jump out) !!!
;**  code length:           35 words
;**  multiplier:            w
;**  multiplicand:    resultlo
;**  result:        resulthi:resultlo
;***************************************************************************
MUL8X8   CODE
Mul8x8                          ;* 2 cycles for call - instruction
GLOBAL Mul8x8, resulthi, resultlo

mult    MACRO
btfsc   STATUS,C
rrf     resulthi,F
rrf     resultlo,F
ENDM

clrf    resulthi                ;* 1 cycle
rrf     resultlo,F              ;* 1 cycle

mult                            ;* 4 cycles
mult                            ;* 4 cycles
mult                            ;* 4 cycles
mult                            ;* 4 cycles
mult                            ;* 4 cycles
mult                            ;* 4 cycles
mult                            ;* 4 cycles
mult                            ;* 4 cycles

retlw 0                 ;* 2 cycles
;***************************************************************************

```

Questions:

i dont get the BSF count,3 line.

i am seeing...

clear result file and count file.
set count bit(s) for desired number of loops.

enter loop, decrement count file 1 bit, skip if clear.
add result to original, shift one place left.

repeat.

setting bit 3 only gives me five loops dont it? 0100= 4. shouldnt it be 000-0111, aka bits 3,2,1, for 8 loops in total? or bit 4? im also confused if i count 0 as a cycle or not :)

or am i not seeing something/not understanding something here?

i must be, noone else is saying anything!
James Newton of MassMind replies: Bit number 3 is the 4th bit in COUNT. Think of it this way, BSF COUNT,0 would set the 1st bit. Setting the first bit, makes COUNT 8.+

+

The destination (where the values are stored) are missing in Andrew Warren's multiplication script, MPLAB told me during the compilation that it will use the default destinations. Is this correct or can anybody tell me the correct destinations (f or w) for all the script lines where a destination should be inserted to specify where the calculation output should be saved?

James Newton of Massmind replies: As the documentation says, the output is save in PRODHI:PRODLO. You decide what those labels point to by assigning the labels values in your code.+

+

write an algorithm in PIC16F874 assembly to 8x8 bit unsigned multiplication . Multiplies M1 & M2 leaving the result in Rhi & Rlo .
It loops through the addition of M2 to itself M1 times.
James Newton replies: "It puts the book on its table and it reads it in..."+

+

• I thought at first there might be a problem if the routine was called when Cy was set, however on examination I realised that PRODLO is shifted 9 times in total = so any spurious Cy shifted in at the start is shifted out at the end = very clever !
Other code snippits I've seen (including Microchips own AN526) waste an instruction to BCF the carry ...
+
• jb79_ATT_gmx_DOTT_at
Hello again!
The code works with the destination "f", I also did some tests, it seems the default destination is everytime the register itself, the W register is never changed by instructions with missing destination.
So we can say (for example): "RRF registername,1" is eqal to "RRF registername" and also "RRF registername,f", the only thing is that some compilers/linkers don't like the shorter instruction code and produce an information message.

+
• jb79_ATT_gmx_DOTT_at
Hello James!
Thanks for the reply, I know about the fact where the result of the multiplication will be stored.
Sorry that my question was not well formulated, so I try again. Some instuctions for the PIC microprocessor like ADDWF need a destination, normally the W register or the register where the value comes from. So the full syntax of this command would be ADDWF PRODHI,0 when the result of the calculation should be stored in the W register or ADDWF PRODHI,1 if the result should overwrite the old value stored in the PRODHI register. That's what my question is about, this destination is missing in the code example from Andrew Warren and my compiler (MPLAB/MPLINK) told me that the default destination was used, maybe this is wrong and so the calcualtion wouldn't work.
James Newton of Massmind replies: Ah, ok... based on the fact that the macro example uses ",f" as the destination, I assume the original code should also use that as the destination. I've edited the page to make that correction. I'm not sure what MPLAB uses as the default destination, but if it is the file register and not W, then the code would work fine as originally written.+

+

 file: /Techref/microchip/math/mul/8x8.htm, 7KB, , updated: 2019/3/4 12:50, local time: 2019/3/20 17:31, TOP NEW HELP FIND:  34.236.145.124:LOG IN

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

 PICList 2019 contributors: o List host: MIT, Site host massmind.org, Top posters @20190320 RussellMc, David Van Horn, AB Pearce - UKRI STFC, Neil, James Cameron, Allen Mulvey, Dwayne Reid, Manu Abraham, Jim, Harold Hallikainen, * 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.

.