Homework 3: Part 1
An LC-3 machine code program that counts the number of bits set in a word
Due
Tuesday, November 13, 2007 at 11:59 pm
Outcomes
After successfully completing this assignment, you will
be able to...
- Enter, debug, and execute programs written in LC-3 machine code
- Explain the different addressing modes of the LC-3 ISA
- Use the LC-3 operate instructions, data movement instructions, and
conditional branch instruction to solve a programming problem
Before Starting
Read Chapters 4 and 5. Also, do Lab 1 to learn
how to use the editor and simulator for the LC-3.
Description of Problem - Count the number of bits that are set in a 16-bit word.
Using LC3edit, create a file named count.bin
.
Your task is to write an LC-3
machine code program that counts the number of bits set to 1 in a 16-bit word.
Your program expects that R0 will contain the address of the
word to be tested (in other words, before executing your
program, you should manually enter the address of the word you will test
into R0). Upon completion of the program, the answer should be in R1. Your program should begin at memory
location x3000.
Example: If the machine is initialized with these values:
R0: x3050
memory location x3050: x8055
then upon completion of your program R1 should contain
the value x0005.
Hint: Recall from our class discussion that adding
a number to itself effectively shifts the bit pattern to the left
by one bit position.
You will use the solution to this problem in Part 2 of Homework 3 next week.
Documentation and Formatting
Your program should include a thorough
and clear explanation of the process that you went through
to come up with a machine code solution to
the given problem. The explanation should be included as a
comment at the top of your .bin file.
Program documentation and formatting will be
worth 50% of the grade for this
program - your explanation should not be considered an afterthought to coding.
It serves to document your thought process prior to coding and illustrates
your understanding of the problem. It will serve as your outline
when you code your solution. It will also serve as a guide to the
TA who is trying to grade programs consisting entirely of 0's and 1's.
It should contain (at least) the following information:
- An explanation of your thought process, and how it led you to
the algorithm you used to solve the problem. When you first sat down
to solve this problem, what steps did you take to understand the
problem? To come up with a solution?
As you consider each step in the solution, explain how you can accomplish
that step using instructions from the LC-3. Use examples for illustration,
if necessary. Your goal is to refine your algorithm to the point where it
can
easily be translated into LC-3 machine code.
- A list of the registers used in the program, and what they are used for
- If the program expects registers or memory locations to be initialized
to certain values prior to the program's execution, that should be
noted. The reader of the program should be told where to find the
program's results upon completion of the program.
When coding the instructions themselves, leave a space between each
field in the instruction, as we did in lab. For example, the following
AND instruction is separated into its component parts (opcode field,
destination and source registers, immediate bit, immediate value):
0101 001 001 1 00000 ;clear R1, to be used as the running sum
Unlike programs written in higher-level languages, machine code
programs should have a comment for every line of code.
Also, main sections of
code should be preceded by explanatory comments.
Deliverables
You must use turnin
to submit your LC-3 file count.bin
before 11:59pm on Tuesday, November 13. Make sure you include your name in a comment at the top of your file.