Lab 04: Programming the LC-3

Due 11:59pm Wednesday September 28, 2011


The program handin33 will only submit files in the cs33/labs/04 directory. (You can run update33 first to set up the directory first.)

You are encouraged to work with a partner; however, you may work alone if you choose to do so.

In this lab, you will write machine language and assembly language programs for the LC-3, convert them to object code for the LC-3 (lc3convert for machine language programs, lc3as for assembly language programs), and then run them in the LC-3 simulator (lc3sim-tk).

Your programs must follow these following guidelines:

For this lab assignment, all programs should begin at x3000 and, unless otherwise specified, should not make any assumptions about the initial state of registers or memory locations.

Machine Language

Write a program called countOnes.bin which counts the number of bits that are set to 1 in memory location x2FFE and stores the result in memory location x2FFF. You will have to manually set the value in memory location x2FFE before your program begins.

Your solution should use a loop to count the bits -- don't just list the same sequence of instructions 16 times to accomplish this task.

Challenge: Can you complete this program in 8 instructions or less -- including the HALT instruction?

Assembly Language

Write a program called dec2bin.asm which converts numbers typed in ASCII into their binary equivalent. Your program should read a decimal value (made up of multiple digits) from the keyboard and then output the binary value to the screen. Keyboard input is terminated when the user presses enter.

For example, here the user enters the decimal value "4304" which yields the binary result "0001000011010000".

Input a character> 4

Input a character> 3

Input a character> 0

Input a character> 4

Input a character> 


To avoid complicating issues, you should assume that the user always enters input from keyboard that consists only of the digits 0 through 9 and Enter -- there's no need to do any error checking. Also, you should assume -- again, don't error check -- that all values are positive, and that the user will not type a value larger than (215 - 1) = 32767.

Though this might sound quite complicated, if you break the problem into two halves, it's quite manageable:

  1. In the first half, you need to read digits from the keyboard and store these digits as a single number in a register. You don't have to actually convert it to binary -- simply by storing the numerical value in the register, you're storing it in binary!
  2. In the second half, you need to print the contents of that register to the screen.

This program is obviously more complicated than the countOnes program, but my fairly straightforward implementation (not doing anything fancy) took 40 instructions including some data. That's not to say your solution won't take more or less: it's just a ballpark number to give you some idea of the complexity of the problem.

Extra challenging (optional): In a file called dec2hex.asm, change your previous program so that it converts decimal input into hexadecimal output. (It's not trivial, but it's not as hard as it seems at first... my implementation took 57 instructions including some data.)