COMPSCI 210 Assignment 1 
2024 Semester 2 
1. Overview
Task Description: You are to implement a basic calculator using the LC3 simulator. The finished product can take inputs from the user and perform calculations according to the operands and arithmetic operation specified by the user. The calculator then displays the calculation results in the console in a wellformatted manner. The arithmetic operations supported by the calculator are: addition, subtraction, multiplication, division, and modulo.
2. GettingStarted(RunningtheLC3Simulator)
This assignment uses the LC3 simulator shown in class, so please make sure you are familiar with the tool before going ahead with the assignment. You can familiarise yourself with the tool by following the instructions provided on the Canvas page L10: LC3 Simulator Resources, and running various example scripts provided on Canvas pages of lectures.
3. LC3Calculator(ExpectedBehaviouroftheFinishedProduct)
The LC3 calculator prompts the user for the first operand, the operation index, and the second operand in that order. Once all three inputs have been entered, the display then prints the appropriate equation with the calculated result (as shown below). Aer printing the result, the calculator then prompts the user again for the next calculation. This process continues until the user presses Enter, which stops the program.
Note 1: The calculator only works with singledigit numbers, so the two operands have to be
within the range of 0~9 (both ends inclusive).
Note 2: Every operation index refers to an arithmetic operation: 1 for addition (+); 2 for
subtraction (); 3 for multiplication (*); 4 for floor division (/); 5 for modulo/remainder (%).
Note 3: The result is also only singledigit. If the result is NOT in the range of 0~9 (both
ends inclusive), then the display prints “OUT OF BOUNDS” as the result.
The following series of pictures show the expected behaviour of the calculator with the key presses as annotated (aer assembling the OS and calculator program files, and loading them into the LC3 simulator).
1
COMPSCI 210 Assignment 1 
2024 Semester 2 
1. Click “Continue” 
2. Click the display 
3. Press “2” on keyboard 
4. Press “3” on keyboard 
5. Press “4” on keyboard 
6. Press “7” on keyboard 
7. Press “1” on keyboard 
8. Press “1” on keyboard 
9. Press “Enter” (Program stops) 
4. Tasks
To get you started, there’s a code skeleton provided to you on the A1 Canvas page. The unmodified code runs without errors, but the results are incorrect. There are three faults due to the unimplemented parts of the code, and your job is to fill in these unimplemented parts. All the unimplemented parts are tagged with “[TODO]”, you can Ctrl+F “[TODO]” or scroll to Line 112 of the code skeleton to find them.
The first fault is that the program uses the addition operation regardless of the operation index entered by the user. You need to write code for this part such that the program branches to the appropriate arithmetic operation according to the operation index entered by the user. The operation index is stored in R2.
The second fault is due to the lazy programmer who called it quits aer writing the addition function (F_PLUS), and the other arithmetic operations are unimplemented. You need to write code to carry out the arithmetic operations. The two operands are stored in R1 and R3 respectively, and your code should carry out the appropriate arithmetic operation using the operands from R1 and R3 and store the result in R4.
The third fault is that the program prints “0” for the result regardless of the result stored in R4. You need to write code such that the program prints the appropriate result according to the value in R4 (the corresponding number character if the result is 0~9, “OUT OF BOUNDS” otherwise).
2
COMPSCI 210 Assignment 1 
2024 Semester 2 
In summary, you need to complete the following tasks to achieve the expected behaviour described in Section 3. (“F” stands for “Function”, “P” stands for “Print”.)

Branch to the appropriate operation (SELECT)

Subtraction (F_MINUS)

Multiplication (F_TIMES)

Floor division (F_DIV)

Modulo/Remainder (F_MOD)

Print result (P_RES)
Note: You may assume that the key presses are always reasonable (only number keys and the Enter key are pressed), and the operations are always welldefined (no dividing by 0). Here are some examples of expected results from the arithmetic operations.
Subtraction (F_MINUS) 
Multiplication (F_TIMES) 
● 93=6 
● 1*7=7 
Floor Division (F_DIV) 
Modulo/Remainder (F_MOD) 
● 6/3=2 ● 9/5=1 ● 8/3=2 ● 5/7=0 ● 0/2=0 
● 6%3=0 ● 9%5=4 ● 8%3=2 ● 5%7=5 ● 0%2=0 
5. Tips

If you want to debug your arithmetic operations more easily, then you may want to finish Task 1 (SELECT) and Task 6 (P_RES) first, since then you can utilise the calculator functionalities to enter and check your operands with ease. Otherwise you can still debug using the values shown in the registers.

You may use the examples listed in Section 4 to debug your implementations of the arithmetic operations, but also come up with your own examples to more thoroughly check for edge cases.
3
COMPSCI 210 Assignment 1 
2024 Semester 2 
3. It may be helpful to understand the program as a whole to more clearly understand the underlying mechanics and how they relate to the functions you write. You can do so by reading and understanding the provided portions of the program. Everything used in the program has been covered in lectures.