Consider the following fragment of C code: for (i-100; i>0 i--) 1+C Assume that
ID: 3914157 • Letter: C
Question
Consider the following fragment of C code: for (i-100; i>0 i--) 1+C Assume that A and B are arrays of 64-bit integers, and C and i are 64-bit integers. Also, 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. Write the code for MIPS. How many instructions are required dynamically? How many memory-data references will be executed? What is the code size in bytes? Note that i contains an index value. To compute the actual address of an array element, it is necessary to multiply 8 to i and it can be easily done by using the DSLL instruction. (a) Write the code in MIPS assembly.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.
Hope this will help
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.