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 code for fact to use fewer instructions.

Short Answer

Expert verified

The following code for fact is rewritten to use fewer instructions.

addi $sp, $sp, -8

sw $ra, 4($sp)

sw $a0, 0($sp)

slti $t0, $a0, 1

beq $t0, $zero, L1

addi $v0, $zero, 1

addi $sp, $sp, 8

jr $ra

L1:

addi $a0, $a0,-1

jal fact

lw $a0, 0($sp)

lw $ra, 4($sp)

addi $sp, $sp, 8

mul $v0, $a0, $v0

jr $ra

Step by step solution

01

fact code

The following is the given code for fact which has more instructions.

subu $sp,$sp,32 # Stack frame is 32 bytes long

sw $ra,20($sp) # Save return address

sw $fp,16($sp) # Save frame pointer

addiu $fp,$sp,28 # Set up frame pointer

sw $a0,0($fp) # Save argument (n)

02

Rewritten code.

The following code is rewritten so as the code for fact uses fewer instructions.

addi $sp, $sp, -8 #adjust stack for 2 items

sw $ra, 4($sp) #save return address

sw $a0, 0($sp) #save argument

slti $t0, $a0, 1 #test for n<1

beq $t0, $zero, L1

addi $v0, $zero, 1 #yes, result is 1

addi $sp, $sp, 8 #pop 2 items from stack

jr $ra #stack and return

L1:

addi $a0, $a0,-1 #else decrement n

jal fact #recursive call

lw $a0, 0($sp) # restore original n

lw $ra, 4($sp) #and return address

addi $sp, $sp, 8 #pop 2 items from stack

mul $v0, $a0, $v0 #multiply to get

jr $ra #result and return

Explanation:

Instruction

Description

addi $sp, $sp, -8

The stack is adjusted for two items.

sw $ra, 4($sp)

Return address is saved.

sw $a0, 0($sp)

Argument is saved.

slti $t0, $a0, 1

Tested if n is less than 1.

addi $v0, $zero, 1

Result is 1.

addi $sp, $sp, 8

Two items are popped from the stack.

jr $ra

Stack and return.

addi $a0, $a0, -1

If result is no, decrement of 1.

jal fact

Recursive call.

lw $a0, 0($sp)

Restore original value of n

lw $ra, 4($sp)

Return address

addi $sp, $sp, 8

Two items are popped from the stack.

mul $v0, $a0, $v0

Multiplied to get result

jr $ra

Result and returns.

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

A.7 [5] Using SPIM, write and test a program that reads in three integers and prints out the sum of the largest two of the three. Use the SPIM system calls described on pages A-43 and A-45. You can break ties arbitrarily.

Assume that X consists of 3 bits : x2, x1, x0. Write four logic functions that are true if and only if

  • X contains only one 0
  • X contains an even number of 0s
  • X when interpreted as an unsigned binary number is less than 4
  • X when interpreted as a signed (two’s complement) number is negative.

A systolic array is an example of an MISD machine. A systolic array is a pipeline network or “wavefront” of data processing elements. Each of these elements does not need a program counter since execution is triggered by the arrival of data. Clocked systolic arrays compute in “lock-step” with each processor undertaking alternate compute and communication phases.

6.12.1 [10] Consider proposed implementations of a systolic array (you can find these in on the internet or in technical publications). Then attempt to program the loop provided in Exercise 6.11 using this MISD model. Discuss any difficulties you encounter.

6.12.2 [10] Discuss the similarities and differences between an MISD and SIMD machine. Answer this question in terms of data-level parallelism.

First, write down a list of the daily activities that you typically do on a weekday. For instance, you might get out of bed, take a shower, get dressed, eat breakfast, dry your hair, and brush your teeth. Make sure to break down your list so you have a minimum of 10 activities.

6.1.1 Now consider which of these activities is already exploiting some form of parallelism (e.g., brushing multiple teeth at the same time, versus one at a time, carrying one book at a time to school, versus loading them all into your backpack and then carry them “in parallel”). For each of your activities, discuss if they are already working in parallel, but if not, why they are not.

6.1.2 Next, consider which of the activities could be carried out concurrently (e.g., eating breakfast and listening to the news ). For each of your activities, describe which other activity could be paired with this activity.

6.1.3 For 6.1.2, what could we change about current systems (e.g., showers, clothes, TVs, cars) so that we could perform more tasks in parallel?

6.1.4 Estimate how much shorter time it would take to carry out these activities if you tried to carry out as many tasks in parallel as possible.

B.21 [10] <§§B.3, B.4> Given the following logic diagram for an accumulator, write down the Verilog module implementation of it. Assume a positive edge triggered register and asynchronous Rst.

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