Problem with CodeWarrior/Eclipce version 11.0 doing long multiplications.

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Problem with CodeWarrior/Eclipce version 11.0 doing long multiplications.

跳至解决方案
1,376 次查看
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 解答
1,281 次查看
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

在原帖中查看解决方案

3 回复数
1,282 次查看
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

1,281 次查看
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 项奖励
回复
1,281 次查看
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 项奖励
回复