DSPRelated.com
Forums

MAC and MPY problem in assembly language

Started by haoyeshen May 14, 2005
I am trying to use either MAC or MPY to make a product of two
variables. The problem I have is that the result is always of twice
of the expected. For example, If I multiply 1 by 1, the the result I
got is always 2 other than 1. The following is the code I used; The C portion:

int multiply(int, int);
int some_global = 4;

int main(void)
{
int a=1, b=1, c=0;

c = multiply(a,b);

some_global = c;

return(0);

}

The assembly portion:

section rtlib
org p:

global Fmultiply

Fmultiply:

clr A
move Y1,X0
mpy Y0,X0,A
move A0,Y0
rts

endsec

Could someone tell me why this is happening. Thanks for the help


The DSP is not a floating point machine, it's fixed point. This is
discussed in the Family Manual as well as several Application Notes...

--
dB --- haoyeshen <haoyeshen@haoy...> wrote:
> I am trying to use either MAC or MPY to make a product of two
> variables. The problem I have is that the result is always of twice
> of the expected. For example, If I multiply 1 by 1, the the result I
> got is always 2 other than 1. The following is the code I used; > The C portion:
>
> int multiply(int, int);
> int some_global = 4;
>
> int main(void)
> {
> int a=1, b=1, c=0;
>
> c = multiply(a,b);
>
> some_global = c;
>
> return(0);
>
> }
>
> The assembly portion:
>
> section rtlib
> org p:
>
> global Fmultiply
>
> Fmultiply:
>
> clr A
> move Y1,X0
> mpy Y0,X0,A
> move A0,Y0
> rts
>
> endsec
>
> Could someone tell me why this is happening. > Thanks for the help >

__________________________________________________




The problem is that MPY is a fixed point factional multiply, while you are likely looking for the integer multiply result.
These commands differ by a 1 bit shift (i.e. x2) from each other in the result. Check in the family manual section on the ALU.
Some DSPs may offer both types of multiply instructions, in which case the extra shift is not required.
 
Floating point also avoids this minor problem, but for number crunching, fixed point is gererally the best solution (cheapest, lowest power consumption).
 
Howard
-----Original Message-----
From: haoyeshen [mailto:h...@yahoo.com]
Sent: Saturday, May 14, 2005 1:11 AM
To: m...@yahoogroups.com
Subject: [motoroladsp] MAC and MPY problem in assembly language

I am trying to use either MAC or MPY to make a product of two
variables. The problem I have is that the result is always of twice
of the expected. For example, If I multiply 1 by 1, the the result I
got is always 2 other than 1. The following is the code I used;The C portion:

int multiply(int, int);
int some_global = 4;

int main(void)
{
      int a=1, b=1, c=0;

      c  = multiply(a,b);
     
      some_global = c;
                       
      return(0);

}

The assembly portion:

      section            rtlib
      org p:
     
      global Fmultiply

Fmultiply:

      clr       A
      move        Y1,X0
      mpy      Y0,X0,A
      move       A0,Y0
      rts

      endsec

Could someone tell me why this is happening.Thanks for the help


NEW!  You can now post a message or access and search the archives of this group on DSPRelated.com:
http://www.dsprelated.com/groups/motoroladsp/1.php

_____________________________________
Note: If you do a simple "reply" with your email client, only the author of this message will receive your answer.  You need to do a "reply all" if you want your answer to be distributed to the entire group.

_____________________________________
About this discussion group:

Archives:  http://www.dsprelated.com/groups/motoroladsp/1.php

To Post:  Send an email to m...@yahoogroups.com

Other DSP Related Groups: http://www.dsprelated.com/groups.php

The information contained in this message is confidential and intended only for use of the designated recipient(s) named above. If the reader of this message is not the intended recipient or an agent responsible for delivering it to the intended recipient, you are hereby notified that you have received this message in error, and that any review, dissemination, distribution, copying, or alteration of this message is strictly prohibited. If you have received this communication in error, please notify the originator immediately and remove it from your system.