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

A.9 [25] Write and test a MIPS assembly language program to compute and print the first 100 prime numbers. A number n is prime if no numbers except 1 and n divide it evenly. You should implement two routines: ■ test_prime (n) Return 1 if n is prime and 0 if n is not prime. ■ main () Iterate over the integers, testing if each is prime. Print the first 100 numbers that are prime. Test your programs by running them on SPIM.

Short Answer

Expert verified

Corresponding tested on SPIM MIPS assembly language program:

.data

Space:

.ascii “\n”

..text

..global main

main:

li $s0, 2

li $s1, 100

li $s2, 0

loop: addi $a0, $s0, 0

jal prime_test

addi $s0, $s0, 1

beqz $v0, loop

addi $s2, $s2, 1

addi $s3, $v0, 0

li $v0, 1

addi $a0, $s0, -1

syscall #print the prime number

li $v0, 4

la $a0, space

syscall

bne $s1, $s2, loop

li $v0, 10 # exit

syscall

prime_test: li $t0, 2

test loop: beq $t0, $a0, test_exit_true a

divide $a0

div $a0, $t0

mfhi $t1

addi $t0, $t0, 1

bnez $t1, test_loop

addi $v0, $zero, 0

jr $ra

test_exit_true: addi $v0, $zero, 1

jr $ra

Step by step solution

01

Define the concept.

  • One of the “I type” MIPS instruction is “addi $t3 $t4 1” “$t4” is the source register, “$t3” is the destination register, and “1” is the immediate value. The purpose of using it for add immediately
  • The “branch-on-equal (beq)” is a decision-making instruction in MIPS assembly language. The purpose of using this MIPS assembly instruction “beq reg1, reg2 Label” is going to the statement “Label” if the value of “reg1” is equal to the “reg2”.
  • The MIPS instruction “la” is used for loading the address.
  • The purpose of using the “branch-not-equal-to-zero (benz)” MIPS assembly instruction “$t1, test_loop” is going to the statement “test_loop” if the value of “reg1” is not equal to zero.
  • The “mfhi” is used for moving from “HI” to the destination register.
02

Determine the calculation.

Corresponding tested on SPIM MIPS assembly language program:

.data

Space:

.ascii “\n”

..text

..global main

main:

li $s0, 2 // 2 is the smallest prime

li $s1, 100

li $s2, 0 // the number of prime numbers are counted

loop: addi $a0, $s0, 0

jal prime_test

addi $s0, $s0, 1 // $s0 is incremented in advance

beqz $v0, loop // if ($v0 == 0) then $s0 is not prime

addi $s2, $s2, 1 // the counter is incremented

addi $s3, $v0, 0

li $v0, 1

addi $a0, $s0, -1 // remember that +1 in advance

syscall #print the prime number

li $v0, 4

la $a0, space

syscall

bne $s1, $s2, loop

li $v0, 10 # exit

syscall

prime_test: li $t0, 2

test loop: beq $t0, $a0, test_exit_true // if ($t0==$a0) then no number 1<x<$a0

divide $a0

div $a0, $t0

mfhi $t1 // remainder of the division

addi $t0, $t0, 1

bnez $t1, test_loop

addi $v0, $zero, 0

jr $ra

test_exit_true: addi $v0, $zero, 1

jr $ra

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

Question: There are times when we want to add a collection of numbers together. Suppose you wanted to add four 4-bit numbers (A, B, E, F) using 1-bit full adders. Let’s ignore carry lookahead for now. You would likely connect the 1-bit adders in the organization at the top of Figure B.14.1. Below the traditional organization is a novel organization of full adders. Try adding four numbers using both organizations to convince yourself that you get the same answer.

The results of the SPEC CPU2006 bzip2 benchmark running on an AMD Barcelona has an instruction count of 2.389E12, an execution time of 750 s, and a reference time of 9650 s.

1.11.1 [5] <§§1.6, 1.9> Find the CPI if the clock cycle time is 0.333 ns.

1.11.2 [5] <§1.9> Find the SPECratio.

1.11.3 [5] <§§1.6, 1.9> Find the increase in CPU time if the number of instructions of the benchmark is increased by 10% without affecting the CPI.

1.11.4 [5] <§§1.6, 1.9> Find the increase in CPU time if the number of instructions of the benchmark is increased by 10% and the CPI is increased by 5%.

1.11.5 [5] <§§1.6, 1.9> Find the change in the SPECratio for this change.

1.11.6 [10] <§1.6> Suppose that we are developing a new version of the AMD Barcelona processor with a 4 GHz clock rate. We have added some additional instructions to the instruction set in such a way that the number of instructions has been reduced by 15%. Th e execution time is reduced to 700 s and the new SPECratio is 13.7. Find the new CPI.

1.11.7 [10] <§1.6> Th is CPI value is larger than obtained in 1.11.1 as the clock rate was increased from 3 GHz to 4 GHz. Determine whether the increase in the CPI is similar to that of the clock rate. If they are dissimilar, why?

1.11.8 [5] <§1.6> By how much has the CPU time been reduced?

58 Chapter 1 Computer Abstractions and Technology

1.11.9 [10] <§1.6> For a second benchmark, libquantum, assume an execution time of 960 ns, CPI of 1.61, and clock rate of 3 GHz. If the execution time is reduced by an additional 10% without aff ecting to the CPI and with a clock rate of 4 GHz, determine the number of instructions.

1.11.10 [10] <§1.6> Determine the clock rate required to give a further 10% reduction in CPU time while maintaining the number of instructions and with the CPI unchanged.

1.11.11 [10] <§1.6> Determine the clock rate if the CPI is reduced by 15% and the CPU time by 20% while the number of instructions is unchanged.

When a program is adapted to run on multiple processors in a multiprocessor system, the execution time on each processor is comprised of computing time and the overhead time required for locked critical sections and/or to send data from one processor to another.

Assume a program requires t = 100 s of execution time on one processor. When run p processors, each processor requires t/p s, as well as an additional 4 s of overhead, irrespective of the number of processors. Compute the per-processor execution time for 2, 4, 8, 16, 32, 64, and 128 processors. For each case, list the corresponding speedup relative to a single processor and the ratio between actual speedup versus ideal speedup (speedup if there was no overhead).

Implement the four functions described in Exercise B.11 using a PLA

Implement the four-input odd-parity function with AND and OR gates using bubbled inputs and outputs.

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