MIPS CPU Simulator Exercise:
Array Indices vs Pointers

 


⇒ 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.

In this exercise, you will be comparing the Loop and Loop-With-Pointers samples in the K Sub-MIPS Simulator, and comparing those loops with the examples in the Arrays and Pointers slide.

Write up your answers in a Markdown file. (There is no template for this exercise.) Don't forget to include your name and the date.


Using the "Save" button, copy both the "COMP 230 Loop" sample program and the "Loop with Pointers" sample program to the output area at the bottom of the simulator so that you can look at them side-by-side.

The two programs have the same functionality: they read a value N from a location in memory, and then they step through N items in an array, summing up the N values in the array.

The C code equivalent for the two programs (assuming N and R are global variables defined outside of main) would be:

        main ()
        {
          int i, sum = 0;
          for (	i = 0; i < N; i++ )
          {
             sum += R[i];
          }
        }
        main ()
        {
          int i, sum = 0;
          for (	ptr = R; ptr < R + N; ptr++ )
          {
             sum += *ptr;
          }
        }
Analysis Questions:
  1. How many assembly language instructions are there in each of the two programs?
  2. Assemble and run the "Loop" sample program in the simulator. How many Fetch/Execute cycles did it take to complete the program?
  3. Click on the Edit tab and clear Memory location M84, which has the result from the first program. Then assemble and run the "Loop with Pointers" sample program in the simulator. How many Fetch/Execute cycles did it take to complete the program? Is this what you expected, based on the previous Loop example?
  4. How many instructions does each example have before the For label? How many instructions are in the loop? How does this explain the difference between the number of instructions fetched and executed in each program?
  5. The two versions of the sample program in the Arrays and Pointers slide are similar to the two Loop examples in the Simulator, but these double the value in every entry in the array rather than summing them up.

    Do a similar analysis for the array and pointer versions in the Arrays and Pointers slide. How many assembly language instructions are there in each of the two programs? How many instructions would be executed in each program? Why is there such a difference between the array and pointer versions when the code looks so similar?

  6. What does this tell us about the difference between array indexing and pointers in C? What questions might we ask about other high-level languages?