created 07/04/2003; edited 05/31/2015


Chapter 25 Programming Exercises


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 and clicking Simulator Run.


*Exercise 1 — Arithmetic Evaluation (stack-based)

Write a program to evaluate 3ab - 2bc - 5a + 20ac - 16

Prompt the user for the values a, b, and c. Try to use a small number of registers. Use the stack to hold intermediate values. Write the final value to the monitor.

Click here to go back to the main menu.


**Exercise 2 — String Reversal (stack-based)

Write a program that asks the user for a string. Read the string into a buffer, then reverse the string using the stack. However, unlike the example in the chapter, don't push a null character on the stack. Keep track of the number of characters on the stack by incrementing a count each time a character is pushed, and decrementing it each time a character is popped. Write out the reversed string.

Click here to go back to the main menu.


***Exercise 3 — Vowel Removal (stack-based)

Write a program that asks the user for a string. Read the string into a buffer. Now scan the string from right to left starting with the right-most character (this is the one just before the null termination.) Push each non-vowel character onto the stack. Skip over vowels.

Now pop the stack character by character back into the buffer. Put characters into the buffer from left to right. End the string with a null byte. The buffer will now contain the string, in the correct order, without vowels.

Write out the final string.

Click here to go back to the main menu.

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


End of Exercises