created 07/04/2003

Chapter 25 Programming Exercises


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 clicking SimulatorGo and then OK in the pop-up panel.


Exercise 1 — Arithmetic Evauation (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 strating 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 right to left. 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.


End of Exercises