CPSC 220, Fall 2012
Lab 3: Machine Language Programming

In this lab, you will write several machine language programs for the Larc computer. These are programs that would be trivial to write in Java. Working in machine language, however, they are not at all trivial. I suggest that for each problem, you should first write out a solution in pseudocode. You should work on the pseudocode to get something that corresponds closely to the Larc machine language. For example, an assignment statement such as Y=2*Y+7 will require the use of several machine language instructions and several registers.

For this lab, you are required to do pair programming. You will work in teams of two. Each team will produce one solution to each programming assignment in the lab. You should discuss each problem and develop pseudocode with your partner. You should then work using the "pilot / navigator" model: One person, acting as navigator, does the typing. The other, acting as pilot, helps with thinking about the program, watches for mistakes, and makes suggestions. You should plan to continue to work together outside of class.

One person on your team should make a folder named lab3 in his or her homework directory. Your machine language programs should be placed in that lab3 folder by the beginning of next week's lab. Each program should have the names of both team members in a comment at the top of the program.

Some Setup

The directory /classes/cs220/larc/bin contains several command-line programs that you will need to work with Larc. For this program, the one that you need is named "sim", the simulator that executes Larc machine language programs. To make it easy to use sim and the other programs, it is convenient to define some aliases. To define the aliases, edit your .bashrc file, for example by giving the command

gedit .bashrc

while in your home directory. Copy-and-paste the following the lines to the end of the .bashrc file, and save it. This will define the aliases that you need to work with Larc, plus an alias for running logisim from the command line:

alias sim='/classes/cs220/larc/bin/sim'
alias asm='/classes/cs220/larc/bin/asm'
alias db='/classes/cs220/larc/bin/db'
alias s2l='java -cp /classes/cs220/util String2Larc'
alias logisim='java -jar /classes/cs220/logisim.jar'

This defines the command sim to be a command that you can use in a Terminal to mean the same thing as /classes/cs220/larc/bin/sim, and similarly for the other aliases. (The .bashrc file is applied when a Terminal is open, so it won't apply to the one that you use to edit .bashrc; you have to open a new Terminal.)

Writing and Running Machine Language Programs

You should have read about the format of Larc machine language programs... In reality, you are just going to write a plain text file. (You can write the files, for example, with gedit or nedit.) You will then run that file through a program that simulates the Larc computer. You should understand that the files that you create are not really machine language. A real machine language program would not be human readable, and it would contain just 16 bits (two bytes) for each instruction.

Every line in the machine language files that you create must contain one of four things:

  1. Exactly 16 0's and 1's, representing a 16-bit binary number
  2. 0x followed by exactly 4 hexadecimal digits, representing a 16-bit binary number
  3. A blank line, which is ignored
  4. A comment line, staring with #, which is ignored.

Your programs should include a comment at the top, giving the names of the two people on your programming team. It should use blank lines to break up the program into functional parts, and you should include comments to say what each part does. (For example, a comment like "# Read a string and store it at location 87" to explain the four machine language instructions that you need to carry out this command.)

The name of a file that contains a Larc machine language program should end with ".out". To run a machine language file named prog.out, you can use the command

sim prog.out

This assumes that you have set up the alias, as discussed in the previous section.

Programming Assignments

  1. Write a program that computes and outputs the value of the expression 6*15-100 and then halts. The program must do the calculation using several machine language instructions. The purpose of this exercise is is to make sure that you can write and run a simple program. There are no branches in the program, and just two system calls.
  2. Write a program that gets two numbers from the user, then computes and prints their sum and their product. The program must ask the user to enter each number by saying, for example, "Enter a number". It must label the output by saying, for example, "The sum is 17" and "The product is 60". There must be a new-line at the end of each line of the output. The purpose of this exercise is mainly to use system calls to do input and output. You will have to convert the strings that you want to use in the output into binary numbers, before you can include them in the program. If you have set up the aliases as instructed above, you can use the command s2l to do that. ("s2l" stands for "string to larc", and the third character is a lower case "L".)
  3. Write a program that gets two numbers from the user, then prints the smaller of the two numbers. (You could write the program by modifying a copy of your solution to the previous exercise.) The main point of the exercise is make a test and use a branch instruction.
  4. Write a program that reads a string from the user and then outputs the length of the string. The output should say something like, "The length is 42." You can assume that the input string is shorter than 127 characters. Recall that a zero will be stored in memory to mark the end of the string. The purpose of this exercise is to use a loop and to count.
  5. Write a program that reads a sequence of strings from the user and outputs the length of each string. The program should halt when the user enters an empty string. (You could start with a copy of your solution to the previous exercise.)