0

I have the following code:

if (a > uplimit)
{
   a = a - uplimit;
   indicator = 2;
}
else if (a < lolimit)
{
   a = a - lolimit;
   indicator = 4;
}

When I compile and run the code, it seems like the code can never go to indicator = 2 or indicator = 4 given a > uplimit or a < lolimit.

However, if I add something at the beginning of each code section, it works.

if (a > uplimit)
{
   c = 1;
   a = a - uplimit;
   indicator = 2;
}
else if (a < lolimit)
{
   c = 2;
   a = a - lolimit;
   indicator = 4;
}

It seems like the compiler is trying to optimize the code. I know using volatile might solve this problem. Does anyone know other method that could solve the problem?

By the way, I am using Code Composer Studio.

10
  • 1
    You need to clarify what your problem is. We can't read your mind.
    – gnasher729
    Feb 4, 2015 at 19:04
  • What is the actual problem you are trying to solve? You just want the debugger to step on the optimized line?
    – Ma3x
    Feb 4, 2015 at 19:05
  • Do you mean? the code can never go to indicator = 2 or indicator = 4 given a <= uplimit and a >= lolimit
    – R Sahu
    Feb 4, 2015 at 19:06
  • This is just one piece of my project. I find the problem must be here. The code can never go to indicator = 2 or indicator = 4 given a <= uplimit or a >= lolimit
    – HeyMan
    Feb 4, 2015 at 19:08
  • 1
    Add a printf("P1: a = %d, lo = %d, hi = %d\n", a, lolimit, uplimit); before the condition in the question; add printf("P2: a = %d, lo = %d, hi = %d, ind=%d\n", a, lolimit, uplimit, indicator); after the code block. Show us the test runs which are mishandled. Note that the 'lolimit' code pushes a further away from the low limit; should that - be a +? And neither guarantees that a is within bounds after the block of code if a is far enough out of bounds beforehand (even if the + vs - issue is fixed). Feb 4, 2015 at 19:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.