Assembly Language Programming Using TI MSP430 and IAR Workbe
Assembly Language Programming Using TI MSP430 and IAR Workbench.
In assembly (MSP430), how can I multiply 2 negative numbers together? I can\'t use the MUL function due to the MSP430. Basically I have this:
#include \"msp430.h\" ; #define controlled include file
PUBLIC sw_mulneg
RSEG CODE
sw_mulneg: MOV #-0x00004,R6 ; insert the number -4 into register 6
MOV #-0x00004,R7 ; insert the number -4 into register 7
I don\'t know where to go from here to multiply these 2 16 bit signed integers? Thanks for any help!
Solution
The division by integer constants is considered in details in the book \"Hacker\'s Delight\" by Henry S. Warren (ISBN 9780201914658).
The first idea for implementing division is to write the inverse value of the denominator in base two.
E.g.,
1/3 = (base-2) 0.0101 0101 0101 0101 0101 0101 0101 0101 .....
So,
a/3 = (a >> 2) + (a >> 4) + (a >> 6) + ... + (a >> 30)
for 32-bit arithmetics.
By combining the terms in an obvious manner we can reduce the number of operations:
b = (a >> 2) + (a >> 4)
b += (b >> 4)
b += (b >> 8)
b += (b >> 16)
