Homework 3, Part 2
Unpacking a packed array (using LC-3 machine code)


Tuesday, November 23, at 5pm


After successfully completing this assignment, you will be able to...

Before Starting

Read Chapters 4 and 5. You should have already completed Homework 3, Part 1. You are welcome to use my solution to HW3, Part 1, if you wish (the solution will be published after the late due date has expired).

Description of Problem

(Adapted from an assignment created by Yale Patt at the University of Texas at Austin)

There are many different ways of interpreting bits in memory. We are already familiar with the idea of a single LC-3 memory location containing either an LC-3 instruction or a 16-bit 2's complement value. Consider a program that deals with small integer values in the range -128 to +127 (decimal). We would need only 8 bits to represent each number, so to conserve memory we could pack two such numbers into a single LC-3 location (hence the term "packed array").

You are to write a program in LC-3 machine code (not assembly language) that will calculate the sum of all the 8-bit 2's complement values in a packed array of N elements. With N elements in the packed array, we require N/2 memory locations. For this assignment, we will assume N is even. The sum of the elements will be stored in one word of memory as a 16-bit 2's complement number.

For each word in the packed array, bits [15:8] contain the first 8-bit value, and bits [7:0] contain the second value.

Using LC3edit, create a file named unpack.bin. You should find the code you wrote for Part 1 useful for Part 2. Your program should begin at location x3000. The size of the packed array, N, is stored in location x3101. You may assume that N <= 100 (decimal). The packed array itself is stored beginning at location x3102. Your program should store the result SUM in location x3100.

Prior to executing the program, you should manually load test values into locations x3101, and x3102 through x3102 + (N/2 -1).

Example: If prior to program execution memory is loaded with these values:

location   contents
x3101       x0006
x3102       x7F03
x3103       xFE7A
x3104       x1B40
then upon completion of the program, location x3100 will contain x0155.

Documentation and Formatting

As with part 1, you should include an explanation of your algorithm as a comment at the beginning of your program. Look at my solution to HW3, Part 1 for guidance on what kind of information to put in this comment. Also, as before, your machine code instructions should be typed so that the individual fields of the instructions are separated, and a comment should be included for each instruction.


You must use turnin to submit your LC-3 file unpack.bin before 5pm on Tuesday, November23. Make sure you include your name and section number at the top of your file.