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

Question: Translate the following C code to MIPS. 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. Assume that the elements of the arrays A and B are 4 byte words:

B[8]=A[i]+A[j];

Short Answer

Expert verified

MIPS code:

sll $t0,$s1,2

add $t0,$t0,$s7

lw $t0, 0($t0)

addi $t0,$t0,1

sll $t0, $t0,2

lw $s0,0($t0)

Step by step solution

01

Define MIPS assembly language.

The MIPS assembly language is the instruction-based programming language. MIPS assembly language uses multiple instructions to perform an operation.

MIPS has operands for 32 registers and in MIPS data must be in registers to perform operations on them.

Some example instructions are as follows:

add $s1,$s2,$s3

The above instruction will add the values in the registers s2 and s3. The result will be stored in the s1 register.

Likewise, MIPS performs all the operations through the registers and memory locations.

02

Determine the MIPS code for the given C statement.

The given C statement:

The registers, $s0, $s1, $s2, $s3, and $s4 are assigned with f, g, h, i, and j respectively.

The base address of the arrays A and B are stored in the registers $s6 and $s7. The elements of the arrays are 4 byte words.

With above information, the MIPS code for the given C statement is as follows:

sll $t0,$s1,2

add $t0,$t0,$s7

lw $t0, 0($t0)

addi $t0,$t0,1

sll $t0, $t0,2

lw $s0,0($t0)

Instruction

Explanation

sll $t0,$s1,2

Shift left logical instruction will shift 4 spaces in $s1 register that is g.

$t0🡨-4*g

add $t0,$t0,$s7

The values of $t0 and $s7 will be added and the result will be stored in $t0.The $t0 will have the address of array B with value g.

$t0🡨Address of B[g]

lw $t0,0($t0)

Now, the value of B[g] will be loaded to $t0.

$t0🡨B[g]

addi $t0,$t0,1

The value of B[g] will be added with the immediate 1.

$t0🡨B[g]+1

sll $t0,$t0,2

Now the logical shift of 4 will be pointed to B[g]+1 and the address of B[g]+1 will be assigned.

lw $s0,0($t0)

The f will be assigned with the value of A[B[g]+1]

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

Question: If the current value of the PC is 0x00000600, can you use a single jump instruction to get to the PC address as shown in Exercise 2.39?

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

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: Provide the type and hexadecimal representation of the following instruction:

sw \(t1, 32(\)t2)

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

add f, g, h

add f, i, f

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