Question

In MIPS assembly, write an assembly language version of the following C code segment: int A[100],...

In MIPS assembly, write an assembly language version of the following C code segment:

int A[100], B[100];

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

A[i] = A[i-1] + B[i];

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C code
int A[100], B[100];
for (i=1; i < 100; i++) {
   A[i] = A[i-1] + B[i];
}

Let the Base address of array A[] is stored in Register $s1
the Base address of array B[] is stored in Register $s2

MIPS code

   addi $t0, $0,100   //$t0 = 100
   addi $t1, $0, 1       //i = 1

Loop:
   slt $t2, $t1, $t0    //if(i<100) set $t2 = 1
   beq $t2, $0, Exit   //if($t2 = 0) branch to Exit
   sll $t2, $t1, 2       //$t2 = 4*i
   add $t3, $t2, $s1   //$t3 = address of A[i]
   add $t4, $t2, $s2   //$t4 = address of B[i]
   lw $t2, -4($t3)       //$t2 = A[i-1]
   lw $t4, 0($t4)       //$t4 = B[i]
   add $t2, $t2, $t4   //$t2 = A[i-1] + B[i]
   sw $t2, 0($t3)       //A[i] = A[i-1] + B[i]
   addi $t1, $t1, 1   // i = i + 1
   j Loop           //Jump to Loop
Exit:
  
   

Add a comment
Know the answer?
Add Answer to:
In MIPS assembly, write an assembly language version of the following C code segment: int A[100],...
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