created 06/27/2003; revised 07/07/2015


Chapter 17 Programming Exercises


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

add divu mflo sll
addi j mult sra
addiu lb multu srl
addu lbu nor sub
and lh or subu
andi lhu ori sw
beq lui sb xor
bne lw sh xori
div mfhi   

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.


*Exercise 1 — Non-ending Loop

Write a program that computes the sum:

1 + 2 + 3 + 4 + 5 + ...

Do this by using the j instruction to implement a non-ending loop. Before the loop, initialize a register to zero to contain the sum, and initialize another register to one to be the counter. Inside the loop add the counter to the sum, increment the counter, and jump to the top of the loop.

Execute the program by single-stepping (by pushing F10). After you have done this enough to confirm that the program works, look at SPIM's menu and select Simulator and Multiple Step. Enter a number of steps (such as 40) into the window and click "OK". Each step is the execution of one machine cycle. Once you see how this works you can do the same thing more easily by pushing F11.

Click here to go back to the main menu.


*Exercise 2 — Non-ending Loop with Overflow

Write a program that adds $8 to itself inside a non-ending loop. Initialize $8 before the loop is entered. Use the add instruction so that when overflow is detected the program ends with a trap.

Now change the add instruction to addu. Now when overflow occurs, nothing happens. Run the program and observe the difference.

Click here to go back to the main menu.


**Exercise 3 — Counting Loop

Write a program that computes the sum:

1 + 2 + 3 + 4 + 5 + ... + 98 + 99 + 100

Do this, as above, by using the j instruction to implement a non-ending loop. Before the loop, initialize a register to zero to contain the sum, initialize another register to one to be the counter, and another register to 10110. Inside the loop add the counter to the sum, increment the counter, and jump to the top of the loop.

However, now, at the top of the loop put in a beq instruction that branches out of the loop when the counter equals 10110. The target of the branch should be something like this:

exit:    j    exit      #  sponge for excess machine cycles
         sll   $0,$0,0

Now run the program by setting the value of the PC to 0x400000 (as usual) and entering 500 or so for the number of Multiple Steps (F11). Your program will not need so many steps, but that is OK. The excess steps will be used up repeatedly executing the statment labeled "exit".

Click here to go back to the main menu.


**Exercise 4 — Fibonacci Series

Write a program that computes terms of the Fibonacci series, defined as:

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...

Each term in the series is the sum of the preceeding two terms. So, for example, the term 13 is the sum of the terms 5 and 8.

Write the program as a counting loop that terminates when the first 100 terms of the series have been computed. Use a register for the current term and a register for the previous term. Each execution of the loop computes a new current term and then copies the old current term to the previous term register.

Notice how few machine language instruction this program takes. It would be hard for a compiler to produce such compact code from a program in a high level language. Of course, our program is not doing any IO.

Click here to go back to the main menu.


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

End of Exercises