[SOLVED] CS2110 - Timed Lab 3 - Subroutines and Calling Conventions

30.00 $

Category:

Description

Rate this product

The purpose of this timed lab is to test your understanding of implementing subroutines in the LC-3 assembly language using the calling convention, from both the callee and caller side.

2.2         Task

You will implement the subroutines listed below in LC-3 assembly language. Please see the detailed instructions for the subroutines on the following pages. We have provided pseudocode for the subroutines—you should follow these algorithms when writing your assembly code. Your subroutines must adhere to the LC-3 calling conventions.

3           Detailed Instructions

For this Timed Lab, you will be implementing three subroutines: ABS, POW3 and MAP. ABS is a non-recursive subroutine that will calculate the absolute value of its argument. POW3 is a recursive subroutine that will compute 3 to the power of its argument. MAP will iterate through an array and apply one of the two subroutines above to each of the elements.

3.1         ABS subroutines

ABS will take one argument that should be loaded from the stack. You should implement the subroutine as shown in the pseudocode below:

// checkpoint 1 int ABS(int x) { if (x < 0) {

return -x;

} else { return x;

}

}

Examples:

  • ABS(5) returns 5
  • ABS(0) returns 0
  • ABS(-3) returns 3

Implementing ABS will be the first checkpoint. Please refer to Checkpoints for details on how ABS will be graded.

3.2         POW3 subroutine

POW3 will take one non-negative integer argument n and compute 3n; that is, 3 raised to the power of the argument. You should implement the subroutine as shown in the pseudocode below:

int POW3(int n) { if (n == 0) { return 1; // (checkpoint 2) base case: 3ˆ0 = 1

} else { return 3 * POW3(n – 1); // (checkpoint 3) recursive case }

}

Examples:

  • POW3(0) returns 1
  • POW3(1) returns 3
  • POW3(3) returns 27

POW3 contains two checkpoints. Checkpoint 2 is the base case: returning 1 when n == 0. Checkpoint 3 is the recursive case: POW3 should return the correct value for all non-negative integers. Please refer to Checkpoints for details on how POW3 will be graded.

3.3         MAP subroutine

MAP takes two arguments:

  • array: The address of the first element in the array to be modified
  • length: The length of the array to be modified

MAP should iterate through the array. For every element at an even index, it should apply ABS to it and replace the element with the result. For every element at an odd index, it should apply POW3 to it and replace the element with the result.

Hint: remember Homework 1. What’s a simple way using bitwise operations to check that a number is even or odd without doing a full mod operation?

You should implement the subroutine as shown in the pseudocode below:

// checkpoint 4 void MAP(array, length) { i = 0; while (i < length) { element = arr[i]; if (i % 2 == 0) {

result = ABS(element);

} else {

result = POW3(element);

} arr[i] = result;

i = i + 1;

}

}

You do not need to return a value from MAP. While you will still leave a spot for a return value on the stack, it will be ignored.

Correctly implementing MAP is checkpoint 4. Please refer to Checkpoints for details on how MAP will be graded.

4           Checkpoints

4.1         Checkpoints (70 points)

In order to get all of the points for this timed lab, your code must meet these checkpoints:

  • Checkpoint 1 (15 points): In ABS, load the parameters x and compute the absolute value to return |x|.
  • Checkpoint 2 (15 points): Implement the base case for POW3 to return 1 when n == 0.
  • Checkpoint 3 (20 points): Implement the recursive case of POW3 to successfully compute and return 3n for any non-negative integer n.
  • Checkpoint 4 (20 points): Implement MAP. You should iterate through the array, and apply the ABS subroutine to elements at all even indices and the POW3 subroutine to elements at all odd indices. The results of each subroutine call must be properly stored back into the array at every index.

4.2         Other Requirements (30 points)

Your subroutine must follow the LC-3 calling convention. Specifically, it must fulfill the following conditions:

  • Your subroutine must be recursive and call itself according to the pseudocode’s description.
  • When your subroutine returns, every register must have its original value preserved (except R6).
  • When your subroutine returns, the stack pointer (R6) must be decreased by 1 from its original value so that it now points to the return value.
  • During the execution of your subroutine, you must make the correct number of calls to ABS and POW3, corresponding to the pseudocode.

If the autograder claims that you are making an unknown subroutine call to some label in your code, it may be that your code has two labels without an instruction between them. Removing one of the labels should appease the autograder.

7           LC-3 Assembly Programming Requirements

7.1         Overview

  1. Your code must assemble with NO WARNINGS OR ERRORS. To assemble your program, open the file with Complx. It will complain if there are any issues. If your code does not assemble, you WILL get a zero for that file.
  2. Comment your code! This is especially important in assembly, because it’s much harder to interpret what is happening later, and you’ll be glad you left yourself notes on what certain instructions are contributing to the code. Comment things like what registers are being used for and what less intuitive lines of code are actually doing. To comment code in LC-3 assembly just type a semicolon (;), and the rest of that line will be a comment.
  3. Avoid stating the obvious in your comments, it doesn’t help in understanding what the code is doing.

Good Comment

ADD R3, R3, -1 ; counter–
BRp LOOP

Bad Comment

; if counter == 0 don’t loop again
ADD R3, R3, -1 ; Decrement R3
BRp LOOP ; Branch to LOOP if positive

 

8           Appendix

8.1         Appendix A: LC-3 Instruction Set Architecture