avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-gcc-list] Why does this code not work?


From: wbounce
Subject: [avr-gcc-list] Why does this code not work?
Date: Wed, 1 Dec 2004 21:43:19 -0500

avr-gcc (GCC) 3.4.1
With no optimization

typedef struct ranges_t
{
uint16_t          Front;   
uint16_t          Back;   
uint16_t          LeftFront;   
uint16_t          LeftBack;   
uint16_t          RightFront;   
uint16_t          RightBack;   
} RANGES;

RANGES Sonar;
.....
if (Sonar.LeftFront - Sonar.LeftBack > 0 ) 
{
 if (Sonar.LeftFront - Sonar.LeftBack > 100)
  {
  BearLeft();
  }
 else
  {
  Forward();
  }
}       
else
{
 if (Sonar.LeftBack - Sonar.LeftFront > 100)
  {
  BearRight();
  }
 else
  {
  Forward();
  }
}

In my simulation Sonar.LeftBack = 1010 and Sonar.LeftFront = 900. The
1st if should fail but it does not. When I look at the disassembly it
looks like this
140:                            if (Sonar.LeftFront - Sonar.LeftBack > 0
) 
+00000D89:   912001DB    LDS     R18,0x01DB       Load direct from data
space
+00000D8B:   913001DC    LDS     R19,0x01DC       Load direct from data
space
+00000D8D:   918001DD    LDS     R24,0x01DD       Load direct from data
space
+00000D8F:   919001DE    LDS     R25,0x01DE       Load direct from data
space
+00000D91:   1728        CP      R18,R24          Compare
+00000D92:   0739        CPC     R19,R25          Compare with carry
+00000D93:   F099        BREQ    PC+0x14          Branch if equal
---- 
142:                                    if (Sonar.LeftFront -
Sonar.LeftBack > 100)
+00000D94:   918001DB    LDS     R24,0x01DB       Load direct from data
space
---- No Source 
+00000D96:   919001DC    LDS     R25,0x01DC       Load direct from data
space
+00000D98:   912001DD    LDS     R18,0x01DD       Load direct from data
space
+00000D9A:   913001DE    LDS     R19,0x01DE       Load direct from data
space
+00000D9C:   1B82        SUB     R24,R18          Subtract without carry
+00000D9D:   0B93        SBC     R25,R19          Subtract with carry
+00000D9E:   3685        CPI     R24,0x65         Compare with immediate
+00000D9F:   0591        CPC     R25,R1           Compare with carry
+00000DA0:   F018        BRCS    PC+0x04          Branch if carry set

It is doing a compare to 0 in the 1st if instead of a subtraction and
then a compare as it does in the 2nd if. What am I doing wrong?




reply via email to

[Prev in Thread] Current Thread [Next in Thread]