Homework 5
CSE240 - Introduction to Computer Architecture
Autumn 2004

Due: Wednesday, Oct 27

This assignment is the first in which you will be actually writing code in the LC-3 assembly language. The first part of this document describes the questions; the second part of this document describes the logistics of writing the code, running the simulator, and turning in the assignment. Be sure to start early, because learning how to use the LC-3 tools will take some time.


Problem 0 (a warmup)

Write the LC-3 code to add 1 to the value in R0 and place the result in R5. This one is so easy, we'll give you the code. You shouldn't turn it in, but try it out in the simulator and test it with the provided test scripts (as described in the "logistics" section below).

Problem 1

Write the LC-3 code to subtract the value in R1 from the value in R0 and place the result in R5. That is, write the assembly code for R5 := R0 - R1.

Problem 2

Write the LC-3 assembly code to determine the maximum of two values. The two input values are in R0 and R1, and the code should place the greater of these two numbers into register R5. Hint: you may find calculating R0 - R1 useful.

Problem 3

Now that you've written code to determine the greater of two numbers, write the LC-3 assembly code for finding the maximum of a list of positive numbers in memory. R0 contains the location in memory of the start of the list, and the end of the list of numbers is signified by a zero or negative number. The code should place the greatest of the values in the list into register R5. If the first number of the list is zero or negative, R5 should contain zero.

For example, if R0 contains the value x4000, and the memory contains the following values:

  x4000: 10
  x4001: 20
  x4002: 15
  x4003: -1
In this case, the code should put the value 20 in register R5. Hint: the example on pages 139-141 of the textbook may help you.

Problem 4

Write the LC-3 assembly code to count the number of "1" bits in register R0 and place the result in register R5. For example, if R0 contains "0000 0001 0010 1100" a result of 4 should be stored in R5. Hint: the example on pages 170-171 of the textbook may help you.


Three Notes:

Getting Ready for the Homework #5

We'll be using the Linux machines in the Moore 100 lab for these assignments (see the map of CETS labs for the location of this lab). Some of you may choose to work remotely, but we'll leave those students most interested to figure out how to do that.

Log into one of the machines in the Moore 100 lab by typing your name and password into the login window (this name and password is the same as for your "eniac" and your @seas.upenn.edu e-mail account). When you're done using the computer, don't forget to logout of the computer.

After logging in, you'll need to start a "terminal" session. A terminal session allows you to type commands for the computer to execute. To do this, simply click on Applications near the top left of the screen and run X Terminal. We'll begin by making a directory (or folder) in which you will store the files for this assignment. Type the following command into the terminal window:

    mkdir cse240hw5
Now, enter this directory and copy all the provided HW5 code and scripts into this directory:
    cd cse240hw5
    cp /mnt/eniac/home1/c/cse240/project/hw/hw5/* .    (Yes, you actually need to type that dot.)
If you want to see what files are present in this directory, type "ls" to get a listing of all the files.

In order to use the LC3 tools, the directory that holds them must be in your search "path" (that is, the set of directories that the computer searches looking for programs). To temporarily add the correct directory to your path, run the following command:

    export PATH=$PATH:/home1/c/cse240/project/bin/
You will need to run this command to set the path every time you log into the machine.

Writing Your Programs

You can use any of the text editors on the Linux machines to code in assembly. For example, emacs is commonly-used text editor on Unix systems. You can run it by typing emacs with the name of your file. Let's just call it p1.asm. So, type:
    emacs p1.asm &
Once emacs has loaded, you can start entering text and writing code. Some useful commands include:
    Ctrl-x-s (Hold Ctrl and type x, then s): Save
    Ctrl-x-c (Hold Ctrl and type x, then c): Exit
Feel free to try out different commands and consult the help page for more help. If you're working on the Linux machines in Moore 100, you should be able to use the pull-down menus to use emacs's features as well. If you don't like emacs, alternatives include gedit, kwrite, and kate. To try these out, just type the name of the program and the filename. For example, "gedit p1.asm &"

Structuring Your Programs

Each program should be placed in it's own .asm file. For example, for homework #5, you will submit a total of four files. The files should be entitled p1.asm, p2.asm, p3.asm, and p4.asm, named according to the problem for which they were created. Each program should begin in memory at address x3000. This is accomplished via the .ORIG directive, which should be the first line in each file. The end of the program should consist of two lines: the penultimate line should contain the HALT instruction, and the last line in the file should contain the .END directive to inform the assembler that this is the end of the program. In summary, all of your submitted files should be of the following form.
       .ORIG   x3000
       your code goes here
Please see p0.asm for an example program. This file is one of the files copied into your directory above, and it contains the solution to problem 0.

Running Your Programs

Running your programs has two steps. The first step is running the LC-3 assembler. For example, to assemble the file p0.asm, type:
    lc3as p0.asm
If the assembler reports any errors, you must fix them and try again. Once the assembler reports no errors, you can launch the graphical version of the LC-3 simulator by typing:
    lc3sim-tk &
After the LC-3 simulator has loaded, type the name of the file in "File to Load" box and hit enter. This will load the file into the simulator and set the program counter (PC) to the start of the file. For more information on running and using the LC-3 simulator, please see the LC-3 tutorial linked from the course web page.

Testing Your Programs

We will provide testing scripts to help test and debug each program. We will not provide an exhaustive set of testing scripts, so you will also need to do your own testing to confirm that your code works. Each of the exercises has a corresponding testing script. To check your code with this script, assemble your program and run the testing script:
    lc3as p0.asm
    lc3sim -s p0.key
The script contains commands to (1) load the binary file into the simulator, (2) set some initial values of registers and/or memory, (3) run your code, and (4) check that the output values are as expected. If any of these checks fail, the output of the simulator reports an error. The end of the output summarizes if your code passed or failed the test.

Submitting Your Programs

Please submit all four programs using the online turnin facility on Eniac. Note that turnin is only available on eniac.seas.upenn.edu (and thus not directly accessible to the Moore 100 lab machines), so you will need to use the ssh program to remotely log in to eniac.seas.upenn.edu to turn in your solutions (as explained below). 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 from the Moore 100 lab machines.
    ssh eniac.seas.upenn.edu
If prompted, enter your eniac password. Continue by typing:
    cd cse240hw5
    turnin -c cse240 -p HW5 p1.asm p2.asm p3.asm p4.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), because our grading scripts will not be able to process your programs.

You may turn in the assignment as many times as you like. However, only the last version will be retained for grading. This ability is useful in testing whether you have any problems getting turnin to work before the last minute.

Start Early!!

Again, please start early! Although the coding is not complex, using the tools, scripts, and turnin facility are far from trivial.