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,...
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...
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)...
Convert C code to MIPS assembly language 1) sum = 0; for(i=0; I < 1000; i++)...
Convert C code to MIPS assembly language 1) sum = 0; for(i=0; I < 1000; i++) sum = sum + I; printf(“sum=%d”, sum); 2) int i, v[10]; int min, k; for(i=0; i < 10; i++) scanf(“%d”, &v[i]); min = v[0] for(i=1; I < 10; i++) if(min > v[i]){ min = v[i] k = I; } printf(“%d=>%d”, k, min);
[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 = "...
Write a code fragment in C language that tests the value of an integer num1. If...
Write a code fragment in C language that tests the value of an integer num1. If the value is 10, square num1. If it is 9, read a new value into num1. If it is 2 or 3, multiply num1 by 99 and print out the result. Implement your code using SWITCH statements
Consider the following sets. (i) All vectors (a, b) in R2 such that ab ≠ 0....
Consider the following sets. (i) All vectors (a, b) in R2 such that ab ≠ 0. (ii) All matrices A in M22 such that AT  =  −A. (iii) All polynomials a0 + a1x + a2x2 in P2 such that a0 = 0. Determine whether each of the above sets is closed under addition or NOT closed under addition .
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT