CPSC 220, Fall 2012
Lab 5: Assembly Language Programming

In this lab, you will write several assembly language programs for the Larc computer. Your work is due at the start of next week's lab. You should place your programs in a folder named lab4 and copy that folder into your homework folder in /classes/cs220/homework.

Writing and Running Assembly Language Programs

You can write an assembly program using a plain text editor, such as gedit. The assembler that you will use requires that files containing assembly code must end with ".s".

The syntax for LARC assembly language programs is fully described in the LARC manual, Chapter 5. You can use all the basic instructions from the table on page 47 plus all the extended instructions from page 48. For register names, you can use either the mnemonic names from the table on page 45 or numeric names. For input and output, you will still need to use traps and the syscall instruction.

Your programs should be well commented, with a comment at the beginning to explain the purpose of the program and short comments in the program to explain what is going on. Remember that you can have a comment on the same line as an assembly instruction or directive; everything after a '#' up to the end-of-line is treated as a comment. I suggest that you have comments to document what values you are putting in various registers.

When doing input/output, your programs should behave well. You should prompt the user for input and should label the output. You should include appropriate spaces and end-of-lines. You might need to include some explanatory output, such as "Enter a list of numbers. Enter a zero at the end of your list."

You should assemble your programs with the asm command and run them with the sim command. You can also debug the assembled program with the db command. You can use these commands provided you have set up aliases in your .bashrc file, as discussed in Lab 3.

(Of course, you could also try running the machine code with your own simulator, from Lab 4.)

I noticed one strange behavior of the assembler program asm: It only allows a single space after an assembler directive such as .asciiz or .word. Two or more spaces between the directive and its parameter will be treated as an error.

Programming Assignments

  1. To get used to using the assembler, write a program that reads two numbers from the user and prints their sum and their product.
  2. Write a program that computes a 3N+1 sequence starting from a positive number that is input by the user. The program should also report the number of steps in the sequence. Use a loop to read numbers from the user until the user inputs a number that is greater than zero. If that number is N, the sequence is defined by repeating the following operation as long as N is greater than 1: if N is even, divide it by two, otherwise multiply it by 3 and add 1. It is possible that the number might become too big to represent in 16 bits. This will happen if 3*N+1 is greater than 32767, which is the same as saying that N is greater than 10922. If this happens, your program should end and report the error. See Section 8.1.3 of the Java textbook for a Java version of the algorithm that you need.
  3. Write an assembly language program that reads a list of numbers from the user, sorts the list, and prints out the sorted list. The program should read numbers until the user enters a zero, which marks the end of the list but is not part of the list. You can use your favorite non-recursive sorting algorithm (selection sort, insertion sort, bubble sort) to sort the list. Another possibility is to keep the list in sorted order as you read it by inserting each input item into its correct position in the list. See Section 7.4 of the Java textbook for Java versions of sorting and insertion.