Question

1. (a) Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[],...

1. (a) Convert the following C function to the corresponding MIPS assembly procedure:

int count(int a[], int n, int x){

        int res = 0;
        int i = 0;
        for(i = 0; i != n; i++)
           if(a[i] == x)
              res = res + 1;

return res;

}

Cannot use converter need hard copy!!!

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

Program:

.data

.data
n: .word 10 #n
a: .word 21, 20, 30, 4, 20, 10, 10, 21 , 15, 10   #array a[]
x: .word 10 #search key 10
.text
.globl main
main:
   la $t0, a # $t0 = &a
   lw $t1, n #s2 = n  
   lw $t2,x #s4 = x
  
  
   li $s0,0 #res=0
   li $s1,0 #i=0
  
loop:   beq $s1,$t1,end #check i<=n if i==n exit loop
   lw $s3,0($t0) #get $s3 = a[i]
   bne $s3,$t2,increment #check for a[i]==x if not skip element else continue
   addi $s0,$s0,1 #increment res value
   j increment
  
increment:   addi $s1,$s1,1   #increment i
   addi $t0,$t0,4 #increment array address value of a[]
   j loop

end:   li $v0,1       #Print res value
   move $a0,$s0      #for the given inputs it sould be 3
   syscall

   li $v0,10
   syscall  

Output:

Mars Messages Run l/O program is finished running Clear

Add a comment
Know the answer?
Add Answer to:
1. (a) Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[],...
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