This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

"else" instruction doesnt get xcuted

Hi All,
Using uVision simulator and Keil arm or
GNU compiler, the "else" instruction never gets xcuted. Using MSVC++ it does get xcuted.

Anyone know why?? puzzleing.
Thanks
----------------------------
int main (void){
unsigned int i=0,result=0;

for (i=0;i<5;i++){
if (i>3)
result=i*10;
else
result=i*5;
}

return(1);
}//main
--------------------------------

Parents
  • Perhaps the Keil optimizer is smarter.

    Looking at the entire loop, it's clear that eventually i must be > 3. In that case, result = i * 10. In fact, this will always occur when i == 4. So, you could replace the entire loop with one statement:

    i = 40;

    For that matter, even this line is useless, since the return is a fixed value. It could be eliminated as well.

    Take a look at the assembly listing and see what the code generator actually produces.
    One way to force the optimizer to preserve "unnecessary" writes is to declare the variables "volatile". Normally, volatile is used when the variable is actually affected by the scope of something other than the local code (hardware registers that change, variables shared with ISRs or other tasks).

    Or, turn optimization off, and see what happens.

Reply
  • Perhaps the Keil optimizer is smarter.

    Looking at the entire loop, it's clear that eventually i must be > 3. In that case, result = i * 10. In fact, this will always occur when i == 4. So, you could replace the entire loop with one statement:

    i = 40;

    For that matter, even this line is useless, since the return is a fixed value. It could be eliminated as well.

    Take a look at the assembly listing and see what the code generator actually produces.
    One way to force the optimizer to preserve "unnecessary" writes is to declare the variables "volatile". Normally, volatile is used when the variable is actually affected by the scope of something other than the local code (hardware registers that change, variables shared with ISRs or other tasks).

    Or, turn optimization off, and see what happens.

Children
No data