CS 33: Lab #04

CS 33: Computer Organization


LAB 04: Programming the LC-3 in Machine Language

Due 11:59pm Wednesday, October 1.

The program handin33 will only submit files in the cs33/lab/04 directory. (You should run update33 first to set up the directory and create any necessary files.)

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

In this lab, we will write machine language programs for the LC-3 in binary (in your favorite text editor), convert them to object code for the LC-3 (lc3convert), and then run them in the LC-3 simulator (lc3sim-tk).

Your programs must follow these following guidelines:

  • Each file should be named using the name provided, and you must follow any instructions concerning particular memory addresses to use. Without this, it makes grading your programs exceedingly difficult.
  • Each file should compile without errors using the lc3convert program and be written in binary.
  • Each binary instruction should be commented with a description of the actual operation you are performing. For example:
         0101 010 011 0 00 101    ; AND R2, R3, R5 (or R2 = R3 AND R5)
         0000 011 000000011       ; BRzp #3
    WARNING: One of the hardest errors to figure out occurs when you change the comment without changing the code. For example, in the BRzp line above, changing BRzp to BRp in the comment does not change your program... you need to also change the binary code. Always verify your program with what the simulator believes you have typed in to be sure you don't get caught by this nasty problem.

  • In your comments, numbers will be assumed to be binary unless you preceed them with a # (e.g. #25) for decimal values, or x (e.g. x1F) for hexadecimal values.
  • Sequences of code which perform a single logical operation should be commented above the block, and a newline should separate it from the next block:
         ; Store in R3 the value of R2 * #-1
         1001 011 010 111111      ; NOT R3, R2 (or R3 = NOT R2)
         0001 011 011 1 00001     ; R3 = R3 + #1
         ; Shift R3 to the left two places
         0001 011 011 0 00 011    ; R3 = R3 + R3
         0001 011 011 0 00 011    ; R3 = R3 + R3
  • If a portion of your program is data, you should indicate that:
         1111 0000 0010 0101      ; TRAP x25 (or HALT)
         ; Data
         0000 0000 1111 1111      ; Bit mask for low order bits
         1111 0111 0011 0001      ; The value xF731

For this lab assignment, you will write a few LC-3 machine-language programs. All programs should begin at x3000 and, unless otherwise specified, you should not make any assumptions about the initial state of registers or memory locations.

  1. 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?

  2. Write a program called dec2bin.bin 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. 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 "appropriate value" in the register, you're storing it in binary! 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.

  3. Extra challenging (optional): In a file called dec2hex.bin, 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.)