Take the following program and translate it into PEP/9 assembly language:
#include
using namespace std;
int fib(int n)
{
int temp;
if (n <= 0)
return 0;
else if (n <= 2)
return 1;
else {
temp = fib(n – 1);
return temp + fib(n-2);
}
}
int main()
{
int num;
cout << "Which fibonacci number? ";
cin >> num;
cout << fib(num) << endl;
return 0;
}
You must use equates to access the stack and follow the call to the function as discussed in the book (pass the parameter, return address, return a value and so on). There are NO global variables in the resulting code (except a global message of "Range num? "). It must be able to do sum a range greater than 2.
Just one fibonaccni number
My instructor wants the C code to be translated into PEP/9 assembly language
;declaring all my local variables
BR main
high: .EQUATE 12 ;#2d
low: .EQUATE 10 ;#2d
odd: .EQUATE 8 ;#2d
f1: .EQUATE 6 ;#2d
f2: .EQUATE 4 ;#2d
f3: .EQUATE 2 ;#2d
result: .EQUATE 0 ;#2d
main: SUBSP 14,i ;Allocate #high,#low,#odd,#f1,#f2,#f3,#result
lda 0,i ; initilize counters odd=0, result=0,f1=0
sta odd,s
sta result,s
sta f1,s
lda 1,i ; initialize f2, and f3 to=1
sta f2,s
sta f3,s
STRO msg1,d ; message to the screen to enter low and high numbers
deci low,s
deci high,s ; program is working good from here and up so far
while: lda f1,s
cpa high,s
BRGT endWh
if: lda f1,s
cpa low,s
BRLT while
LDA result,s
adda 1,i
sta result,s
lda f1,s
adda f2,s
sta f1,s
lda f2,s
adda f3,s
sta f2,s
lda f3,s
adda f1,s
adda f2,s
sta f3,s
br while
endWh: deco result,s
STOP
msg1: .ASCII "Enter low and high limits:\x00"
.END
Get Answers For Free
Most questions answered within 1 hours.