Bill, Because a and b get multiplied before they are assigned to c as a 32 bit value. You probably only need to cast a as a long to get the correct results because then the multiply will be done as 32 bits.
\/ Sincerely, David Smead http://www.amplepower.com On Thu, 1 Jul 2004, Bill Hall wrote: > Unsigned 16b multiply question: > > When doing unsigned 16bit multiplys, why is it (apparently) > necessary to perform casts to 32b unsigned first? > > Using v3.2.3 from ~ 4weeks ago (linux) on MSP430x149 > > For Example: > > int main(void) > { > unsigned int a= 0xFFFF ; > unsigned int b= 0xFFFF ; > unsigned long c ; > unsigned long d ; > > c = a * b; > > d = (unsigned long)a * (unsigned long)b ; > return(0); > } > > /* Resulting List > <main>: > 31 40 00 0a mov #2560, r1 ;#0x0a00 > 3f 43 mov #-1, r15 ;r3 As==11 > 02 12 push r2 ; > 32 c2 dint > 03 43 nop > 82 4f 32 01 mov r15, &0x0132 ;<** Signed ??? > 82 4f 38 01 mov r15, &0x0138 ; > 1e 42 3a 01 mov &0x013a,r14 ;0x013a > 32 41 pop r2 ; > 02 12 push r2 ; > 32 c2 dint > 03 43 nop > 82 4f 30 01 mov r15, &0x0130 ; > 82 4f 38 01 mov r15, &0x0138 ; > 1e 42 3a 01 mov &0x013a,r14 ;0x013a > 1f 42 3c 01 mov &0x013c,r15 ;0x013c > 32 41 pop r2 ; > 0f 43 clr r15 ; > 30 40 78 11 br #0x1178 ; > */ > > - Bill Hall > > > ------------------------------------------------------- > This SF.Net email sponsored by Black Hat Briefings & Training. > Attend Black Hat Briefings & Training, Las Vegas July 24-29 - > digital self defense, top technical experts, no vendor pitches, > unmatched networking opportunities. Visit www.blackhat.com > _______________________________________________ > Mspgcc-users mailing list > Mspgcc-users@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/mspgcc-users >