Skip to main content
Department of Information Technology

Lab MIPS: Stacks, subroutines and register-conventions

Goal : The lab goal is to get familiar with:

  • how the stack is managed in MIPS-processorn architecture
  • how the subroutines are writen in MIPS-assembler
  • what register-conventions must be followed.

Background

In high level languages, it is common that routines and functions arguments are passed through the underlying machine stack. One of the lab points is to understand the mechanisms of passing the arguments. In the first part, we will use the call by value ,i.e., the arguments are passed via the stack, it will be interpreted as the subroutine making a local copy of the data. In the second part, we will use call by references which means that the subroutine will not use a local data but pointers to the original data. Two examples in C/C++:

int sum_1(int length, int v1, int v2, int v3, int v4, ...);

int sum_2(int length, int vector[]);

OBS : Each subroutine will be regarded as independant from the rest of the code. You have to follow the conventions for passing the arguments and the use of the register in MIPS-assembler. (see ex.
Patterson and Hennessy, Appendix A.6)

Assignment

You have to write two subroutines(sum_1 and sum_2) which calculate the sum of the elements of a vector of integers. The subroutines differ in the way you call them (how you pass arguments) from the main. Argumments are the size of the vector (length) and the values of the integers. Both subroutines will return the sum. You have also to modify the main part of the program before calling the subroutines. Start from the following file lab.s. All your changes must be done on a copy of that file and all code lines added must be commented. Besides your answers to the questions must be included as comments in the end.

The assignment involves:

  1. Write clearly sum_1. Write a subroutine which gets all its data from the stack, the size of the vector will also be passed to the subroutine sum_1 through the stack. You have to consider that the number of elements in the vector may change. So the length argumentshould be in top of the stack. Do not forget to restore the stack after the execution of sum_1.
  2. Draw a representation of the stack for the vector 1,2,3,4,5 (including $ra and length=5) just before the call of the procedure sum_1 (see ex. Patterson & Hennessy, Appendix A.5 -- Memory Usage).
  3. Write clearly sum_2. Sending all the values to be added is both long and non effective. Instead send two arguments: the length and the address to the first element (this corresponds to the address "vector" in the example). Note that in this case non local data is pointed to.
  4. Motivate, shortly (with maximum three arguments) why must registers $s0-$s7 and $ra be saved on the stack when they are changed by the subroutine (see ex. Patterson & Hennessy, Appendix A.6, expecially Figure A.10 and the text on page A-25).
  5. To which address does $ra point to when the main function begins its execution? In other words, what does $ra contain in the begining of the program? Tips: use step-function in the SPIM simulator.

Updated  2006-02-13 17:37:52 by Ahmed Rezine.