Searching \ for 'multiplication routing 24x8' in subject line. ()
Help us get a faster server
FAQ page: www.piclist.com/techref/method/math.htm?key=multiplication
Search entire site for: 'multiplication routing 24x8'.

Truncated match.
'multiplication routing 24x8'
2000\03\22@193352 by

Byron,

Thank you for your code. I tried to run it, but I am having certain
problems. Take for example,
the following problem, in 2s complement.

The following variable declerations are used:

TWOFOUR2 B'00001000'  Equivalent: c0=534035 (dec)
TWOFOUR1 B'00100110'
TWOFOUR0 B'00010011'

MULTIPLIER B'00000111'  Equivalent: u0=7 (dec)

Result: (MPLAB)

PROD2        B'00001000'   Equivalent: c0u0 (dec)
PROD1        B'00100110'
PROD0      B'00010011'

This does not seem to be the correct answer.

Any ideas on where the error could possibly be, or is it just me getting
confused with 2's comp...?

Regards,

Saurabh
======================================================CODE
USED================================

mult24x8:
clrf    prod0           ; Clear the product
clrf    prod1
clrf    prod2
clrf    prod3
clrf    twofour3        ; Clear the top byte of the multiplicand
return

multloop:
movf    mulitiplier,W   ; Check to see if eight bit is empty
btfsc   STATUS,Z        ; Done if zero
return
bcf     STATUS,C        ; clear carry
rrf     mulitiplier,F   ; Get the next bit
btfss   STATUS,C        ; Add if carry is set
goto    multshift       ; shift only if 0
movf    twofour0,W      ; Add the 32 bits of multiplicand to product
btfsc   STATUS,C        ; Don't increment if no carry
incf    prod1,F         ; Add one to next byte if carry occured
movf    twofour1,W      ;
btfsc   STATUS,C        ; Don't increment if no carry
incf    prod2,F         ; Add one to next byte if carry occured
movf    twofour2,W      ;
btfsc   STATUS,C        ; Don't increment if no carry
incf    prod3,F         ; Add one to next byte if carry occured
movf    twofour3,W      ;

multshift:
bcf     STATUS,C        ; clear carry
rlf     twofour0,F      ; Shift the 24 bits one bit to the left
rlf     twofour1,F
rlf     twofour2,F
rlf     twofour3,F
return
return

Hi,

movf    twofour0,w

movf    twofour1,w
btfsc   STATUS,c
incfsz twofour1,w

movf    twofour2,w
btfsc   STATUS,c
incfsz twofour2,w

movf    twofour3,w
btfsc   STATUS,c
incfsz twofour3,w

regards,
Reggie

Saurabh Sinha wrote:
{Quote hidden}

Hi,

Sorry, this still does not work. (I think!)

TWOFOUR0    B'00010011'
TWOFOUR1    B'00100110'
TWOFOUR2    B'00000000'
TWOFOUR3    B'00000000'
PROD0       B'00010011'
PROD1       B'00100110'
PROD2       B'00000000'
PROD3       B'00000000'
MULITIPLIER B'00000111'

if I use the code below.

Thanks in advance for taking a look.

Regards,

Saurabh

==============================================
mult24x8:
clrf    prod0           ; Clear the product
clrf    prod1
clrf    prod2
clrf    prod3
clrf    twofour3        ; Clear the top byte of the multiplicand
return

multloop:
movf    mulitiplier,W   ; Check to see if eight bit is empty
btfsc   STATUS,Z        ; Done if zero
return
bcf     STATUS,C        ; clear carry
rrf     mulitiplier,F   ; Get the next bit
btfss   STATUS,C        ; Add if carry is set
goto    multshift       ; shift only if 0

movf    twofour0,w

movf    twofour1,w
btfsc   STATUS,c
incfsz twofour1,w

movf    twofour2,w
btfsc   STATUS,c
incfsz twofour2,w

movf    twofour3,w
btfsc   STATUS,c
incfsz twofour3,w

multshift:
bcf     STATUS,C        ; clear carry
rlf     twofour0,F      ; Shift the 24 bits one bit to the left
rlf     twofour1,F
rlf     twofour2,F
rlf     twofour3,F
return
return
==============================================

{Quote hidden}

It's not. It's the partial product generated when you add the original
multiplicand to the first product, namely 0. Note that it's exactly the
same as the original number in TWOFOUR.
>
> Any ideas on where the error could possibly be, or is it just me getting
> confused with 2's comp...?

Sure. The program didn't loop to multloop so it never finished the
multiplication.

I'm sure I said that I generated that code off the top of my head. Just checked
the archive and the goto at the bottom of the loop is there.

I've now compiled it with picasm and tested it with gpsim on my Linux box.
Surprisingly enough I nailed it the first time. It does generate the
expected result if the goto is listed at the bottom of the routine.

Also note that I set up mult24x8 as a subroutine to be called.

>

No problem.

{Quote hidden}

goto    multloop ; need to add all partial products.

>  return ; Neither of these returns are required.
> return ; The routine will return when the multiplier is 0.

>

> mult24x8:
>         clrf    prod0           ; Clear the product
>         clrf    prod1
>         clrf    prod2
>         clrf    prod3
>         clrf    twofour3        ; Clear the top byte of the multiplicand
> return

The total effect of calling this routine is to clear these five variables.
The rest of the code is irrelevant since it is unreachable.  Get rid of
this bogus 'return', and replace the two 'return's at the very bottom with
a 'goto multloop', and it should work.
Jason Harper

Byron,

Thank you for your code.  It is 100%........

Regards,

Saurabh

----- Original Message -----
From: Byron A Jeff <byronCC.GATECH.EDU>
To: <PICLISTMITVMA.MIT.EDU>
Sent: Thursday, March 23, 2000 8:50 AM
Subject: Re: multiplication routing 24x8

{Quote hidden}

checked
{Quote hidden}

product
{Quote hidden}

>
> Hi,
>
> Sorry, this still does not work. (I think!)

I still don't see the goto at the bottom of the routine and the returns at
the bottom are still in plae.

{Quote hidden}

How exactly are you defining these values above? It's looks like you're
doing equates. Let me give you the rest of the setup code I used for my test:

prod0           equ     20
prod1           equ     21
prod2           equ     22
prod3           equ     24

twofour0        equ     25
twofour1        equ     26
twofour2        equ     27
twofour3        equ     28

multiplier      equ     29

STATUS                       EQU     H'0003'
C                            EQU     H'0000'
Z                            EQU     H'0002'
W                               EQU     0
F                               EQU     1

movlw   7
movwf   multiplier
;TWOFOUR2 B'00001000'  ; Equivalent: c0=534035 (dec)
movlw   B'00001000'  ;
movwf   twofour2
;TWOFOUR1 B'00100110'
movlw   B'00100110'  ;
movwf   twofour1
;TWOFOUR0 B'00010011'
movlw   B'00010011'  ;
movwf   twofour0
call    mult24x8
loop    goto    loop

{Quote hidden}

Hi,

I think the routine may not work on some values because of the 32 bit
addition.  The carry will not be passed to prod2 if prod1 is 0xFF and
there is a carry from the last addition of prod0 and twofour0.  Example:
0xFFFFFF * 3 will have and erroneous result.

regards,
Reggie

Saurabh Sinha wrote:
>
> Byron,
>
> Thank you for your code.  It is 100%........
>
> Regards,
>
> Saurabh
>
> {Original Message removed}
>
> Hi,
>
> I think the routine may not work on some values because of the 32 bit
> addition.  The carry will not be passed to prod2 if prod1 is 0xFF and
> there is a carry from the last addition of prod0 and twofour0.  Example:
> 0xFFFFFF * 3 will have and erroneous result.

Agreed. There needs to be some additional code to propogate the carry all the
way through. Let me take a stab at it.

The updates are below. I tested them and they seem to work OK.

BAJ
{Quote hidden}

> > {Original Message removed}
Hi Byron,

There is a generic multibyte addition routine that is on the Microchip
docs.  I don't know who made it but works like a charm.

;A = A + B

movf    B0,w

movf    B1,w            ; W = next byte
skpnc                   ; skip next instr if there's a carry
incfsz B1,w            ; inc W and skip next if B1 = 0xFF, C is still set
addwf A1,f            ; add it except if C and B1 = 0xFF

movf    B2,w            ; carry is propagated here if B1 was 0xFF
skpnc                   ; and C was set
incfsz B2,w            ; or from the last addition

movf    B3,w
skpnc
incfsz B3,w

.
.
.

subtraction is also similar:

; A = A - B

movf    B0,w
subwf   A0,f

movf    B1,w
skpc
incfsz B1,w
subwf A1,f

.
.
.

regards,
Reggie

Byron A Jeff wrote:
{Quote hidden}

> > > {Original Message removed}
<x-flowed>At 01:14 PM 3/24/00 +0800, Regulus Berdin wrote:
{Quote hidden}

same kind of typo that crept into my post:

>         movf    B3,w
>         skpnc
>          incfsz B3,w

dwayne

Dwayne Reid   <dwaynerplanet.eon.net>
Trinity Electronics Systems Ltd    Edmonton, AB, CANADA
(780) 489-3199 voice          (780) 487-6397 fax

Celebrating 16 years of Engineering Innovation (1984 - 2000)

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Do NOT send unsolicited commercial email to this email address.
This message neither grants consent to receive unsolicited
commercial email nor is intended to solicit commercial email.

</x-flowed>
{Quote hidden}

I see. It basically does the same carry propogation I did in line instead
of out of sequence.

BAJ

{Quote hidden}

I've updated the web site to:

mult24x8:
clrf    prod0           ; Clear the product
clrf    prod1
clrf    prod2
clrf    prod3
clrf    twofour3        ; Clear the top byte of the multiplicand
multloop:
movf    eight,W         ; Check to see if eight bit is empty
btfsc   STATUS,Z        ; Done if zero
return
bcf     STATUS,C        ; clear carry
rrl     eight,F         ; Get the next bit
btfss   STATUS,C        ; Add if carry is set
goto    multshift       ; shift only if 0
movf    twofour0,W      ; Add the 32 bits of multiplicand to product
btfsc   STATUS,C        ; Don't increment if no carry
call    carryprop1      ; Propigate carry to next byte(s)
movf    twofour1,W      ;
btfsc   STATUS,C        ; Don't increment if no carry
call    carryprop2      ; Propigate carry to next byte(s)
movf    twofour2,W      ;
btfsc   STATUS,C        ; Don't increment if no carry
incf    prod3,F         ; Add one to next byte if carry occured
movf    twofour3,W      ;
multshift:
bcf     STATUS,C        ; clear carry
rlf     twofour0,F      ; Shift the 24 bits one bit to the left
rlf     twofour1,F
rlf     twofour2,F
rlf     twofour3,F
goto    multloop

carryprop1:
incfsz  prod1,F
return
carryprop2:
incfsz  prod2,F
return
carryprop3:
incf    prod3,F
return

Please let me know if this is not correct.

---
James Newton jamesnewtongeocities.com 1-619-652-0593
http://techref.massmind.org NEW! FINALLY A REAL NAME!