created 05/17/2015

# Chapter 9 Programming Exercises

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

In the Simulator/Settings/MIPS menu of SPIM set Bare Machine ON, Accept Pseudo Instructions OFF, Enable Delayed Branches ON, Enable Delayed Loads ON, Enable Mapped I/O OFF, Load Exception Handler OFF.

Run the programs by single stepping (pushing F10). Observing the results in the SPIM Int Regs window. Check the Registers menu that the registers are displayed in HEX.

## *Exercise 1

Create a source file for the program discussed in this chapter:

```## Program to add two plus three
.text
.globl  main

main:
ori     \$8,\$0,0x2       # put two's comp. two into register 8
ori     \$9,\$0,0x3       # put two's comp. three into register 9
addu    \$10,\$8,\$9       # add register 8 and 9, put result in 10
```

Load it into QtSPIM as explained in the chapter. Run the program by pushing F10 to single step. Check the register display after each step.

## *Exercise 2

Play with the program:

1. Reverse the first two statements. Does this have any effect on the result in register `\$10`?
2. Reverse the last two statements. Does this have any effect on the result?
3. Start with the original program. Reverse the order of the last two registers in the last statement. Does this have any effect on the result?

## *Exercise 3

Start with the original program, but change the program so that it adds two other values, 0xF and 0x7. Load the program into QtSPIM, run it, and observe the result in register `\$10`.

```## Program to add fifteen plus seven
.text
.globl  main

main:
ori     \$8,\$0,0xF       # put two's comp. fifteen into register 8
ori     \$9,\$0,0x7       # put two's comp. seven into register 9
addu    \$10,\$8,\$9       # add register 8 and 9, put result in 10
```

Change the program again so that the assembly statements uses decimal to express the same two values:

```## Program to add fifteen plus seven
.text
.globl  main

main:
ori     \$8,\$0,15        # put two's comp. fifteen into register 8
ori     \$9,\$0,7         # put two's comp. seven into register 9
addu    \$10,\$8,\$9       # add register 8 and 9, put result in 10
```

Run the program. Notice that the results are the same as when hex was used to describe the bit patterns (values) that were added.

Notice: assembly instructions can use decimal or hex to specify bit patterns. In the above `15` and `0xF` result in the same bit pattern.

## **Exercise 4

Create a program that computes 5+5+5. Do this by modifying (yet again) the chapter's program. You will need to use two `addu` instructions.

```   * == easy program