Question

In: Computer Science

Consider the following fragment of C code: for (i=0; i<100; i++) { A[i]=B[i]+C; } Assume that...

Consider the following fragment of C code:

for (i=0; i<100; i++) {

A[i]=B[i]+C;

}

Assume that A and B are arrays of 64-bit integers, and C and i are 64-bit integers.

Assume that all data values and their addresses are kept in memory (at

addresses 1000, 3000, 5000, and 7000 for A, B, C, and i, respectively) except when

they are operated on. Assume that values in registers are lost between iterations of

the loop. Assume all addresses and words are 64 bits. (Write the code in MIPS )   c. [20]<A.2>Write the code for a stack machine. Assume all operations occur

on top of the stack. Push and pop are the only instructions that access memory;

all others remove their operands from the stack and replace them with the result.

The implementation uses a hardwired stack for only the top two stack entries,

which keeps the processor circuit very small and low in cost. Additional stack

positions are kept in memory locations, and accesses to these stack positions

require memory references. How many instructions are required dynamically?

How many memory-data references will be executed?

Solutions

Expert Solution

DADD R1, R0, R0 ; R0 =0; initialize i = 0

SW R1, 7000(R0) ; store i

Loop: LD R1, 7000(R0) ; get value of i

DSLL R2, R1, #3 ; R2 = word offset of B[i]

DADDI R3, R2, #3000 ; add base address of B to R2

LD R4, 0(R3) ; load B[i]

LD R5, 5000(R0) ; load C

DADD R6, R4, R5 ; B[i] + C

LD R1, 7000(R0) ; get value of i

DSLL R2, R1, #3 ; R2 = word offset of A[i]

DADDI R7, R2, #1000 ; add base address of A to R2

SD R6, 1000(R7) ; A[i] = B[i] + C

LD R1, 7000(R0) ; get value of i

DADDI R1, R1, #1 ; increment i

SD R1, 7000(R0) ; store i

LD R1, 7000(R0) ; get value of i

DADDI R8, R1, #-101 ; is counter at 101?

BNEZ R8, loop ; if not 101 then repeat

-------------------------------------

b)Dynamic instructions are executed = initialization instructions plus loop instructions times the number of iterations

= 2 + (16 X 101) = 1618.

------------------------------------

c) Memory data reference is a count of load-store instructions = 0 + (8 X 101) = 808.


Related Solutions

Javascript. Consider the following code fragment, that is supposed to compute the pixel value let c...
Javascript. Consider the following code fragment, that is supposed to compute the pixel value let c = image.getPixel(x, y); const m1 = (c[0] + c[1] + c[2]) / 3; c = image.getPixel(x + 1, y); const m2 = (c[0] + c[1] + c[2]) / 3; image.setPixel(x, y, [m1 - m2, m1 - m2, m1 - m2]); Give three pairs of pixel values (x, y) = [?, ?, ?] and (x+1, y) = [?, ?, ?] in the input image, for...
Determine the output using C++ of the following code fragment i) double *pt; double a[3] =...
Determine the output using C++ of the following code fragment i) double *pt; double a[3] = {1.2, 2.3, 3.4}; pt = &a[1]; pt += 1; cout << *pt << endl; ii) int i = 1; while (i <= 4) { int num = 1; for (int j = 1; j <= i; j++) { cout << num << "bb"; num *= 3; } cout << endl; i++; }
Given the following code: for (i=2;i<100;i=i+1) { a[i] = b[i] + a[i]; /* S1 */ c[i-1]...
Given the following code: for (i=2;i<100;i=i+1) { a[i] = b[i] + a[i]; /* S1 */ c[i-1] = a[i] + d[i]; /* S2 */ a[i-1] = 2 * b[i]; /* S3 */ b[i+1] = 2 * b[i]; /* S4 */ } a. List all the dependencies by their types (TD: true-data, AD: anti-data, OD: output-data dependencies). b. Show how Software Pipelining can exploit parallelism in this code to its fullest potential. How many functional units would be sufficient to achieve the...
C++ Q2.   Given the code as below. Which statement is correct? int myAry[100]; for(int i=0; i<100;...
C++ Q2.   Given the code as below. Which statement is correct? int myAry[100]; for(int i=0; i<100; i++) myAry = i + 2; for(int i=100; i>0; i--) cout << myAry[i] << '\t'; The first for loop assigns myAry 99 values and the null character. The second for loop prints out myAry elements backwards. The index in the second for loop is out of bounds. Only the first loop needs the null character. Q3. A value returning function that takes one parameter,...
Define spatial and temporal locality.  Consider the following code:               for (i=0; i < 50; i++)           &nbsp
Define spatial and temporal locality.  Consider the following code:               for (i=0; i < 50; i++)             for (j =0; j < 30; j++)                   a[i] = a[i] * j; Give an example of spatial locality in the above code. b. Give an example of temporal locality in the above code.
What output is produced by the following code fragment? int num = 0, max = 20;...
What output is produced by the following code fragment? int num = 0, max = 20; while (num < max) { System.out.println(num); num += 4; }
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...
Let a, b, c ∈ Q, (a, b) /= (0, 0), and consider the line L...
Let a, b, c ∈ Q, (a, b) /= (0, 0), and consider the line L = {(x, y) ∈ R2 ; ax + by = c}. State and prove a criterion in terms of the data a, b, c ∈ Q to check whether L∩Z2 = ∅, i.e., the line passes through no point with integer coordinates in the plane. Give an explicit example of such a line that is neither parallel to the x-axis nor to the y-axis!
[after §3.22 − easy] String Processing : Consider the following code fragment: 1 int a =...
[after §3.22 − easy] String Processing : Consider the following code fragment: 1 int a = 20; 2 int b; 3 double x = 3.5; 4 String s = "All"; 5 char ch; 6 7 x += a; 8 x--; 9 a /= 4 - 1; 10 b = s.length(); 11 b += 4; 12 s += "is well"; 13 ch = s.charAt(b); 14 System.out.println("a = " + a + ", b = " + b); 15 System.out.println("x = "...
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)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT