⇒ 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; } } |
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?