created 07/03/2003

# Chapter 22 Programming Exercises

For these programming exercises, use only those instructions that have been discussed so far in these notes:

Basic Instructions
and lbu or sub
andi lh ori subu
beq lhu sb sw
bgez lui sh xor
bltz lw sll xori
bne mfhi

PseudoInstructions
la lw nop syscall
li move sw

In the Settings menu of SPIM set Bare Machine OFF, Accept Pseudo Instructions ON, Enable Branch Delays ON, Enable Load Delays ON, Enable Mapped IO OFF, Load Exception Handler ON.

Run the programs by loading the exception handler (as explained in the chapter) and clicking Simulator Run. End your program using service 10 of the exception handler.

Use the `lw` and `sw` with symbolic addresses for loading and storing registers. Use mnemonic register names and use registers in their conventional roles.

Check that the option Enable Mapped IO is turned OFF. In the previous chapters, this was not important, but with the exception handler for QtSpim this option must be OFF.

## *Exercise 1 — Miles per Gallon

Write a program that repeatedly prompts the user for the number of miles traveled and the gallons of gasoline consumed, and then prints out the miles per gallon. Use integer math. Exit when the user enters 0 for the number of miles.

## **Exercise 2 — Miles per Gallon, Fixed-Point Math

As in Exercise1, write a program that repeatedly prompts the user for the number of miles traveled and the gallons of gasoline consumed, and then prints out the miles per gallon. Exit when the user enters 0 for the number of miles.

Use fixed-point arithmetic for this. Multiply input value of miles by 100. Divide miles by gallons to get miles per gallon. The answer will 100 times too large. So divide it by 100 to get whole miles per gallon in `hi` and hundredths in `lo`. Print out lo followed by "." followed by hi to get an output that looks like: "32.45 mpg".

## ***Exercise 3 — String to Integer

Write a program that asks the user for a string of digits that represent a positive integer. Read in the string using service number 8, the "read string" service. Now convert the string of digits into an integer by the following algorithm:

```value = 0;

for each digit starting with the left-most:
{
convert the digit into an integer D by subtracting 0x30
value = value*10 + D
}
```

You might recognize this as Horner's method. After converting the integer, check if it is correct by writing it out on the simulated monitor using service 1.

Notes: assume that the input is correct (that it contains only digits and can be converted to a 32-bit integer). Be sure to deal properly with the end-of-line character that is at the end of the user's input.

## ***Exercise 4 — Fahrenheit/Celsius Converter

Write a program that repeatedly asks the user for a scale F or a C (for "Fahrenheit" or "Celsius") on one line followed by an integer temperature on the next line. It then converts the given temperature to the other scale. Use the formulas:

```F = (9/5)C + 32

C = (5/9)(F - 32)
```

Exit the loop when the user types "Q". Assume that all input is correct. For example:

```Enter Scale      : F
Enter Temperature: 32
Celsius Temperature: 0C

Enter Scale      : C
Enter Temperature: 100
Fahrenheit Temperature: 212F

Enter Scale      : Q
done
```

```   * == easy program
** == moderately easy program
*** == harder program
**** == project
```

End of Exercises