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;

}

Homework Answers

Answer #1

Please find the required conversion as the following:

=================================================================

count:

daddiu $sp,$sp,-48

sd $fp,40($sp)

move $fp,$sp

sd $4,16($fp)

move $3,$5

move $2,$6

sll $3,$3,0

sw $3,24($fp)

sll $2,$2,0

sw $2,28($fp)

sw $0,0($fp)

sw $0,4($fp)

sw $0,4($fp)

b .L2

nop

.L4:

lw $2,4($fp)

dsll $2,$2,2

ld $3,16($fp)

daddu $2,$3,$2

lw $3,0($2)

lw $2,28($fp)

bne $2,$3,.L3

nop

lw $2,0($fp)

addiu $2,$2,1

sw $2,0($fp)

.L3:

lw $2,4($fp)

addiu $2,$2,1

sw $2,4($fp)

.L2:

lw $3,4($fp)

lw $2,24($fp)

bne $3,$2,.L4

nop

lw $2,0($fp)

move $sp,$fp

ld $fp,40($sp)

daddiu $sp,$sp,48

j $31

nop

================================================================

Know the answer?
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
Similar Questions
Translate the following C function into MIPS assembly code. Note that function f1 is defined somewhere...
Translate the following C function into MIPS assembly code. Note that function f1 is defined somewhere else. int f2(int x, int y) { int i, z = 0; for (i = x; i <= y; i++) z = z + f1(i, 5); return z; }
Consider the function with header: int foo(int s, int n, int a, int b). Write MIPS...
Consider the function with header: int foo(int s, int n, int a, int b). Write MIPS assembly code for the following statement located inside the above function: return(s+n);
Convert the following C program to inline assembly int main(){ int i; printf("Hex Dec Oct Ch\n");...
Convert the following C program to inline assembly int main(){ int i; printf("Hex Dec Oct Ch\n"); for (i = 32; i < 256; i++){ printf("%2x %3d %3o %c\n",i,i,i,i); } return 0; } the code below shows where to do the assembly at: int main(){ int i = 0; char *hdr = "Hex Dec Oct Ch\n"; char *msg = " %2x %3d %3o %c\n"; printf("%s\n",hdr); ———asm{ // DO ASSEMBLY HERE } system("pause"); }
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]; }
a. Write MIPS assembly code for the following machine code. Assume that the segment is placed...
a. Write MIPS assembly code for the following machine code. Assume that the segment is placed starting at location 8000. Create labels for branch and jump instructions and indicate the 0001 0001 0010 1000 0000 0000 0000 0011 1000 1110 0001 0001 0000 0000 0000 1000 0010 0010 0010 1000 0000 0000 0000 0110 0000 1000 0000 0000 0000 0001 0000 0010 b. Translate the following C function into MIPS assembly code. int f1(int ar1[], int ar2[], int n) {...
How do you write x86 assembly code for the above main procedure and Addarrays function? Main()...
How do you write x86 assembly code for the above main procedure and Addarrays function? Main() {      int A[100];      int   B[100];      // initialization etc.      length = 100;      Call Addarrays(A, B, length) } Addarrays(int[] X, int[] Y, count) {    i = 0;    while (i < count) {        X[i] = X[i] + Y[i];         I++    } }
Translate C code into MIPS. Do not include an exit syscall int proc1( int a, int...
Translate C code into MIPS. Do not include an exit syscall int proc1( int a, int b ) { if ( proc2( a, b ) >= 0 ) return 1; else return -1; } int proc2( int a, int b ) { return (a*10) & (b*10); } int main() { int a = 9; int b = -10; int c = a + b + proc1( a, b ); printf("%d\n", c ); return 0; }
Given the following function in C++: int main(void) { int a = 2; int b =...
Given the following function in C++: int main(void) { int a = 2; int b = myFunction(a); a = b + 1; b = myFunction(a); cout << ”b = ” << b << endl; return 0; } int z = myFunction(int x) { static int n = 0; n = n + 1; int z = x + n; return z; } What is printed by the cout on the screen?
int function3(int arr1[], int arr2[], int n) {    int count = 0; for (int i...
int function3(int arr1[], int arr2[], int n) {    int count = 0; for (int i = 1; i <= n / i && arr1[i] != arr2[i]; i++) { count++; } return count; find time complex
Given the following function:      int C(int n, int k)                  {              
Given the following function:      int C(int n, int k)                  {                     if (k= =0) return 1;                        else return (C(n-1, k-1) * n)/k;                                       } What type of function is this? Recursive or Iterative. Explain your answer.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT