View previous topic :: View next topic |
Author |
Message |
calvin
Joined: 18 Aug 2010 Posts: 10
|
Int32 comparison problem |
Posted: Fri May 06, 2011 12:43 am |
|
|
Hi!
I have a the following code:
Code: |
int32 AA,BB;
AA=-4;
if(AA > 0){
// do some thing 1
}
BB=0:
if(AA > BB){
// do some thing 2
}
|
Allways the code executes "do some thing 1" and "do some thing 2".
Why??
Thanks for your help |
|
|
calvin
Joined: 18 Aug 2010 Posts: 10
|
Another case: |
Posted: Fri May 06, 2011 1:01 am |
|
|
For more illustration:
Code: |
if(-2 < -4){
EVENTO(196);
}
if(-2 > -4){
EVENTO(197);
}
|
CCS generates the following .lst
Code: |
.................... if(-2 < -4){
.................... EVENTO(196);
.................... }
....................
.................... if(-2 > -4){
.................... EVENTO(197);
2156: MOVLW C5
2158: MOVWF x9C
215A: MOVLB 0
215C: CALL 0CE6
.................... }
|
why??? |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Fri May 06, 2011 1:47 am |
|
|
With 8 Bit CCS compilers, int defaults to unsigned (unless overridden by a #TYPE statement).
So all negative constants are interpreted as positive values, the result is correct. |
|
|
calvin
Joined: 18 Aug 2010 Posts: 10
|
|
Posted: Fri May 06, 2011 7:02 am |
|
|
Thanks FvM I will read about that.But, is fine too that the compile doesnt generate code for some statements? that is not for signed or unsigned data types. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Fri May 06, 2011 8:27 am |
|
|
If the compiler can tell at compile time that certain code can never be reached, it will not generate machine code for those statements. They are optimized out of existence. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Fri May 06, 2011 8:36 am |
|
|
Quote: | But, is fine too that the compile doesnt generate code for some statements? |
Sure. Conditional code after a constant compare that evaluates to false will be removed. |
|
|
calvin
Joined: 18 Aug 2010 Posts: 10
|
|
Posted: Fri May 06, 2011 11:54 am |
|
|
Thanks to all for your help. |
|
|
|