CS255 - Home/Project Assignment

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

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

### 1. A Recursive Function

Consider the following recursive function:

 ``` int F(int i, int j, int k) { int s, t; if ( i <= 0 || j <= 0 ) return(-1); else if ( i + j < k ) return (i+j); else { s = 0; for (t = 1; t < k; t++) { s = s + F(i-t, j-t, k-1) + 1; } return(s); } } ```

Do not try to figure what it does, just code it in M68000 assembler.

### 2. Assignment

Write the recursive function int F(int i, int j, int k) in M68000 inside the file:

 ``` recursion.s ```

that you have copied in the preparation step above.

The parameters of int F(int i, int j, int k) are passed on the system tack in the following order:

 ``` push i push j push k ```

The return value of F( ) must be returned in register D0.

### 3. Compiling and running this project

Use the following command to compile project 3:

 ``` as255 hw7 recursion ```

Use Egtapi to run the program.

Type hw7 into the Program field.

### 4. The hw7.s test program

I have provide the hw7.s program file (it's part of the handouts that you copied in the preparation step) for you to test the recursive function:

 ```Start: move.l #-3, -(a7) move.l #5, -(a7) move.l #5, -(a7) bsr F * F(-3,5,5) adda.l #12, a7 move.l d0, ans Stop1: move.l #5, -(a7) move.l #-4, -(a7) move.l #5, -(a7) bsr F * F(5,-4,5) adda.l #12, a7 move.l d0, ans Stop2: move.l #2, -(a7) move.l #5, -(a7) move.l #9, -(a7) bsr F * F(2,5,9) adda.l #12, a7 move.l d0, ans .... ```

This program calls F() 5 times and test all the possible cases that F() must handle:

 ``` Case 1: F(-3, 5, 5) - base case 1 (ans = -1) Case 2: F( 5, -4, 5) - base case 2 (ans = -1) Case 3: F( 2, 5, 9) - base case 3 (ans = 7) Case 4: F( 2, 2, 2) - 1 level recursion (ans = 1) Case 5: F( 5, 5, 5) - multi-level recursion (ans = 23) ```

The program will stop after each call and you should check if your value is the correct value (or not)

### 5. A tool to find the calling sequence of the recursive function F()

To aid you in determining if your program is correct, the handouts includes a C program that you can use to display the calling sequence of the recursive calls to F():

 ``` hw7-check ```

How to use the hw7-check program:

 ``` ./hw7-check i-value j-value k-value ```

Example 1:

 ``` ./hw7-check 2 2 2 Call sequence: >> F(2, 2, 2) >> F(1, 1, 1) Ans = 1 ```

Meaning:

 F(2,2,2) will call F(1,1,1) F(2,2,2) will return the value 1

• Example 2:

 ``` ./hw7-check 5 4 3 Call sequence: >> F(5, 4, 3) >> F(4, 3, 2) >> F(3, 2, 1) >> F(3, 2, 2) >> F(2, 1, 1) Ans = 4 ```

Meaning:

 F(5,4,3) will first call F(4,3,2)       F(4,3,2) will then call F(3, 2, 1) F(5,4,3) will next call F(3,2,2)       F(3,2,2) will then call F(2, 1, 1)            F(5,4,3) will return the value 4

• How to use hw7-check in this project:

 The calling sequence is the same in your assembler program and you should see the same sequence of parameters on the stack if you set a breakpoint at the subroutine F

### 6. The System Stack

In this project, you have to look carefully in the Stack Window to see if you maintain the program stack correctly. Pay special attention to the stack when you change it: when you enter the recursive function and when you leave it.

### 7. Debug Trick

• Make certain that you clean up the stack correctly when your program executes the rts instruction.

• You can best stop the program right before it going to execute the rts instruction.

You can do that as follows:

• Add a label to every rts instruction and xdef that label

Example:

 ``` xdef D1 Start: ... ... F: .... .... D1: rts * Label each rts instruction ```

• Add the label as a breakpoint in Egtapi (m68000)

### 8. Turn in

Turn in your program recursion.s as hw7 using the /home/cs255001/turnin command (executed while you're in your cs255 directory):

 ``` /home/cs255001/turnin recursion.s hw7 ```

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

 ``` /home/cs255001/req-ext hw7 ```

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