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

Rewrite the loop from Exercise 2.29 to reduce the number of MIPS instructions executed

Short Answer

Expert verified

The reduce number of Mips instructions are:

addi$t1,$s0,400

LOOP: lw$s1,0$t1

add$s2,$s2,$s1

addi$t1,$t1,-4

bne $t1,$s0,LOOP

Step by step solution

01

MIPS instructions used in Exercise 2.29

addi: It is short for add immediate

lw: It is short for load word

add: It is an arithmetic operation used to add

slti: It is short for set on less than or equal

bne: it is short for the branch on not equal it test if registers are not equal

02

Step 2:Given MIPS instruction

addi$t1,$0,$0

LOOP: lw$s1,0$s0

add$t2,$s0,

addi$s0,$s0,4

addi$t1,$t1,1

slti$t2,$t1,100

bne$t2,$s0, LOOP

03

Rewrite the loop to Minimize the number of MIPS instruction

Here, we used variable only for loop control

Now, reduce the number of MIPS instruction

addi$t1,$s0,400 # add a constant number

LOOP: lw $s1,0$t1# load word

add $s2,$s2,$s1# add first element

addi $t1,$t1,-4# add immediate and immediate means a constant no.

bne$t2,$s0, LOOP #Loop until the pointer reaches limit

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

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
;

}

Question: Can we use the tail-call optimization in this function? If no, explain why not. If yes, what is the difference in the number of executed instructions in f with and without the optimization?

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

Question:2.28 [5] How many MIPS instructions does it take to implement the C code from Exercise 2.27? If the variables a and b are initialized to 10 and 1 and all elements of D are initially 0, what is the total number of MIPS instructions that is executed to complete the loop?

Question 2.4 [5] For the MIPS assembly instructions below, what is the corresponding C statement? 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.

sll \)t0, \(s0, 2 # \)t0 = f * 4

add \(t0, \)s6, \(t0 # \)t0 = &A[f]

sll \(t1, \)s1, 2 # \(t1 = g * 4

add \)t1, \(s7, \)t1 # \(t1 = &B[g]

lw \)s0, 0(\(t0) # f = A[f]

addi \)t2, \(t0, 4

lw \)t0, 0(\(t2)

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

sw \(t0, 0(\)t1)

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