created 07/07/2003

Chapter 28 Programming Exercises

NOTE: Use the Frame-based Linkage Convention for these programs.

In the Settings menu of SPIM set Bare Machine OFF, Allow Pseudo Instructions ON, Load Trap File ON, Delayed Branches OFF, Delayed Loads OFF, Mapped IO ON, Quiet OFF.

**Exercise 1 — Play Compiler

Translate the following little program into MIPS assembly language. Do a line-by-line translation as a simple compiler might do (as done in the chapter). Use stack-based variables for the program's local variables. Assemble and run your program under SPIM

  int f;
  int c;
  f = 50;
  c = toCent( f );
  print( "f=", f, "c=", c );

int toCent( int x )
  int v;
  v =  x - 32;
  v =  5*v
  v =  v/9;
  return v;

You could, of course, write the program without using variables since there are plenty of "T" registers available. But practice using local variables as in the chapter. For each assignment statement, for each variable on the right of the equal sign retrieve a value from a variable on the stack. For the variable on the left of an assignment statement, store a value into the variable, even if the very next statement requires that the value be retrieved.

Click here to go back to the main menu.

***Exercise 2 — Triangle Numbers

Write a program that computes the value of triangle numbers using a recursive subroutine. The definition of triangle numbers is:

Triangle( 1 ) = 1

Triangle( N ) = N + Triangle( N-1 )

For examples:

Triangle( 1 ) = 1

Triangle( 2 ) = 3

Triangle( 3 ) = 6

Triangle( 4 ) = 10

Click here to go back to the main menu.

End of Exercises