Question

In: Computer Science

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);

Solutions

Expert Solution

Given C# code as follows:

int recursive(int x) {

return (x >= 0xFF) ? (recursive(x - 3) + recursive(x - 2)): ((x >= 0xF) ? recursive(x - 1) + 1 : 1);

}

Converted MIPS assembly language code:

CODE                                                                                COMMENT

recursive:                                                                        //function started

addiu $sp,$sp,-40                                                                    //intializing stack pointer

sw $31,36($sp)                                                                    //save word from stack pointer at $31

sw $fp,32($sp)           //save word from stack pointer at file pointer    sw $16,28($sp)                                                               //save word from stack pointer at $16

move $fp,$sp                                                               //move content of stack pointer to file pointer

sw $4,40($fp)                                                            //store word at $4 from file pointer

lw $2,40($fp)                                                           //load word from $2 to file pointer

nop                                                                          //do nothing

slt $2,$2,255                                                          //set $2 if $2<0xF

bne $2,$0,$L2                                                     //branch to L2 if $2 not equal to $0

nop                                                                   //do nothing

lw $2,40($fp)                                                // load $2 with file pointer value

nop                                                               //do nothing

addiu $2,$2,-3                                              //x=x-3 implemented and result stored at $2

move $4,$2                                                 //move the content of $2 to $4

jal recursive                                               //call recurisve function as recurisve(x-3)

nop                                                          //do nothing

move $16,$2                                       //move the content of $2 to $16

lw $2,40($fp)                                       //load the content of file pointer to $2

nop                                                    //do nothing

addiu $2,$2,-2                                   //set x=x-2 store result at $2

move $4,$2                                      //move content of $2 to $4

jal recursive                                      //call recursive function recursive(x-2)

nop                                                     //do nothing

addu $2,$16,$2                              //Add content of $16 and $2 and store the result in $2

b $L6                                              //goto branch L6

nop                                               //do nothing

$L2:

lw $2,40($fp)                         //load the content of $2 with file pointer

nop                                       //do nothing

slt $2,$2,15                          //set $2 if $2<0xF

bne $2,$0,$L4                  //if $2 not equal to 0 then goto branch L4

nop                                   //do nothing

lw $2,40($fp)                        //load the content of $2 with file pointer

nop                                       //do nothing

addiu $2,$2,-1                   //set $2 as x-1

move $4,$2                       //move content of $2 to $4

jal recursive                        //call recursive function as recursive(x-1)

nop                                    //do nothing

addiu $2,$2,1                 //Add 1 to $2 content

b $L6                             //branch to L6

nop                              // do nothing

$L4:

li $2,1 # 0x1                  //set $2 as 1

$L6:

move $sp,$fp           //move file pointer content to stack pointer

lw $31,36($sp)       //load stack pointer to $31

lw $fp,32($sp)        //load file pointer with stack pointer

lw $16,28($sp)        //load $16 with stack pointer

addiu $sp,$sp,40       //Add 40 to stack pointer

j $31                         //jump with $31

nop                           //do nothing


Related Solutions

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);
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...
3. Translate the following C code to MIPS assembly code (in two separate files). int main()...
3. Translate the following C code to MIPS assembly code (in two separate files). int main() { printf(“before subroutine!\n”); Subfunc(); printf(“after subroutine!\n!”); } void Subfunc() {printf(“I am subroutine!\n”);} Submission file: Lab4_3a.asm for the main routine and Lab4_3b.asm for the sub-routine.
3. Translate the following C code to MIPS assembly code (in two separate files). int main()...
3. Translate the following C code to MIPS assembly code (in two separate files). int main() { printf(“before subroutine!\n”); Subfunc(); printf(“after subroutine!\n!”); } void Subfunc() {printf(“I am subroutine!\n”);} 4. Translate the following C code to MIPS assembly (in two separate files). Run the program step by step and observe the order of instructions being executed and the value of $sp. int main() { int x=2; z=Subfunc(x); printf(“Value of z is: %d”, z); } int Subfunc(int x) { return x+1;}
Translate c++ code into mips assembly: int main() {                 cout << "Numbers:" << endl;            &nbs
Translate c++ code into mips assembly: int main() {                 cout << "Numbers:" << endl;                                 int x[] = {18, 12, 6, 500, 54, 3, 2, 122};                 int i;                                 for (i=0; i<8; i++)                 {                                                 cout << x[i] << endl;                 }                 return 0; } below is my code: .data        str1: .ascii "Numbers:"     str2: .ascii "\n"    x: .word 18,12,6,500,54,3,2,122       .text                      ...
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:" <<...
2. Translate the following C/Java code to MIPS assembly code. Assume that the values of a,...
2. Translate the following C/Java code to MIPS assembly code. Assume that the values of a, i, and j are in registers $s0, $t0, and $t1, respectively. Assume that register $s2 holds the base address of the array A (add comments to your MIPS code). j = 0; for(i=0 ; i<a ; i++) A[i]=i+j++;
4.Translate the following C code to MIPS assembly code. Assume that the value of i is...
4.Translate the following C code to MIPS assembly code. Assume that the value of i is in register $t0, and $s0 holds the base address of the integer MemArray if (i > 10) MemArray[i] = 0; else MemArray[i] = -MemArray[i]; 6.Translate the following C code to MIPS assembly code. Use a minimum number of instructions. Assume that the values of a, b, i, and j are in registers $s0, $s1, $t0, and $t1, respectively. Also, assume that register $s2 holds...
Translate the following C code into MIPS assembly code. Assume a in $s0, i in $s1,...
Translate the following C code into MIPS assembly code. Assume a in $s0, i in $s1, and base address of b[] in $s2, respectively. Note that the element’s data type in array b is integer (word). Please do not change the C code structure and please comment your MIPS code. for (i = 100; i>a; i--) {          b[i] = 16*i; b[i+1] = 16*(i+1); b[i+2] = 2*i + 16; }
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; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT