If statements with C2000

From Texas Instruments Wiki
Jump to: navigation, search

If you are debugging C2000 code that has "if" statements, it may seem like the "If" statements are always executed, no matter what the condition evaluates to. For example, consider this code:

void foo(int *p)
   if(x == *p)
     do stuff ...

In this case, it may seem like "do stuff" is always executing no matter what the "if" condition evaluates to.

To understand what is happening here, look closely at the assembly instructions either in the CCS debugger or the assembly file generated by the compiler. If there is a MOVB instruction, then this is a conditional instruction, so the move will only happen if the condition is true - and thus the instruction itself does the check for the "if" statement.

For example, consider the instruction MOVB *-SP[3], #0x10, EQ
The 3rd operand in the MOVB instruction is the condition (EQ, NEQ, GT etc). This instruction will move the value 0x10 to stack only if the conditional equal is true.

So the instruction itself will always execute (which is why things may look confusing when stepping through code in the debugger), but the outcome of the instruction is dependent on the condition. If the condition is true, then the move occurs, otherwise it does not.

This instruction is single cycle, and much more efficient than using a branch.