CS 208: Computer Organization & Architecture

Function-calling in MIPS

You may work on this assignment alone or with a partner, as you prefer.

High-level languages like Java, C, C++, Python, Lisp, Pascal, Haskell, Go, Swift, Algol 58,... all give the program the ability to write functions of one sort or another. As a programmer using C, for example, you specify your function's prototype or signature, giving the function a name, a list of parameters and their types, and the function's return type. For example:

int greatestCommonDivisor(int a, int b)

For many functions, the prototype is enough to tell other programmers how to use the function. For others, a comment describing the operation of the function is needed to turn a prototype into a usable functional interface. But in any case, once you have specified the prototype of a function and provided an implementation of the function, other programmers can use that function in their own programs by using the language's function-calling syntax:

int result = greatestCommonDivisor(51, 119);

Regardless of the particulars of the syntax of functions in your high-level language of choice, at some stage of the compilation and execution of your program, that function call needs to be translated into machine language for the particular processor your computer is using. For "compile to machine language" languages like C, C++, Objective C, Pascal, etc., the compiled version of your program is assembly or machine language, and is different for every type of CPU for which you compile your program. For "compile to intermediate language" languages like Java, the compiled form of your program (e.g. the Java .class files) is the same regardless of the target processor, but then there needs to be a separate interpreter program on the target device (e.g. the "java" program when I execute "java MyMainClass").

Either way, eventually function-calling has to be implemented at the machine language level. For this assignment, you will implement a function-calling mechanism in MIPS, plus a recursive function to test it.

A simplified function-calling system in MIPS

As we discussed in class Friday, MIPS has three registers supporting function calling: $ra (return address), $sp (stack pointer), and $fp (frame pointer). We took a close look at how the gcc C compiler translates C function calling into MIPS stack frame manipulation.

For this assignment, you will implement a simplified version of function calling in MIPS. In this system, every function call will have a rigid 4-word stack frame structure:

0($sp) -- parameter 4($sp) -- return address 8($sp) -- local variable 12($sp) -- return value

This implies a very limited function structure, of course, with just one 32-bit parameter and a 32-bit return value, and space for one 32-bit local variable which the function may use or not use depending on its needs.

Your code will consist of four pieces:

To call a function under this system:

  1. Put the function's parameter in $a0
  2. Put the function's address in $a1
  3. jal call

To return from a function:

  1. Make sure you have stored the return value in its proper place in the stack frame.
  2. jal return

Notes

Have fun!

This one is really cool when you get it working. Questions are welcome, as always.