Collaboration
For assignments in CS 2400, you will complete each of them on your own or solo. However, you may discuss high-level ideas with other students, but any viewing, sharing, copying, or dictating of code is forbidden. If you are worried about whether something violates academic integrity, please post on Ed or contact the instructor.
Setup
If you haven’t already, you should follow the VMWare setup.
If you can’t get the VM setup or don’t want to use it for this assignment, you will need to have a version of Java Runtime Environment(JRE) installed (the VM uses open JDK/JRE 11) and download the PennSim file here: PennSim.jar. To run PennSim.jar:
- On Windows: double click the PennSim.jar file and it should boot
- On Linux/Max: from the terminal, once you are in the same directory as the PennSim.jar file, run
java -jar PennSim.jar
Instructions
In this assignment you will write a number of short LC4 assembly programs and test them in PennSim. In order to do this assignment, you will need to have installed the virtual machine that we have distributed along with the relevant tools like text editors and PennSim. Alternatively, one could do this outside of the Virtual Machine, but limited support can be provided outside of the instructions mentioned in the setup section above. We will have demos of PennSimm in Lecture and Recitation and a section below that gives some instrucitons on using PennSim. As always course staff are available to help answer your questions.
We highly suggest that you look at the multiply.asm, sum_numbers.asm and strlen.asm code examples and make sure that you can get them working in PennSim before you begin the assignment. Please refer to the PennSim section below for instructions on how to assemble, load, and run these programs. Similar information can be found in lecture and will be demonstrated in recitation.
When writing a program in assembly you will find it helpful to work your algorithm out on paper perhaps with the aid of a flow chart before you start writing the code. You should also write down what variables you need to maintain in your program and which registers will store those values; this is known as a register allocation.
For each of the programs you are required to submit a script file similar to multiply_script.txt
which can be used to assemble, load and configure your program.
Your asm file should have a label called END
to mark the end of the program.
The script file for each program should also set a break point called END
at the end of the program so that we can easily run and test your program.
Remember again that your assembly and script programs must be saved as text files. You can create and edit text files with an editor like vim, emacs, nano, Sublime, and many others.
Problem 1: sqrt
For this problem you can assume that R0 is set to a 2C value greater than or equal to 0.
Your job is to determine the integer sqaure root of R0 and report the answer in R1.
The integer square root of a non-negative integer n
is the smallest integer greater than or equal to the square root of n
.
For example, the integer square root of 25 is 5, while the integer square root of 26 is 6.
At the end of your program R1 should contain the intger square root of R0.
Your asm file should be entitled sqrt.asm
and the corresponding script should be titled sqrt_script.txt
.
Problem 2: parity
For this problem your job is to write a program that considers a 16 bit field initially stored in R0.
Your program should determine the number of 1 bits in this 16 bit field.
Specifically after your program has run R1 should contain the number of 1 bits in R0.
Egs if the 16 bit field contains 6 1 bits R1 should be set to 6.
Your ASM file should titled parity.asm
and the corresponding script should be titled parity_script.txt
.
Problem 3: strcopy
Your job is to write a program that takes a string and creates a copy at the specified address.
At the beginning of your program, R0 should contain the address of the start of the string to be copied and R1 should contain the address of the start of the memory location where the copy should be stored.
You can assume that R0 is a properly formatted string that has its end marked by the null-terminator character (which is equal to 0) and that the memory location specified by R1 can safely store a copy of the string specified by R0.
After your program has run, the string that R0 initially points to should be unchanged and a copy of the string should be stored starting at the address initially stored in R1.
Note that you are allowed to modify the address stored in R0 and R1 over the duration of your program.
While you are developing your code, you may find it useful to refer to the strlen.asm example discussed in lecture.
Your asm file should be entitled strcopy.asm
and the corresponding script should be titled strcopy_script.txt
.
Please note that your final solution should not contain this test code that fills data memory or sets the R0 and R1 registers since it will interfere with our autograder.
Problem 4: reverse_array
Your job is to write a program that takes in an array of 2C numbers stored in memory and reverses that array.
At the beginning of your program, R0 should contain the address of the start of the array to be reversed and R1 should contain the length of the array.
You can assume that the length of the string (R1) will be non-negative.
After your program has run, the elements in the array should be flipped so that the array is now in th reverse order as it started.
For example, if an array of [0, 2, -7, 5]
is provided, the array should look like [5, -7, 2, 0]
after the function has ended.
While you are developing your code, you may find it useful to refer to the sum_numbers.asm example discussed in lecture.
Your asm file should be entitled reverse_array.asm
and the corresponding script should be titled reverse_array_script.txt
.
Please note that your final solution should not contain this test code that fills data memory or sets the R0 and R1 registers since it will interfere with our autograder.
PennSim
This section details how to use PennSim, a Java application for assembling, running and testing LC4 assembly programs. All of the details here are covered within the demonstrations in lecture and in recitations, we recommend watching the lecture recording if you are having any issues, but feel free to ask on Ed or in office hours. (The demonstration is in the beginning of Lecture 09 from October 5th.)
Starting PennSim
PennSim is provided with the course virtual machine, though instructions to get it are also in the #setup of this specification. To start PennSim in the course VM, you should be able to double click on the PennSim.jar icon on the desktop. If that doesn’t work, open the terminal and try running:
~$ cd Desktop
~/Dektop/$ java -jar PennSim.jar
With PennSim open, you should be able to see something similar to lecture, where the state of registers and memory are visible, and there is a command line to enter commands towards the top.
Compiling and Testing
Once PennSim is open, you can start assembling our LC4 programs into something runnable. If we wanted to do this for the sample program multiply.asm, we should make sure that multiply.asm is in the same folder as our PennSim.jar (which is the desktop on the VM) and type in the command line at the top of PennSim: as multiply multiply
. There should now be a file called multiply.obj
in the same directory. After assemling a .obj
file, we can load it in to PennSim by running ld multiply
. We can then set any registers to their initial values using set \<REGISTER\> \<VALUE\>
, so if I wanted to set R5 to 12, we could run set R5 12
. We need to make sure we set the Program Counter (PC) to the start of our code, and set a breakpoint at the end of our program. We can now hit the “continue” button in PennSim to run our program.
Scripts
Instead of typing in the commands mentioned above everytime you want to test your program, we suggest writing a script file. A script file for PennSim is a text file that contains a list of PennSim commands. These commands are then executed in order. An example script is multiply_script.txt. You will need to create a script for each of your LC4 programs and we highly suggest you base them off of our provided examples.
Examples
Below is a list of the example programs and scripts used in lecture:
- multiply.asm
- multiply_script.txt
- sum_numbers.asm
- sum_numbers_script.txt
- strlen.asm
- strlen_script.txt
Submission
Please submit all of your asm files and their corresponding script files to Gradescope This should include:
sqrt.asm
sqrt_script.txt
parity.asm
parity_script.txt
strcopy.asm
strcopy_srcipt.txt
reverse_array.asm
reverse_array_script.txt
Make sure that the files you submit follow the instructions laid out above. Most notably:
- The files should be named as specified. After submitting to gradescope, there should be a visible test that will complain if a file is not found.
- Each script file sets a breakpoint at the label END to mark at what point the program has finished executing.
- The Input Registers should NOT be set inside of your asm files, but instead should be setup in your script file for
sqrt
andparity
, or not at all forstrcopy
andreverse_array
.