Question

In: Computer Science

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

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;
int j = 0;
int loc[];
for(i = 0; i != n; i++)
if(a[i] == x) {
res = res + 1;
loc [j] = i;
j = j+1}
return res, loc;
}

Solutions

Expert Solution

Important Note:

I added code for printing the locations of x also the below code is for that if you don't want this code and only the converted c function, please follow the second program I submitted in this answer.

Program:

.data
n: .word 7 #n
a: .word 10, 20, 30, 4, 20, 10, 11   #array a[]
loc: .word 0:7 #array loc[]
x: .word 20
pos: .asciiz "\nPosition of x is at : "
.text
.globl main
main:
   la $s1, a # $s1 = &a
   lw $s2, n #s2 = n
   la $s3,loc #$s3 = &loc
   lw $s4,x #s4 = x
   li $t0,0 #res=0
   li $t1,0 #i=0
   li $t2,0 #j=0
   la $s6,loc
  
loop:   beq $t1,$s2,loop2
   lw $t3,0($s1) #get a[i]
   bne $t3,$s4,inc
   addi $t0,$t0,1 #increment res value
   sw $t1,0($s3) #loc[j] = i
   addi $s3,$s3,4 #increment array address value
   addi $t2,$t2,1 #increment j to find size of loc[] elements are stored
   j inc
  
inc:   addi $t1,$t1,1   #increments i
   addi $s1,$s1,4 #increments array valuesin a[]
   j loop
  
  
loop2:   beq $t4,$t2,end  
   lw $t6,0($s6)
   li $v0,4
   la $a0,pos
   syscall
   li $v0,1
   move $a0,$t6
   syscall
   addi $t4,$t4,1
   addi $s6,$s6,4
   j loop2
  
end:   li $v0,10
   syscall  
  
  
  
Output:

Exact conversion of given c-program

Program:

.data
n: .word 7 #n
a: .word 10, 20, 30, 4, 20, 10, 11   #array a[]
loc: .word 0:7 #array loc[]
x: .word 20
.text
.globl main
main:
   la $s1, a # $s1 = &a
   lw $s2, n #s2 = n
   la $s3,loc #$s3 = &loc
   lw $s4,x #s4 = x
   li $t0,0 #res=0
   li $t1,0 #i=0
   li $t2,0 #j=0
  
loop:   beq $t1,$s2,end
   lw $t3,0($s1) #get a[i]
   bne $t3,$s4,inc
   addi $t0,$t0,1 #increment res value
   sw $t1,0($s3) #loc[j] = i
   addi $s3,$s3,4 #increment array address value
   addi $t2,$t2,1 #increment j to find size of loc[] elements are stored
   j inc
  
inc:   addi $t1,$t1,1   #increments i
   addi $s1,$s1,4 #increments array valuesin a[]
   j loop

end:   li $v0,10
   syscall  
  

Output:


Related Solutions

Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
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; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
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; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
convert following C++ code into MIPS assembly: int main() {                                 &
convert following C++ code into MIPS assembly: int main() {                                         int x[10], occur, count = 0;                                                              cout << "Type in array numbers:" << endl; for (int i=0; i<10; i++) // reading in integers                               { cin >> x[i];        } cout << "Type in occurrence value:" << endl;                                 cin >> occur;                                                 // Finding and printing out occurrence indexes in the array                                  cout << "Occurrences indices are:" <<...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c,...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c, int d) { return func(func(a,b), func(b+c,d)); } Assume the followings. • The prototype of function func is “int func(int a, int b);”. • You do not need to implement function func. The first instruction in function func is labeled “FUNC”. • In the implementation of function f, if you need to use registers $t0 through $t7, use the lower-numbered registers first. • In the...
Write a full MIPS assembly code for: Function Compare(int A[][], int B[][], int M, int N,...
Write a full MIPS assembly code for: Function Compare(int A[][], int B[][], int M, int N, int i, int j, int k, int l). This function compares the elements of the array A placed between (i,j) and (k,l) with the corresponding elements of the array B placed between the same indexes. The function returns the number of elements of both matrices that coincide in the same position (c,d). That is, it is necessary to check each element A[c,d] with the...
Translate the following C code to MIPS assembly. int a = 1; int b = 2;...
Translate the following C code to MIPS assembly. int a = 1; int b = 2; if (a<b)           a=a+1; b = b + a; printf("The value of b is: %d", b); Translate the following C code to MIPS assembly. int a = 2; int b = 2; if (a<b)           a=a+1; else           a=a-1; b = b + a; printf("The value of b is: %d", b);
Write the MIPS assembly version of this C code: int weird(char[] s, int x) { int...
Write the MIPS assembly version of this C code: int weird(char[] s, int x) { int i; for (i = 0; i < x; i++) { if (s[i] == ‘A’) { return power(10, i); } } return -1; } int power(int base, int i) { int j = 0; while (j < base) { base = base * base; j++; } return base; }
Translate following C# code into MIPS assembly language int recursive(int x) { return (x >= 0xFF)...
Translate following C# code into MIPS assembly language int recursive(int x) { return (x >= 0xFF) ? (recursive(x - 3) + recursive(x - 2)) : ((x >= 0xF) ? recursive(x - 1) + 1 : 1);
Please code C# Convert the following for loop into a while loop: for(int count = 8;...
Please code C# Convert the following for loop into a while loop: for(int count = 8; count > 0; count--) { Console.WriteLine(count); }
Our MIPS assembly version will “compile” the following C program: #include void Compare(int b1, int h1,...
Our MIPS assembly version will “compile” the following C program: #include void Compare(int b1, int h1, int b2, int h2); int Area(int b, int h); int main(int argc, char **argv) { int base1, base2, height1, height2; base1=10; base2=12; height1=8; height2=7; Compare(base1, height1, base2, height2); return 0; } void Compare(int b1, int h1, int b2, int h2) { int A1=Area(b1, h1); int A2=Area(b2, h2); if (A1>=A2) printf("Area1 is greater than Area2.\n"); else printf("Area2 is greater than Area1.\n"); } int Area(int b,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT