Lab 5: MIPS Calling Convention


Due Tuesday, February 16 at 11:59:59 PM

Goals for This Week

By the time you have completed this work, you should be able to:

Provided files: Documentation:

Step by Step Instructions

You will perform two tasks for this week, some with multiple steps. You may complete them in any order, though it is recommended to start with Task 1, as Task 2 assumes you already understand Task 1. The tasks are listed below:

You may use any MIPS instructions or psuedoinstructions you want in order to implement these tasks. However, you will be somewhat restricted on exams. You may want to read the grading policies regarding MIPS assembly instructions for more information.

Both of these tasks require you to follow the MIPS calling convention. Be sure to actually follow the calling convention; solutions which do not follow the MIPS calling convention will receive no credit!

The initial step below describes how to get the files you will need into the appropriate place.

Initial Step: Create a Directory for This Lab and Copy Over the Files

After you log in, go into your cs64 directory that you created last time:

cd cs64

Then create a new directory for this lab: lab5:

mkdir lab4

Then go into that directory.

Now copy over all of the files necessary for this week's tasks:

cp ~kyledewey/public_html/cs64/labs/5/functions.asm ~kyledewey/public_html/cs64/labs/5/functions_testing_stub.asm ~kyledewey/public_html/cs64/labs/5/functions_tester.pl ~kyledewey/public_html/cs64/labs/5/iterative_max.asm ~kyledewey/public_html/cs64/labs/5/iterative_max_testing_stub.asm ~kyledewey/public_html/cs64/labs/5/iterative_max_tester.pl ~kyledewey/public_html/cs64/labs/5/partner.txt .

Note the use of the trailing . in the above command, which stipulates that the specified files should be copied into the current directory.

Task 1: Implement the PrintReverse Function

For this task, you will implement the PrintReverse function in functions.asm using the MIPS calling convention. PrintReverse takes two arguments:

  1. The address of an array of word-long (four byte) signed integers, held in $a0.
  2. The length of the array as an unsigned integer, held in $a1.

The function should print the provided array in reverse order. For example, if given:

1 2 3

...the function should print:

3 2 1

In addition to printing out the elements in reverse order, you must call another provided function named ConventionCheck each time you print out an element. Do not implement ConventionCheck youself, or modify it in any way. On our end, we are going to use our own version of ConventionCheck while grading, so the only assumptions you can make about ConventionCheck are that it will follow the MIPS calling convention.

While we provide an array in the file, your code should work on any arbitrary array. On our end, for testing purposes we will use different arrays, so your code should work with any other valid (non-empty) array.

The main function provided in functions.asm calls PrintReverse on your behalf, and it will display the contents of the array in non-reversed order before and after calling PrintReverse.

Your output should have the format shown below. Note that the specific numbers used in the output differ from the numbers in your file; the important part is not what the absolute numbers are, but that the resulting output displays the numbers properly with respect to the particular input. The first two lines and the last line are generated by the main function, so you do not have to implement anything to print those portions out. The lines containing the words “Convention Check” are produced by the ConventionCheck function you must call after printing each input. To help clarify which output you need to produce and which output is produced for you, output you must produce is marked in bold.

Current Array:
0 1 -1 5 32 6 -66 33 12 58 -4 64 
64
Convention Check
-4
Convention Check
58
Convention Check
12
Convention Check
33
Convention Check
-66
Convention Check
6
Convention Check
32
Convention Check
5
Convention Check
-1
Convention Check
1
Convention Check
0
Convention Check
0 1 -1 5 32 6 -66 33 12 58 -4 64

Remember to follow the MIPS calling convention while implementing your PrintReverse function. Otherwise, the program may crash hard, and will receive no credit.

Hint: Use “jal label” to call other functions, replacing label with the name of the function you want to call. For example, if you want to call the ConventionCheck function, then you ultimately need to use the following code snippet:

jal ConventionCheck

In order to test your code, you may use the provided functions_tester.pl script on csil, like so:

./functions_tester.pl

Task 2: Implement the IterativeMax Function

In this task, you will implement the IterativeMax function in iterative_max.asm using the MIPS calling convention. IterativeMax take two arguments:

  1. The address of an array of word-long (four byte) signed integers, held in $a0.
  2. The length of the array as an unsigned integer, held in $a1.

The main purpose of the function is to determine which element of the provided array is the largest (i.e., the maximum element in the array). This is achieved by going through the array one element at a time. On each element, the function should do the following, in order:

  1. Print out the current element of the array
  2. Determine what the new maximal element of the array is so far. For example, if the old maximum was 5 and 7 was just observed in the array, then the maximum should be set to 7.
  3. Print out the new maximum element of the array so far (after setting the new maximum in the previous step)
  4. Call the ConventionCheck function, to help make sure the proper MIPS calling convention is being followed

Do not implement ConventionCheck youself, or modify it in any way. On our end, we are going to use our own version of ConventionCheck while grading, so the only assumptions you can make about ConventionCheck are that it will follow the MIPS calling convention.

While we provide an array in the file, your code should work on any arbitrary array. On our end, for testing purposes we will use different arrays, so your code should work with any other valid (non-empty) array.

The main function provided in iterative_max.asm calls IterativeMax on your behalf, and it will display the contents of the array in non-reversed order before and after calling IterativeMax.

Your output should have the format shown below. Note that the specific numbers used in the output differ from the numbers in your file; the important part is not what the absolute numbers are, but that the resulting output displays the numbers properly with respect to the particular input. The first two lines and the last line are generated by the main function, so you do not have to implement anything to print those portions out. The lines containing the words “Convention Check” are produced by the ConventionCheck function you must call after printing each input. To help clarify which output you need to produce and which output is produced for you, output you must produce is marked in bold.

Current Array:
0 1 -1 5 32 6 -66 33 12 58 -4 64 
0
0
Convention Check
1
1
Convention Check
-1
1
Convention Check
5
5
Convention Check
32
32
Convention Check
6
32
Convention Check
-66
32
Convention Check
33
33
Convention Check
12
33
Convention Check
58
58
Convention Check
-4
58
Convention Check
64
64
Convention Check
0 1 -1 5 32 6 -66 33 12 58 -4 64

Remember to follow the MIPS calling convention while implementing your IterativeMax function. Otherwise, the program may crash hard, and will receive no credit.

You may assume that the provided array will always be non-empty. That is, it is guaranteed that the provided array will always contain at least one element.

Some hints follow, which you may or may not find useful:

In order to test your code, you may use the provided iterative_max_tester.pl script on csil, like so:

./iterative_max_tester.pl

Turn in Everything Using turnin

If you partnered with someone, record the email address they are using for the class in partner.txt. For example, if your partner had the email address foo@bar.com, then the contents of partner.txt should be the following (and only the following):

Partner: foo@bar.com

If you did not partner with anyone, you do not need to (and should not) edit partner.txt.

Assuming you are in the cs64/lab5 directory you created at the beginning, you can send in your answers via the following command:

turnin lab5@cs64 functions.asm iterative_max.asm partner.txt

You may turn in the same assignment up to 100 times, which is useful if you are working on it incrementally. Note that only the last version you submitted will be graded.

Even if you did not partner with anyone, you should still turn in partner.txt, which should not have been modified.


Prepared for Computer Science 64 by Diana Franklin, with slight adaptation by Kyle Dewey.