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

3) Consider the following IA32 assembly language code fragment. Assume that a, b and c are...
3) Consider the following IA32 assembly language code fragment. Assume that a, b and c are integer variables declared in the data segment. movl a, %eax movl b, %ebx cmpl %ebx, %eax jge L1 movl %eax, %ecx jmp L2 L1: movl %ebx, %ecx L2: movl %ecx, c Write the C code which is equivalent to the above assembly language code. You don't need to include the variable declarations, a function or anything like that, just show the 1 to 4...
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...
Translate the following C code into MIPS assembly code. Assume a in $s0, i in $s1,...
Translate the following C code into MIPS assembly code. Assume a in $s0, i in $s1, and base address of b[] in $s2, respectively. Note that the element’s data type in array b is integer (word). Please do not change the C code structure and please comment your MIPS code. for (i = 100; i>a; i--) {          b[i] = 16*i; b[i+1] = 16*(i+1); b[i+2] = 2*i + 16; }
Write assembly code for the following C procedure: i = 1; sum = 0; while (i...
Write assembly code for the following C procedure: i = 1; sum = 0; while (i ≤ n) { sum += i; ++i; } i and sum corresponds to $s1 and $s2. n is preloaded into $s3
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT