Question

In: Computer Science

Use the Compiler Explorer with the MIPS compiler to compile the following C code. Assuming this...

Use the Compiler Explorer with the MIPS compiler to compile the following C code. Assuming this function is called with the parameter n = 5…

int summarize(int n) {
      int sum = 0;
      for (int i = 0; i < n; i++) {
            sum += i;
      }
      return sum;
}

a) Accounting for delay slots, what is the dynamic instruction count of this routine? What is the dynamic instruction count if you add the –O1 compiler option, which asks the compiler to optimize for speed (at least to one level)? Assuming all instructions have the same CPI, what is the speedup?

Solutions

Expert Solution

Solution: Dynamic instructions are referred to as those instructions present within a program that is actually executed by the CPU to implement a certain functionality within the code, for example, for-loop, while-loop, do-while loop, etc. On the other hand, the instructions that are executed by the CPU for the normal functionalities such as declaring a variable, printing a value, and initializing the variable are known as static instructions.

Now, coming to the code that is being mentioned-above, the number of dynamic instructions is: 4

int i = 0;       -Dynamic Instruction 1
i < n;           -Dynamic Instruction 2
i++              -Dynamic Instruction 3
sum += i;        -Dynamic Instruction 4

This because all these instructions are executed within the loop.

On the other hand, the number of static instructions within the code is: 3

int summarize(int n)     -Static Instruction 1
int sum = 0;             -Static Instruction 2
int sum = 0;             -Static Instruction 3

This is because all of these instructions are executed to implement the normal functionalities.

Here's the solution to your question, please provide it a 100% rating. Thanks for asking and happy learning!!


Related Solutions

Use the Compiler Explorer with the MIPS compiler to compile the following C code. Assuming this...
Use the Compiler Explorer with the MIPS compiler to compile the following C code. Assuming this function is called with the parameter n = 5… int summarize(int n) { int sum = 0; for (int i = 0; i < n; i++) { sum += i; } return sum; } c) Compile the code using the ARM gcc 8.2 compiler. Add the –O1 compiler option, which asks the compiler to optimize for speed (at least to one level). What is...
Compile the following C code to MIPS assembly. a. Assume that i and j are stored...
Compile the following C code to MIPS assembly. a. Assume that i and j are stored in register $s1 and $s2, respectively. i = i – 2 + j; b. Assume base address of Array B and the variable i are stored in registers $s3 and $s1, respectively. B[1] = (B[0] x 4) - I; Explain each step in detail! my professor has the following answer: 1) addi $t0, $s1, -2 add $s1, $$t0, $s2 2) lw $t0, 0 ($s3)...
Translate the following C code into MIPS Assembly code, assuming Loop Variable k is in $s0...
Translate the following C code into MIPS Assembly code, assuming Loop Variable k is in $s0 and initially containing 0 . Also assume base of array Arr is in $s3 while ( k < = 10 ) { Arr[k] = k ; k = k + 1; }
convert following C++ code into MIPS assembly: int main() {                                 &
convert following C++ code into MIPS assembly: int main() {                                         int x[10], occur, count = 0;                                                              cout << "Type in array numbers:" << endl; for (int i=0; i<10; i++) // reading in integers                               { cin >> x[i];        } cout << "Type in occurrence value:" << endl;                                 cin >> occur;                                                 // Finding and printing out occurrence indexes in the array                                  cout << "Occurrences indices are:" <<...
Write MIPS assembly code for the following C code. for (i = 10; i < 30;...
Write MIPS assembly code for the following C code. for (i = 10; i < 30; i ++) { if ((ar[i] > b) || (ar[i] <= c)) ar[i] = 0; else ar[i] = a; }
2. Translate the following C/Java code to MIPS assembly code. Assume that the values of a,...
2. Translate the following C/Java code to MIPS assembly code. Assume that the values of a, i, and j are in registers $s0, $t0, and $t1, respectively. Assume that register $s2 holds the base address of the array A (add comments to your MIPS code). j = 0; for(i=0 ; i<a ; i++) A[i]=i+j++;
4.Translate the following C code to MIPS assembly code. Assume that the value of i is...
4.Translate the following C code to MIPS assembly code. Assume that the value of i is in register $t0, and $s0 holds the base address of the integer MemArray if (i > 10) MemArray[i] = 0; else MemArray[i] = -MemArray[i]; 6.Translate the following C code to MIPS assembly code. Use a minimum number of instructions. Assume that the values of a, b, i, and j are in registers $s0, $s1, $t0, and $t1, respectively. Also, assume that register $s2 holds...
1.) Translate the following C code to MIPS assembly code. Assume that the variables f, g,...
1.) Translate the following C code to MIPS assembly code. Assume that the variables f, g, h, i, and j are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and $s7, respectively   B[8] = A[i-j]; 2.Translate the following C code to MIPS assembly code. Assume that the values of v in $a0, k in $a1, temp in $t0.    // leaf procedure that...
I need assistance translating a custom C++ program to MIPS. My C++ code is the following:...
I need assistance translating a custom C++ program to MIPS. My C++ code is the following: I have made numerous attempts on my own to no avail, any assistance is appreciated. Also, template code for this solution is provided below: #include int moveRobots(int *, int *, int, int ); int getNew(int, int); int main() { int x[4], y[4], i, j, myX = 25, myY = 25, move, status = 1; // initialize positions of four robots x[0] = 0; y[0]...
Convert the following C++ code into MIPS assembely. For testing I will be change the values...
Convert the following C++ code into MIPS assembely. For testing I will be change the values for q,y,x with few different values. //q -> $s0 //y -> $s1 //x -> $s2 int main(){ int q = 5; int y = 17; int x = 77; if ( q < 10){ cout << "inside if"; } elseif ( x > 0 || y < 10) { cout << "inside elseif"; } else { cout << "inside else"; } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT