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
> add | div | mflo | slt, slti
> addi | divu | mult | sltu, sltiu
> addiu | j | multu | sra
> addu | lb | nor | srl
> 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, 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.

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

## *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.

Click here to go back to the main menu.

## **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".

Click here to go back to the main menu.

## ***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.

Click here to go back to the main menu.

## ***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

Click here to go back to the main menu.

End of Exercises