created 07/04/2003

# Chapter 26 Programming Exercises

NOTE: The example programs in the chapter included load and branch delays to show how actual MIPS hardware works. However, for these exercises you can turn them off (or leave them on if you prefer.)

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 clicking SimulatorGo and then OK in the pop-up panel.

## Exercise 1 -- Arithmetic Expression

Write a subroutine that takes three arguments, A, X, and Y. It then computes A*X*Y and returns it.

Use the subroutine to evaluate the following for various values of u and v:

    5u2 - 12uv + 6v2

The main method, in a loop, prompts the user for values of u and v and prints out the result. End the loop when the user enters zero for both u and v.

Click here to go back to the main menu.

## Exercise 2 -- User Prompt

Write a subroutine that prompts the user for an integer. The subroutine reads the string the user enters as a string (using trap hadler service 8). Then, if the string forms a proper integer, it is converted to two's complement form and returned in register $v0.

If the input was converted, set register $v1 to one. If the input was not converted, set register $v1 to zero, and set $v0 to zero. If the user enters "done", set register $v1 to two, and set $v0 to zero.

Use the subroutine in a program that computes the sum of a list of integers that the user enters. The user signals the end of input by typing "done".

Click here to go back to the main menu.

## Exercise 3 -- Arithmetic Evaluation with User Prompt

Combine the previous two programs:

Write a program that evaluate the following for various values of u and v:

    5u2 - 12uv + 6v2

The values for u and v are prompted for in a loop. The user enters the values and the value of the expression is printed out. If illegal characters are entered for either u or v, print out an error message and continue.

End the loop when the user enters "done" for the value of u.

Click here to go back to the main menu.

End of Exercises