Problem with CodeWarrior/Eclipce version 11.0 doing long multiplications.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Problem with CodeWarrior/Eclipce version 11.0 doing long multiplications.

Jump to solution
607 Views
perhojfeldt
Contributor III

I have a problem doing a simple long multiplication.

lValue = (long)(1000 * 3850); Return in lValue 0xFFFFBF10. Wrong should be 0x003ABF10.

lValue = 3850000; Return correct lValue 0x003ABF10.

Is this a serious compiler error? Or what do I do wrong?

Attached is a little project named Long_Mul and 2 pdf files that show the source code made as Tiny and Minimal Startup code. MCU used is a MC9SO8QD4 with 4K flash and 255 bytes RAM.

Sincerely Yours

Per Højfeldt

1 Solution
512 Views
BlackNight
NXP Employee
NXP Employee

Hi Per,

The compiler is doing everything fine and to the standard: keep in mind that on the HCS08 an integer is 16bits.

Your multiplication (1000*3850) is performed with int, so gives 0xBF10. This is signed and gets sign extended with your cast to 32bit, so it gives 0xFFFFBF10.

If you want to do this multiplication on 32bit, you have have to use for example (1000L*3850).

I hope this helps,

Erich

View solution in original post

3 Replies
513 Views
BlackNight
NXP Employee
NXP Employee

Hi Per,

The compiler is doing everything fine and to the standard: keep in mind that on the HCS08 an integer is 16bits.

Your multiplication (1000*3850) is performed with int, so gives 0xBF10. This is signed and gets sign extended with your cast to 32bit, so it gives 0xFFFFBF10.

If you want to do this multiplication on 32bit, you have have to use for example (1000L*3850).

I hope this helps,

Erich

512 Views
perhojfeldt
Contributor III

Thanks Erich.

Forgot that. Going from 32 bit MCU to 8 bit MCU can be tough. Haha.

Thanks for reminding me.

Per.

0 Kudos
512 Views
perhojfeldt
Contributor III

Forgot to say that on the multiplication the compiler gives a warning. See attached pdf files in folder Long_Mul.zip

0 Kudos