Lab MIPS: Stacks, subroutines and register-conventions
Goal : The lab goal is to get familiar with:
- how the stack is managed in MIPS-processor architecture
- how the subroutines are written in MIPS-assembler
- what register-conventions must be followed.
- You are supposed to go through the tutorial pages before you start the labs.
- Deadlines: first deadline 2010-05-16 23.59, completion 2010-05-30 23.59.
- It is important that you follow the submission rules described in this page.
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. This 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);
Start by downloading the following file lab_1.s.
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. Arguments 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. 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.
OBS : Each subroutine will be regarded as independent 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)
- 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 argument should be in top of the stack. Do not forget to restore the stack after the execution of sum_1.
- 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).
- 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.
- 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, especially Figure A.10 and the text on page A-25).
- To which address does $ra point to when the main function begins its execution? In other words, what does $ra contain in the beginning of the program? Tips: use step-function in the SPIM simulator.