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

Translate function f into MIPS assembly language. If you need to use registers \(t0 through \)t7, use the lower-numbered registers first. Assume the function declaration for func is “int f(int a, int b);”. The code for function f is as follows:

int f(int a, int b, int c, int d)

returnfunc(funca,b,c+d);

Short Answer

Expert verified

The MIPS code for the given function is:

f: addi $sp,$sp,-12

sw $ra,8($sp)

sw $s1,4($sp)

sw $s0,0($sp)

move $s1,$a2

move $s0,$a3

jal func

move $a0,$v0

add $a1,$s0,$s1

jal func

lw $ra,8($sp)

lw $s1,4($sp)

lw $s0,0($sp)

addi $sp,$sp,12

jr $ra

Step by step solution

01

Determine the MIPS instruction for a procedure and the registers used for the procedure.

MIPS code for the procedure must have the following aspects:

  • Control of the program should be transferred to the procedure.
  • Acquire the needed storage for the procedure.
  • Place the result in the accessible part
  • Always return control to the point of origin.

The register used for procedure calling is as follows:

$a0-$a3- Argument register that is used to pass parameters to the functions.

$v0-$v1- value registers to which values are returned.

$ra is the return address register to return to the point of origin.

02

Determine the contents of the stack after the function call.

Given function:

intf(int a, int b, int c, int d)

returnfuncfunca,b,c+d;

MIPS code :

f: addi $sp,$sp,-12

sw $ra,8($sp)

sw $s1,4($sp)

sw $s0,0($sp)

move $s1,$a2

move $s0,$a3

jal func

move $a0,$v0

add $a1,$s0,$s1

jal func

lw $ra,8($sp)

lw $s1,4($sp)

lw $s0,0($sp)

addi $sp,$sp,12

jr $ra

Explanation:

The registers that can be used are $t0 to $t7 and the low numbered registers must be used first. Here $s registers are used. The given function f will take four arguments. f will add a+b and c+d. It is written with a return type integer.

In MIPS, the stack pointer is initiated and the four parameters are pushed onto the stack. jal instruction will jump back to the address where $ra is stored. The addition of $s0 and $s1 will be stored in $a1 and the value is returned by $v0. Then after updating the stack values again when the function is called, the process repeats.

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

Assume that registers \(s0 and \)s1 hold the values 0x80000000 and 0xD0000000, respectively.

1. What is the value of \(t0 for the following assembly code?

add \)t0,\(s0,\)s1

2. Is the result in \(t0 the desired result, or has there been an overflow?

3. For the contents of registers \)s0 and \(s1 as specified above, what is the value of \)t0 for the following assembly code?

sub \(t0,\)s0,\(s1

4. Is the result in \)t0 the desired result, or has there been an overflow?

5. For the contents of registers \(s0 and \)s1 as specified above, What is the value of \(t0 for the following assembly code?

add \)t0,\(s0,\)s1

add \(t0,\)t0,\(s0

6. Is the result in \)t0 the desired result, or has there been an overflow?

Question: Provide the type, assembly language instruction, and binary representation of instruction described by the following MIPS fields: op=0, rs=3, rt=2, rd=3, shamt=0, funct=34

Assume \(t0 holds the value 0x00101000. What is the value of \)t2 after the following instructions?

slt \(t2, \)0, \(t0

bne \)t2, \(0, ELSE

j DONE

ELSE: addi \)t2,$t2, 2

DONE:

Provide the type, assembly language instruction, and binary representation of instruction described by the following MIPS fields: op=0, rs=3, rt=2, rd=3, shamt=0, funct=34

Question: 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.

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