Consider the following fragment of C code: for (i = 0; i <= 100; i++) { A[i] = B
ID: 640057 • Letter: C
Question
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 operatedon. Assume that values in registers are lost between iterations of the loop. a. [20] Write the code for MIPS. How many instructions are required dynamically? Howmany memory-data references will be executed? What is the code size in bytes? b. [20] Write the code for x86. How many instructions are required dynamically? How many memory-datareferences will be executed? What is the code size in bytes?
Explanation / Answer
MIPS CODE for the above code mentioned:
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 = 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.
d)Code size. Since MIPS instructions are 4 bytes in size then code size = 18 * 4 = 72 bytes.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.