MIPS CPU Simulator Exercise:
Conditions and Loops

 


⇒ Bring up the "K Sub-MIPS Simulator referred to in these instructions in a separate, side-by-side browser window, if you haven't already.

You will be writing three MIPS assembly language code fragments or programs in this exercise. The first two are variations of the Conditions and Loop code examples in the K Sub-MIPS Simulator. The third is a new program involving nested loops.

Download this Markdown template to record your answers to the questions below and submit them to Kit.


Conditions

Look over the "COMP 230 Conditions" example code in the K Sub-MIPS Simulator, which is an assembly language translation of the following C program. (This example is similar to Example 2 near the top of the Conditions in MIPS slide.)

      /* assume a, b, and c have values before this snippet */
      int temp = a;
      if ( a != b )
         temp = (a + b)/2;    /* Find midpoint */
      temp += c;              /* temp is midpoint + c */

The assembly language program assumes that the values for a, b, and c are in memory locations M80, M84, and M88, and finishes with the result in register $v0.

Program 1:

Manually run the Conditions code sample to be sure you understand what it does. (It is just a code fragment, so there is no HALT instruction.)

Write an assembly language equivalent of the following C code fragment. (This code is from the exercise at the bottom of the Conditions in MIPS slide, after introduction of the slt instruction.)

      /* Compute diff = c - max(a, b). */
      int max = a;              /* Start by assuming a is the max */
      if ( a < b )              /* ... Check: was assumption wrong? */
         max = b;
      int diff = c - max;

Like the example program, assume that the values for a, b, and c are in memory locations M80, M84, and M88. Your final value of diff should be in register $v0 at the end of this snippet. Since this is just a code snippet, not a complete program, you don't need the HALT instruction.

Tip: You can easily modify existing Assembly Lang Program code. "Save" an existing program to the Saved Output area and then copy it into the text area on the right, edit it there, and click on the "Read in to Assembly Lang Program Area" button.

Test your program, then "Save" and copy it into the Markdown file.


Loops

The Loop example in the Simulator is an assembly language translation of the following C program. (This code is equivalent to the Loop example on the Loops in MIPS slide.)

        /* Sum values in an array; N and R are global variables. */
        main ()
        {
          int i, sum = 0;
          for (	i = 0; i < N; i++ )
          {
             sum += R[i];
          }
        }

The assembly language program assumes that the value for N is stored in M80, and the values in array R are stored in M88 - M100.

Program 2:

Manually run the Loop sample program to be sure you understand what it does.

Write an assembly language equivalent of the following C program:

        main ()
        {
          int i;
          for (	i = 0; i < N; i++ )
          {
             B[i] = A[i] * 2;
          }
        }

Assume that the value for N is again stored in M80, and the values in array A are stored in M88 - M100. Choose an appropriate memory location for the start of array B.

Test your program, then "Save" and copy it into the Markdown file.


Nested Loops

The C program below stores the values for a 4x4 grid in a one-dimensional array. The program stores 0 in each element unless it's on the diagonal, in which case it is set to 1.

        1   0   0   0
        0   1   0   0
        0   0   1   0
        0   0   0   1

As a one-dimensional array, this looks like

        1   0   0   0   0   1   0   0   0   0   1   0   0   0   0   1

As we traverse the grid in row-major order, we increment the index of the one-dimensional array by 1 each time.

Program 3:

Write an assembly language equivalent of the following C program:

        main ()
        {
            int R[16];      // holds a one-dimensional version of 4 x 4 array
            int dim = 4;    // R represents a 4 x 4 grid
            int row, col;
            int index = 0;
            for ( row = 0; row < dim; row++ )
                for ( col = 0; col < dim; col++, index++ )
                {
                    if ( row == col )
                        R[index] = 1;
                    else
                        R[index] = 0;
                }
        }

Assume that R[0] - R[15] are stored in the consecutive memory locations M80 - M136.

Here is a template you could use to get started:

        addi $gp, $zero, 80       # R starts at M80
        addi $s0, $zero, 4        # s0 = 4    (s0 is dim)
        add $t0, $zero, $zero     # t0 = index
        add $t1, $zero, $zero     # t1 = row
 OUTLP: slt $t3, $t1, $s0         # t3 = 1 if row < 4
        [ maybe branch ]
        add $t2, $zero, $zero     # t2 = col (init must be inside outer loop)
 INLP:  ... [do stuff]
        [ maybe branch ]
        ... [do stuff inside the loop, including if/else]
        ... [ increment col and index ]
        j INLP
 ENDI:  ... [ increment row ]
        j OUTLP
 ENDO:

Test your program, then "Save" and copy it into the Markdown file.