Hi guys,is anyone familiar with the pep/8 virtual machine to write assembly
language. I have the below program that I would like to translate to assembly language but its not giving me the correct output.
program reads two integers from the user and then prints all the Fibonacci numbers that lie between the two integers entered. Also, print how many numbers were printed and how many of them were odd.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
#include <iostream>
using namespace std;
int main()
{
int high;
int low;
int odd=0;
// Initialize first three Fibonacci Numbers
int f1 = 0, f2 = 1, f3 = 1;
// Count fibonacci numbers in given range
int result = 0;
cout << "Enter low and high limits:" << endl;
cin >> low;
cin >> high;
while (f1 <= high)
{
if (f1 >= low)
result++;
f1 = f2;
f2 = f3;
f3 = f1 + f2;
// take the numbers that are between low and high only and check to see if they are odd
if (f3 >= low && f3 <=high )
{
if( f3 % 2 == 1 )
odd++;
cout << f3 << " "; //this one could go out of the if stement
}
}
cout << endl;
cout << odd << " of the " << result << " numbers printed were odd" << endl;
//cout << result << " ";
return 0;
}
| |
This is the pep/8 assembly language I have so far,but it does not give me the correct number of Fibonacci numbers that lie between the two integers entered.
I'm guessing thats because I'm using the wrong branches.
;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
BRLE endWh ; IM HAVING issues here
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
deco f3,s
charo ' ',i
br while
endWh: charo '\n',i
deco result,s
STOP
msg1: .ASCII "Enter low and high limits:\x00"
.END