Share

C to assembly: if and switch statements

This article covers the code generation for if-else and switch statements. We have already covered C function calls and other C statements in previous articles.

Code generation for "if-else" statement

Code generation for an if-else statement is straight forward. The assembly code exactly mirrors the C code.

Code generation for if-else statement

if (x == y)
{
   z = 1;
}
else
{
   z = 0;
}

    // Move x into Data Register R7
    MOVE _x, R7
    // Now compare x with y
    COMPARE _y, R7
    // If not equal branch to the else leg specified by label L1
    BRANCH_IF_NOT_EQUAL L1
    // Moving 1 into z
    MOVE #1, _z
    // Branch beyond the if-then-else statement
    BRANCH_ALWAYS L2
    // Else portion of the if-else statement
    // Set z to zero
 L1 MOVE #0, _z
    // End of if-else statement. if portion of the statement executes an 
    // unconditional branch to reach L2. The else leg falls through into L2
 L2 . . .
      

Code generation for switch statement

The code generated for a switch statement varies a lot from one compiler to another. In fact, a given compiler might generate different code in different scenarios. The choice of the code to be generated depends upon the number and range spread of individual case statements.

Different cases of generation of a switch statement are:

Case values in narrow range

If the case values are placed in a narrow range, the compiler can avoid performing a comparison for every case leg in the switch statement. In such cases, the compiler generates a jump table which contains addresses of the actions to be taken on different legs. The value on which the switch is being performed is manipulated to convert it into an index into the jump table. In this implementation, the time taken in the switch statement is much less than the time taken in an equivalent if-else-if statement cascade. Also, the time taken in the switch statement is independent of the number of case legs in the switch statement.

Switch jump table

Code generation for a switch statement (case values are in a narrow range)

switch (num)
{
case 3:
   . . .
   break;
   
case 4:
   . . .
   break;
   
case 5:
   . . .
   break;
   
case 6:
   . . .
   break;
}
             // Execution of the switch statement starts with branching to starting
             // point of the switch statement. The switch comparision will be made
             // by instructions at SWITCH_START
             BRANCH_ALWAYS SWITCH_START
             
             
             // The code for handling each case leg of the statement is 
             // shown next. The labels for the beginning of each leg are
             // marked as assembler labels. These labels will be included
             // in a compiler generated switch statement table.
CASE3_LABEL  . . .
             BRANCH_ALWAYS BEYOND
CASE4_LABEL  . . .
             BRANCH_ALWAYS BEYOND
CASE5_LABEL  . . .
             BRANCH_ALWAYS BEYOND
CASE6_LABEL  . . .
             BRANCH_ALWAYS BEYOND
             
             
             // Compiler generates a jump table for the starting address of
             // each case label. This is used in quick indexing to the right
             // option without resorting to cascaded if-else-if statements.
             // Note: Each entry in the jump table is 32 bit address.
CASE_JUMP    DATA CASE3_LABEL
             DATA CASE4_LABEL
             DATA CASE5_LABEL
             DATA CASE6_LABEL
             
             
             // Move num into Data Register R7
SWITCH_START MOVE _num, R7
             // Subtract the lowest value of the range from R7
             ADD #-3, R7
             // Get the base address of jump table into Address Register A0
             MOVE CASE_JUMP, R0
             // Now multiply R7 by 4 to obtain the offset into the jump table
             // Note that the compiler saves a mulitply by using a shift left by 2 bits
             // instruction to implement the muliply by 4
             LEFT_SHIFT #2, R7
             // Now the offset and jump table base address are used to jump to the
             // appropriate case leg
             JUMP (R0, R7)
BEYOND       . . .
      

Case values in wide range

If the case legs of the switch statement have a wide deviation in values, the compiler cannot make a jump table to handle the switch statement. In such cases, the jump table would be huge in size and filled very sparingly. Thus the compiler resorts to using a cascade of comparisons to implement the switch. The code generated for the switch statement in such cases will look more like a series of if-else-if statements. Here the time taken to execute the switch statement increases with the number of case legs in the switch.

Generation for switch statement (case values are in a wide range)

switch (num)
{
case 1:
   . . .
   break;
   
case 10:
   . . .
   break;
   
case 1000:
   . . .
   break;
   
case 100:
   . . .
   break;
}
             // Execution of the switch statement starts with branching to starting
             // point of the switch statement. The switch comparision will be made
             // by instructions at SWITCH_START
             BRANCH_ALWAYS SWITCH_START
             
             
             // The code for handling each case leg of the statement is 
             // shown next. The labels for the beginning of each leg are
             // marked as assembler labels.
CASE1_LABEL  . . .
             BRANCH_ALWAYS BEYOND
CASE10_LABEL  . . .
             BRANCH_ALWAYS BEYOND
CASE1000_LABEL  . . .
             BRANCH_ALWAYS BEYOND
CASE100_LABEL  . . .
             BRANCH_ALWAYS BEYOND
             
             
             // Move num into Data Register R7
SWITCH_START MOVE _num, R7
             // Since the case values are not closely spaced compiler
             // cannot use a jump table. In such cases, cascade of comparison
             // instructions are used.
             // Check if Data Register R7 matches 1
             COMPARE #1, R7
             // If it matches 1, branch to CASE1_LABEL
             BRANCH_IF_EQUAL CASE1_LABEL
             // Check if Data Register R7 matches 10
             COMPARE #10, R7
             // If it matches 10, branch to CASE10_LABEL
             BRANCH_IF_EQUAL CASE10_LABEL
             // Check if Data Register R7 matches 1000
             COMPARE #1000, R7
             // If it matches 1000, branch to CASE1000_LABEL
             BRANCH_IF_EQUAL CASE1000_LABEL
             // Check if Data Register R7 matches 100
             COMPARE #100, R7
             // If it matches 100, branch to CASE100_LABEL
             BRANCH_IF_EQUAL CASE100_LABEL
      
BEYOND       . . .      
      

Big switch statement with wide distribution

If the switch statement has a very large number of case legs and the values are widely distributed, some compilers use binary search to select the case leg. The different case values are sorted by the compiler at compile time for a binary search.

Explore more