CS255 - Home/Project Assignment
CS 255 - Computer Organization/Architecture I
Homework 5

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/hw5 cp /home/cs255001/Handouts/hw5/* ~/cs255/hw5 ```

### 1. Euler's Algorithm

The famous Euler algorithm for determining the Greatest Common Divisor (GCD) of two numbers A and B is as follows:
```    int A, B, GCD;

// First, make sure A is largest of the two numbers...
if (B > A)
{ X = A;    { Swap A and B }
A = B;
B = X;
}

X = A % B;
while (X != 0)
{  A = B;
B = X;
X = A % B;
}
GCD = B;
```
When the algorithm ends, the variable GCD will contain the Greatest Common Divisor of A and B.

### 2. Assignment

Your assignment is to write the Euler algorithm in M68000 assembler language between the Start and Stop labels in hw5.s that will determine the GCD of the integer variables A and B.

The variables A, B, and GCD have already been defined in hw5.s - they are all integer variables.

Also, the variables A and B have been initialized with some value. You need to write the Euler Algorithm beginning at the label Start. The GCD computed by the algorithm must be stored in the integer variable GCD.

Note:

 Your program must work using any value stored in the variables A and B (I will use different values in A and B to test your program !!!) You can assume that A and B are both positive numbers

Note:

• You will need to use the DIVS instruction in this project to compute A % B.

• Recall that DIVS divides a 32 bits quotient by a 16 bits divider.

Although the divider B is 32 bits long, you can treat it as 16 bits because the value is small:

 For small values, the lower 16 bits of a 32 bits representation will represent the same value E.g.: 00000000000000000000000000001001 represents the value 9 in 32 bits So does 0000000000001001 (the lower 16 bits of the representation) in 16 bits

• Furthermore, remember that the DIVS instruction stores the remainder in the UPPER HALF of the data register.

Use the SWAP instruction to exchange the upper half and lower half of the register to put the remainder in the lower half of the register.

For more details, see the online class notes on "DIVS" and "SWAP" instructions:

### 3. The Debug file

I have made an Egtapi startup file (egt.hw5) for use in this project that will display the variables A, B and GCD. You have already made a copy of this debug file in the preparation section

You may need to add more display variables and/or breakpoints to help you debug the assembler program.

 Enter the name of the variable in the variable watch area Select the data type (signed int, signed short, etc) Select the base (binary, decimal or hexadecimal) Then click Add Var

Make sure that:

• The variable name is defined as:

 ``` xdef Varname ```

at the start of your program (you will need to add this assembler directive to your program)

 Enter the name of a label in the Break Points panel of Egtapi Click the Add Brkpoint button

You must have defined the label in your program (and compiled it.)

Make sure that:

• The label is defined as:

 ``` xdef label ```

at the start of your program (you will need to add this assembler directive to your program)

### 4. Some Information on Breakpoints

This is your first real assembler programming project; the program is no longer a bunch of move instructions. Your program will surely have bugs that you need to find, so here is a little advice on what to do to find them.

To find bugs in an assembler program, you have to look the the execution of the portion of the program that is in error. First, you have to make the program stop at the place that contains an error - that is done using "break points".

A breakpoint is a location in the program that when the program reaches that location, Egtapi will stop and await your next command. After the program stopped, you can: look at the value of the variables to see if the program has progressed correctly so far.

After examining various things (e.g. variables), you can then choose to continue the execution of the program from the breakpoint with one of these two actions:

• Continue (double right arrow button - located at the top right corner of the Egtapi window).

When you click on the Continue button, Egtapi will run the program until it reaches the next break point.

• Single Step (right arrow with a vertical bar button - located at the top right corner of the Egtapi window, next to the "Continue" button).

When you click on the Single Step button, Egtapi will execute one assembler instruction and stop. When Egtapi stops, it will show you the current value of all registers and variables that you have requested (in the variable watch window).

The Single Step action is very helpful to find bugs in your program.

### 5. How to debug assembler programs

To find bugs in a program, you need to

1. determine what is the correct behavior of your program and then
2. follow the execution of the program to see if it is indeed doing what it is supposed to do.

If the result prodcued by the program differs from what it supposed to do, you found yourself a bug....

Ofcourse, this requires from you that you know what the assembler program is supposed to do. If you don't have a clue about the assembler program is supposed to do, Egtapi will not be much help to you.... - because you do not know what to look for. (Analogy: if you know nothing about radiology, showing you an X-ray picture is useless since you don't know what to look for....)

So to be successful in finding bugs:

• Find out what values of the variables is supposed to be changed to.

• When you detect that the values of the variable are NOT what they should be, STOP.

Find out what caused the program to stray !

 I have written an extensive guide on how to use breakpoints in Egtapi here: click here --- Read Sections 3 and 4 !!!

### 6. Turn in

Turn in your hw5.s assembler program as hw5 using the /home/cs255001/turnin command (execute the command while you're inside your cs255 directory):

 ``` /home/cs255001/turnin hw5.s hw5 ```

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 hw5, you must execute the following command before the deadline of the homework:

 ``` /home/cs255001/req-ext hw5 ```

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