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

Note: In the question, size of array 'loc[]' is not defined, so i assumed arrary size 5 i.e.'loc[5]'.
count:
        .frame  $fp,48,$31              # vars= 32, regs= 1/0, args= 0, gp= 8
        .mask   0x40000000,-4
        .fmask  0x00000000,0
        .set    noreorder
        .set    nomacro
        addiu   $sp,$sp,-48
        sw      $fp,44($sp)
        move    $fp,$sp
        sw      $4,48($fp)
        sw      $5,52($fp)
        movz    $31,$31,$0
        sw      $6,56($fp)
        sw      $0,8($fp)
        sw      $0,12($fp)
        sw      $0,16($fp)
        sw      $0,12($fp)
        b       $L2
        nop

$L4:
        lw      $2,12($fp)
        nop
        sll     $2,$2,2
        lw      $3,48($fp)
        nop
        addu    $2,$3,$2
        lw      $3,0($2)
        lw      $2,56($fp)
        nop
        bne     $3,$2,$L3
        nop

        lw      $2,8($fp)
        nop
        addiu   $2,$2,1
        sw      $2,8($fp)
        lw      $2,16($fp)
        nop
        sll     $2,$2,2
        addiu   $3,$fp,8
        addu    $2,$3,$2
        lw      $3,12($fp)
        nop
        sw      $3,12($2)
        lw      $2,16($fp)
        nop
        addiu   $2,$2,1
        sw      $2,16($fp)
$L3:
        lw      $2,12($fp)
        nop
        addiu   $2,$2,1
        sw      $2,12($fp)
$L2:
        lw      $3,12($fp)
        lw      $2,52($fp)
        nop
        bne     $3,$2,$L4
        nop

        move    $2,$0
        move    $sp,$fp
        lw      $fp,44($sp)
        addiu   $sp,$sp,48
        j       $31
        nop

        .set    macro
        .set    reorder
        .end    count
        .size   count, .-count
        .ident  "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609"

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