lab05 : MIPS Calling Convention

num ready? description assigned due
lab05 true MIPS Calling Convention Thu 02/14 08:00AM Wed 02/20 11:59PM

Lab 5: MIPS Calling Convention


Due *WEDNESDAY*, FEB. 20th at 11:59:59 PM

Goals for This Week

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

  • Call a predefined function properly, using the MIPS calling convention
  • Define your own functions, in accordance with the MIPS calling convention

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:

  • Task 1: Swapping Array Contents (does not use MIPS Calling Convention)
  • Task 2: Implement the PrintReverse Function (uses MIPS Calling Convention)
  • Task 3: Implement the IterativeMax Function (uses MIPS Calling Convention)

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 lab5

Then go into that directory.

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

cp ~zmatni/public_html/cs64w19/labs/5/swap_array.asm . ~zmatni/public_html/cs64w19/labs/5/functions.asm ~zmatni/public_html/cs64w19/labs/5/functions_testing_stub.asm ~zmatni/public_html/cs64w19/labs/5/functions_tester.pl ~zmatni/public_html/cs64w19/labs/5/iterative_max.asm ~zmatni/public_html/cs64w19/labs/5/iterative_max_testing_stub.asm ~zmatni/public_html/cs64w19/labs/5/iterative_max_tester.pl ~zmatni/public_html/cs64w19/labs/5/partner.txt .

chmod +x ./*

Note the use of the trailing . in the above command, which stipulates that the specified files should be copied into the current directory. Also note that you may have to change file permissions once you copy them over using the chmod command.

Task 1: Swapping Array Contents

In this task, you will swap values around from within an array. In order to allow us to produce different test cases, the main code is separate from your code. We will supply a different main file depending on what we want the initial values to be.

Step 1: Familiarize Yourself With the Code

Open the file swap_array.asm. In the .data section, a number of prompts have been defined, along with an array named myArray which you will modify the contents of. In the .text section, some code for running tests is in place, along with a function stub named doSwap, which you will define. As with the previous task, we have not discussed functions yet, and so this might be a little confusing. The restrictions are that you may only use registers $v0 - $v1, $t0 - $t7, and $a0 - $a3. Additionally, the last line of each function may not be removed (which is always jr $ra).

Step 2: Implement doSwap

Now look at the stub for doSwap, which contains a snippet of C code in the comments which you must implement. This snippet is duplicated below for convenience:

unsigned int x = 0;
unsigned int y = 8;

while (x != 4) {
  int temp = myArray[x];
  myArray[x] = myArray[y];
  myArray[y] = temp;
  x++;
  y--;
}

Your assembly code must do the same thing as the above C/C++ code. Make sure your code does not depend on initial values. While you may assume that myArray will always contain exactly 9 elements, you can make no assumptions regarding what values these are. Remember that the program we run it on will have different global declarations.

In addition, the point made regarding the practical distinction between add/addi and addu/addiu in the previous task is still relevant for this task. Correct implementations must utilize both forms, and in the proper manner (that is, addu/addiu for memory addresses and other unsigned things, and add/addi for signed values).

For testing, the code in main will ultimately use your doSwap implementation, and it features a built-in correctness check. If you would like to test on other array contents, feel free to change the values of myArray (keeping sure to keep it exactly 9 elements long). If you do change the values in myArray, be sure to also change the corresponding values in expectedMyArray, which holds what the result should be. When it comes to your implementation, do not simply programmatically copy the values in expectedMyArray into myArray; while this will appear to work on your end, on our end the values in expectedMyArray will not sync up with myArray as it does in your code, so this would end up always giving the wrong result on our end.

Task 2: 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 3: 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:

  • One way of initializing things is to set the initial maximum to the first element of the array, and then iterate over the array starting from the second element. However, this would require some extra steps in order to get the same output as above. As such, a simpler strategy may be to set the initial value to the most negative possible signed integer, and start iterating from the first element. As the array is guaranteed to contain at least one element, this is safe; the only way this most negative possible signed integer could be returned is if the array contains only this element.
  • 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 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 swap_array.asm 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.


Copyright 2018, Ziad Matni, CS Dept, UC Santa Barbara. Adapted from work by Diana Franklin and Kyle Dewey. Permission to copy for non-commercial, non-profit, educational purposes granted, provided appropriate credit is given; all other rights reserved.