CS 202 - Homework 7 - 10/29/07

Due: Monday, 11/5/07, in class (or by 2pm the latest)

This homework has two parts and covers both circuits and assembly programming. You may work in groups of two, or by yourself if you prefer. I only need one submission per group. On your submission, please write down how much time it took you.


Part 1: Quicksort in assembly

Implement quicksort in assembly. Use the following C routine as your model:
void quicksortC(int a[], int first, int last)
{
  int i, pivot, pivotPos, tmp;
  
  if (first < last) {
    
    /* partition; let pivotPos be final position of pivot */

    pivot = a[first];	/* use the first element as pivot */
    pivotPos = first;
    for (i = first+1; i <= last; i++)
      if (a[i] < pivot) {	/* if you find a smaller element a[i] */
	pivotPos++;		/* increase pivotPos */
	tmp = a[pivotPos];	/* and swap a[i] with a[pivotPos] */
	a[pivotPos] = a[i];
	a[i] = tmp;
      }
    /* Now, pivotPos has the correct value, and we swap the pivot there */
    tmp = a[pivotPos];
    a[pivotPos] = a[first];
    a[first] = tmp;

    /* now, recursively sort the two halves: */
    quicksortC(a, first, pivotPos-1);
    quicksortC(a, pivotPos+1, last);
  }
}
A driver "callquick.c", the C version "quicksortC.c", and a skeleton assembly file "quicksortA.s" are provided in
   ~schar/cs202/hw7/
Hint: First, implement and test the partitioning code (comment out the recursive calls in "quicksortC.c"). Then, tackle the recursive calls. Make sure you save and restore any caller-saved registers (eax, ecx, edx) whose values you still need after the call. Also, before restoring any callee-saved registers (ebx, esi, edi) using "popl" instructions at the end of your function, make sure the stack pointer has the correct value as was discussed in class.


Part 2: 8x8 ROM

Implement a 8x8 ROM in LogicWorks. We will use 3 of these circuits for the implementation of the full Sniac circuit later in the semester. As discussed in class, your circuit should contain 16 hex keyboards. (Each ROM can be programmed by double-clicking on the device symbol and changing the hex keyboards.) Your circuit should have 5 inputs and 8 outputs. The inputs are the 3 address lines A2, A1, A0, as well as CS (chip select) and READ. The outputs are the 8 data lines D7, ..., D0. The output lines should be in "Z" state if READ=0 or if CS=0. Only of both READ=1 and CS=1, the outputs should show the ROM contents of the cell addressed by A2, A1, A0 (i.e., the state of two of the 16 hex keyboards).

Note: Use (at least) 16 "Buffer-4 T.S." from "Simulation Logic.clf". These are "tri-state buffers": if OE (output enable) is active (= 0), the outputs Q are the same as the inputs D. But if OE is not active (= 1), the outputs have state "Z" (i.e., not connected). This allows you to connect multiple outputs, as long as only one of them is enabled. You may want to package the "Buffer-4 T.S." into a smaller subcircuit (with the same pins) so you can fit 16 hex keyboards and 16 buffers close together. Make good use of LogicWork's busses (see chapter 6 of the manual). Your entire ROM should fit into the circuit window in "normal size".

Besides the hex keyboards and the buffers, you will need a few other components to decode the addresses and inputs CS and READ. Feel free to use any of LogicWork's predefined circuits for this. (Hint: search for "decoder".) But if you prefer, you can also define your own subcircuits from simple gates. Once your circuit is completed, test two of your ROMs using an arrangement like this:

Hand in printouts of your top-level test circuit, of your 8x8 ROM implementation, and of any subcircuits you define.


Hand in your paper submission for both parts on Monday. In addition, please submit your assembly program electronically by typing

  ~schar/bin/submit202
in the directory containing your assembly file.