Question

5. Write the MIPS minimal sequence of instructions for the following C procedure code: int array_sum...

5. Write the MIPS minimal sequence of instructions for the following C procedure code:

int array_sum (a[], b[])
                                 { int i;
                                      i =1;

for (i=1; i<100; i=i+1)

{ b[i] = D + a[i-1] + a[i] + a[i+1] ;}

                         
                                  }

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 $S1 is associated with variable i.
  • “D” is in register $S0 and
0 0
Add a comment Improve this question Transcribed image text
Answer #1

C code
int array_sum (a[], b[]) {
   int i;
        i =1;
   for (i=1; i<100; i=i+1){
   b[i] = D + a[i-1] + a[i] + a[i+1] ;
   }
}

$a0 = base address of array a[]
$a1 = base address of array b[]
$s1 = i
$s0 = D

MIPS code
addi    $s1, $0, 1   //i = 1
addi     $t0, $a0, 4   //$t0 = &a[1]
addi     $t1, $a1, 4   //$t1 = &b[1]

Loop:  
   slti    $t2, $s1, 100   //set $t2 when (i<100)
   beq    $t2, $0, Exit   //if($t2 == 0) branch to Exit
   lw    $t2, -4($t0)   //$t2 = a[i-1]
   lw    $t3, 0($t0)   //$t3 = a[i]
   add    $t2, $t2, $t3   //$t2 = a[i-1] + a[i]
   lw    $t3, 4($t0)   //$t3 = a[i+1]
   add    $t2, $t2, $t3   //$t2 = a[i-1] + a[i] + a[i+1]
   add    $t2, $t2, $s0   //$t2 = D + a[i-1] + a[i] + a[i+1]
   sw    $t2, 0($t1)   //b[i] = D + a[i-1] + a[i] + a[i+1]
   addi    $t0, $t0 ,4   //$t0 = $a[i+1]
   addi    $t1, $t1 ,4   //$t1 = $b[i+1]  
   addi    $s1, $s1, 1   //i = i+1
   j    Loop       //Jump to Loop
Exit:

Add a comment
Know the answer?
Add Answer to:
5. Write the MIPS minimal sequence of instructions for the following C procedure code: int array_sum...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT