## Homework 5: A Gentle Introduction to LC-3 Assembly Programming CSE240 - Introduction to Computer Architecture Autumn 2006

### Due: Wednesday, October 25

This assignment is the first in which you will actually write code in LC-3 assembly language. Be sure to start early, because learning how to use the LC-3 tools will take some time. Please see the PennSim Simulator Guide for instructions on using the simulator (the PennSim Manual has additional information). You can download the simulator from the PennSim distribution page.

For your convenience, you can download all the code associated with this assignment (all test scripts and assembly code, including the LC-3 OS) as a ZIP archive. Note that this archive does not include the simulator itself.

### Problem 0 (warmup)

Write LC-3 assembly code to increment the value in R2 by 1 and place the result in R5. Actually, this is so simple that we'll give you the code for it. Don't turn anything in for this problem, the objective is to try running the code using the simulator and the test scripts (script1 script2 script3) we provide. Please see the PennSim Simulator Guide for step-by-step instructions on setting up and using the simulator.

### Problem 1

Write LC-3 assembly code to subtract the value in R2 from the value in R3 and place the result in R5. That is, write the assembly code for R5 := R3 - R2. Your program can destroy the values in R2 and R3; the only value that we will check is that in R5.

We've provided some skeleton code to help you get started. For this problem, you should electronically turn in a file named p1.asm, per the submission instructions, below. The PennSim Simulator Guide contains general instructions on how to go about writing and testing LC-3 code in the simulator. Make sure to check your code against the test cases we provide (script1 script2 script3 script4), as well as additional test cases that you create.

### Problem 2

You may have noticed that the LC-3 doesn't have an OR instruction. Using your knowledge of logical completeness and DeMorgan's Laws, write LC-3 assembly code that takes the values in R2 and R3 and puts the result of OR-ing them together into R5. That is, implement R5 := R2 OR R3. Your program can destroy the values in R2 and R3; the only value that we will check is that in R5.

Start from the skeleton code, and debug and test your code as you did for the previous problem. Test your code using our test cases (script1 script2 script3 script4) plus additional ones you create. Turn in a file named p2.asm per the submission instructions, below.

### Problems 3 and 4

These problems involve finding bugs in small assembly programs. The problems themselves are described on another handout. Please write your answers on that handout, and turn it in before the start of class on the due date.

You should also grab the assembly code for each problem: the code for problem 3, and for problem 4.

### Problem 5

In the original implementation of the LC-3 ISA, there was no multiply instruction. The current LC-3 uses the reserved opcode 1101 for multiply. In the spirit of those who have taken the class before you, write LC-3 assembly code to take the value in R2, multiply it by the value in R3, and place the result in R5. Of course, you are not allowed to use the MUL instruction! You can make two simplifying assumptions in your solution: the values in both R2 and R3 will be >= 0, and the result of the multiplication will not overflow a 16-bit representation. Essentially, implement R5 := R2 x R3. Your program can destroy the values in R2 and R3; the only value that we will check is that in R5.

You can start from the skeleton code, as with the other problems, and test your code with scripts (script1 script2 script3 script4). Turn in a file named p5.asm per the submission instructions, below. HINT: This program took us about 7 instructions to implement; if you're using many more than this, you may want to reconsider your approach.

## Submission Instructions

Your solutions for this homework must be submitted before class on the due date. Please write down your answers for problems 3 and 4 on the provided PDF handout and turn them in before the start of lecture. You will turn in your solutions to problems 1, 2 and 5 electronically.

Please submit your solutions to problems 1, 2, and 5 using the Linux-based online turnin facility (on eniac.seas.upenn.edu as well as the Linux-based lab machines). Turnin requires that you specify course number, homework number, and all the files you want to turn in. Below are the commands you will need to submit your programs:

```    turnin -c cse240 -p hw5 p1.asm p2.asm p5.asm
```
Please don't turn in the whole directory or include extraneous files. Also make sure your files are named correctly (e.g., P1.ASM is not the same as p1.asm), to allow our grading scripts to be able to process your programs.

You may submit the assignment as many times as you like; however, only the last submission will be graded. This ability is useful in testing whether you have any problems getting turnin to work before the last minute. You can check which files you've turned in with the following command:

```    turnin -c cse240 -p hw5 -v
```

#### Start Early!!

Again, please start early! Although the coding is not complex, using the tools, scripts, and turnin facility can be tricky, so don't leave it to the last second.