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:
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.