-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFibonacciProgram.ts
47 lines (40 loc) · 1.15 KB
/
FibonacciProgram.ts
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
41
42
43
44
45
46
47
import {Computer} from "../../Computer";
describe("finonacci number", function () {
it("should work", function () {
const fibCount = 12;
const fibonacci = function (n) {
let first = 0;
let second = 1;
let fib = 1;
let counter = 0;
while (counter++ < n) {
let temp = fib + second;
first = second;
second = fib;
fib = temp;
}
return fib;
};
const program = [
"LOAD 0 %A",
"LOAD 1 %B",
"LOAD 1 %C",
"LOAD 0 %D",
"loop:",
"CMP " + fibCount + " %D",
"INC %D",
"JMPLEQ $halt",
"ADD %C %B %E",
"LOAD %A %B",
"LOAD %B %C",
"LOAD %C %E",
"JMP $loop",
"halt:",
"HALT",
].join("\n");
const computer = new Computer();
computer.loadProgram(program);
computer.cpu.runSynchronouslyUntilHalted();
expect(computer.cpu.registers.C.value).toEqual(fibonacci(fibCount));
});
});