Gettysburg College

CS 221
Computer Organization and Assembly Language Programming

Fall 2024

Assignment 7

Due: Thu, Oct 24, by 11:59pm

Readings

Description

This assignment will focus on the following tasks: Build the RAM circuits described in Project 3:

N2T: Project 3

Chapters 3 provides the contract for each chip, i.e. description of its behavior, names and number inputs, names and state of outputs. The API is available here:

The Hack Chipset

Assembly Language Programming

vvv
vvv Scroll to the bottom. Consider starting with the Assembly section.
vvv

Design in Logisim

Note the following requirements: Here are additional specific requirements:

Design in HDL

Here are additional specific requirements:

Assembly Language Programming

If you have implemented Bubble Sort previously, do not refer to your previous work.

Implement the Bubble Sort algorithm. Briefly the algorithm works as follows:

1. Go through the whole array examining each pair of items. Exchange the items in a pair if the first is bigger than the second.

2. Repeat Step 1. n-1 times, where n is the size of the array.

// does one run of the bubble sort algorithm -- every
// pair of neighbors that are out of order are swapped
void bubbleRun(int[] numbers)
{
    for (int i = 0; i < numbers.length-1; i = i + 1)
    {
        if (numbers[i] > numbers[i+1])     // if out of order:
        {
            int temp = numbers[i];         // swap them    
            numbers[i] = numbers[i+1];
            numbers[i+1] = temp;
        }
    }
}

// sorts an array in increasing order -- simply executes *n-1* 
// bubble runs through the array, where *n* is the array length
void bubbleSort(int[] numbers)
{
    int n = numbers.length;
    for (int run = 1; run <= n-1; run = run + 1)
    {
        bubbleRun(numbers);
    }
}
Here is a visualization of the process from Wikipedia:
Hint: Ensure that SP is loaded with 0x100 (see array example at top).

Hint: Instead of size you might find it more convenient to have a variable that represents number of pairs.

Hint: In general, the meaning of a comparison should be flipped from what it would be in CS111.

Hint: An if statement is set up similar to a loop. It still has a jump/branch that is designed to skip over the section that normally would have been executed. The same structure was used for the loop, where the focus was on when to stop looping and to skip the body.

Part I

In file bubbleRunSimple.asm write a program that only implements Step 1. of Bubble Sort. Create an array of 6 elements at the top and check if the array was modified correctly.

To view the contents of the main memory, at the top select "Tools:=View memory contents". This will show the section of main memory where the array is located. It may be good to select Dec at the bottom to see the decimal versions of the cells.

As you step through the code both the register and main memory will update and you can check if the code behaves as expected.

Make sure to test with: scrambled array, sorted array, reverse sorted array.

Part II

In file bubbleSortSimple.asm copy the contents of bubbleRunSimple.asm and make the following changes.

Wrap the copied code in another loop so that bubbleRun is called exactly n-1 times.

Make sure to test with: scrambled array, sorted array, reverse sorted array.

Part III

In file bubbleRun.asm copy the contents of bubbleRunSimple.asm and make the following changes.

Ensure that when bubbleRun finishes register R10 is set to 0 if no swap was made during the run; otherwise, R10 should be set to 1 to indicate a comparison took place. Do not count how many comparisons were made.

Make sure to test with: scrambled array, sorted array, reverse sorted array.

Part IV

In file bubbleSort.asm copy the contents of bubbleRun.asm and make the following changes.

Keep looping around the code from bubbleRun as long as register R10 is 1.

Make sure to test with: scrambled array, sorted array, reverse sorted array.

Part V

Modify file bubbleSort.asm so that the code from bubbleRun loops less and less each time. That is:

Make sure to test with: scrambled array, sorted array, reverse sorted array.


What to turn in

Zip folder a7/ in file named a7.zip and upload it to the Moodle dropbox. When a7.zip is unzipped it should produce folder a7/ with the files inside.