.无符号数乘加操作 result="n1"*n2+n3*n4。第一操作数n1(16位无符号)赋给MPY寄存器,第二操作数n2(16位无符号)赋给OP2寄存器,第三操作数n3(16位无符号)赋给MAC寄存器,第四操作数n4(16位无符号)赋给OP2寄存器,结果result(32位无符号)就可以在RESLO(低16位)和RESHI(高165位)中读取。SUMEXT寄存器中保存进位标志。
代码如下:
MPY = n1; // Load first operand -unsigned mult
OP2 = n2; // Load second operand
MAC = n3; // Load 3rd operand -unsigned mult
OP2 = n4; // Load 4rd operand
result = RESHI; // Load RESHI word result
result = (result<<16)|RESLO; // Shift RESHI left and concat with
这是16位的,32位的类似
//******************************************************************************
// MSP430x26x Demo - 8x8 Signed Multiply Accumulate
//
// Description: Hardware multiplier is used to multiply two numbers.
// The calculation is automatically initiated after the second operand is
// loaded. A second multiply accumulate operation is performed after that.
// Results are stored in RESLO and RESHI. SUMEXT contains the extended sign of
// result.
// ACLK = 32.768kHz, MCLK = SMCLK = default DCO
//
// MSP430F261x/241x
// -----------------
// /|\| |
// | | |
// --|RST |
// | |
// | |
//
// B. Nisarga
// Texas Instruments Inc.
// September 2007
// Built with CCE Version: 3.2.0 and IAR Embedded Workbench Version: 3.42A
//******************************************************************************
#include
void main(void)
{
unsigned char value1,value2;
WDTCTL = WDTPW+WDTHOLD; // Stop WDT
MPY = 0x1234; // Load 1st operand - unsigned mult
OP2 = 0x5678; // Load second operand
value1 = 0x12; // Load first operand - signed MAC
value2 = 0x96; // Load second operand
// Sign-extend the values
if (value1 >=0x80)
MACS = 0xFF00 | value1;
else
MACS = value1;
if (value2 >=0x80)
OP2 = 0xFF00 | value2;
else
OP2 = value2;