28.9k views
4 votes
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 words and the base address of a is in $a0 and the base address of b is in $a1. Register $t0 holds the variable i and register $s0 the constant C. Write the code for MIPS. How many instructions are executed during the running of your code? How many memory data references will be made during execution?

User Mtbkrdave
by
9.1k points

1 Answer

4 votes

Answer:

711; 202

Step-by-step explanation:

MIPS code for the C fragment:

addi $t6, $zero, 101 # the loop termination value

add $t0, $zero, $zero # i = 0

addi $t2, $a0, 0 # ptr to current A[i]

addi $t3, $a1, 0 # ptr to current B[i]

loop: lw $t4, 0($t3) # load B[i]

add $t4, $t4, $s0 # B[i] + c

sw $t4, 0($t2) # store in A[i]

addi $t0, $t0, 1 # i++

addi $t2, $t2, 4 # ptr to next A[i]

addi $t3, $t3, 4 # ptr to next B[i]

bne $t0, $t6, loop # if i < 101, goto loop

The loop is executed 101 times and the loop contains 7 statements. There are 4 statements outside the loop. Therefore, the total number of instructions executed is 4 + (7*101) = 711.

The total number of memory data reference: 101 * 2 = 202.

User Shelef
by
7.8k points