From d6bb9bd762d690897702e209fedc777c18419a36 Mon Sep 17 00:00:00 2001 From: Benjamin Ramhorst Date: Wed, 10 Jun 2020 20:10:34 +0100 Subject: [PATCH] Added infrastructure for Fibonacci's number --- testing/fibonacci.cpp | 23 ++++++++++++++ testing/fibonacci.txt | 44 ++++++++++++++++++++++++++ testing/fibonacci_alternative.cpp | 51 +++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 testing/fibonacci.cpp create mode 100644 testing/fibonacci.txt create mode 100644 testing/fibonacci_alternative.cpp diff --git a/testing/fibonacci.cpp b/testing/fibonacci.cpp new file mode 100644 index 0000000..670cdba --- /dev/null +++ b/testing/fibonacci.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; + +/* + Takes one input via stdin, the integer n +*/ + +int fib(const int n){ + int y; + if (n <= 1) { + y = 1; + }else { + y = fib(n-1); + y = y + fib(n-2); + } + return y; +} + +int main(){ + int n; + cin >> n; + cout << fib(n) << endl; +} diff --git a/testing/fibonacci.txt b/testing/fibonacci.txt new file mode 100644 index 0000000..8867dda --- /dev/null +++ b/testing/fibonacci.txt @@ -0,0 +1,44 @@ +Comments: + R1 holds y, as described in the source code + R2 holds a, as described in the source code + R3 holds b, as described in the source code + R4 holds n, as described in the source code + R5 holds the output (sum), as described in the source code + R6 holds the number 0xB (for jumping to the end of the program) + R7 holds the number 0x6 (for jumping back) + +(0x0) LDA R1 ##MEMORY_LOCATION_OF_s## (loads the seed, s, into R1, which then becomes y as in the source code) +(0x1) LDA R2 ##MEMORY_LOCATION_OF_a## (loads a into R2) +(0x2) LDA R3 ##MEMORY_LOCATION_OF_b## (loads b into R3) +(0x3) LDA R4 ##MEMORY_LOCATION_OF_n## (loads n into R4) +(0x4) LDA R6 ##MEMORY_LOCATION_CONTAINING_0xA## (loads 0xB, memory location of program end to R6) +(0x5) LDA R7 ##MEMORY_LOCATION_CONTAINING_0x6## (loads 0x6, memory location for jumping back) +(0x6) JC4 R6 R4 (if R4, in this case, n, is equal to 0, then jump to end, STP) +(0x7) MLA R1 R2 R3 (multiply and add, same as in the source code y=y*a+b) +(0x8) ADD R5 R5 R1 (adds y to the sum, so that sum+=y, as in the source code) +(0x9) SBO R4 R4 (decrease R4, i.e. n by one, so no infinite loops occur) +(0xA) JMP R7 (jumps back to check whether n>0 and if so, repeats again) +(0xB) STP + + +Example that can be used with the instruction generator program: +LDA R1 0 +LDA R2 1 +LDA R3 2 +LDA R4 3 +LDA R6 4 +LDA R7 5 +JC4 R6 R4 +MLA R1 R2 R3 +ADD R5 R5 R1 +SBO R4 R4 +JMP R7 R6 +STP + +Requires setting the following data memory location: +Set location '0' to value of s; +Set location '1' to value of a; +Set location '2' to value of b; +Set location '3' to value of n; +Set location '4' to value of 0xB; +Set location '5' to value of 0x6; diff --git a/testing/fibonacci_alternative.cpp b/testing/fibonacci_alternative.cpp new file mode 100644 index 0000000..3c26971 --- /dev/null +++ b/testing/fibonacci_alternative.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +int fib(int n){ + int temp = n; //temporary variable, set equal to n + stack s; + //if n is non-negative, decrease until reaches zero to fill up stack + while(temp>1){ + s.push(0); //n is not 0 or 1, hence push 0 onto stack, come back later + temp--; //prevents an infinite loop + } + s.push(1); + s.push(1); + + //go back, increase temp until equal to n + while(temp> n; + cout << fib(n) << endl; +}