Spring 2001 CSCI 255 Lab 7

This lab is scheduled for the week of 12 March - 16 March.

Goals and Methods

This week we'll look at:

  1. "Compiling" machine language LC-2 programs
  2. Assembling LC-2 programs
  3. Performing some C logical operations on the LC-2

Start off creating and then connecting to a directory called csci/255/lab7.

Machine language programming on the LC-2

Starting with the LC-2 instruction format table given in Figure 5.1 (page 94) and assuming that R0 and R1 refer to registers 0 and 1, write in binary an LC-2 program to perform the following computation.


    if (R1 >= 0)
       R0 = 5 * R1 ;
    else
       R0 = 0 ;

Your program should not make any assumptions about the initial value of any register other than R1. In particular, you should not assume that the initial value of R0 is 0. Also, your code should not change the value of any register, other than R0, when it executes. Finally, your code should terminate by calling the TRAP instruction with the argument $25.

Entering the bits

First, write the binary encoding of your program onto the Lab 7 check off sheet. If you need more space, you are doing something wrong.

Now use the pico text editor to enter your program onto the computer. (Hopefully, you remember how to use pico from CSCI 107.) Your should store your program in the file lab7ex1.bin. The program will consists of several lines containing 0's and 1's. The first line will be the binary address at which your program is to be loaded. We suggest you use 0011000000000000 (x3000 hex). The following lines will be the LC-2 instructions of your program.

Converting to object form

The file you have just typed, lab7ex1.bin, is not in the proper object file format for the LC-2 simulator. To convert it to an object file, type the following command.

This command will create a very small file, less than 20 bytes, in your directory.

Running the program

Verify that your program works by running it in the lc2sim simulator. If it has a bug, fix it.

Assembly language programming on the LC-2

Now you're going to write an assembly language program for the LC-2. The program will set the values of registers R0, R1, and R2 based on the values of registers R3 and R4. Your program should not change the values of R3 and R4; however, it is free to use R5, R6, and R7 as "scratch" registers. Finally, be sure to load your program at x3000.

Here is the C-code you are supposed to implement:


    R0 = R3 <= R4 ;
    R1 = !R3 ;
    R2 = R3 && R4 ;

Keep in mind that the C <=, !, and && operators always return either 0, for false, or 1, for true. Your code should do likewise. For example, if the value of R3 is 6 and the value of R4 is 5; your code should set R0 to 0, R1 to 0, and R1 to 1.

Entering the assembly language program

First, write the assembly language program by hand onto the Lab 7 check off sheet. We're doing this to make sure you think before you type! Next enter it into the computer using pico and save it into a file named lab7ex2.asm.

Assembling your program

Type the following command to assemble your program:

This command will create an object file called lab7ex2.obj in your directory.

Running the program

Again verify that your program works with the lc2sim simulator.

Going home

When you are done, exit the simulator and log out of the Linux workstation. Do not delete your files. Your lab instructor will examine them later.