AnsweredAssumed Answered

Please Help Me Understand 32-bit Multiplications in CW 5.7

Question asked by Greg Smith on Oct 10, 2008
Latest reply on Oct 10, 2008 by Greg Smith

I'm using CodeWarrior 5.7, Build 2211 for the HCS12, specifically the MC9S12E128 microcontroller. 

The following compiler options are activated:

-CpPPAGE=0x30 -D_HCS12 -D__NO_FLOAT__ -Lasm=%n.lst -Lasmc=ehpv -Mb -Os -Onu -Onf -OnB -Onbf -Onbt -Onca -Oncn -OnCopyDown -OnCstVar -One -OnP -OnPMNC -Ont -Or -Rpe -WmsgSd1860

I have the following code:

#define ULONG          INT32U
#define TICKSPERMINUTE  0x00001770 /*Number of ticks per minute*/
static INT32U HexMins;
RtcDateTime.Minutes = 0x25;
 HexMins =  (RtcDateTime.Minutes)* TICKSPERMINUTE);

The result from the above calculation is HexMins = 0x6330, but it appears that the upper two bytes of the 32-bit value are being optimized away.

If I adjust the calculation as follows:
 HexMins =  (INT32U)(RtcDateTime.Minutes)* (INT32U)TICKSPERMINUTE);

Then I see the correct result HexMins = 0x36330.  It appears that casting both parts of the equation as 32-bit unsigned long are necessary to make things work.  Is this an optimization that I can adjust or turn off?

Greg Smith