Instructions for implementing Control Structures
(if-else, while, case/switch)

   In order to make many programs useful, it is often necessary to implement control structures that allow us to loop through a set of values, create branches within the code or decide which set of code segments should be selected for execution. Programs would be extremely simplistic if it were not for these control structures. The intent of this section is demonstrate some of the common C style control structures and their associated implementations using the MIPS Assembly Language.


The if branch

The simplest of all control expressions is the if statement. This statement cause the machine to branch to a section of code if a particular condition is met.

The example C code:

if (a == b)
{
  a = a + b;
}

In this case we note that we want to execute the code inside of the braces only if a is equal to b. From this code we can note the following:

  • We will need to use the beq instruction branching on equal.
  • The beq instruction requires 3 operands. The first two operands are the register locations where the values of a and b are stored. The third operand is the location label to branch to if the values of operands 1 and 2 are equal.
  • We need to label the location of the assembly for a = a + b
  • For this example we will assume that a and b are stored in $s3 and $s4 respectively.
  • We also need to account for jumping over the code within the braces if the condition of the if is false

The MIPS Assembly:

    ...              # code prior to if statement
    beq $s3,$s4,L1   #test to see if a and b are equal
    j L2             #only get here if a != b, jump around L1
L1: add $s3,$s3,$s4  #code to execute if a == b
L2: ...              #continue on after if statement



The if-else branch

Now we want to add second branch of code to execute if the condition a == b evaluates as false.

The example C code:

if (a == b)
{
  a = a + b;
}
else
{
  b = b + a;
}

Many of the same characteristics apply to this section of code as to the simple if statement. However, we note that we want to execute the code inside of the braces only if a is equal to b, otherwise we execute a secondary set of instructions. From this code we can note the following:

  • We will need to use the beq instruction branching on equal.
  • The beq instruction requires 3 operands. The first two operands are the register locations where the values of a and b are stored. The third operand is the location label to branch to if the values of operands 1 and 2 are equal.
  • We need to label the location of the assembly for a = a + b
  • We need to label the location of the assembly for b = b + a
  • We need to label the location of the assembly for the code to execute after the if-else block.
  • For this example we will assume that a and b are stored in $s3 and $s4 respectively.

The MIPS Assembly:

    ...              # code prior to if-else block
    beq $s3,$s4,L1   #test to see if a and b are equal
    j L2             #only get here if a != b, jump around L1
L1: add $s3,$s4,$s3  #code to execute if a == b
    j L3             #finished if true case jump to end
L2: add $s4,$s4,$s3  #code to execute if a != b, fall through to L3
L3: ...              #continue on after if-else block



The while loop

In this example, we want to iterate through the loop at most (b - a) - 1 times.

The example C code:

while (a < b)
{
  a = a + 1;
}

From this code we can note the following:

  • We will need to use the slt instruction to set a temporary variable for the test
    of a < b.
  • The slt instruction requires 3 operands. The first operand is the location of the register to store the result of the comparison. For our purposes we will use $s1 to store the temporary result. The second operand is the left hand operand of a < b comparison (a),and the third is the right hand operand(b). If a < b then $s1 will contain the value 1, otherwise it will contain the value 0.
  • We need to label the location of the assembly for the top of the loop structure
  • We need to label the location of the assembly for the code directly after the while loop.
  • For this example we will assume that a and b are stored in $s3 and $s4 respectively.

The MIPS Assembly:

           ...              # code prior to while loop
Loop:  slt $s1,$s3,$s4      #set temporary variable
       beq $s1,$zero,Exit   #test to see if $s1 contains 0, jump if true.
       add $s3,$s3,1        #code to execute if a < b
       j Loop               #jump to top of loop
Exit: ...                   #continue on after while loop



The switch/case

In this example, we want to implement the C style switch/case block

The example C code:

switch (a)
{
  case 0: a = a + 1;break;
  case 1: a = a - 1;break;
  case 2: a = a + a;break;
  default: a = a - 2;break;
}

From this code we can note the following:

  • We need to test if a < 0 or a > 2 to see if we need to jump to the default case.
  • We need a temp variable $t0 to store the jump value into the appropriate case statement.
  • We need a temp variable $t1 to calculate the jump value into the appropriate case statement.
  • We need a temp variable $t2 to hold the address of the first case statement(Top of Jump Table).
  • We need to label the location of the assembly for the top of every case statement (C0, C1, C2).
  • We need to label the location of the assembly for the default case.
  • We need to label the location of the assembly for the code directly after the switch block.
  • For this example we will assume that a and our temporary variable are stored in $s3 and $s1 respectively.

The MIPS Assembly:

           ...              # code prior to the switch/case block
       slt $s1,$s3,$zero    #set temporary variable
       beq $s1,$one,Dflt    #test to see if $s1 contains 1, jump if true.
       slt $s1,$s3,$four    #set temporary variable
       beq $s1,$zero,Dflt   #test to see if $s1 contains 1, jump if true.
       add $t1,$s3,$s3      #need to multiply k by 4 to get offset
       add $t1,$t1,$t1
       add $t1,$t1,$t4      #add offset to address of jump table
       lw  $t0,0($t1)       #$t0 contains address of selected case
       jr  $t0              #jump to address in $t0
C0:    add $s3,$s3,$one     #case 0
       j   Exit             #jump Exit
C1:    sub $s3,$s3,$one     #case 1
       j   Exit             #jump Exit
C2:    add $s3,$s3,$s3      #case 2
       j   Exit             #jump Exit
Dflt:  sub $s3,$s3,$two     #default case
Exit: ...                   #continue on after switch block



References:

Hennesey, John L. and David A. Patterson (1998). Computer Organization and Design: the Hardware/Software Interface (2nd ed.). San Francisco: Morgan Kaufmann.


MIPS Home