-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from wildmaples/fibonacci-element-2
Implement `FibonacciElement` and `StaticsTest`
- Loading branch information
Showing
15 changed files
with
1,191 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// This file is part of www.nand2tetris.org | ||
// and the book "The Elements of Computing Systems" | ||
// by Nisan and Schocken, MIT Press. | ||
// File name: projects/08/FunctionCalls/FibonacciElement/Main.vm | ||
|
||
// Computes the n'th element of the Fibonacci series, recursively. | ||
// n is given in argument[0]. Called by the Sys.init function | ||
// (part of the Sys.vm file), which also pushes the argument[0] | ||
// parameter before this code starts running. | ||
|
||
function Main.fibonacci 0 | ||
push argument 0 | ||
push constant 2 | ||
lt // checks if n<2 | ||
if-goto IF_TRUE | ||
goto IF_FALSE | ||
label IF_TRUE // if n<2, return n | ||
push argument 0 | ||
return | ||
label IF_FALSE // if n>=2, returns fib(n-2)+fib(n-1) | ||
push argument 0 | ||
push constant 2 | ||
sub | ||
call Main.fibonacci 1 // computes fib(n-2) | ||
push argument 0 | ||
push constant 1 | ||
sub | ||
call Main.fibonacci 1 // computes fib(n-1) | ||
add // returns fib(n-1) + fib(n-2) | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// This file is part of www.nand2tetris.org | ||
// and the book "The Elements of Computing Systems" | ||
// by Nisan and Schocken, MIT Press. | ||
// File name: projects/08/FunctionCalls/FibonacciElement/Sys.vm | ||
|
||
// Pushes a constant, say n, onto the stack, and calls the Main.fibonacii | ||
// function, which computes the n'th element of the Fibonacci series. | ||
// Note that by convention, the Sys.init function is called "automatically" | ||
// by the bootstrap code. | ||
|
||
function Sys.init 0 | ||
push constant 4 | ||
call Main.fibonacci 1 // computes the 4'th fibonacci element | ||
label WHILE | ||
goto WHILE // loops infinitely |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// This file is part of www.nand2tetris.org | ||
// and the book "The Elements of Computing Systems" | ||
// by Nisan and Schocken, MIT Press. | ||
// File name: projects/08/FunctionCalls/StaticsTest/Class1.vm | ||
|
||
// Stores two supplied arguments in static[0] and static[1]. | ||
function Class1.set 0 | ||
push argument 0 | ||
pop static 0 | ||
push argument 1 | ||
pop static 1 | ||
push constant 0 | ||
return | ||
|
||
// Returns static[0] - static[1]. | ||
function Class1.get 0 | ||
push static 0 | ||
push static 1 | ||
sub | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// This file is part of www.nand2tetris.org | ||
// and the book "The Elements of Computing Systems" | ||
// by Nisan and Schocken, MIT Press. | ||
// File name: projects/08/FunctionCalls/StaticsTest/Class2.vm | ||
|
||
// Stores two supplied arguments in static[0] and static[1]. | ||
function Class2.set 0 | ||
push argument 0 | ||
pop static 0 | ||
push argument 1 | ||
pop static 1 | ||
push constant 0 | ||
return | ||
|
||
// Returns static[0] - static[1]. | ||
function Class2.get 0 | ||
push static 0 | ||
push static 1 | ||
sub | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// This file is part of www.nand2tetris.org | ||
// and the book "The Elements of Computing Systems" | ||
// by Nisan and Schocken, MIT Press. | ||
// File name: projects/08/FunctionCalls/StaticsTest/Sys.vm | ||
|
||
// Tests that different functions, stored in two different | ||
// class files, manipulate the static segment correctly. | ||
function Sys.init 0 | ||
push constant 6 | ||
push constant 8 | ||
call Class1.set 2 | ||
pop temp 0 // Dumps the return value | ||
push constant 23 | ||
push constant 15 | ||
call Class2.set 2 | ||
pop temp 0 // Dumps the return value | ||
call Class1.get 0 | ||
call Class2.get 0 | ||
label WHILE | ||
goto WHILE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.