Question

MIPS (MARS CODE) Please covert this C++ code to MIPS code and leave comments on each...

MIPS (MARS CODE) Please covert this C++ code to MIPS code and leave comments on each line to what it does!

int fib( int n )

{

if ( n <= 1 )

return n;

else

return fib(n - 1 ) + fib( n - 2 );

}

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

there are two recursive calls in the function. we need to save the result of first fib before calling it again

1. give register names to variables and determine which is base case and which is recursive. Only single input, n is passed in register $a0. The base case is the “then” clause. The recursive case is the “else” clause.

2. Convert the code for the base case.

  fib:

bgt $a0, 1, recurse

move $v0, $a0

jr $ra

3. Save callee- and caller-saved registers on the stack for not calling again and to know who called whom.

recurse:

sub $sp, $sp, 12 # We need to store 3 registers to stack

sw $ra, 0($sp) # $ra is the first register

sw $a0, 4($sp) # $a0 is the second register, we cannot assume

# $a registers will not be overwritten by callee

4. calling fib recursively

  addi $a0, $a0, -1 # N-1

  jal fib

sw $v0, 8($sp) # store $v0, the third register to be stored on

  # the stack so it doesn’t get overwritten by callee

5. Call fib recursively again.

lw $a0, 4($sp) # retrieve original value of N

addi $a0, $a0, -2 # N-2

  jal fib

6. Clean up the stack and return the result.

  lw $t0, 8($sp) # retrieve first function result

add $v0, $v0, $t0

  lw $ra, 0($sp) # retrieve return address

addi $sp, $sp, 12

jr $ra

Add a comment
Know the answer?
Add Answer to:
MIPS (MARS CODE) Please covert this C++ code to MIPS code and leave comments on each...
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
  • Convert the below C code to basic MIPS. Please leave comments for explanation #include <stdio.h> int main(void) {...

    Convert the below C code to basic MIPS. Please leave comments for explanation #include <stdio.h> int main(void) { printf("Insert two numbers\n"); int a,b; scanf("%d",&a); scanf("%d",&b); a=a<<2; b=b<<2; printf("%d&%d\n",a,b); return 0; }

  • Convert the below C code to basic MIPS. Leave comments for explanation please #include <stdio.h> int main(void) {...

    Convert the below C code to basic MIPS. Leave comments for explanation please #include <stdio.h> int main(void) { printf("Insert two numbers\n"); int a,b,c; scanf("%d",&a); scanf("%d",&b); c=a|b; printf("%d|%d=%d\n",a,b,c); return 0; }

  • MIPS assembly language Covert this code to MIPS: #include <stdio.h> int function (int a) int main)i...

    MIPS assembly language Covert this code to MIPS: #include <stdio.h> int function (int a) int main)i int x=5 ; int y: y function(x); printf "yd",y); return 0; int function (int a) return 3*a+5; Assumptions: . Place arguments in $a0-$a3 . Place return values in $vO-$v1 Return address saved automatically in $ra . lgnore the stack for this example. (Thus, the function will destroy registers used by calling function

  • please answer in C code and leave comments on what the code does in the code...

    please answer in C code and leave comments on what the code does in the code switch (n % 10) case 1: x = n; break: case 3: - y = 10; case 5: x-n.2: break; default: x=0; y = 0; break; ] What will be values of x and y equal to after exiting this switch operator if right before the switch, values of and y were both equal to -35, and n was one of these: n before...

  • Objective Create a MIPS program in Mars to perform a sorting algorithm. Specification: 1) Download the...

    Objective Create a MIPS program in Mars to perform a sorting algorithm. Specification: 1) Download the file Assign2.asm. Read it and make sure it can be compiled and executed in Mars. 2) Write code to finish all the tasks listed in Assign2.asm. In Assign2.asm, an array‘a’ of ‘n’=11 integers are given at the beginning (make sure do not change these integers): 43, -5, 11, 12, 64, -7, 14, 71, 70, 13, -27 The finished Assign2.asm should be filled with your...

  • MIPS 1(a): Using MARS (MIPS assembly simulator), write and debug a program with comments that will...

    MIPS 1(a): Using MARS (MIPS assembly simulator), write and debug a program with comments that will store words in a RAM array using the instruction sw and indirect addressing as specified in the data table below. Although the addresses and data are in given hexadecimal, your code will contain corresponding values in decimal. Use (268501056)_10 = (10010040)_16 as the base address of the array. Since the instruction sw and indirect addressing (using offsets) are needed to store the data in...

  • C Language! Please, comment what each line of code does in the code below and answer...

    C Language! Please, comment what each line of code does in the code below and answer this question: (sentence is fine) 1. Aside from pressing ctrl-(d/z) at the beginning of a line causing the getnchar() function to return NULL, is there any way to enter less than 9 characters? Code: void exer1(void) {    char input[LEN];    char *check; getchar(); // Clearing character buffer. printf("Please enter 9 characters: "); // Prompting the user to enter // a specific input.   ...

  • MIPS MIPS MIPS PLEASE INCLUDE COMMENTS AND OUTPUT Sort array using Bubble sort algorithm. 1) First...

    MIPS MIPS MIPS PLEASE INCLUDE COMMENTS AND OUTPUT Sort array using Bubble sort algorithm. 1) First ask the user how many elements of his/her array. 2) Then, read the integer array elements as input from the User. 3) Then, print out the array before the sorting 4) Apply Bubble sort algorithm on your array 5) Print out the array after the sorting 6) Print some welcome text to th user 7) Add comments to your code to describe how is...

  • CONVERT THE FOLLOWING FUNCTION IN MARS SIMULATOR (MIPS) This FUNCTION should be Runnable on MARS (MIPS...

    CONVERT THE FOLLOWING FUNCTION IN MARS SIMULATOR (MIPS) This FUNCTION should be Runnable on MARS (MIPS Assembler and Runtime Simulator) IDE C FUNCTION WHICH NEEDS TO BE CONVERTED (ONLY USE MARS) void decrypt(int numberOfChar, int key, char * cipherText, char * plainText) { int j = 0; int i =0; int k = 0; int reverseOrderedKey[4]; int n =4; int c =0; int d =0; int orderedKey[4] = {0,0,0,0};   while(key) { reverseOrderedKey[k] = key %10; key /= 10; k++; }...

  • Covert C++ code into MIPS. -If x has a value if 2, print "bbb" if x...

    Covert C++ code into MIPS. -If x has a value if 2, print "bbb" if x has a value of 3, print "ccc" if x has a value of 4, print "ddd" if x has a value other than 2, 3, or 4 print "eee". result = ""; switch (x) { case 2: result = result + "bbb"; break; case 3: result = result + "ccc"; break; case 4: result = result + "ddd"; break; default: result = result +...

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