CPSC 220, Fall 2022
Lab 2: Larc Programming, Part 1

This is the first of two labs on programming the Larc model computer. You will be using a very simple assembly language, which is really no more than an alternate notation for the Larc machine language. You will use a Larc simulator to run the programs.

Before starting the lab, you should have studied the short web pages that document the Larc machine language and the Larc simulator. The simulator is written in Java and is available as an executable jar file, larcsim.jar.

For this lab, you will not use the Larc memory instructions (sw and lw) or the jalr instruction. You will not work with strings. And the only syscalls that you will use are syscall 0 (halt), syscall 2 (print int), and syscall 4 (read int). The unused features are left for next week's lab.

The assignment for the lab is to write several Larc programs, as specified below. The programs will be written as plain text files, which you should turn in by the start of next week's lab. You will turn in a folder named lab2 containing files named ex1.x, ex2.s, ex3.s, ex4.s, and ex5.s.

Getting Files, Running Larcsim, and Turning in Your Work

You can get the files that you need for this lab by downloading them using links on this web page. Alternatively, if you are signed into your Linux account, you can find them in the folder /classes/cs220/lab2-files. You could use the files directly from that folder. Or, you can copy that folder onto your Linux desktop, if you want, using the GUI file manager or on the command line with the command

cp  -r  /classes/cs220/lab2-files  ~/Desktop

The tilde character ("~") in "~/Desktop" refers to your home directory. The "-r" is required when copying an entire folder instead of just a single file. From another computer, you could use scp to get the files, with a command like

scp  -r  zz999@math.hws.edu:/classes/cs220/lab2-files  .

Remember that a period (".") refers to the directory that you are working in.


To run Larcsim on your own computer, you must have a JDK (Java Development Kit) installed, such as one downloaded from adoptium.net. If you have installed a JDK, you can probably just double-click the larcsim.jar file to run the program.

You can always run Larcsim on the command line (assuming you have a JDK) using the following command in a directory containing larcsim.jar:

java  -jar  larcsim.jar

(For Linux users, our version of Linux is annoyingly picky about running executable .jar files using the GUI. If you want to run larcsim.jar by clicking its icon, the larcsim.jar file must be marked as "executable." That's true for the copy in /classes/cs220, but if you got the file by downloading from this web page, you need to make it executable — right-click the file icon, select "Properties" from the popup menu, go to the "Permissions" tab, and check the box labeled "Allow executing file as program". Now, you can run the program by right-clicking the icon and selecting "Open with" / "OpenJDK Java 17 Runtime". If you want to be able to run executable jar files by double-clicking their file icons, right-click the icon, select "Open with" / "Other Application", select "OpenJDK Java 17 Runtime", and click the "Set as Default" button.)


For turning in your work, you should create a new folder named lab2 and store the program files that you create for this lab in that folder. When you are ready to turn in your work, copy the entire lab2 folder into your homework submission folder. If you are working in your Linux account, you can use the GUI, or use a command of the following form in the directory that contains the lab2 folder:


cp  -r  lab2  /classes/cs220/homework/LastName

From another computer, you could use SFTP or a command similar to the following:

scp  -r  lab2  zz9999@math.hws.edu:/classes/cs220/homework/LastName

Practice with the Larc Simulator

Before you start writing your own programs, you should make sure that you know how to use the Larc simulator, larcsim.jar. Be sure to read the documentation!

Once you have Larcsim running, you can use the "Load Memory from File" button to load a program from a text file into the Larc computer's memory. Assuming that no syntax errors are found in the program, you can then step through the program with the "Step" button, or run it with the "Run" button. If you want to start the program again from the beginning, first click "Reset CPU".

You should try stepping through two sample programs, 3N+1.s and product-and-sum.s. (Note that ".s" is a common file extension for assembly language programs.) You should also open the two programs in a text editor and read the comments. We already looked at these programs in class.

The program 3N+1.s only uses registers. The program product-and-sum.s uses registers, but it also does integer I/O using syscalls number 2 and 4. You should note how the syscalls work in the simulator. In particular, when a syscall 4 is executed, you will not be able to continue with the program until you have typed a legal integer into the Input/Output section of the window and pressed return.

I suggest that you also play with the "Display Style" for the Registers and for the Memory, and make sure that you understand what they do. You need to understand that what is actually stored in a memory location or register is a 16-bit binary number, which can be interpreted in several different ways.

The Assignments

Your assignment for the lab is to write five programs for the Larc computer. For each exercise, you will write a plain text file containing the program. You should name the files ex1.s, ex2.s, ex3.s, ex4.s, and ex5.s. Each program must have comments, similar to the comments in 3N+1.s and product-and-sum.s. And you should end each program with a Halt syscall. (My most complex sample solution, for Exercise 5, uses 17 instructions.)

Exercise 1: Simple arithmetic. The program ex1.s should compute the value of the expression  7*256-25*70. The program should start by storing the numbers 7, 256, 25, and 70 in different registers. Note that 256 is 0x0100 in hexadecimal! The program should then compute the answer using arithmetic instructions, and leave the final result in register number 10.

Exercise 2: Big Immediate. The program ex2.s should put the value 0xFACE into register 10, using only li, lui, and arithmetic instructions (that is, not using lw, which could do it in one step). The problem here is that the instruction li $2 0xCE actually loads 0xFFCE into register 2, since the immediate value in the li instruction is sign-extended. But to complete the process, what you really need is 0xCE in a register. (Suggestion: Set Register Display Style to "Hexadecimal" while running this program.)

Exercise 3: Maximum value. The program ex3.s should input two numbers from the user and print the larger of the two numbers (interpreted as signed integers, since that is what the slt instruction does). You might want to start with a copy of product-and-sum.s.

Exercise 4: 35. The program ex4.s should compute 3 raised to the 5th power, and it should output the answer. It should work as follows: Start by storing 3 and 5 in registers 1 and 2. Then start with a 1, and use a loop to multiply it repeatedly by the value in register 1. In each iteration of the loop, decrement the value in register 2. End the loop when the value in register 2 is zero. Your program should work no matter what values are loaded into registers 1 and 2 at the start of the program. That is, you are actually writing a routine to compute xy for any x and y. (The answer, of course, will be truncated to 16 bits.)

Exercise 5: Counting Ones. The program ex5.s should input an integer from the user and should output the number of ones in the binary representation of the input value. You might want to look at 3N+1.s to see how it gets the rightmost bit from a binary number. I suggest that you use a loop that repeatedly shifts the input value right by one bit position, until it becomes equal to zero.