## CS 202 - Homework 5

This homework has three parts and covers both circuits and assembly programming. Part 1 is already due in 2 days. The rest is due Friday before break. You may work in pairs on parts 2 and 3.

### Part 1: Logisim warmup exercise

#### Due: this Wednesday, 3/14, at the beginning of class

This is a short exercise to get you acquainted with Logisim, the digital simulator software to be used in this course. No teamwork is allowed on part 1.

Here's what to do:

1. Install Logisim on your computer. This may also require installing Java.
Mac users: if you get an error that the Logisim App cannot run since it's not from the Mac App Store, then right-click on it and select "Open".

2. Do the Beginner's tutorial. There is nothing to be handed in for this part.

3. Implement a 3-bit binary to unary converter, analogous to the 2-bit converter discussed in class, shown below.

Your circuit should have 3 inputs (A2, A1, A0) and 7 outputs (Y1, ..., Y7). Try to minimize the number of gates - it is possible to have a total number of inputs on all gates that is less than 20. Come up with a clean layout of wires, gates (use "narrow" gates with the correct number of inputs), and input and output pins, and be sure to label all inputs and outputs. Also, please add a label with your name, the HW number (5.1), and a description of your circuit.

Once your circuit works and is nicely labeled, take a screen shot, save it in PNG format as "hw5-1.png", and upload it using the HW 5.1 submission page by the beginning of your class section on Wednesday.

## Parts 2 and 3

#### Due: Friday 3/23, at the beginning of class

You may work on parts 2 and 3 in teams of 2, or by yourself. If you choose to work in a pair, you must work on all parts together. You should perform pair programming, that is, share a single computer and always work together. Each person must contribute equally; you may not "divide and conquer." And don't forget, all files must contain both of your names.

Implement a 4-bit adder/subtractor as discussed in class. Your circuit should have 9 inputs (A0..3, B0..3, Sub) and 5 outputs (Y0..3, Carry). You may only use parts from the "Wiring" and "Gates" menus. You may want to define subcircuits for repeated components, for instance a full adder, and perhaps also a half adder.

Once your circuit works, package it into a subcircuit and create a custom appearance for it. Use a 1-tick spacing for the 4-bit inputs and outputs, so that splitters can readily be attached. Also attach 4-bit pins to inputs and output, as well as decimal unsigned probes. Overall your circuit should look similar to the one below, which illustrates the subtraction 12 - 5 = 7.

You will submit both the .circ file for circuit, and a PDF file containing screen shots of the top level circuit and all subcircuits. Before taking screen shots, be sure that each circuit is labeled with your name and the circuit's function. Copy and paste your screenshots into an editor, then save the whole thing in PDF. Use the file names addsub.circ and addsub.pdf.

### Part 3: Selection Sort in Assembly

Just so you don't immediately forget your assembly programming skills, you also get to implement selection sort in x86-64 assembly for this homework. Here is a C function you should use as a model for your assembly function:

```/* sort array elements a[0]..a[n-1] using selection sort */
void selSort(long a[], long n)
{
long i, j, maxpos, tmp;

/* i = number of unsorted elements */
for (i=n-1; i>0; i--) {

/* find maximum among unsorted elements */
maxpos = 0;
for (j=0; j<=i; j++) {
if (a[j] > a[maxpos])
maxpos = j;
}

/* swap max and last unsorted element */
tmp = a[maxpos];
a[maxpos] = a[i];
a[i] = tmp;
}
}
```

Name your assembly file selSort.s. A driver main.c to test your program is provided in ~schar/cs202/hw5/. Also, take a look at our last assembly program from class, arrmax.s - you may even be able to reuse part of this code. Please document your assembly code well. In particular, please include a comment explaining the register use.

#### Electronic submission for parts 2 and 3

Upload your files addsub.circ, addsub.pdf, and selSort.s using the HW 5 submission page by the beginning of your class section on the due date. As always, make sure your files contain your name and section, and that everything is properly documented. If you work in a pair, one person should submit all 3 files. In case your team is split across sections, you need to submit by the earlier deadline (for section A).