Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Implement the following C code in MIPS assembly. What is the total number of MIPS instructions needed to execute the function?

int fib(int n){

ifn==0

return 0;

else ifn=1

return 1;

else

return fibn-1+fibn-2
;

}

Short Answer

Expert verified

The total number of MIPS instructions needed to execute the function is 38.

Step by step solution

01

MIPS language

MIPS (Microprocessor without Interlocked Pipelined Stages)[1] is a RISC instruction set architecture invented by MIPS Computer Systems, now MIPS Technologies, in the United States. MIPS is available in several versions, including MIPS I, II, III, IV, and V, as well as five MIPS32/64 releases (for 32- and 64-bit implementations, respectively). MIPS architectures were originally 32-bit, although 64-bit versions were eventually developed. MIPS32/64 Release 6 is the most recent version as of April 2017. In addition to the user-mode architecture, MIPS32/64 differentiates from MIPS I–V by establishing the privileged kernel mode System Control Coprocessor.

02

MIPS instruction for the above function                  

The function fib() returns the integers as the output. The fib() gives the Fibonacci numbers according to the user input. The term of the Fibonacci number is the sum of its last two terms. The MIPS instruction are as belows:

fib:

addiu $sp,$sp,-3 //add immediate.

sw $31,28($sp) // store word from 28th register in 31th.

sw $fp,24($sp) // store word from 24th register to frame pointer.

sw $16,20($sp) // store word from 20th register into 16th .

move $fp,$sp //move the contents from stack pointer to frame pointer.

sw $4,32($fp) // store the contents fron frame pointer to 4th register.

lw $2,32($fp) //load the words from frame pointer to 2nd register

bne $2,$0,.L2 //branch or not equal

nop //does nothing

move $2,$0 //move the contents from 0th register to 2nd register.

b .L3 //branch instruction

nop //does nothing

.L2:

lw $3,32($fp) //load word the from frame pointer to 3rd register.

li $2,1 // 0x1

bne $3,$2,.L4 //branch or not equal

nop //does nothing

li $2,1 // load immediate fron 2nd register to 1st register.

b .L3 //branch instruction

nop //does nothing

.L4:

lw $2,32($fp) ) //load contents from frame pointer to 2nd register.

addiu $2,$2,-13 //add immediate from the 2nd register.

move $4,$2 //move the contents from the 2nd register to 4th register.

jal fib //jump and link

nop //does nothing

move $16,$2 //move the contents from 2nd register to 16th .

lw $2,32($fp) //load word from frame pointer to the 2nd register.

addiu $2,$2,-23 //add immeediate

move $4,$2 //move the contents from 2nd regitsers to 4th register.

jal fib //jump and link

nop //does nothing

addu $2,$16,$2 // add immediate

.L3:

move $sp,$fp //move the contents from the frame to stack pointer.

lw $31,28($sp) //load contents from the stack pointer to 31th register.

lw $fp,24($sp) //load contents from stack pointer to frame pointer.

lw $16,20($sp) //load word from stack pointer to 16th register.

addiu $sp,$sp,323 //add immeediate

jr $31 //unconditionally jump

nop //does nothing

The “nop” in the MIPS instruction is an instruction that does nothing(has no side-effect).

The “lw” in the MIPS instruction is an instruction that loads a word into the register.

The “li” in the MIPS instruction is an instruction that stands for load immediate.

The “addiu” in the MIPS instruction is an instruction that is used to add constants to signed integers when we don’t care about overflow.

The “move” in the MIPS instruction is an instruction that stands for load immediate.

The “jr” in the MIPS instruction is an instruction that causes pc to jump to the contents of the first source register.

The “sw” in the MIPS instruction is an instruction that stands for store word.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Write a program in MIPS assembly language to convert an ASCII number string containing positive and negative integer decimal strings, to an integer. Your program should expect register \(a0 to hold the address of a null-terminated string containing some combination of the digits 0 through 9. Your program should compute the integer value equivalent to this string of digits, then place the number in register \)v0. If a non-digit character appears anywhere in the string, your program should stop with the value -1 in register \(v0. For example, if register \)a0 points to a sequence of three bytes 50ten, 52ten, 0ten (the null-terminating string “24”), then when the program stops, register $v0 should contain the value 24ten.

For the following MIPS assembly instructions above, what is a corresponding C statement?

add f, g, h

add f, i, f

The table below shows 32-bit values of an array stored in memory.

2.6.1 For the memory locations in the table above, write C code to sort the data from lowest to highest, placing the lowest value in the smallest memory location shown in the figure. Assume that the data shown represents the C variable called Array, which is an array of type int, and that the first number in the array shown is the first element in the array. Assume that this particular machine is a byte-addressable machine and a word consists of four bytes.

2.6.2 For the memory locations in the table above, write MIPS code to sort the data from lowest to highest, placing the lowest value in the smallest memory location. Use a minimum number of MIPS instructions. Assume the base address of Array is stored in register $s6.

Question: For the following C statement, what is the corresponding MIPS assembly code? Assume that the variables f, g, h, i, and j are assigned to registers \(s0, \)s1, \(s2, \)s3, and \(s4, respectively. Assume that the base address of the arrays A and B are in registers \)s6 and $s7, respectively.

For the following C statement, write a minimal sequence of MIPS assembly instructions that does the identical operation. Assume \(t1=A, \)t2=B and $s1 is the base address of C.

A=C04;

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free