Problem with CodeWarrior/Eclipce version 11.0 doing long multiplications.

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Problem with CodeWarrior/Eclipce version 11.0 doing long multiplications.

ソリューションへジャンプ
1,392件の閲覧回数
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,297件の閲覧回数
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,298件の閲覧回数
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,297件の閲覧回数
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,297件の閲覧回数
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 件の賞賛
返信