A. Consider the following fragment of C code: for (i=0; i<=100; i++) { a[i] = b[
ID: 3929950 • Letter: A
Question
A. 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 this fragment in MIPS. How many total instructions are executed during the running of your code? How many memory data references will be made during execution? Typing your answer is recommended.
B. Add comments to each line of the following MIPS code. Then, describe what it computes (one or two sentences). Assume that $a0 is used for the input and initially contains n, a positive integer. Assume that $v0 is used for returning the output value.
begin: addi $t0, $zero, 0
addi $t1, $zero, 1
loop: slt $t2, $a0, $t1
bne $t2, $zero, finish
add $t0, $t0, $t1
addi $t1, $t1, 2
j loop
finish: add $v0, $t0, $zero
Explanation / Answer
(1) One example of MIPS code:
clear $t0;
addi $s0, $zero, 100
loop: lw $t1, 0($a1) # $t1 = b[i]
add $t1, $t1, $s0 # $t1 = a[i]
sw $t1, 0($a0) # store $t1 to address of a[i]
addi $a0, $a0, 4 # $a0 = address of a[i+1]
addi $a1, $a1, 4 # $a0 = address of a[i+1]
addi $t0, $t0, 1 # $t0 = $t0 + 1
beq $t0, $s0, finish # if ($t0 = 100) finish
j loop
finish:
b. instructions before loop are executed 1 time; 7 instructions between loop are executed 101 times; instruction
“j loop” executed 100 times. Therefore: Total instructions executed (in this case): 2*1 + 7*101 + 1*100 = 809.
c.
Memory data reference: 101 * 2 = 202.
B.
t=0;
if(b==0) go to finish
t = t+a, increase t by a
b = b-1,
go to loop
t = t+100
$v0 = t, output
t=0;
while(b!=0)
{
t = t + a;
b = b – 1;
}
t = t + 100;
v = t;
So, v = a*b+100
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.