created 07/03/2003

Chapter 23 Programming Exercises


In the Settings menu of SPIM set Bare Machine OFF, Allow Pseudo Instructions ON, Load Trap File ON, Delayed Branches ON, Delayed Loads ON, Mapped IO ON, Quiet OFF.

Run the programs by loading the trap handler (as explained in the chapter) and clicking SimulatorGo and then OK in the pop-up panel.


**Exercise 1 -- Prime Number Tester

Write a program that asks the user for a positive integer. The program then tests if the integer is prime and writes out its result.

A number is prime if it cannot be divided evenly by any number other than 1 and itself. So you will need a counting loop that starts at two and divides the number for each value of the loop counter until the number is divided evenly or the limit is reached.

For the upper limit of the loop use the number divided by two. (A better limit would be the square root of the number, but this is not easily calculated.)

Click here to go back to the main menu.


End of Exercises