CS255 - Home/Project Assignment

## CS 255 - Computer Organization/Architecture I Homework 6

Due date: See Class Webpage

### 0. Preparations

• To do this project, you must obtain a copy of the prepared files with these commands:

 ``` mkdir ~/cs255/hw6 cp /home/cs255001/Handouts/hw6/* ~/cs255/hw6 ```

### 1. Project Description

Write a Bubble sort subroutine in M68000 assembler language. Some of you may have forgotten what the Bubble sort algorithm is, so let me describe that in Section 2.

### 2. The BubbleSort Algorithm

For those who does not know what bubble sort is, the following is the C++/Java program that you need to implement:

 ``` // How the variables are used: // // input A: array of integers that needs to be sorted // N: size of array A // // Done: integer (functioning as a boolean: 0 = false, 1 = true) // Help: integer (help variable for swapping) void BubbleSort(int[] A, int N) { Done = 0; // 0 represents false k = 1; while (Done == 0) { Done = 1; // 1 represents true. for (i = 0; i < N-k; i++) { if (A[i] > A[i+1]) { Help = A[i]; A[i] = A[i+1]; A[i+1] = Help; Done = 0; // Not sorted... } } k = k + 1; } } ```

Basically, the bubble sort algorithm swaps two adjacent elements A[i] and A[i+1] when the latter is smaller. When two adjacent element are exchanged, the Done variable is set to FALSE , indicating that another iteration is necessary.

### 3. The main Program

I have provided a main program that calls your bubble sort subroutine.

The main program is stored in the file hw6.s

The main program will call (i.e., executes a bsr BubbleSort instruction) the BubbleSort subroutine twice, each time with a different array.

The main program calls the BubbleSort with 2 parameters:

 The address of the array that needs to be sorted The number of elements in the array

The type of the elements of the array is int.

The main program passes the parameters as follows:

 the address of the array is stored in register D0 and the number of elements of the array stored in register D1.

Here are the code segments that shows you how the main program calls the BubbleSort subroutine:

 ``` move.l #A, D0 move.l #5, D1 bsr BubbleSort * Sort array A (5 elements) .... move.l #B, D0 move.l #10, D1 bsr BubbleSort * Sort array B (10 elements) ```

Therefore, your BubbleSort subroutine must use as parameters:

 D0 = address of the array to be sorted D1 = number of elements in the array

Note:

 When the BubbleSort subroutine returns, the array that was passed to BubbleSort will become sorted

### 4. The bubblesort.s program file

• Write your BubbleSort subroutine inside the file bubblesort.s.

Right now, the bubblesort.s looks like this:

 ``` xdef BubbleSort BubbleSort: ****************************************************** * Write your bubble sort assembler subroutine here ****************************************************** rts * ***************************************************************************** * If you need local variables, you can add variable definitions below this line * ***************************************************************************** end ```

You must write the BubbleSort function in M68000 assembler code and put the code after the BubbleSort (and before the rts instruction)

 Make sure you use the parameter passing agreement defined above !!!

This is also the file that you must turn in.

### 5. Compile and run project 2

• To compile the programs in this project, use the command:

 ``` as255 hw6 bubblesort ```

This command is different from the one you used to use !!!!

Notice that you need to specify BOTH hw6 and bubblesort !!!!!

• To run this project:

 Type hw6 into the Program box in Egtapi and click Run

### 6. The Egtapi Debug file

I have prepared a egt.hw6 file that displays the arrays used in Project 2. You have copied this file in the preparation step.

I am sure you will have bugs in your program with this assignment.

• So check (read !!!) out Section 4 + 5 of Homework 5 ( click here ) and the Section 3 and 4 ( click here ) in the Egtapi manual on how to:

 set break points and use breakpoints to debug your assembler program.

The breakpoints are very helpful in finding errors in assembler programs.

• Correct behaviour:

• When the program starts, you should see 2 unsorted array (named A and B) in a pop up window

• When you click on Continue (double arrow), the program will stop at the label BubbleSort

 I made the program stop at the start of the BubbleSort function to help you debug the program

When you click on Continue again, the program will stop at the label Start2

 The array A (in the pop up windown) will now be sorted

• When you click on Continue (double arrow) again, the program will stop at the label BubbleSort again (because it is a breakpoint)

Click Continue again, and the program will stop at the label Stop:

 The array B (in the pop up windown) will now be sorted

### 7. Turn in

Turn in your source file bubblesort.s as hw6 using the /home/cs255001/turnin command (executed while you're in your cs255 directory):

 ``` /home/cs255001/turnin bubblesort.s hw6 ```

You do not need to turn in the hw6.s file !!!

As usual, I want the source (so I can read it). DO NOT turn in the executable or the object code !

• Extension request

• To request an extension for hw6, you must execute the following command before the deadline of the homework:

 ``` /home/cs255001/req-ext hw6 ```

You request will be successful if you have not exceeded the maximum number of "free" (no-questions-asked) requests allowed