Due date: See Class Webpage
The alternative method to get the prepared files is to enter these commands in the EGTAPI terminal:
mkdir ~/cs255/pj8 For CS255-1 students: cp /home/cs255001/Handouts/pj8/* ~/cs255/pj8 For CS255-2 students: cp /home/cs255002/Handouts/pj8/* ~/cs255/pj8 |
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 ARM assembler code.
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 k push j push i |
The return value of F( ) must be returned in register r0.
Use the following command to compile project 8
Open the Terminal in EGTAPI and type these commands:
cd ~/cs255/pj8 /home/cs255001/bin/as255 pj8.s recursion.s |
You can also compile it with EGTAPI's file browser:
1. First: press the CONTROL key and select pj8.s 2. THEN: On a Windows PC: press the CONTROL key and select recursion.s On a Mac PC: press the COMMAND key and select recursion.s NOTE: the ORDER of clicking is VERY IMPORTANT DO NOT click on recursion.s first !!!! (If you do, EGTAPI will generate the output "recursion.arm" and not pj8.arm) 3. Finally: click "Compile" |
Use Egtapi to run the program compiled program pj8.arm.
I have provide the pj8.s program file (it's part of the handouts that you copied in the preparation step) for you to test the recursive function:
main: // Call F(-3, 5, 5) mov r0, #5 push {r0} mov r0, #5 push {r0} mov r0, #-3 push {r0} bl F add sp, sp, #12 movw r1, #:lower16:ans movt r1, #:upper16:ans str r0, [r1] // ans = F() Stop1: // Call F(5, -4, 5) mov r0, #5 push {r0} mov r0, #-4 push {r0} mov r0, #5 push {r0} bl F add sp, sp, #12 movw r1, #:lower16:ans movt r1, #:upper16:ans str r0, [r1] // ans = F() ... (and so on) |
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)
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():
/home/cs255001/Handouts/pj8/pj8-check |
How to use the pj8-check program:
/home/cs255001/Handouts/pj8/pj8-check i-value j-value k-value |
Example 1:
/home/cs255001/Handouts/pj8/pj8-check 2 2 2 Call sequence: >> F(2, 2, 2) >> F(1, 1, 1) Ans = 1 |
Meaning:
|
/home/cs255001/Handouts/pj8/pj8-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:
|
|
Another explanation on pj8-check:
p8-check helps you tracks the recursive execution of your assembler program. You run it using the parameters that you pass to the recursive function. pj8-check will print out information to the terminal to help you track what should happen inside your assembler program. The main program will call F() 5 times as follows: 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) You can use pj8-check to predict what should happen in your assembler program. For example: to track HOW the first case (F(-3, 5, 5)) should run in your assembler program, you run: /home/cs255001/Handouts/pj8/pj8-check -3 5 5 It shows the call sequence and the answer that F() returns: Call sequence: >> F(-3, 5, 5) Ans = -1 The call sequence shows that F( ) will NOT call any other F( ); so this is a base case. When you step execute your assembler program, it should enter a base base (and return -1). Another example: the last example F( 5, 5, 5) How should this call be executed ? You can see it by using: /home/cs255001/Handouts/pj8/pj8-check 5 5 5 The output shows the call sequence: Call sequence: >> F(5, 5, 5) >> F(4, 4, 4) >> F(3, 3, 3) >> F(2, 2, 2) >> F(1, 1, 1) >> F(1, 1, 2) >> F(0, 0, 1) >> F(2, 2, 3) >> F(1, 1, 2) >> F(0, 0, 1) >> F(0, 0, 2) >> F(1, 1, 3) >> F(3, 3, 4) >> F(2, 2, 3) >> F(1, 1, 2) >> F(0, 0, 1) >> F(0, 0, 2) >> F(1, 1, 3) >> F(0, 0, 3) >> F(2, 2, 4) >> F(1, 1, 3) >> F(0, 0, 3) >> F(-1, -1, 3) >> F(1, 1, 4) Ans = 23 That means: F(5,5,5) will first call F(4,4,4) F(4,4,4) will first call F(3,3,3) ... and so on If you STEP execute your program, you MUST see: 5, 5, 5, pushed as parameters first Then: 4, 4, 4, pushed as parameters next, Then: 3,3,3, pushed as parameters, and so on... It will help you debug your code.
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.
|
(In EGTAPI, select: File Browser, Turnin, click on the file you want to turn in (recursion.s), use the turn in code: pj8)
You can also use the turnin command (executed while you're in your cs255 directory)
Open the Terminal in EGTAPI and type in these command:
cd ~/cs255/pj8 /home/cs255001/turnin recursion.s pj8 // If you're in section 1 or /home/cs255002/turnin recursion.s pj8 // If you're in section 2 |
As usual, I want the source (so I can read it). DO NOT turn in the executable or the object code !
9. Extension request
However, you need to use pj8 as homework code to make extension for this homework/project.
/home/cs255001/req-ext pj8 // IF you're in section 1 or: /home/cs255002/req-ext pj8 // IF you're in section 2 |
You request will be successful if you have not exceeded the maximum number of "free" (no-questions-asked) requests allowed