Department of Electrical and Computer Engineering

The University of Texas at Austin

EE 306, Fall 2009
Programming Assignment 1
Due: 4 October, 11:59 pm
Yale N. Patt, Instructor
TAs: Aater Suleman, Chang Joo Lee, Ameya Chaudhari, Antonius Keddis, Arvind Chandrababu, Bhargavi Narayanasetty, Eshar Ben-dor, Faruk Guvenilir, Marc Kellerman, RJ Harden, Veynu Narasiman

You must do the programming assignment by yourself. You are permitted to get help ONLY from the TAs and the instructor. When you have completed the program, and tested it sufficiently so that you are comfortable that it works on any input (i.e., value initially stored in x3100), submit it for grading according to the instructions Submit your program at the end of this handout.


Sign-Extend a 2's complement number

Background: The addressability of the LC-3 is 16-bits i.e. each memory location contains 16-bits of data. In general, all 16 bits in a memory location are used to store only a single piece of information such as a number. However, it is possible to save memory by packing numbers which are small and do not require all 16-bits. For example, we can store two 8-bit 2's complement numbers in a single memory location. This can be done by storing one of those numbers in bits[7:0] and the other number in bits[15:8]. However, the LC-3 operates on 16 bits at a time and thus if we want to use the LC-3 ADD instruction to operate on these numbers, we must first sign-extend the number to 16-bits. We need your help in writing a program which can perform this sign extension.

Your job: Write a program in LC-3 machine language to sign-extend an 8-bit 2's complement number to a 16-bit 2's complement number. The program takes one input and produces one output.

Program input: The 8-bit 2's complement number to be sign-extended. This number is stored in bits[7:0] of memory location x3100 before your program starts. Your program must load this input from memory. Note: Do NOT make any assumptions about bits[15:8] of this number: they may or may not be zeros.

Program output: The 16-bit 2's complement number obtained by sign-extending the input. Your program must store this value in memory location x3101 after the sign extension. When your program finishes, the following must be true for the output value that you will store in location x3101:

Bits[7:0] of the output will contain the same value as bits [7:0] of the input.

If the sign bit (bit 7) of the input is a 0, bits [15:8] of the output will contain eight 0's.

If the sign bit (bit 7) of the input is a 1, bits [15:8] of the output will contain eight 1's.

Example: If memory location x3100 contains:


  15  14  13  12  11  10  9   8   7   6   5   4   3   2  1   0
-----------------------------------------------------------------
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
-----------------------------------------------------------------
                                  ^
                                 sign

When the program finishes, location x3101 must contain:

  15  14  13  12  11  10  9   8   7   6   5   4   3   2  1   0
-----------------------------------------------------------------
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
-----------------------------------------------------------------

Hint 1: You can test the value of one (or more) bits by taking an AND of that number with a bit mask. You are allowed to load bit masks in the memory before the program execution begins. You can load them in memory locations after the HALT instruction.

Hint 2: A OR 0 == A ADD 0

Hint 3: Familiarize yourself with the software and documentation, and the submission instructions well before the deadline so that you can get help from a TA if you cannot figure out the mechanics by yourself.

Follow the following steps:

  • Write your program: Type your program in the LC3Edit text editor. Your program file needs to be in plain text format. The first line of your program must specify the memory address of the first instruction of your program. For this assignment, you should place your program starting at x3000 (i.e. the first line of your program should contain the bit pattern 0011000000000000).
  • Test your program: Load your program in the LC-3 simulator and test it thoroughly by running it with multiple different input values. Read Software and Documentation section for more help on how to use the simulator.
  • Submit your program: The program you are to submit is the .bin file. Save your .bin file, and give it the name sign.bin. Follow the submission instructions for uploading your sign.bin file for grading.

    Note: We understand that to test your program, you must first create a .obj file. The .obj file is not to be submitted for grading. We will create a .obj file from your .bin file when we grade your program.