├── projects ├── 10 │ ├── Complier │ │ ├── .idea │ │ │ ├── .name │ │ │ ├── copyright │ │ │ │ └── profiles_settings.xml │ │ │ ├── scopes │ │ │ │ └── scope_settings.xml │ │ │ ├── encodings.xml │ │ │ ├── vcs.xml │ │ │ ├── modules.xml │ │ │ ├── compiler.xml │ │ │ └── misc.xml │ │ ├── out │ │ │ └── production │ │ │ │ └── Complier │ │ │ │ └── SyntaxAnalyzer │ │ │ │ ├── JackAnalyzer.class │ │ │ │ ├── JackTokenizer.class │ │ │ │ └── CompilationEngine.class │ │ └── Complier.iml │ ├── Square │ │ ├── Main.jack │ │ └── MainT.xml │ ├── ExpressionlessSquare │ │ ├── Main.jack │ │ ├── MainT.xml │ │ └── Main.xml │ ├── ArrayTest │ │ └── Main.jack │ └── README.txt ├── 11 │ ├── Square │ │ ├── Main.vm │ │ ├── Main_1.vml │ │ └── Main.jack │ ├── Pong │ │ ├── Main.vm │ │ ├── Main.vml │ │ ├── Main.jack │ │ └── Main.jack.bak │ ├── Seven │ │ └── Main.jack │ ├── src │ │ └── Symbol.java │ ├── Average │ │ └── Main.jack │ └── README.txt ├── 12 │ ├── ArrayTest │ │ ├── ArrayTest.out │ │ ├── ArrayTest.cmp │ │ ├── Array.vm │ │ ├── ArrayTest.tst │ │ └── Main.jack │ ├── OutputTest │ │ ├── OutputTestOutput.gif │ │ └── Main.jack │ ├── ScreenTest │ │ ├── ScreenTestOutput.gif │ │ └── Main.jack │ ├── StringTest │ │ └── StringTestOutput.gif │ ├── KeyboardTest │ │ ├── KeyboardTestOutput.gif │ │ └── Keyboard.vm │ ├── MemoryTest │ │ ├── MemoryTest.cmp │ │ ├── MemoryTest.out │ │ ├── MemoryTest.tst │ │ └── Main.jack │ ├── MathTest │ │ ├── MathTest.cmp │ │ ├── MathTest.out │ │ ├── MathTest.tst │ │ └── Main.jack │ ├── Array.jack │ ├── SysTest │ │ ├── Main.jack │ │ └── Sys.vm │ └── Sys.jack ├── 07 │ ├── .idea │ │ ├── .name │ │ ├── copyright │ │ │ └── profiles_settings.xml │ │ ├── scopes │ │ │ └── scope_settings.xml │ │ ├── encodings.xml │ │ ├── vcs.xml │ │ ├── modules.xml │ │ ├── compiler.xml │ │ └── misc.xml │ ├── MemoryAccess │ │ ├── StaticTest │ │ │ ├── StaticTest.cmp │ │ │ ├── StaticTest.out │ │ │ ├── StaticTest.tst │ │ │ ├── StaticTestVME.tst │ │ │ ├── StaticTest.vm │ │ │ └── StaticTest.asm │ │ ├── PointerTest │ │ │ ├── PointerTest.cmp │ │ │ ├── PointerTest.out │ │ │ ├── PointerTest.tst │ │ │ ├── PointerTestVME.tst │ │ │ ├── PointerTest.vm │ │ │ └── PointerTest.asm │ │ └── BasicTest │ │ │ ├── BasicTest.cmp │ │ │ ├── BasicTest.out │ │ │ ├── BasicTestVME.tst │ │ │ ├── BasicTest.tst │ │ │ └── BasicTest.vm │ ├── StackArithmetic │ │ ├── SimpleAdd │ │ │ ├── SimpleAdd.out │ │ │ ├── SimpleAdd.cmp │ │ │ ├── SimpleAdd.asm │ │ │ ├── SimpleAdd.vm │ │ │ ├── SimpleAdd.tst │ │ │ └── SimpleAddVME.tst │ │ └── StackTest │ │ │ ├── StackTest.out │ │ │ ├── StackTest.cmp │ │ │ ├── StackTest.tst │ │ │ ├── StackTestVME.tst │ │ │ └── StackTest.vm │ └── README.txt ├── 01 │ ├── Not.cmp │ ├── Not.out │ ├── Or8Way.out │ ├── Or.out │ ├── And.out │ ├── Or.cmp │ ├── Xor.out │ ├── And.cmp │ ├── Or8Way.cmp │ ├── Xor.cmp │ ├── DMux.out │ ├── DMux.cmp │ ├── Not16.out │ ├── Not16.cmp │ ├── Mux.out │ ├── Mux.cmp │ ├── Not.hdl │ ├── DMux4Way.out │ ├── DMux4Way.cmp │ ├── Not.tst │ ├── And16.out │ ├── Or16.out │ ├── Or16.cmp │ ├── And16.cmp │ ├── Or.hdl │ ├── And.hdl │ ├── Xor.hdl │ ├── Mux.hdl │ ├── DMux.hdl │ ├── DMux.tst │ ├── Or.tst │ ├── And.tst │ ├── Xor.tst │ ├── Or8Way.tst │ ├── Mux16.out │ ├── Or8Way.hdl │ ├── Mux16.cmp │ ├── Mux4Way16.hdl │ ├── Not16.tst │ ├── DMux4Way.hdl │ ├── Mux8Way16.hdl │ ├── DMux8Way.hdl │ ├── DMux4Way.tst │ ├── Mux.tst │ ├── Or16.tst │ ├── And16.tst │ ├── Mux16.tst │ ├── Not16.hdl │ ├── Mux4Way16.tst │ ├── Mux4Way16.out │ ├── Mux4Way16.cmp │ ├── Or16.hdl │ ├── And16.hdl │ ├── DMux8Way.out │ ├── DMux8Way.cmp │ ├── Mux16.hdl │ ├── DMux8Way.tst │ └── Mux8Way16.tst ├── 08 │ ├── FunctionCalls │ │ ├── FibonacciElement │ │ │ ├── FibonacciElement.out │ │ │ ├── FibonacciElement.cmp │ │ │ ├── FibonacciElementVME.tst │ │ │ ├── FibonacciElement.tst │ │ │ ├── Sys.vm │ │ │ └── Main.vm │ │ ├── StaticsTest │ │ │ ├── StaticsTest.cmp │ │ │ ├── StaticsTest.out │ │ │ ├── StaticsTest.tst │ │ │ ├── StaticsTestVME.tst │ │ │ ├── Class1.vm │ │ │ ├── Class2.vm │ │ │ └── Sys.vm │ │ └── SimpleFunction │ │ │ ├── SimpleFunction.cmp │ │ │ ├── SimpleFunction.out │ │ │ ├── SimpleFunction.vm │ │ │ ├── SimpleFunction.tst │ │ │ ├── SimpleFunctionVME.tst │ │ │ └── SimpleFunction.asm │ ├── ProgramFlow │ │ ├── BasicLoop │ │ │ ├── BasicLoop.cmp │ │ │ ├── BasicLoop.out │ │ │ ├── BasicLoop.tst │ │ │ ├── BasicLoopVME.tst │ │ │ ├── BasicLoop.vm │ │ │ └── BasicLoop.asm │ │ └── FibonacciSeries │ │ │ ├── FibonacciSeries.out │ │ │ ├── FibonacciSeries.cmp │ │ │ ├── FibonacciSeries.tst │ │ │ ├── FibonacciSeriesVME.tst │ │ │ └── FibonacciSeries.vm │ └── README.txt ├── 09 │ ├── Jack OS API.pdf │ ├── HelloWorld │ │ ├── Main.jack │ │ └── Main.vm │ ├── Square │ │ ├── Main.vm │ │ └── Main.jack │ ├── PigDice │ │ ├── Player.jack │ │ ├── Player.vm │ │ └── Random.jack │ └── Readme.txt ├── 05 │ ├── Add.hack │ ├── Max.hack │ ├── Rect.hack │ ├── ComputerAdd-external.out │ ├── ComputerAdd-external.cmp │ ├── ComputerRect-external.out │ ├── ComputerRect-external.cmp │ ├── ComputerAdd-external.tst │ ├── ComputerAdd.tst │ ├── ComputerRect-external.tst │ ├── ComputerRect.tst │ ├── Computer.hdl │ ├── ComputerAdd.out │ ├── ComputerAdd.cmp │ ├── ComputerMax-external.tst │ ├── ComputerMax.tst │ ├── ComputerMax-external.out │ ├── ComputerMax-external.cmp │ ├── ComputerMax.out │ ├── ComputerMax.cmp │ └── Memory.hdl ├── 00 │ ├── And.cmp │ ├── Xor.cmp │ ├── And.tst │ ├── Xor.hdl │ ├── Xor.tst │ ├── Register.cmp │ ├── RAM8.cmp │ ├── Mux8Way16.cmp │ ├── Register.tst │ ├── Mux8Way16.tst │ └── RAM8.tst ├── 02 │ ├── HalfAdder.out │ ├── HalfAdder.cmp │ ├── Inc16.out │ ├── Inc16.cmp │ ├── FullAdder.out │ ├── FullAdder.cmp │ ├── Inc16.hdl │ ├── Add16.out │ ├── Add16.cmp │ ├── HalfAdder.hdl │ ├── FullAdder.hdl │ ├── HalfAdder.tst │ ├── Inc16.tst │ ├── FullAdder.tst │ ├── Add16.tst │ └── Add16.hdl ├── 04 │ ├── ex.asm │ ├── mult │ │ ├── Mult.out │ │ ├── Mult.cmp │ │ ├── Mult.hack │ │ ├── Mult.asm │ │ └── Mult.tst │ ├── fill │ │ ├── Fill.tst │ │ ├── Fill.hack │ │ └── Fill.asm │ └── ex.hack ├── 06 │ ├── add │ │ └── Add.asm │ ├── max │ │ ├── MaxL.asm │ │ └── Max.asm │ ├── rect │ │ ├── RectL.asm │ │ └── Rect.asm │ ├── README.txt │ └── src │ │ └── FileHelper.java └── 03 │ ├── Bit.hdl │ ├── a │ ├── Bit.hdl │ ├── PC.hdl │ ├── Register.hdl │ ├── RAM8.hdl │ ├── RAM64.hdl │ ├── PC.out │ ├── PC.cmp │ └── PC.tst │ ├── PC.hdl │ ├── RAM16K.hdl │ ├── b │ ├── RAM16K.hdl │ ├── RAM4K.hdl │ └── RAM512.hdl │ ├── Register.hdl │ ├── RAM8.hdl │ ├── RAM64.hdl │ ├── RAM4K.hdl │ └── RAM512.hdl └── README.md /projects/07/.idea/.name: -------------------------------------------------------------------------------- 1 | VMtranslator -------------------------------------------------------------------------------- /projects/10/Complier/.idea/.name: -------------------------------------------------------------------------------- 1 | Complier -------------------------------------------------------------------------------- /projects/07/MemoryAccess/StaticTest/StaticTest.cmp: -------------------------------------------------------------------------------- 1 | |RAM[256]| 2 | | 1110 | 3 | -------------------------------------------------------------------------------- /projects/07/MemoryAccess/StaticTest/StaticTest.out: -------------------------------------------------------------------------------- 1 | |RAM[256]| 2 | | 1110 | 3 | -------------------------------------------------------------------------------- /projects/01/Not.cmp: -------------------------------------------------------------------------------- 1 | | in | out | 2 | | 0 | 1 | 3 | | 1 | 0 | 4 | -------------------------------------------------------------------------------- /projects/01/Not.out: -------------------------------------------------------------------------------- 1 | | in | out | 2 | | 0 | 1 | 3 | | 1 | 0 | 4 | -------------------------------------------------------------------------------- /projects/08/FunctionCalls/FibonacciElement/FibonacciElement.out: -------------------------------------------------------------------------------- 1 | | RAM[0] |RAM[261]| 2 | -------------------------------------------------------------------------------- /projects/08/ProgramFlow/BasicLoop/BasicLoop.cmp: -------------------------------------------------------------------------------- 1 | | RAM[0] |RAM[256]| 2 | | 257 | 6 | 3 | -------------------------------------------------------------------------------- /projects/08/ProgramFlow/BasicLoop/BasicLoop.out: -------------------------------------------------------------------------------- 1 | | RAM[0] |RAM[256]| 2 | | 257 | 6 | 3 | -------------------------------------------------------------------------------- /projects/07/StackArithmetic/SimpleAdd/SimpleAdd.out: -------------------------------------------------------------------------------- 1 | | RAM[0] | RAM[256] | 2 | | 257 | 15 | 3 | -------------------------------------------------------------------------------- /projects/09/Jack OS API.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xctom/Nand2Tetris/HEAD/projects/09/Jack OS API.pdf -------------------------------------------------------------------------------- /projects/07/StackArithmetic/SimpleAdd/SimpleAdd.cmp: -------------------------------------------------------------------------------- 1 | | RAM[0] | RAM[256] | 2 | | 257 | 15 | 3 | -------------------------------------------------------------------------------- /projects/08/FunctionCalls/FibonacciElement/FibonacciElement.cmp: -------------------------------------------------------------------------------- 1 | | RAM[0] |RAM[261]| 2 | | 262 | 3 | 3 | -------------------------------------------------------------------------------- /projects/07/StackArithmetic/StackTest/StackTest.out: -------------------------------------------------------------------------------- 1 | | RAM[0] | RAM[256] | RAM[257] | RAM[258] | RAM[259] | RAM[260] | 2 | -------------------------------------------------------------------------------- /projects/08/FunctionCalls/StaticsTest/StaticsTest.cmp: -------------------------------------------------------------------------------- 1 | | RAM[0] |RAM[261]|RAM[262]| 2 | | 263 | -2 | 8 | 3 | -------------------------------------------------------------------------------- /projects/08/FunctionCalls/StaticsTest/StaticsTest.out: -------------------------------------------------------------------------------- 1 | | RAM[0] |RAM[261]|RAM[262]| 2 | | 263 | -2 | 8 | 3 | -------------------------------------------------------------------------------- /projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries.out: -------------------------------------------------------------------------------- 1 | |RAM[3000]|RAM[3001]|RAM[3002]|RAM[3003]|RAM[3004]|RAM[3005]| 2 | -------------------------------------------------------------------------------- /projects/12/ArrayTest/ArrayTest.out: -------------------------------------------------------------------------------- 1 | |RAM[8000]|RAM[8001]|RAM[8002]|RAM[8003]| 2 | | 222 | 122 | 100 | 10 | 3 | -------------------------------------------------------------------------------- /projects/07/.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /projects/12/ArrayTest/ArrayTest.cmp: -------------------------------------------------------------------------------- 1 | |RAM[8000]|RAM[8001]|RAM[8002]|RAM[8003]| 2 | | 222 | 122 | 100 | 10 | 3 | -------------------------------------------------------------------------------- /projects/10/Complier/.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /projects/12/OutputTest/OutputTestOutput.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xctom/Nand2Tetris/HEAD/projects/12/OutputTest/OutputTestOutput.gif -------------------------------------------------------------------------------- /projects/12/ScreenTest/ScreenTestOutput.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xctom/Nand2Tetris/HEAD/projects/12/ScreenTest/ScreenTestOutput.gif -------------------------------------------------------------------------------- /projects/12/StringTest/StringTestOutput.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xctom/Nand2Tetris/HEAD/projects/12/StringTest/StringTestOutput.gif -------------------------------------------------------------------------------- /projects/01/Or8Way.out: -------------------------------------------------------------------------------- 1 | | in | out | 2 | | 00000000 | 0 | 3 | | 11111111 | 1 | 4 | | 00010000 | 1 | 5 | | 00000001 | 0 | 6 | -------------------------------------------------------------------------------- /projects/12/KeyboardTest/KeyboardTestOutput.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xctom/Nand2Tetris/HEAD/projects/12/KeyboardTest/KeyboardTestOutput.gif -------------------------------------------------------------------------------- /projects/05/Add.hack: -------------------------------------------------------------------------------- 1 | 0000000000000010 2 | 1110110000010000 3 | 0000000000000011 4 | 1110000010010000 5 | 0000000000000000 6 | 1110001100001000 7 | -------------------------------------------------------------------------------- /projects/07/MemoryAccess/PointerTest/PointerTest.cmp: -------------------------------------------------------------------------------- 1 | |RAM[256]| RAM[3] | RAM[4] |RAM[3032|RAM[3046| 2 | | 6084 | 3030 | 3040 | 32 | 46 | 3 | -------------------------------------------------------------------------------- /projects/07/MemoryAccess/PointerTest/PointerTest.out: -------------------------------------------------------------------------------- 1 | |RAM[256]| RAM[3] | RAM[4] |RAM[3032|RAM[3046| 2 | | 6084 | 3030 | 3040 | 32 | 46 | 3 | -------------------------------------------------------------------------------- /projects/01/Or.out: -------------------------------------------------------------------------------- 1 | | a | b | out | 2 | | 0 | 0 | 0 | 3 | | 0 | 1 | 1 | 4 | | 1 | 0 | 1 | 5 | | 1 | 1 | 1 | 6 | -------------------------------------------------------------------------------- /projects/09/HelloWorld/Main.jack: -------------------------------------------------------------------------------- 1 | class Main{ 2 | function void main(){ 3 | do Output.printString("Hello World"); 4 | do Output.println(); 5 | return; 6 | } 7 | } -------------------------------------------------------------------------------- /projects/01/And.out: -------------------------------------------------------------------------------- 1 | | a | b | out | 2 | | 0 | 0 | 0 | 3 | | 0 | 1 | 0 | 4 | | 1 | 0 | 0 | 5 | | 1 | 1 | 1 | 6 | -------------------------------------------------------------------------------- /projects/01/Or.cmp: -------------------------------------------------------------------------------- 1 | | a | b | out | 2 | | 0 | 0 | 0 | 3 | | 0 | 1 | 1 | 4 | | 1 | 0 | 1 | 5 | | 1 | 1 | 1 | 6 | -------------------------------------------------------------------------------- /projects/01/Xor.out: -------------------------------------------------------------------------------- 1 | | a | b | out | 2 | | 0 | 0 | 0 | 3 | | 0 | 1 | 1 | 4 | | 1 | 0 | 1 | 5 | | 1 | 1 | 0 | 6 | -------------------------------------------------------------------------------- /projects/12/MemoryTest/MemoryTest.cmp: -------------------------------------------------------------------------------- 1 | |RAM[8000]|RAM[8001]|RAM[8002]|RAM[8003]|RAM[8004]|RAM[8005]| 2 | | 333 | 334 | 222 | 122 | 100 | 10 | 3 | -------------------------------------------------------------------------------- /projects/12/MemoryTest/MemoryTest.out: -------------------------------------------------------------------------------- 1 | |RAM[8000]|RAM[8001]|RAM[8002]|RAM[8003]|RAM[8004]|RAM[8005]| 2 | | 333 | 334 | 222 | 122 | 100 | 10 | 3 | -------------------------------------------------------------------------------- /projects/00/And.cmp: -------------------------------------------------------------------------------- 1 | | a | b | out | 2 | | 0 | 0 | 0 | 3 | | 0 | 1 | 0 | 4 | | 1 | 0 | 0 | 5 | | 1 | 1 | 1 | 6 | -------------------------------------------------------------------------------- /projects/00/Xor.cmp: -------------------------------------------------------------------------------- 1 | | a | b | out | 2 | | 0 | 0 | 0 | 3 | | 0 | 1 | 1 | 4 | | 1 | 0 | 1 | 5 | | 1 | 1 | 0 | 6 | -------------------------------------------------------------------------------- /projects/01/And.cmp: -------------------------------------------------------------------------------- 1 | | a | b | out | 2 | | 0 | 0 | 0 | 3 | | 0 | 1 | 0 | 4 | | 1 | 0 | 0 | 5 | | 1 | 1 | 1 | 6 | -------------------------------------------------------------------------------- /projects/01/Or8Way.cmp: -------------------------------------------------------------------------------- 1 | | in | out | 2 | | 00000000 | 0 | 3 | | 11111111 | 1 | 4 | | 00010000 | 1 | 5 | | 00000001 | 1 | 6 | | 00100110 | 1 | 7 | -------------------------------------------------------------------------------- /projects/01/Xor.cmp: -------------------------------------------------------------------------------- 1 | | a | b | out | 2 | | 0 | 0 | 0 | 3 | | 0 | 1 | 1 | 4 | | 1 | 0 | 1 | 5 | | 1 | 1 | 0 | 6 | -------------------------------------------------------------------------------- /projects/08/FunctionCalls/SimpleFunction/SimpleFunction.cmp: -------------------------------------------------------------------------------- 1 | | RAM[0] | RAM[1] | RAM[2] | RAM[3] | RAM[4] |RAM[310]| 2 | | 311 | 305 | 300 | 3010 | 4010 | 1196 | 3 | -------------------------------------------------------------------------------- /projects/08/FunctionCalls/SimpleFunction/SimpleFunction.out: -------------------------------------------------------------------------------- 1 | | RAM[0] | RAM[1] | RAM[2] | RAM[3] | RAM[4] |RAM[310]| 2 | | 289 | 285 | 280 | 3000 | 4000 | 1234 | 3 | -------------------------------------------------------------------------------- /projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries.cmp: -------------------------------------------------------------------------------- 1 | |RAM[3000]|RAM[3001]|RAM[3002]|RAM[3003]|RAM[3004]|RAM[3005]| 2 | | 0 | 1 | 1 | 2 | 3 | 5 | 3 | -------------------------------------------------------------------------------- /projects/07/.idea/scopes/scope_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /projects/07/MemoryAccess/BasicTest/BasicTest.cmp: -------------------------------------------------------------------------------- 1 | |RAM[256]|RAM[300]|RAM[401]|RAM[402]|RAM[3006|RAM[3012|RAM[3015|RAM[11] | 2 | | 472 | 10 | 21 | 22 | 36 | 42 | 45 | 510 | 3 | -------------------------------------------------------------------------------- /projects/07/MemoryAccess/BasicTest/BasicTest.out: -------------------------------------------------------------------------------- 1 | |RAM[256]|RAM[300]|RAM[401]|RAM[402]|RAM[3006|RAM[3012|RAM[3015|RAM[11] | 2 | | 472 | 10 | 21 | 22 | 36 | 42 | 45 | 510 | 3 | -------------------------------------------------------------------------------- /projects/10/Complier/.idea/scopes/scope_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /projects/01/DMux.out: -------------------------------------------------------------------------------- 1 | | in | sel | a | b | 2 | | 0 | 0 | 0 | 0 | 3 | | 0 | 1 | 0 | 0 | 4 | | 1 | 0 | 1 | 0 | 5 | | 1 | 1 | 0 | 1 | 6 | -------------------------------------------------------------------------------- /projects/10/Complier/out/production/Complier/SyntaxAnalyzer/JackAnalyzer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xctom/Nand2Tetris/HEAD/projects/10/Complier/out/production/Complier/SyntaxAnalyzer/JackAnalyzer.class -------------------------------------------------------------------------------- /projects/10/Complier/out/production/Complier/SyntaxAnalyzer/JackTokenizer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xctom/Nand2Tetris/HEAD/projects/10/Complier/out/production/Complier/SyntaxAnalyzer/JackTokenizer.class -------------------------------------------------------------------------------- /projects/01/DMux.cmp: -------------------------------------------------------------------------------- 1 | | in | sel | a | b | 2 | | 0 | 0 | 0 | 0 | 3 | | 0 | 1 | 0 | 0 | 4 | | 1 | 0 | 1 | 0 | 5 | | 1 | 1 | 0 | 1 | 6 | -------------------------------------------------------------------------------- /projects/02/HalfAdder.out: -------------------------------------------------------------------------------- 1 | | a | b | sum | carry | 2 | | 0 | 0 | 0 | 0 | 3 | | 0 | 1 | 1 | 0 | 4 | | 1 | 0 | 1 | 0 | 5 | | 1 | 1 | 0 | 1 | 6 | -------------------------------------------------------------------------------- /projects/07/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /projects/02/HalfAdder.cmp: -------------------------------------------------------------------------------- 1 | | a | b | sum | carry | 2 | | 0 | 0 | 0 | 0 | 3 | | 0 | 1 | 1 | 0 | 4 | | 1 | 0 | 1 | 0 | 5 | | 1 | 1 | 0 | 1 | 6 | -------------------------------------------------------------------------------- /projects/07/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /projects/10/Complier/out/production/Complier/SyntaxAnalyzer/CompilationEngine.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xctom/Nand2Tetris/HEAD/projects/10/Complier/out/production/Complier/SyntaxAnalyzer/CompilationEngine.class -------------------------------------------------------------------------------- /projects/10/Complier/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /projects/10/Complier/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /projects/07/StackArithmetic/SimpleAdd/SimpleAdd.asm: -------------------------------------------------------------------------------- 1 | @7 2 | D=A 3 | @SP 4 | A=M 5 | M=D 6 | @SP 7 | M=M+1 8 | @8 9 | D=A 10 | @SP 11 | A=M 12 | M=D 13 | @SP 14 | M=M+1 15 | @SP 16 | AM=M-1 17 | D=M 18 | A=A-1 19 | M=M+D 20 | -------------------------------------------------------------------------------- /projects/02/Inc16.out: -------------------------------------------------------------------------------- 1 | | in | out | 2 | | 0000000000000000 | 0000000000000001 | 3 | | 1111111111111111 | 0000000000000000 | 4 | | 0000000000000101 | 0000000000000110 | 5 | | 1111111111111011 | 1111111111111100 | 6 | -------------------------------------------------------------------------------- /projects/02/Inc16.cmp: -------------------------------------------------------------------------------- 1 | | in | out | 2 | | 0000000000000000 | 0000000000000001 | 3 | | 1111111111111111 | 0000000000000000 | 4 | | 0000000000000101 | 0000000000000110 | 5 | | 1111111111111011 | 1111111111111100 | 6 | -------------------------------------------------------------------------------- /projects/04/ex.asm: -------------------------------------------------------------------------------- 1 | //Adds 1+...+100 2 | @i 3 | M=1 4 | @sum 5 | M=0 6 | (LOOP) 7 | @i 8 | D=M 9 | @100 10 | D=D-A 11 | @END 12 | D;JGT 13 | @i 14 | D=M 15 | @sum 16 | M=D+M 17 | @i 18 | M=M+1 19 | @LOOP 20 | 0;JMP 21 | (END) 22 | @END 23 | 0;JMP -------------------------------------------------------------------------------- /projects/09/Square/Main.vm: -------------------------------------------------------------------------------- 1 | function Main.main 1 2 | call SquareGame.new 0 3 | pop local 0 4 | push local 0 5 | call SquareGame.run 1 6 | pop temp 0 7 | push local 0 8 | call SquareGame.dispose 1 9 | pop temp 0 10 | push constant 0 11 | return 12 | -------------------------------------------------------------------------------- /projects/11/Square/Main.vm: -------------------------------------------------------------------------------- 1 | function Main.main 1 2 | call SquareGame.new 0 3 | pop local 0 4 | push local 0 5 | call SquareGame.run 1 6 | pop temp 0 7 | push local 0 8 | call SquareGame.dispose 1 9 | pop temp 0 10 | push constant 0 11 | return 12 | -------------------------------------------------------------------------------- /projects/11/Square/Main_1.vml: -------------------------------------------------------------------------------- 1 | function Main.main 1 2 | call SquareGame.new 0 3 | pop local 0 4 | push local 0 5 | call SquareGame.run 1 6 | pop temp 0 7 | push local 0 8 | call SquareGame.dispose 1 9 | pop temp 0 10 | push constant 0 11 | return 12 | -------------------------------------------------------------------------------- /projects/12/ArrayTest/Array.vm: -------------------------------------------------------------------------------- 1 | function Array.new 0 2 | push argument 0 3 | call Memory.alloc 1 4 | return 5 | function Array.dispose 0 6 | push argument 0 7 | pop pointer 0 8 | push pointer 0 9 | call Memory.deAlloc 1 10 | pop temp 0 11 | push constant 0 12 | return 13 | -------------------------------------------------------------------------------- /projects/01/Not16.out: -------------------------------------------------------------------------------- 1 | | in | out | 2 | | 0000000000000000 | 1111111111111111 | 3 | | 1111111111111111 | 0000000000000000 | 4 | | 1010101010101010 | 0101010101010101 | 5 | | 0011110011000011 | 1100001100111100 | 6 | | 0001001000110100 | 1110110111001011 | 7 | -------------------------------------------------------------------------------- /projects/01/Not16.cmp: -------------------------------------------------------------------------------- 1 | | in | out | 2 | | 0000000000000000 | 1111111111111111 | 3 | | 1111111111111111 | 0000000000000000 | 4 | | 1010101010101010 | 0101010101010101 | 5 | | 0011110011000011 | 1100001100111100 | 6 | | 0001001000110100 | 1110110111001011 | 7 | -------------------------------------------------------------------------------- /projects/04/mult/Mult.out: -------------------------------------------------------------------------------- 1 | | RAM[0] | RAM[1] | RAM[2] | 2 | | 0 | 0 | 0 | 3 | | 1 | 0 | 0 | 4 | | 0 | 2 | 0 | 5 | | 3 | 1 | 3 | 6 | | 2 | 4 | 8 | 7 | | 6 | 7 | 42 | 8 | -------------------------------------------------------------------------------- /projects/04/mult/Mult.cmp: -------------------------------------------------------------------------------- 1 | | RAM[0] | RAM[1] | RAM[2] | 2 | | 0 | 0 | 0 | 3 | | 1 | 0 | 0 | 4 | | 0 | 2 | 0 | 5 | | 3 | 1 | 3 | 6 | | 2 | 4 | 8 | 7 | | 6 | 7 | 42 | -------------------------------------------------------------------------------- /projects/11/Pong/Main.vm: -------------------------------------------------------------------------------- 1 | function Main.main 1 2 | call PongGame.newInstance 0 3 | pop temp 0 4 | call PongGame.getInstance 0 5 | pop local 0 6 | push local 0 7 | call PongGame.run 1 8 | pop temp 0 9 | push local 0 10 | call PongGame.dispose 1 11 | pop temp 0 12 | push constant 0 13 | return 14 | -------------------------------------------------------------------------------- /projects/11/Pong/Main.vml: -------------------------------------------------------------------------------- 1 | function Main.main 1 2 | call PongGame.newInstance 0 3 | pop temp 0 4 | call PongGame.getInstance 0 5 | pop local 0 6 | push local 0 7 | call PongGame.run 1 8 | pop temp 0 9 | push local 0 10 | call PongGame.dispose 1 11 | pop temp 0 12 | push constant 0 13 | return 14 | -------------------------------------------------------------------------------- /projects/06/add/Add.asm: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/06/add/Add.asm 5 | 6 | // Computes R0 = 2 + 3 7 | 8 | @2 9 | D=A 10 | @3 11 | D=D+A 12 | @0 13 | M=D 14 | -------------------------------------------------------------------------------- /projects/07/StackArithmetic/StackTest/StackTest.cmp: -------------------------------------------------------------------------------- 1 | | RAM[0] | RAM[256] | RAM[257] | RAM[258] | RAM[259] | RAM[260] | 2 | | 266 | -1 | 0 | 0 | 0 | -1 | 3 | | RAM[261] | RAM[262] | RAM[263] | RAM[264] | RAM[265] | 4 | | 0 | -1 | 0 | 0 | -91 | 5 | -------------------------------------------------------------------------------- /projects/12/MathTest/MathTest.cmp: -------------------------------------------------------------------------------- 1 | |RAM[8000]|RAM[8001]|RAM[8002]|RAM[8003]|RAM[8004]|RAM[8005]|RAM[8006]|RAM[8007]|RAM[8008]|RAM[8009]|RAM[8010]|RAM[8011]|RAM[8012]|RAM[8013]| 2 | | 6 | -180 | -18000 | -18000 | 0 | 3 | -3000 | 0 | 3 | 181 | 123 | 123 | 27 | 32767 | 3 | -------------------------------------------------------------------------------- /projects/12/MathTest/MathTest.out: -------------------------------------------------------------------------------- 1 | |RAM[8000]|RAM[8001]|RAM[8002]|RAM[8003]|RAM[8004]|RAM[8005]|RAM[8006]|RAM[8007]|RAM[8008]|RAM[8009]|RAM[8010]|RAM[8011]|RAM[8012]|RAM[8013]| 2 | | 6 | -180 | -18000 | -18000 | 0 | 3 | -3000 | 0 | 3 | 181 | 123 | 123 | 27 | 32767 | 3 | -------------------------------------------------------------------------------- /projects/07/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /projects/10/Complier/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /projects/01/Mux.out: -------------------------------------------------------------------------------- 1 | | a | b | sel | out | 2 | | 0 | 0 | 0 | 0 | 3 | | 0 | 0 | 1 | 0 | 4 | | 0 | 1 | 0 | 0 | 5 | | 0 | 1 | 1 | 1 | 6 | | 1 | 0 | 0 | 1 | 7 | | 1 | 0 | 1 | 0 | 8 | | 1 | 1 | 0 | 1 | 9 | | 1 | 1 | 1 | 1 | 10 | -------------------------------------------------------------------------------- /projects/07/StackArithmetic/SimpleAdd/SimpleAdd.vm: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/07/StackArithmetic/SimpleAdd/SimpleAdd.vm 5 | 6 | // Pushes and adds two constants. 7 | push constant 7 8 | push constant 8 9 | add 10 | -------------------------------------------------------------------------------- /projects/01/Mux.cmp: -------------------------------------------------------------------------------- 1 | | a | b | sel | out | 2 | | 0 | 0 | 0 | 0 | 3 | | 0 | 0 | 1 | 0 | 4 | | 0 | 1 | 0 | 0 | 5 | | 0 | 1 | 1 | 1 | 6 | | 1 | 0 | 0 | 1 | 7 | | 1 | 0 | 1 | 0 | 8 | | 1 | 1 | 0 | 1 | 9 | | 1 | 1 | 1 | 1 | 10 | -------------------------------------------------------------------------------- /projects/01/Not.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/Not.hdl 5 | 6 | /** 7 | * Not gate: out = not in 8 | */ 9 | 10 | CHIP Not { 11 | IN in; 12 | OUT out; 13 | 14 | PARTS: 15 | Nand(a=in,b=in,out=out); 16 | } -------------------------------------------------------------------------------- /projects/05/Max.hack: -------------------------------------------------------------------------------- 1 | 0000000000000000 2 | 1111110000010000 3 | 0000000000000001 4 | 1111010011010000 5 | 0000000000001010 6 | 1110001100000001 7 | 0000000000000001 8 | 1111110000010000 9 | 0000000000001100 10 | 1110101010000111 11 | 0000000000000000 12 | 1111110000010000 13 | 0000000000000010 14 | 1110001100001000 15 | 0000000000001110 16 | 1110101010000111 17 | -------------------------------------------------------------------------------- /projects/01/DMux4Way.out: -------------------------------------------------------------------------------- 1 | | in | sel | a | b | c | d | 2 | | 0 | 00 | 0 | 0 | 0 | 0 | 3 | | 0 | 01 | 0 | 0 | 0 | 0 | 4 | | 0 | 10 | 0 | 0 | 0 | 0 | 5 | | 0 | 11 | 0 | 0 | 0 | 0 | 6 | | 1 | 00 | 1 | 0 | 0 | 0 | 7 | | 1 | 01 | 0 | 1 | 0 | 0 | 8 | | 1 | 10 | 0 | 0 | 1 | 0 | 9 | | 1 | 11 | 0 | 0 | 0 | 1 | 10 | -------------------------------------------------------------------------------- /projects/01/DMux4Way.cmp: -------------------------------------------------------------------------------- 1 | | in | sel | a | b | c | d | 2 | | 0 | 00 | 0 | 0 | 0 | 0 | 3 | | 0 | 01 | 0 | 0 | 0 | 0 | 4 | | 0 | 10 | 0 | 0 | 0 | 0 | 5 | | 0 | 11 | 0 | 0 | 0 | 0 | 6 | | 1 | 00 | 1 | 0 | 0 | 0 | 7 | | 1 | 01 | 0 | 1 | 0 | 0 | 8 | | 1 | 10 | 0 | 0 | 1 | 0 | 9 | | 1 | 11 | 0 | 0 | 0 | 1 | 10 | -------------------------------------------------------------------------------- /projects/01/Not.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/Not.tst 5 | 6 | load Not.hdl, 7 | output-file Not.out, 8 | compare-to Not.cmp, 9 | output-list in%B3.1.3 out%B3.1.3; 10 | 11 | set in 0, 12 | eval, 13 | output; 14 | 15 | set in 1, 16 | eval, 17 | output; 18 | -------------------------------------------------------------------------------- /projects/04/fill/Fill.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/04/fill/Fill.tst 5 | 6 | load Fill.hack; 7 | echo "First, make sure that 'No Animation' is selected. Then, select the Keyboard, click on any key, and check the screen."; 8 | 9 | repeat { 10 | ticktock; 11 | } 12 | -------------------------------------------------------------------------------- /projects/02/FullAdder.out: -------------------------------------------------------------------------------- 1 | | a | b | c | sum | carry | 2 | | 0 | 0 | 0 | 0 | 0 | 3 | | 0 | 0 | 1 | 1 | 0 | 4 | | 0 | 1 | 0 | 1 | 0 | 5 | | 0 | 1 | 1 | 0 | 1 | 6 | | 1 | 0 | 0 | 1 | 0 | 7 | | 1 | 0 | 1 | 0 | 1 | 8 | | 1 | 1 | 0 | 0 | 1 | 9 | | 1 | 1 | 1 | 1 | 1 | 10 | -------------------------------------------------------------------------------- /projects/06/max/MaxL.asm: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/06/max/MaxL.asm 5 | 6 | // Symbol-less version of the Max.asm program. 7 | 8 | @0 9 | D=M 10 | @1 11 | D=D-M 12 | @10 13 | D;JGT 14 | @1 15 | D=M 16 | @12 17 | 0;JMP 18 | @0 19 | D=M 20 | @2 21 | M=D 22 | @14 23 | 0;JMP 24 | -------------------------------------------------------------------------------- /projects/02/FullAdder.cmp: -------------------------------------------------------------------------------- 1 | | a | b | c | sum | carry | 2 | | 0 | 0 | 0 | 0 | 0 | 3 | | 0 | 0 | 1 | 1 | 0 | 4 | | 0 | 1 | 0 | 1 | 0 | 5 | | 0 | 1 | 1 | 0 | 1 | 6 | | 1 | 0 | 0 | 1 | 0 | 7 | | 1 | 0 | 1 | 0 | 1 | 8 | | 1 | 1 | 0 | 0 | 1 | 9 | | 1 | 1 | 1 | 1 | 1 | 10 | -------------------------------------------------------------------------------- /projects/02/Inc16.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/02/Inc16.hdl 5 | 6 | /** 7 | * 16-bit incrementer: out = in + 1 (arithmetic addition) 8 | */ 9 | 10 | CHIP Inc16 { 11 | IN in[16]; 12 | OUT out[16]; 13 | 14 | PARTS: 15 | Add16(a=in,b[0]=true,b[1..15]=false,out=out); 16 | } -------------------------------------------------------------------------------- /projects/01/And16.out: -------------------------------------------------------------------------------- 1 | | a | b | out | 2 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 3 | | 0000000000000000 | 1111111111111111 | 0000000000000000 | 4 | | 1111111111111111 | 1111111111111111 | 1111111111111111 | 5 | | 1010101010101010 | 0101010101010101 | 0000000000000000 | 6 | | 0011110011000011 | 0000111111110000 | 0000110011000000 | 7 | | 0001001000110100 | 1001100001110110 | 0001000000110100 | 8 | -------------------------------------------------------------------------------- /projects/01/Or16.out: -------------------------------------------------------------------------------- 1 | | a | b | out | 2 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 3 | | 0000000000000000 | 1111111111111111 | 1111111111111111 | 4 | | 1111111111111111 | 1111111111111111 | 1111111111111111 | 5 | | 1010101010101010 | 0101010101010101 | 1111111111111111 | 6 | | 0011110011000011 | 0000111111110000 | 0011111111110011 | 7 | | 0001001000110100 | 1001100001110110 | 1001101001110110 | 8 | -------------------------------------------------------------------------------- /projects/02/Add16.out: -------------------------------------------------------------------------------- 1 | | a | b | out | 2 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 3 | | 0000000000000000 | 1111111111111111 | 1111111111111111 | 4 | | 1111111111111111 | 1111111111111111 | 1111111111111110 | 5 | | 1010101010101010 | 0101010101010101 | 1111111111111111 | 6 | | 0011110011000011 | 0000111111110000 | 0100110010110011 | 7 | | 0001001000110100 | 1001100001110110 | 1010101010101010 | 8 | -------------------------------------------------------------------------------- /projects/04/ex.hack: -------------------------------------------------------------------------------- 1 | 0000000000010000 2 | 1110111111001000 3 | 0000000000010001 4 | 1110101010001000 5 | 0000000000010000 6 | 1111110000010000 7 | 0000000001100100 8 | 1110010011010000 9 | 0000000000010010 10 | 1110001100000001 11 | 0000000000010000 12 | 1111110000010000 13 | 0000000000010001 14 | 1111000010001000 15 | 0000000000010000 16 | 1111110111001000 17 | 0000000000000100 18 | 1110101010000111 19 | 0000000000010010 20 | 1110101010000111 21 | -------------------------------------------------------------------------------- /projects/01/Or16.cmp: -------------------------------------------------------------------------------- 1 | | a | b | out | 2 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 3 | | 0000000000000000 | 1111111111111111 | 1111111111111111 | 4 | | 1111111111111111 | 1111111111111111 | 1111111111111111 | 5 | | 1010101010101010 | 0101010101010101 | 1111111111111111 | 6 | | 0011110011000011 | 0000111111110000 | 0011111111110011 | 7 | | 0001001000110100 | 1001100001110110 | 1001101001110110 | 8 | -------------------------------------------------------------------------------- /projects/01/And16.cmp: -------------------------------------------------------------------------------- 1 | | a | b | out | 2 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 3 | | 0000000000000000 | 1111111111111111 | 0000000000000000 | 4 | | 1111111111111111 | 1111111111111111 | 1111111111111111 | 5 | | 1010101010101010 | 0101010101010101 | 0000000000000000 | 6 | | 0011110011000011 | 0000111111110000 | 0000110011000000 | 7 | | 0001001000110100 | 1001100001110110 | 0001000000110100 | 8 | -------------------------------------------------------------------------------- /projects/02/Add16.cmp: -------------------------------------------------------------------------------- 1 | | a | b | out | 2 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 3 | | 0000000000000000 | 1111111111111111 | 1111111111111111 | 4 | | 1111111111111111 | 1111111111111111 | 1111111111111110 | 5 | | 1010101010101010 | 0101010101010101 | 1111111111111111 | 6 | | 0011110011000011 | 0000111111110000 | 0100110010110011 | 7 | | 0001001000110100 | 1001100001110110 | 1010101010101010 | 8 | -------------------------------------------------------------------------------- /projects/01/Or.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/Or.hdl 5 | 6 | /** 7 | * Or gate: out = 1 if {a==1 or b==1}, 0 otherwise 8 | */ 9 | 10 | CHIP Or { 11 | IN a, b; 12 | OUT out; 13 | 14 | PARTS: 15 | Not(in=a,out=nota); 16 | Not(in=b,out=notb); 17 | Nand(a=nota,b=notb,out=out); 18 | } 19 | -------------------------------------------------------------------------------- /projects/12/ArrayTest/ArrayTest.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/12/ArrayTest/ArrayTest.tst 5 | 6 | load, 7 | output-file ArrayTest.out, 8 | compare-to ArrayTest.cmp, 9 | output-list RAM[8000]%D2.6.1 RAM[8001]%D2.6.1 RAM[8002]%D2.6.1 RAM[8003]%D2.6.1; 10 | 11 | repeat 1000000 { 12 | vmstep; 13 | } 14 | 15 | output; 16 | -------------------------------------------------------------------------------- /projects/04/mult/Mult.hack: -------------------------------------------------------------------------------- 1 | 0000000000010000 2 | 1110101010001000 3 | 0000000000010001 4 | 1110111111001000 5 | 0000000000010001 6 | 1111110000010000 7 | 0000000000000000 8 | 1111010011010000 9 | 0000000000010010 10 | 1110001100000001 11 | 0000000000000001 12 | 1111110000010000 13 | 0000000000010000 14 | 1111000010001000 15 | 0000000000010001 16 | 1111110111001000 17 | 0000000000000100 18 | 1110101010000111 19 | 0000000000010000 20 | 1111110000010000 21 | 0000000000000010 22 | 1110001100001000 23 | -------------------------------------------------------------------------------- /projects/07/MemoryAccess/StaticTest/StaticTest.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/07/MemoryAccess/StaticTest/StaticTest.tst 5 | 6 | load StaticTest.asm, 7 | output-file StaticTest.out, 8 | compare-to StaticTest.cmp, 9 | output-list RAM[256]%D1.6.1; 10 | 11 | set RAM[0] 256, 12 | 13 | repeat 200 { 14 | ticktock; 15 | } 16 | 17 | output; 18 | -------------------------------------------------------------------------------- /projects/07/MemoryAccess/StaticTest/StaticTestVME.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/07/MemoryAccess/StaticTest/StaticTestVME.tst 5 | 6 | load StaticTest.vm, 7 | output-file StaticTest.out, 8 | compare-to StaticTest.cmp, 9 | output-list RAM[256]%D1.6.1; 10 | 11 | set sp 256, 12 | 13 | repeat 11 { 14 | vmstep; 15 | } 16 | 17 | output; 18 | -------------------------------------------------------------------------------- /projects/11/Seven/Main.jack: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/11/Seven/Main.jack 5 | 6 | /** 7 | * Computes the value of 1 + (2 * 3) and prints the result 8 | * at the top-left of the screen. 9 | */ 10 | class Main { 11 | 12 | function void main() { 13 | do Output.printInt(1 + (2 * 3)); 14 | return; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /projects/07/StackArithmetic/SimpleAdd/SimpleAdd.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/07/StackArithmetic/SimpleAdd/SimpleAdd.tst 5 | 6 | load SimpleAdd.asm, 7 | output-file SimpleAdd.out, 8 | compare-to SimpleAdd.cmp, 9 | output-list RAM[0]%D2.6.2 RAM[256]%D2.6.2; 10 | 11 | set RAM[0] 256, 12 | 13 | repeat 60 { 14 | ticktock; 15 | } 16 | 17 | output; 18 | -------------------------------------------------------------------------------- /projects/10/Complier/Complier.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /projects/01/And.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/And.hdl 5 | 6 | /** 7 | * And gate: out = 1 if {a==1 and b==1}, 0 otherwise 8 | * And gate: if {a==1 and b==1} then out = 1 else out = 0 9 | */ 10 | 11 | CHIP And { 12 | IN a, b; 13 | OUT out; 14 | 15 | 16 | PARTS: 17 | Nand(a=a,b=b,out=nandout); 18 | Not(in=nandout,out=out); 19 | } -------------------------------------------------------------------------------- /projects/07/StackArithmetic/SimpleAdd/SimpleAddVME.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/07/StackArithmetic/SimpleAdd/SimpleAddVME.tst 5 | 6 | load SimpleAdd.vm, 7 | output-file SimpleAdd.out, 8 | compare-to SimpleAdd.cmp, 9 | output-list RAM[0]%D2.6.2 RAM[256]%D2.6.2; 10 | 11 | set RAM[0] 256, 12 | 13 | repeat 3 { 14 | vmstep; 15 | } 16 | 17 | output; 18 | -------------------------------------------------------------------------------- /projects/01/Xor.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/Xor.hdl 5 | 6 | /** 7 | * Exclusive-or gate: out = !(a == b). 8 | */ 9 | 10 | CHIP Xor { 11 | IN a, b; 12 | OUT out; 13 | 14 | PARTS: 15 | Not(in=a,out=nota); 16 | Not(in=b,out=notb); 17 | And(a=nota,b=b,out=t1); 18 | And(a=a,b=notb,out=t2); 19 | Or(a=t1,b=t2,out=out); 20 | } -------------------------------------------------------------------------------- /projects/08/FunctionCalls/SimpleFunction/SimpleFunction.vm: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/08/FunctionCalls/SimpleFunction/SimpleFunction.vm 5 | 6 | // Performs a simple calculation and returns the result. 7 | function SimpleFunction.test 2 8 | push local 0 9 | push local 1 10 | add 11 | not 12 | push argument 0 13 | add 14 | push argument 1 15 | sub 16 | return 17 | -------------------------------------------------------------------------------- /projects/12/MemoryTest/MemoryTest.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/12/MemoryTest/MemoryTest.tst 5 | 6 | load, 7 | output-file MemoryTest.out, 8 | compare-to MemoryTest.cmp, 9 | output-list RAM[8000]%D2.6.1 RAM[8001]%D2.6.1 RAM[8002]%D2.6.1 RAM[8003]%D2.6.1 RAM[8004]%D2.6.1 RAM[8005]%D2.6.1; 10 | 11 | repeat 1000000 { 12 | vmstep; 13 | } 14 | 15 | output; 16 | -------------------------------------------------------------------------------- /projects/01/Mux.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/Mux.hdl 5 | 6 | /** 7 | * Multiplexor. If sel==1 then out=b else out=a. 8 | */ 9 | 10 | CHIP Mux { 11 | IN a, b, sel; 12 | OUT out; 13 | 14 | PARTS: 15 | Not(in=sel,out=notsel); 16 | And(a=a,b=notsel,out=anotsel); 17 | And(a=b,b=sel,out=bsel); 18 | Or(a=anotsel,b=bsel,out=out); 19 | } -------------------------------------------------------------------------------- /projects/01/DMux.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/DMux.hdl 5 | 6 | /** 7 | * Demultiplexor. 8 | * {a,b} = {in,0} if sel==0 9 | * {0,in} if sel==1 10 | */ 11 | 12 | 13 | CHIP DMux { 14 | IN in, sel; 15 | OUT a, b; 16 | 17 | PARTS: 18 | And(a=in,b=sel,out=b); 19 | Not(in=sel,out=notsel); 20 | And(a=in,b=notsel,out=a); 21 | } 22 | -------------------------------------------------------------------------------- /projects/02/HalfAdder.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/02/HalfAdder.hdl 5 | 6 | /** 7 | * Computes the sum of two bits. 8 | */ 9 | 10 | CHIP HalfAdder { 11 | IN a, b; // 1-bit inputs 12 | OUT sum, // Right bit of a + b 13 | carry; // Left bit of a + b 14 | 15 | PARTS: 16 | And(a=a,b=b,out=carry); 17 | Xor(a=a,b=b,out=sum); 18 | } 19 | -------------------------------------------------------------------------------- /projects/07/MemoryAccess/StaticTest/StaticTest.vm: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/07/MemoryAccess/StaticTest/StaticTest.vm 5 | 6 | // Executes pop and push commands using the static segment. 7 | push constant 111 8 | push constant 333 9 | push constant 888 10 | pop static 8 11 | pop static 3 12 | pop static 1 13 | push static 3 14 | push static 1 15 | sub 16 | push static 8 17 | add 18 | -------------------------------------------------------------------------------- /projects/08/FunctionCalls/StaticsTest/StaticsTest.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/08/FunctionCalls/StaticsTest/StaticsTest.tst 5 | 6 | load StaticsTest.asm, 7 | output-file StaticsTest.out, 8 | compare-to StaticsTest.cmp, 9 | output-list RAM[0]%D1.6.1 RAM[261]%D1.6.1 RAM[262]%D1.6.1; 10 | 11 | set RAM[0] 256, 12 | 13 | repeat 2500 { 14 | ticktock; 15 | } 16 | 17 | output; 18 | -------------------------------------------------------------------------------- /projects/06/rect/RectL.asm: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/06/rect/RectL.asm 5 | 6 | // Symbol-less version of the Rect.asm program. 7 | 8 | @0 9 | D=M 10 | @23 11 | D;JLE 12 | @16 13 | M=D 14 | @16384 15 | D=A 16 | @17 17 | M=D 18 | @17 19 | A=M 20 | M=-1 21 | @17 22 | D=M 23 | @32 24 | D=D+A 25 | @17 26 | M=D 27 | @16 28 | MD=M-1 29 | @10 30 | D;JGT 31 | @23 32 | 0;JMP 33 | -------------------------------------------------------------------------------- /projects/03/Bit.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/03/a/Bit.hdl 5 | 6 | /** 7 | * 1-bit register. 8 | * If load[t]=1 then out[t+1] = in[t] 9 | * else out does not change (out[t+1]=out[t]) 10 | */ 11 | 12 | CHIP Bit { 13 | IN in, load; 14 | OUT out; 15 | 16 | PARTS: 17 | Mux(a=dout,b=in,sel=load,out=out1); 18 | DFF(in=out1,out=dout,out=out); 19 | } 20 | -------------------------------------------------------------------------------- /projects/03/a/Bit.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/03/a/Bit.hdl 5 | 6 | /** 7 | * 1-bit register. 8 | * If load[t]=1 then out[t+1] = in[t] 9 | * else out does not change (out[t+1]=out[t]) 10 | */ 11 | 12 | CHIP Bit { 13 | IN in, load; 14 | OUT out; 15 | 16 | PARTS: 17 | Mux(a=dout,b=in,sel=load,out=out1); 18 | DFF(in=out1,out=dout,out=out); 19 | } 20 | -------------------------------------------------------------------------------- /projects/08/ProgramFlow/BasicLoop/BasicLoop.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/08/ProgramFlow/BasicLoop/BasicLoop.tst 5 | 6 | load BasicLoop.asm, 7 | output-file BasicLoop.out, 8 | compare-to BasicLoop.cmp, 9 | output-list RAM[0]%D1.6.1 RAM[256]%D1.6.1; 10 | 11 | set RAM[0] 256, 12 | set RAM[1] 300, 13 | set RAM[2] 400, 14 | set RAM[400] 3, 15 | 16 | repeat 600 { 17 | ticktock; 18 | } 19 | 20 | output; 21 | -------------------------------------------------------------------------------- /projects/01/DMux.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/DMux.tst 5 | 6 | load DMux.hdl, 7 | output-file DMux.out, 8 | compare-to DMux.cmp, 9 | output-list in%B3.1.3 sel%B3.1.3 a%B3.1.3 b%B3.1.3; 10 | 11 | set in 0, 12 | set sel 0, 13 | eval, 14 | output; 15 | 16 | set sel 1, 17 | eval, 18 | output; 19 | 20 | set in 1, 21 | set sel 0, 22 | eval, 23 | output; 24 | 25 | set sel 1, 26 | eval, 27 | output; 28 | -------------------------------------------------------------------------------- /projects/01/Or.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/Or.tst 5 | 6 | load Or.hdl, 7 | output-file Or.out, 8 | compare-to Or.cmp, 9 | output-list a%B3.1.3 b%B3.1.3 out%B3.1.3; 10 | 11 | set a 0, 12 | set b 0, 13 | eval, 14 | output; 15 | 16 | set a 0, 17 | set b 1, 18 | eval, 19 | output; 20 | 21 | set a 1, 22 | set b 0, 23 | eval, 24 | output; 25 | 26 | set a 1, 27 | set b 1, 28 | eval, 29 | output; 30 | -------------------------------------------------------------------------------- /projects/08/FunctionCalls/StaticsTest/StaticsTestVME.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/08/FunctionCalls/StaticsTest/StaticsTestVME.tst 5 | 6 | load, // Load all the VM files from the current directory. 7 | output-file StaticsTest.out, 8 | compare-to StaticsTest.cmp, 9 | output-list RAM[0]%D1.6.1 RAM[261]%D1.6.1 RAM[262]%D1.6.1; 10 | 11 | set sp 261, 12 | 13 | repeat 36 { 14 | vmstep; 15 | } 16 | 17 | output; 18 | -------------------------------------------------------------------------------- /projects/08/ProgramFlow/BasicLoop/BasicLoopVME.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/08/ProgramFlow/BasicLoop/BasicLoopVME.tst 5 | 6 | load BasicLoop.vm, 7 | output-file BasicLoop.out, 8 | compare-to BasicLoop.cmp, 9 | output-list RAM[0]%D1.6.1 RAM[256]%D1.6.1; 10 | 11 | set sp 256, 12 | set local 300, 13 | set argument 400, 14 | set argument[0] 3, 15 | 16 | repeat 33 { 17 | vmstep; 18 | } 19 | 20 | output; 21 | -------------------------------------------------------------------------------- /projects/01/And.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/And.tst 5 | 6 | load And.hdl, 7 | output-file And.out, 8 | compare-to And.cmp, 9 | output-list a%B3.1.3 b%B3.1.3 out%B3.1.3; 10 | 11 | set a 0, 12 | set b 0, 13 | eval, 14 | output; 15 | 16 | set a 0, 17 | set b 1, 18 | eval, 19 | output; 20 | 21 | set a 1, 22 | set b 0, 23 | eval, 24 | output; 25 | 26 | set a 1, 27 | set b 1, 28 | eval, 29 | output; 30 | -------------------------------------------------------------------------------- /projects/01/Xor.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/Xor.tst 5 | 6 | load Xor.hdl, 7 | output-file Xor.out, 8 | compare-to Xor.cmp, 9 | output-list a%B3.1.3 b%B3.1.3 out%B3.1.3; 10 | 11 | set a 0, 12 | set b 0, 13 | eval, 14 | output; 15 | 16 | set a 0, 17 | set b 1, 18 | eval, 19 | output; 20 | 21 | set a 1, 22 | set b 0, 23 | eval, 24 | output; 25 | 26 | set a 1, 27 | set b 1, 28 | eval, 29 | output; 30 | -------------------------------------------------------------------------------- /projects/05/Rect.hack: -------------------------------------------------------------------------------- 1 | 0000000000000000 2 | 1111110000010000 3 | 0000000000010111 4 | 1110001100000110 5 | 0000000000010000 6 | 1110001100001000 7 | 0100000000000000 8 | 1110110000010000 9 | 0000000000010001 10 | 1110001100001000 11 | 0000000000010001 12 | 1111110000100000 13 | 1110111010001000 14 | 0000000000010001 15 | 1111110000010000 16 | 0000000000100000 17 | 1110000010010000 18 | 0000000000010001 19 | 1110001100001000 20 | 0000000000010000 21 | 1111110010011000 22 | 0000000000001010 23 | 1110001100000001 24 | 0000000000010111 25 | 1110101010000111 26 | -------------------------------------------------------------------------------- /projects/07/MemoryAccess/PointerTest/PointerTest.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/07/MemoryAccess/PointerTest/PointerTest.tst 5 | 6 | load PointerTest.asm, 7 | output-file PointerTest.out, 8 | compare-to PointerTest.cmp, 9 | output-list RAM[256]%D1.6.1 RAM[3]%D1.6.1 10 | RAM[4]%D1.6.1 RAM[3032]%D1.6.1 RAM[3046]%D1.6.1; 11 | 12 | set RAM[0] 256, 13 | 14 | repeat 450 { 15 | ticktock; 16 | } 17 | 18 | output; 19 | -------------------------------------------------------------------------------- /projects/07/MemoryAccess/PointerTest/PointerTestVME.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/07/MemoryAccess/PointerTest/PointerTestVME.tst 5 | 6 | load PointerTest.vm, 7 | output-file PointerTest.out, 8 | compare-to PointerTest.cmp, 9 | output-list RAM[256]%D1.6.1 RAM[3]%D1.6.1 RAM[4]%D1.6.1 10 | RAM[3032]%D1.6.1 RAM[3046]%D1.6.1; 11 | 12 | set RAM[0] 256, 13 | 14 | repeat 15 { 15 | vmstep; 16 | } 17 | 18 | output; 19 | -------------------------------------------------------------------------------- /projects/08/FunctionCalls/FibonacciElement/FibonacciElementVME.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/08/FunctionCalls/FibonacciElement/FibonacciElementVME.tst 5 | 6 | load, // Load all the VM files from the current directory 7 | output-file FibonacciElement.out, 8 | compare-to FibonacciElement.cmp, 9 | output-list RAM[0]%D1.6.1 RAM[261]%D1.6.1; 10 | 11 | set sp 261, 12 | 13 | repeat 110 { 14 | vmstep; 15 | } 16 | 17 | output; 18 | -------------------------------------------------------------------------------- /projects/00/And.tst: -------------------------------------------------------------------------------- 1 | // This file is part of the materials accompanying the book 2 | // "The Elements of Computing Systems" by Nisan and Schocken, 3 | // MIT Press. Book site: www.nand2tetris.org 4 | // File name: projects/00/And.tst 5 | 6 | load And.hdl, 7 | output-file And.out, 8 | compare-to And.cmp, 9 | output-list a%B3.1.3 b%B3.1.3 out%B3.1.3; 10 | 11 | set a 0, 12 | set b 0, 13 | eval, 14 | output; 15 | 16 | set a 0, 17 | set b 1, 18 | eval, 19 | output; 20 | 21 | set a 1, 22 | set b 0, 23 | eval, 24 | output; 25 | 26 | set a 1, 27 | set b 1, 28 | eval, 29 | output; -------------------------------------------------------------------------------- /projects/08/FunctionCalls/StaticsTest/Class1.vm: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/08/FunctionCalls/StaticsTest/Class1.vm 5 | 6 | // Stores two supplied arguments in static[0] and static[1]. 7 | function Class1.set 0 8 | push argument 0 9 | pop static 0 10 | push argument 1 11 | pop static 1 12 | push constant 0 13 | return 14 | 15 | // Returns static[0] - static[1]. 16 | function Class1.get 0 17 | push static 0 18 | push static 1 19 | sub 20 | return 21 | -------------------------------------------------------------------------------- /projects/08/FunctionCalls/StaticsTest/Class2.vm: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/08/FunctionCalls/StaticsTest/Class2.vm 5 | 6 | // Stores two supplied arguments in static[0] and static[1]. 7 | function Class2.set 0 8 | push argument 0 9 | pop static 0 10 | push argument 1 11 | pop static 1 12 | push constant 0 13 | return 14 | 15 | // Returns static[0] - static[1]. 16 | function Class2.get 0 17 | push static 0 18 | push static 1 19 | sub 20 | return 21 | -------------------------------------------------------------------------------- /projects/01/Or8Way.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/Or8Way.tst 5 | 6 | load Or8Way.hdl, 7 | output-file Or8Way.out, 8 | compare-to Or8Way.cmp, 9 | output-list in%B2.8.2 out%B2.1.2; 10 | 11 | set in %B00000000, 12 | eval, 13 | output; 14 | 15 | set in %B11111111, 16 | eval, 17 | output; 18 | 19 | set in %B00010000, 20 | eval, 21 | output; 22 | 23 | set in %B00000001, 24 | eval, 25 | output; 26 | 27 | set in %B00100110, 28 | eval, 29 | output; -------------------------------------------------------------------------------- /projects/02/FullAdder.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/02/FullAdder.hdl 5 | 6 | /** 7 | * Computes the sum of three bits. 8 | */ 9 | 10 | CHIP FullAdder { 11 | IN a, b, c; // 1-bit inputs 12 | OUT sum, // Right bit of a + b + c 13 | carry; // Left bit of a + b + c 14 | 15 | PARTS: 16 | HalfAdder(a=a,b=b,sum=sum1,carry=carry1); 17 | HalfAdder(a=c,b=sum1,sum=sum,carry=carry2); 18 | Or(a=carry1,b=carry2,out=carry); 19 | } -------------------------------------------------------------------------------- /projects/08/FunctionCalls/FibonacciElement/FibonacciElement.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/08/FunctionCalls/FibonacciElement/FibonacciElement.tst 5 | 6 | // FibonacciElement.asm is the result of translating both Main.vm and Sys.vm. 7 | 8 | load FibonacciElement.asm, 9 | output-file FibonacciElement.out, 10 | compare-to FibonacciElement.cmp, 11 | output-list RAM[0]%D1.6.1 RAM[261]%D1.6.1; 12 | 13 | repeat 6000 { 14 | ticktock; 15 | } 16 | 17 | output; 18 | -------------------------------------------------------------------------------- /projects/02/HalfAdder.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/02/HalfAdder.tst 5 | 6 | load HalfAdder.hdl, 7 | output-file HalfAdder.out, 8 | compare-to HalfAdder.cmp, 9 | output-list a%B3.1.3 b%B3.1.3 sum%B3.1.3 carry%B3.1.3; 10 | 11 | set a 0, 12 | set b 0, 13 | eval, 14 | output; 15 | 16 | set a 0, 17 | set b 1, 18 | eval, 19 | output; 20 | 21 | set a 1, 22 | set b 0, 23 | eval, 24 | output; 25 | 26 | set a 1, 27 | set b 1, 28 | eval, 29 | output; 30 | -------------------------------------------------------------------------------- /projects/09/Square/Main.jack: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/09/Square/Main.jack 5 | 6 | /** 7 | * The Main class initializes a new Square Dance game and starts it. 8 | */ 9 | class Main { 10 | 11 | /** Initializes a new game and starts it. */ 12 | function void main() { 13 | var SquareGame game; 14 | 15 | let game = SquareGame.new(); 16 | do game.run(); 17 | do game.dispose(); 18 | 19 | return; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /projects/10/Square/Main.jack: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/09/Square/Main.jack 5 | 6 | /** 7 | * The Main class initializes a new Square Dance game and starts it. 8 | */ 9 | class Main { 10 | 11 | /** Initializes a new game and starts it. */ 12 | function void main() { 13 | var SquareGame game; 14 | 15 | let game = SquareGame.new(); 16 | do game.run(); 17 | do game.dispose(); 18 | 19 | return; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /projects/11/Pong/Main.jack: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/11/Pong/Main.jack 5 | 6 | /** 7 | * The main class of the Pong game. 8 | */ 9 | class Main { 10 | 11 | /** Initializes the Pong game and starts it. */ 12 | function void main() { 13 | var PongGame game; 14 | 15 | do PongGame.newInstance(); 16 | let game = PongGame.getInstance(); 17 | do game.run(); 18 | do game.dispose(); 19 | 20 | return; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /projects/11/Square/Main.jack: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/09/Square/Main.jack 5 | 6 | /** 7 | * The Main class initializes a new Square Dance game and starts it. 8 | */ 9 | class Main { 10 | 11 | /** Initializes a new game and starts it. */ 12 | function void main() { 13 | var SquareGame game; 14 | 15 | let game = SquareGame.new(); 16 | do game.run(); 17 | do game.dispose(); 18 | 19 | return; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /projects/11/Pong/Main.jack.bak: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/11/pong/Main.jack 5 | 6 | /** 7 | * The main class of the Pong game. 8 | */ 9 | class Main { 10 | 11 | /** Initializes the Pong game and starts it. */ 12 | function void main() { 13 | var PongGame game; 14 | 15 | do PongGame.newInstance(); 16 | let game = PongGame.getInstance(); 17 | do game.run(); 18 | do game.dispose(); 19 | 20 | return; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /projects/01/Mux16.out: -------------------------------------------------------------------------------- 1 | | a | b | sel | out | 2 | | 0000000000000000 | 0000000000000000 | 0 | 0000000000000000 | 3 | | 0000000000000000 | 0000000000000000 | 1 | 0000000000000000 | 4 | | 0000000000000000 | 0001001000110100 | 0 | 0000000000000000 | 5 | | 0000000000000000 | 0001001000110100 | 1 | 0001001000110100 | 6 | | 1001100001110110 | 0000000000000000 | 0 | 1001100001110110 | 7 | | 1001100001110110 | 0000000000000000 | 1 | 0000000000000000 | 8 | | 1010101010101010 | 0101010101010101 | 0 | 1010101010101010 | 9 | | 1010101010101010 | 0101010101010101 | 1 | 0101010101010101 | 10 | -------------------------------------------------------------------------------- /projects/02/Inc16.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/02/Inc16.tst 5 | 6 | load Inc16.hdl, 7 | output-file Inc16.out, 8 | compare-to Inc16.cmp, 9 | output-list in%B1.16.1 out%B1.16.1; 10 | 11 | set in %B0000000000000000, // in = 0 12 | eval, 13 | output; 14 | 15 | set in %B1111111111111111, // in = -1 16 | eval, 17 | output; 18 | 19 | set in %B0000000000000101, // in = 5 20 | eval, 21 | output; 22 | 23 | set in %B1111111111111011, // in = -5 24 | eval, 25 | output; 26 | -------------------------------------------------------------------------------- /projects/12/MathTest/MathTest.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/12/MathTest/MathTest.tst 5 | 6 | load, 7 | output-file MathTest.out, 8 | compare-to MathTest.cmp, 9 | output-list RAM[8000]%D2.6.1 RAM[8001]%D2.6.1 RAM[8002]%D2.6.1 RAM[8003]%D2.6.1 RAM[8004]%D2.6.1 RAM[8005]%D2.6.1 RAM[8006]%D2.6.1 RAM[8007]%D2.6.1 RAM[8008]%D2.6.1 RAM[8009]%D2.6.1 RAM[8010]%D2.6.1 RAM[8011]%D2.6.1 RAM[8012]%D2.6.1 RAM[8013]%D2.6.1; 10 | 11 | repeat 1000000 { 12 | vmstep; 13 | } 14 | 15 | output; 16 | -------------------------------------------------------------------------------- /projects/01/Or8Way.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/Or8Way.hdl 5 | 6 | /** 7 | * 8-way or gate: out = in[0] or in[1] or ... or in[7]. 8 | */ 9 | 10 | CHIP Or8Way { 11 | IN in[8]; 12 | OUT out; 13 | 14 | PARTS: 15 | Or(a=in[0],b=in[1],out=or1); 16 | Or(a=in[2],b=in[3],out=or2); 17 | Or(a=in[4],b=in[5],out=or3); 18 | Or(a=in[6],b=in[7],out=or4); 19 | Or(a=or1,b=or2,out=or5); 20 | Or(a=or3,b=or4,out=or6); 21 | Or(a=or5,b=or6,out=out); 22 | } -------------------------------------------------------------------------------- /projects/07/MemoryAccess/PointerTest/PointerTest.vm: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/07/MemoryAccess/PointerTest/PointerTest.vm 5 | 6 | // Executes pop and push commands using the 7 | // pointer, this, and that segments. 8 | push constant 3030 9 | pop pointer 0 10 | push constant 3040 11 | pop pointer 1 12 | push constant 32 13 | pop this 2 14 | push constant 46 15 | pop that 6 16 | push pointer 0 17 | push pointer 1 18 | add 19 | push this 2 20 | sub 21 | push that 6 22 | add 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Nand2Tetris 2 | =========== 3 | 4 | All projects for Nand2Teris 5 | 6 | Nand2Tetris website: http://nand2tetris.org/ 7 | 8 | * Project 0: Introduction 9 | * Project 1: Boolean Logic 10 | * Project 2: Boolean Arithmetic 11 | * Project 3: Sequential Logic 12 | * Project 4: Machine Language 13 | * Project 5: Computer Architecture 14 | * Project 6: Assembler 15 | * Project 7: VM I: Stack Arithmetic 16 | * Project 8: VM II: Program Control 17 | * Project 9: High-Level Language 18 | * Project 10: Compiler I: Syntax Analysis 19 | * Project 11: Compiler II: Code Generation 20 | * Project 12: Operating System 21 | -------------------------------------------------------------------------------- /projects/01/Mux16.cmp: -------------------------------------------------------------------------------- 1 | | a | b | sel | out | 2 | | 0000000000000000 | 0000000000000000 | 0 | 0000000000000000 | 3 | | 0000000000000000 | 0000000000000000 | 1 | 0000000000000000 | 4 | | 0000000000000000 | 0001001000110100 | 0 | 0000000000000000 | 5 | | 0000000000000000 | 0001001000110100 | 1 | 0001001000110100 | 6 | | 1001100001110110 | 0000000000000000 | 0 | 1001100001110110 | 7 | | 1001100001110110 | 0000000000000000 | 1 | 0000000000000000 | 8 | | 1010101010101010 | 0101010101010101 | 0 | 1010101010101010 | 9 | | 1010101010101010 | 0101010101010101 | 1 | 0101010101010101 | 10 | -------------------------------------------------------------------------------- /projects/01/Mux4Way16.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/Mux4Way16.hdl 5 | 6 | /** 7 | * 4-way 16-bit multiplexor. 8 | * out = a if sel==00 9 | * b if sel==01 10 | * c if sel==10 11 | * d if sel==11 12 | */ 13 | 14 | CHIP Mux4Way16 { 15 | IN a[16], b[16], c[16], d[16], sel[2]; 16 | OUT out[16]; 17 | 18 | PARTS: 19 | Mux16(a=a,b=b,sel=sel[0],out=out1); 20 | Mux16(a=c,b=d,sel=sel[0],out=out2); 21 | Mux16(a=out1,b=out2,sel=sel[1],out=out); 22 | } -------------------------------------------------------------------------------- /projects/01/Not16.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/Not16.tst 5 | 6 | load Not16.hdl, 7 | output-file Not16.out, 8 | compare-to Not16.cmp, 9 | output-list in%B1.16.1 out%B1.16.1; 10 | 11 | set in %B0000000000000000, 12 | eval, 13 | output; 14 | 15 | set in %B1111111111111111, 16 | eval, 17 | output; 18 | 19 | set in %B1010101010101010, 20 | eval, 21 | output; 22 | 23 | set in %B0011110011000011, 24 | eval, 25 | output; 26 | 27 | set in %B0001001000110100, 28 | eval, 29 | output; -------------------------------------------------------------------------------- /projects/12/Array.jack: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/12/Array.jack 5 | 6 | /** 7 | * Represents an array. Can be used to hold any type of object. 8 | */ 9 | class Array { 10 | 11 | /** Constructs a new Array of the given size. */ 12 | function Array new(int size) { 13 | return Memory.alloc(size); 14 | } 15 | 16 | /** De-allocates the array and frees its space. */ 17 | method void dispose() { 18 | do Memory.deAlloc(this); 19 | return; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /projects/01/DMux4Way.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/DMux4Way.hdl 5 | 6 | /** 7 | * 4-way demultiplexor. 8 | * {a,b,c,d} = {in,0,0,0} if sel==00 9 | * {0,in,0,0} if sel==01 10 | * {0,0,in,0} if sel==10 11 | * {0,0,0,in} if sel==11 12 | */ 13 | 14 | CHIP DMux4Way { 15 | IN in, sel[2]; 16 | OUT a, b, c, d; 17 | 18 | PARTS: 19 | DMux(in=in,sel=sel[1],a=out1,b=out2); 20 | DMux(in=out1,sel=sel[0],a=a,b=b); 21 | DMux(in=out2,sel=sel[0],a=c,b=d); 22 | } -------------------------------------------------------------------------------- /projects/08/FunctionCalls/FibonacciElement/Sys.vm: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/08/FunctionCalls/FibonacciElement/Sys.vm 5 | 6 | // Pushes n onto the stack and calls the Main.fibonacii function, 7 | // which computes the n'th element of the Fibonacci series. 8 | // The Sys.init function is called "automatically" by the 9 | // bootstrap code. 10 | 11 | function Sys.init 0 12 | push constant 4 13 | call Main.fibonacci 1 // Compute the 4'th fibonacci element 14 | label WHILE 15 | goto WHILE // Loop infinitely 16 | -------------------------------------------------------------------------------- /projects/10/ExpressionlessSquare/Main.jack: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/10/ExpressionlessSquare/Main.jack 5 | 6 | // Expressionless version of Main.jack. 7 | 8 | /** 9 | * The Main class initializes a new Square Dance game and starts it. 10 | */ 11 | class Main { 12 | 13 | // Initializes the square game and starts it. 14 | function void main() { 15 | var SquareGame game; 16 | 17 | let game = game; 18 | do game.run(); 19 | do game.dispose(); 20 | 21 | return; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /projects/00/Xor.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/00/Xor.hdl 5 | 6 | // This file is included here for those who wish to follow the Hardware Simulator Tutorial 7 | // and play with the Xor.hdl implementation. 8 | 9 | /** 10 | * Exclusive-or gate: out = !(a == b). 11 | */ 12 | 13 | CHIP Xor { 14 | 15 | IN a, b; 16 | OUT out; 17 | 18 | PARTS: 19 | Not(in=a, out=nota); 20 | Not(in=b, out=notb); 21 | And(a=a, b=notb, out=w1); 22 | And(a=nota, b=b, out=w2); 23 | Or(a=w1, b=w2, out=out); 24 | } 25 | -------------------------------------------------------------------------------- /projects/07/StackArithmetic/StackTest/StackTest.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/07/StackArithmetic/StackTest/StackTest.tst 5 | 6 | load StackTest.asm, 7 | output-file StackTest.out, 8 | compare-to StackTest.cmp, 9 | output-list RAM[0]%D2.6.2 10 | RAM[256]%D2.6.2 RAM[257]%D2.6.2 RAM[258]%D2.6.2 RAM[259]%D2.6.2 RAM[260]%D2.6.2; 11 | 12 | set RAM[0] 256, 13 | 14 | repeat 1000 { 15 | ticktock; 16 | } 17 | 18 | output; 19 | output-list RAM[261]%D2.6.2 RAM[262]%D2.6.2 RAM[263]%D2.6.2 RAM[264]%D2.6.2 RAM[265]%D2.6.2; 20 | output; 21 | -------------------------------------------------------------------------------- /projects/07/StackArithmetic/StackTest/StackTestVME.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/07/StackArithmetic/StackTest/StackTestVME.tst 5 | 6 | load StackTest.vm, 7 | output-file StackTest.out, 8 | compare-to StackTest.cmp, 9 | output-list RAM[0]%D2.6.2 10 | RAM[256]%D2.6.2 RAM[257]%D2.6.2 RAM[258]%D2.6.2 RAM[259]%D2.6.2 RAM[260]%D2.6.2; 11 | 12 | set RAM[0] 256, 13 | 14 | repeat 38 { 15 | vmstep; 16 | } 17 | 18 | output; 19 | output-list RAM[261]%D2.6.2 RAM[262]%D2.6.2 RAM[263]%D2.6.2 RAM[264]%D2.6.2 RAM[265]%D2.6.2; 20 | output; 21 | -------------------------------------------------------------------------------- /projects/08/FunctionCalls/StaticsTest/Sys.vm: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/08/FunctionCalls/StaticsTest/Sys.vm 5 | 6 | // Tests that different functions, stored in two different 7 | // class files, manipulate the static segment correctly. 8 | function Sys.init 0 9 | push constant 6 10 | push constant 8 11 | call Class1.set 2 12 | pop temp 0 // Dumps the return value 13 | push constant 23 14 | push constant 15 15 | call Class2.set 2 16 | pop temp 0 // Dumps the return value 17 | call Class1.get 0 18 | call Class2.get 0 19 | label WHILE 20 | goto WHILE 21 | -------------------------------------------------------------------------------- /projects/07/MemoryAccess/BasicTest/BasicTestVME.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/07/MemoryAccess/BasicTest/BasicTestVME.tst 5 | 6 | load BasicTest.vm, 7 | output-file BasicTest.out, 8 | compare-to BasicTest.cmp, 9 | output-list RAM[256]%D1.6.1 RAM[300]%D1.6.1 RAM[401]%D1.6.1 10 | RAM[402]%D1.6.1 RAM[3006]%D1.6.1 RAM[3012]%D1.6.1 11 | RAM[3015]%D1.6.1 RAM[11]%D1.6.1; 12 | 13 | set sp 256, 14 | set local 300, 15 | set argument 400, 16 | set this 3000, 17 | set that 3010, 18 | 19 | repeat 25 { 20 | vmstep; 21 | } 22 | 23 | output; 24 | -------------------------------------------------------------------------------- /projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries.tst 5 | 6 | load FibonacciSeries.asm, 7 | output-file FibonacciSeries.out, 8 | compare-to FibonacciSeries.cmp, 9 | output-list RAM[3000]%D1.6.2 RAM[3001]%D1.6.2 RAM[3002]%D1.6.2 10 | RAM[3003]%D1.6.2 RAM[3004]%D1.6.2 RAM[3005]%D1.6.2; 11 | 12 | set RAM[0] 256, 13 | set RAM[1] 300, 14 | set RAM[2] 400, 15 | set RAM[400] 6, 16 | set RAM[401] 3000, 17 | 18 | repeat 1100 { 19 | ticktock; 20 | } 21 | 22 | output; 23 | -------------------------------------------------------------------------------- /projects/00/Xor.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/00/Xor.tst 5 | 6 | // This file is included here for those who wish to follow the Hardware Simulator Tutorial 7 | // and play with the Xor.hdl implementation. 8 | 9 | load Xor.hdl, 10 | output-file Xor.out, 11 | compare-to Xor.cmp, 12 | output-list a%B3.1.3 b%B3.1.3 out%B3.1.3; 13 | 14 | set a 0, 15 | set b 0, 16 | eval, 17 | output; 18 | 19 | set a 0, 20 | set b 1, 21 | eval, 22 | output; 23 | 24 | set a 1, 25 | set b 0, 26 | eval, 27 | output; 28 | 29 | set a 1, 30 | set b 1, 31 | eval, 32 | output; 33 | -------------------------------------------------------------------------------- /projects/01/Mux8Way16.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/Mux8Way16.hdl 5 | 6 | /** 7 | * 8-way 16-bit multiplexor. 8 | * out = a if sel==000 9 | * b if sel==001 10 | * etc. 11 | * h if sel==111 12 | */ 13 | 14 | CHIP Mux8Way16 { 15 | IN a[16], b[16], c[16], d[16], 16 | e[16], f[16], g[16], h[16], 17 | sel[3]; 18 | OUT out[16]; 19 | 20 | PARTS: 21 | Mux4Way16(a=a,b=b,c=c,d=d,sel=sel[0..1],out=out1); 22 | Mux4Way16(a=e,b=f,c=g,d=h,sel=sel[0..1],out=out2); 23 | Mux16(a=out1,b=out2,sel=sel[2],out=out); 24 | } -------------------------------------------------------------------------------- /projects/07/MemoryAccess/BasicTest/BasicTest.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/07/MemoryAccess/BasicTest/BasicTest.tst 5 | 6 | load BasicTest.asm, 7 | output-file BasicTest.out, 8 | compare-to BasicTest.cmp, 9 | output-list RAM[256]%D1.6.1 RAM[300]%D1.6.1 RAM[401]%D1.6.1 10 | RAM[402]%D1.6.1 RAM[3006]%D1.6.1 RAM[3012]%D1.6.1 11 | RAM[3015]%D1.6.1 RAM[11]%D1.6.1; 12 | 13 | set RAM[0] 256, 14 | set RAM[1] 300, 15 | set RAM[2] 400, 16 | set RAM[3] 3000, 17 | set RAM[4] 3010, 18 | 19 | repeat 600 { 20 | ticktock; 21 | } 22 | 23 | output; 24 | -------------------------------------------------------------------------------- /projects/08/ProgramFlow/FibonacciSeries/FibonacciSeriesVME.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/08/ProgramFlow/FibonacciSeries/FibonacciSeriesVME.tst 5 | 6 | load FibonacciSeries.vm, 7 | output-file FibonacciSeries.out, 8 | compare-to FibonacciSeries.cmp, 9 | output-list RAM[3000]%D1.6.2 RAM[3001]%D1.6.2 RAM[3002]%D1.6.2 10 | RAM[3003]%D1.6.2 RAM[3004]%D1.6.2 RAM[3005]%D1.6.2; 11 | 12 | set sp 256, 13 | set local 300, 14 | set argument 400, 15 | set argument[0] 6, 16 | set argument[1] 3000, 17 | 18 | repeat 73 { 19 | vmstep; 20 | } 21 | 22 | output; 23 | -------------------------------------------------------------------------------- /projects/04/mult/Mult.asm: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/04/Mult.asm 5 | 6 | // Multiplies R0 and R1 and stores the result in R2. 7 | // (R0, R1, R2 refer to RAM[0], RAM[1], and RAM[3], respectively.) 8 | 9 | // Put your code here. 10 | @sum 11 | M=0 //sum=0 12 | @i 13 | M=1 //i=1 14 | (LOOP) 15 | @i 16 | D=M //D=i 17 | @R0 18 | D=D-M //D=i-R0 19 | @END 20 | D;JGT // if(i-R0) > 0 goto END 21 | @R1 22 | D=M //D=R1 23 | @sum 24 | M=D+M //sum+=R1 25 | @i 26 | M=M+1 //i=i+1 27 | @LOOP 28 | 0;JMP //Goto LOOP 29 | (END) 30 | @sum 31 | D=M 32 | @R2 33 | M=D -------------------------------------------------------------------------------- /projects/01/DMux8Way.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/DMux8Way.hdl 5 | 6 | /** 7 | * 8-way demultiplexor. 8 | * {a,b,c,d,e,f,g,h} = {in,0,0,0,0,0,0,0} if sel==000 9 | * {0,in,0,0,0,0,0,0} if sel==001 10 | * etc. 11 | * {0,0,0,0,0,0,0,in} if sel==111 12 | */ 13 | 14 | 15 | CHIP DMux8Way { 16 | IN in, sel[3]; 17 | OUT a, b, c, d, e, f, g, h; 18 | 19 | PARTS: 20 | DMux(in=in,sel=sel[2],a=out1,b=out2); 21 | DMux4Way(in=out1,sel=sel[0..1],a=a,b=b,c=c,d=d); 22 | DMux4Way(in=out2,sel=sel[0..1],a=e,b=f,c=g,d=h); 23 | } -------------------------------------------------------------------------------- /projects/05/ComputerAdd-external.out: -------------------------------------------------------------------------------- 1 | | time |reset|RAM16K[0]|RAM16K[1]|RAM16K[2]| 2 | | 0 | 0 | 0 | 0 | 0 | 3 | | 1 | 0 | 0 | 0 | 0 | 4 | | 2 | 0 | 0 | 0 | 0 | 5 | | 3 | 0 | 0 | 0 | 0 | 6 | | 4 | 0 | 0 | 0 | 0 | 7 | | 5 | 0 | 0 | 0 | 0 | 8 | | 6 | 0 | 5 | 0 | 0 | 9 | | 7 | 1 | 0 | 0 | 0 | 10 | | 8 | 0 | 0 | 0 | 0 | 11 | | 9 | 0 | 0 | 0 | 0 | 12 | | 10 | 0 | 0 | 0 | 0 | 13 | | 11 | 0 | 0 | 0 | 0 | 14 | | 12 | 0 | 0 | 0 | 0 | 15 | | 13 | 0 | 5 | 0 | 0 | 16 | -------------------------------------------------------------------------------- /projects/06/rect/Rect.asm: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/06/rect/Rect.asm 5 | 6 | // Draws a rectangle at the top-left corner of the screen. 7 | // The rectangle is 16 pixels wide and R0 pixels high. 8 | 9 | @0 10 | D=M 11 | @INFINITE_LOOP 12 | D;JLE 13 | @counter 14 | M=D 15 | @SCREEN 16 | D=A 17 | @address 18 | M=D 19 | (LOOP) 20 | @address 21 | A=M 22 | M=-1 23 | @address 24 | D=M 25 | @32 26 | D=D+A 27 | @address 28 | M=D 29 | @counter 30 | MD=M-1 31 | @LOOP 32 | D;JGT 33 | (INFINITE_LOOP) 34 | @INFINITE_LOOP 35 | 0;JMP 36 | -------------------------------------------------------------------------------- /projects/05/ComputerAdd-external.cmp: -------------------------------------------------------------------------------- 1 | | time |reset|RAM16K[0]|RAM16K[1]|RAM16K[2]| 2 | | 0 | 0 | 0 | 0 | 0 | 3 | | 1 | 0 | 0 | 0 | 0 | 4 | | 2 | 0 | 0 | 0 | 0 | 5 | | 3 | 0 | 0 | 0 | 0 | 6 | | 4 | 0 | 0 | 0 | 0 | 7 | | 5 | 0 | 0 | 0 | 0 | 8 | | 6 | 0 | 5 | 0 | 0 | 9 | | 7 | 1 | 0 | 0 | 0 | 10 | | 8 | 0 | 0 | 0 | 0 | 11 | | 9 | 0 | 0 | 0 | 0 | 12 | | 10 | 0 | 0 | 0 | 0 | 13 | | 11 | 0 | 0 | 0 | 0 | 14 | | 12 | 0 | 0 | 0 | 0 | 15 | | 13 | 0 | 5 | 0 | 0 | 16 | -------------------------------------------------------------------------------- /projects/07/MemoryAccess/BasicTest/BasicTest.vm: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/07/MemoryAccess/BasicTest/BasicTest.vm 5 | 6 | // Executes pop & push commands using the virtual memory segments. 7 | push constant 10 8 | pop local 0 9 | push constant 21 10 | push constant 22 11 | pop argument 2 12 | pop argument 1 13 | push constant 36 14 | pop this 6 15 | push constant 42 16 | push constant 45 17 | pop that 5 18 | pop that 2 19 | push constant 510 20 | pop temp 6 21 | push local 0 22 | push that 5 23 | add 24 | push argument 1 25 | sub 26 | push this 6 27 | push this 6 28 | add 29 | sub 30 | push temp 6 31 | add 32 | -------------------------------------------------------------------------------- /projects/09/HelloWorld/Main.vm: -------------------------------------------------------------------------------- 1 | function Main.main 0 2 | push constant 11 3 | call String.new 1 4 | push constant 72 5 | call String.appendChar 2 6 | push constant 101 7 | call String.appendChar 2 8 | push constant 108 9 | call String.appendChar 2 10 | push constant 108 11 | call String.appendChar 2 12 | push constant 111 13 | call String.appendChar 2 14 | push constant 32 15 | call String.appendChar 2 16 | push constant 87 17 | call String.appendChar 2 18 | push constant 111 19 | call String.appendChar 2 20 | push constant 114 21 | call String.appendChar 2 22 | push constant 108 23 | call String.appendChar 2 24 | push constant 100 25 | call String.appendChar 2 26 | call Output.printString 1 27 | pop temp 0 28 | call Output.println 0 29 | pop temp 0 30 | push constant 0 31 | return 32 | -------------------------------------------------------------------------------- /projects/07/MemoryAccess/StaticTest/StaticTest.asm: -------------------------------------------------------------------------------- 1 | @111 2 | D=A 3 | @SP 4 | A=M 5 | M=D 6 | @SP 7 | M=M+1 8 | @333 9 | D=A 10 | @SP 11 | A=M 12 | M=D 13 | @SP 14 | M=M+1 15 | @888 16 | D=A 17 | @SP 18 | A=M 19 | M=D 20 | @SP 21 | M=M+1 22 | @24 23 | D=A 24 | @R13 25 | M=D 26 | @SP 27 | AM=M-1 28 | D=M 29 | @R13 30 | A=M 31 | M=D 32 | @19 33 | D=A 34 | @R13 35 | M=D 36 | @SP 37 | AM=M-1 38 | D=M 39 | @R13 40 | A=M 41 | M=D 42 | @17 43 | D=A 44 | @R13 45 | M=D 46 | @SP 47 | AM=M-1 48 | D=M 49 | @R13 50 | A=M 51 | M=D 52 | @19 53 | D=M 54 | @SP 55 | A=M 56 | M=D 57 | @SP 58 | M=M+1 59 | @17 60 | D=M 61 | @SP 62 | A=M 63 | M=D 64 | @SP 65 | M=M+1 66 | @SP 67 | AM=M-1 68 | D=M 69 | A=A-1 70 | M=M-D 71 | @24 72 | D=M 73 | @SP 74 | A=M 75 | M=D 76 | @SP 77 | M=M+1 78 | @SP 79 | AM=M-1 80 | D=M 81 | A=A-1 82 | M=M+D 83 | -------------------------------------------------------------------------------- /projects/09/PigDice/Player.jack: -------------------------------------------------------------------------------- 1 | /** 2 | Object for player 3 | */ 4 | class Player{ 5 | field int score; 6 | field Random random; 7 | 8 | constructor Player new(){ 9 | let random = Random.new(0); 10 | let score = 0; 11 | 12 | return this; 13 | } 14 | 15 | method int roll() { 16 | var int num; 17 | 18 | let num = random.randRange(5) + 1; 19 | 20 | if(num = 1){ 21 | let score = 0; 22 | } 23 | 24 | if(~(num = 1)){ 25 | let score = score + num; 26 | } 27 | 28 | return num; 29 | } 30 | 31 | method int getScore(){ 32 | return score; 33 | } 34 | 35 | method void changeRandom(int c){ 36 | 37 | do random.addCallTime(c); 38 | return; 39 | } 40 | 41 | method void dispose(){ 42 | do random.dispose(); 43 | do Memory.deAlloc(this); 44 | return; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /projects/00/Register.cmp: -------------------------------------------------------------------------------- 1 | | time | in |load | out | 2 | | 0+ | 0 | 0 | 0 | 3 | | 1 | 0 | 0 | 0 | 4 | | 1+ | 0 | 1 | 0 | 5 | | 2 | 0 | 1 | 0 | 6 | | 2+ | -32123 | 0 | 0 | 7 | | 3 | -32123 | 0 | 0 | 8 | | 3+ | 11111 | 0 | 0 | 9 | | 4 | 11111 | 0 | 0 | 10 | | 4+ | -32123 | 1 | 0 | 11 | | 5 | -32123 | 1 | -32123 | 12 | | 5+ | -32123 | 1 | -32123 | 13 | | 6 | -32123 | 1 | -32123 | 14 | | 6+ | -32123 | 0 | -32123 | 15 | | 7 | -32123 | 0 | -32123 | 16 | | 7+ | 12345 | 1 | -32123 | 17 | | 8 | 12345 | 1 | 12345 | 18 | | 8+ | 0 | 0 | 12345 | 19 | | 9 | 0 | 0 | 12345 | 20 | | 9+ | 0 | 1 | 12345 | 21 | | 10 | 0 | 1 | 0 | 22 | -------------------------------------------------------------------------------- /projects/01/DMux4Way.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/DMux4Way.tst 5 | 6 | load DMux4Way.hdl, 7 | output-file DMux4Way.out, 8 | compare-to DMux4Way.cmp, 9 | output-list in%B2.1.2 sel%B2.2.2 a%B2.1.2 b%B2.1.2 c%B2.1.2 d%B2.1.2; 10 | 11 | set in 0, 12 | set sel %B00, 13 | eval, 14 | output; 15 | 16 | set sel %B01, 17 | eval, 18 | output; 19 | 20 | set sel %B10, 21 | eval, 22 | output; 23 | 24 | set sel %B11, 25 | eval, 26 | output; 27 | 28 | set in 1, 29 | set sel %B00, 30 | eval, 31 | output; 32 | 33 | set sel %B01, 34 | eval, 35 | output; 36 | 37 | set sel %B10, 38 | eval, 39 | output; 40 | 41 | set sel %B11, 42 | eval, 43 | output; 44 | -------------------------------------------------------------------------------- /projects/08/ProgramFlow/BasicLoop/BasicLoop.vm: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/08/ProgramFlow/BasicLoop/BasicLoop.vm 5 | 6 | // Computes the sum 1 + 2 + ... + argument[0] and pushes the 7 | // result onto the stack. Argument[0] is initialized by the test 8 | // script before this code starts running. 9 | push constant 0 10 | pop local 0 // initialize sum = 0 11 | label LOOP_START 12 | push argument 0 13 | push local 0 14 | add 15 | pop local 0 // sum = sum + counter 16 | push argument 0 17 | push constant 1 18 | sub 19 | pop argument 0 // counter-- 20 | push argument 0 21 | if-goto LOOP_START // If counter > 0, goto LOOP_START 22 | push local 0 23 | -------------------------------------------------------------------------------- /projects/07/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /projects/02/FullAdder.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/02/FullAdder.tst 5 | 6 | load FullAdder.hdl, 7 | output-file FullAdder.out, 8 | compare-to FullAdder.cmp, 9 | output-list a%B3.1.3 b%B3.1.3 c%B3.1.3 sum%B3.1.3 carry%B3.1.3; 10 | 11 | set a 0, 12 | set b 0, 13 | set c 0, 14 | eval, 15 | output; 16 | 17 | set c 1, 18 | eval, 19 | output; 20 | 21 | set b 1, 22 | set c 0, 23 | eval, 24 | output; 25 | 26 | set c 1, 27 | eval, 28 | output; 29 | 30 | set a 1, 31 | set b 0, 32 | set c 0, 33 | eval, 34 | output; 35 | 36 | set c 1, 37 | eval, 38 | output; 39 | 40 | set b 1, 41 | set c 0, 42 | eval, 43 | output; 44 | 45 | set c 1, 46 | eval, 47 | output; 48 | -------------------------------------------------------------------------------- /projects/10/Complier/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /projects/01/Mux.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/Mux.tst 5 | 6 | load Mux.hdl, 7 | output-file Mux.out, 8 | compare-to Mux.cmp, 9 | output-list a%B3.1.3 b%B3.1.3 sel%B3.1.3 out%B3.1.3; 10 | 11 | set a 0, 12 | set b 0, 13 | set sel 0, 14 | eval, 15 | output; 16 | 17 | set sel 1, 18 | eval, 19 | output; 20 | 21 | set a 0, 22 | set b 1, 23 | set sel 0, 24 | eval, 25 | output; 26 | 27 | set sel 1, 28 | eval, 29 | output; 30 | 31 | set a 1, 32 | set b 0, 33 | set sel 0, 34 | eval, 35 | output; 36 | 37 | set sel 1, 38 | eval, 39 | output; 40 | 41 | set a 1, 42 | set b 1, 43 | set sel 0, 44 | eval, 45 | output; 46 | 47 | set sel 1, 48 | eval, 49 | output; 50 | -------------------------------------------------------------------------------- /projects/08/FunctionCalls/SimpleFunction/SimpleFunction.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/08/FunctionCalls/SimpleFunction/SimpleFunction.tst 5 | 6 | load SimpleFunction.asm, 7 | output-file SimpleFunction.out, 8 | compare-to SimpleFunction.cmp, 9 | output-list RAM[0]%D1.6.1 RAM[1]%D1.6.1 RAM[2]%D1.6.1 10 | RAM[3]%D1.6.1 RAM[4]%D1.6.1 RAM[310]%D1.6.1; 11 | 12 | set RAM[0] 317, 13 | set RAM[1] 317, 14 | set RAM[2] 310, 15 | set RAM[3] 3000, 16 | set RAM[4] 4000, 17 | set RAM[310] 1234, 18 | set RAM[311] 37, 19 | set RAM[312] 1000, 20 | set RAM[313] 305, 21 | set RAM[314] 300, 22 | set RAM[315] 3010, 23 | set RAM[316] 4010, 24 | 25 | repeat 300 { 26 | ticktock; 27 | } 28 | 29 | output; 30 | -------------------------------------------------------------------------------- /projects/04/fill/Fill.hack: -------------------------------------------------------------------------------- 1 | 0100000000000000 2 | 1110110000010000 3 | 0000000000010000 4 | 1110001110001000 5 | 0110000000000000 6 | 1111110000010000 7 | 0000000000001010 8 | 1110001100000001 9 | 0000000000011001 10 | 1110101010000111 11 | 0110000000000000 12 | 1111110000010000 13 | 0000000000010000 14 | 1111010011010000 15 | 0000000000000100 16 | 1110001100000010 17 | 0000000000010000 18 | 1111110000100000 19 | 1110111010001000 20 | 0000000000010000 21 | 1111110111010000 22 | 0000000000010000 23 | 1110001100001000 24 | 0000000000000100 25 | 1110101010000111 26 | 0100000000000000 27 | 1110110010010000 28 | 0000000000010000 29 | 1111010011010000 30 | 0000000000000100 31 | 1110001100000010 32 | 0000000000010000 33 | 1111110000100000 34 | 1110101010001000 35 | 0000000000010000 36 | 1111110010010000 37 | 0000000000010000 38 | 1110001100001000 39 | 0000000000000100 40 | 1110101010000111 41 | -------------------------------------------------------------------------------- /projects/08/FunctionCalls/SimpleFunction/SimpleFunctionVME.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/08/FunctionCalls/SimpleFunction/SimpleFunctionVME.tst 5 | 6 | load SimpleFunction.vm, 7 | output-file SimpleFunction.out, 8 | compare-to SimpleFunction.cmp, 9 | output-list RAM[0]%D1.6.1 RAM[1]%D1.6.1 RAM[2]%D1.6.1 10 | RAM[3]%D1.6.1 RAM[4]%D1.6.1 RAM[310]%D1.6.1; 11 | 12 | set sp 317, 13 | set local 317, 14 | set argument 310, 15 | set this 3000, 16 | set that 4000, 17 | set argument[0] 1234, 18 | set argument[1] 37, 19 | set argument[2] 9, 20 | set argument[3] 305, 21 | set argument[4] 300, 22 | set argument[5] 3010, 23 | set argument[6] 4010, 24 | 25 | repeat 10 { 26 | vmstep; 27 | } 28 | 29 | output; 30 | -------------------------------------------------------------------------------- /projects/06/max/Max.asm: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/06/max/Max.asm 5 | 6 | // Computes M[2] = max(M[0], M[1]) where M stands for RAM 7 | 8 | @0 9 | D=M // D = first number 10 | @1 11 | D=D-M // D = first number - second number 12 | @OUTPUT_FIRST 13 | D;JGT // if D>0 (first is greater) goto output_first 14 | @1 15 | D=M // D = second number 16 | @OUTPUT_D 17 | 0;JMP // goto output_d 18 | (OUTPUT_FIRST) 19 | @0 20 | D=M // D = first number 21 | (OUTPUT_D) 22 | @2 23 | M=D // M[2] = D (greatest number) 24 | (INFINITE_LOOP) 25 | @INFINITE_LOOP 26 | 0;JMP // infinite loop 27 | -------------------------------------------------------------------------------- /projects/01/Or16.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/Or16.tst 5 | 6 | load Or16.hdl, 7 | output-file Or16.out, 8 | compare-to Or16.cmp, 9 | output-list a%B1.16.1 b%B1.16.1 out%B1.16.1; 10 | 11 | set a %B0000000000000000, 12 | set b %B0000000000000000, 13 | eval, 14 | output; 15 | 16 | set a %B0000000000000000, 17 | set b %B1111111111111111, 18 | eval, 19 | output; 20 | 21 | set a %B1111111111111111, 22 | set b %B1111111111111111, 23 | eval, 24 | output; 25 | 26 | set a %B1010101010101010, 27 | set b %B0101010101010101, 28 | eval, 29 | output; 30 | 31 | set a %B0011110011000011, 32 | set b %B0000111111110000, 33 | eval, 34 | output; 35 | 36 | set a %B0001001000110100, 37 | set b %B1001100001110110, 38 | eval, 39 | output; -------------------------------------------------------------------------------- /projects/01/And16.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/And16.tst 5 | 6 | load And16.hdl, 7 | output-file And16.out, 8 | compare-to And16.cmp, 9 | output-list a%B1.16.1 b%B1.16.1 out%B1.16.1; 10 | 11 | set a %B0000000000000000, 12 | set b %B0000000000000000, 13 | eval, 14 | output; 15 | 16 | set a %B0000000000000000, 17 | set b %B1111111111111111, 18 | eval, 19 | output; 20 | 21 | set a %B1111111111111111, 22 | set b %B1111111111111111, 23 | eval, 24 | output; 25 | 26 | set a %B1010101010101010, 27 | set b %B0101010101010101, 28 | eval, 29 | output; 30 | 31 | set a %B0011110011000011, 32 | set b %B0000111111110000, 33 | eval, 34 | output; 35 | 36 | set a %B0001001000110100, 37 | set b %B1001100001110110, 38 | eval, 39 | output; -------------------------------------------------------------------------------- /projects/02/Add16.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/02/Add16.tst 5 | 6 | load Add16.hdl, 7 | output-file Add16.out, 8 | compare-to Add16.cmp, 9 | output-list a%B1.16.1 b%B1.16.1 out%B1.16.1; 10 | 11 | set a %B0000000000000000, 12 | set b %B0000000000000000, 13 | eval, 14 | output; 15 | 16 | set a %B0000000000000000, 17 | set b %B1111111111111111, 18 | eval, 19 | output; 20 | 21 | set a %B1111111111111111, 22 | set b %B1111111111111111, 23 | eval, 24 | output; 25 | 26 | set a %B1010101010101010, 27 | set b %B0101010101010101, 28 | eval, 29 | output; 30 | 31 | set a %B0011110011000011, 32 | set b %B0000111111110000, 33 | eval, 34 | output; 35 | 36 | set a %B0001001000110100, 37 | set b %B1001100001110110, 38 | eval, 39 | output; 40 | -------------------------------------------------------------------------------- /projects/07/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 1.7 12 | 13 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /projects/05/ComputerRect-external.out: -------------------------------------------------------------------------------- 1 | | time | 2 | | 0 | 3 | | 1 | 4 | | 2 | 5 | | 3 | 6 | | 4 | 7 | | 5 | 8 | | 6 | 9 | | 7 | 10 | | 8 | 11 | | 9 | 12 | | 10 | 13 | | 11 | 14 | | 12 | 15 | | 13 | 16 | | 14 | 17 | | 15 | 18 | | 16 | 19 | | 17 | 20 | | 18 | 21 | | 19 | 22 | | 20 | 23 | | 21 | 24 | | 22 | 25 | | 23 | 26 | | 24 | 27 | | 25 | 28 | | 26 | 29 | | 27 | 30 | | 28 | 31 | | 29 | 32 | | 30 | 33 | | 31 | 34 | | 32 | 35 | | 33 | 36 | | 34 | 37 | | 35 | 38 | | 36 | 39 | | 37 | 40 | | 38 | 41 | | 39 | 42 | | 40 | 43 | | 41 | 44 | | 42 | 45 | | 43 | 46 | | 44 | 47 | | 45 | 48 | | 46 | 49 | | 47 | 50 | | 48 | 51 | | 49 | 52 | | 50 | 53 | | 51 | 54 | | 52 | 55 | | 53 | 56 | | 54 | 57 | | 55 | 58 | | 56 | 59 | | 57 | 60 | | 58 | 61 | | 59 | 62 | | 60 | 63 | | 61 | 64 | | 62 | 65 | | 63 | 66 | -------------------------------------------------------------------------------- /projects/11/src/Symbol.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by xuchen on 11/26/14. 3 | */ 4 | public class Symbol { 5 | 6 | public static enum KIND {STATIC, FIELD, ARG, VAR, NONE}; 7 | 8 | private String type; 9 | private KIND kind; 10 | private int index; 11 | 12 | public Symbol(String type, KIND kind, int index) { 13 | this.type = type; 14 | this.kind = kind; 15 | this.index = index; 16 | } 17 | 18 | public String getType() { 19 | return type; 20 | } 21 | 22 | public KIND getKind() { 23 | return kind; 24 | } 25 | 26 | public int getIndex() { 27 | return index; 28 | } 29 | 30 | @Override 31 | public String toString() { 32 | return "Symbol{" + 33 | "type='" + type + '\'' + 34 | ", kind=" + kind + 35 | ", index=" + index + 36 | '}'; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /projects/01/Mux16.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/Mux16.tst 5 | 6 | load Mux16.hdl, 7 | output-file Mux16.out, 8 | compare-to Mux16.cmp, 9 | output-list a%B1.16.1 b%B1.16.1 sel%D2.1.2 out%B1.16.1; 10 | 11 | set a 0, 12 | set b 0, 13 | set sel 0, 14 | eval, 15 | output; 16 | 17 | set sel 1, 18 | eval, 19 | output; 20 | 21 | set a %B0000000000000000, 22 | set b %B0001001000110100, 23 | set sel 0, 24 | eval, 25 | output; 26 | 27 | set sel 1, 28 | eval, 29 | output; 30 | 31 | set a %B1001100001110110, 32 | set b %B0000000000000000, 33 | set sel 0, 34 | eval, 35 | output; 36 | 37 | set sel 1, 38 | eval, 39 | output; 40 | 41 | set a %B1010101010101010, 42 | set b %B0101010101010101, 43 | set sel 0, 44 | eval, 45 | output; 46 | 47 | set sel 1, 48 | eval, 49 | output; -------------------------------------------------------------------------------- /projects/01/Not16.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/Not16.hdl 5 | 6 | /** 7 | * 16-bit Not gate: for i=0..15: out[i] = not in[i] 8 | */ 9 | 10 | CHIP Not16 { 11 | IN in[16]; 12 | OUT out[16]; 13 | 14 | PARTS: 15 | Not(in=in[0],out=out[0]); 16 | Not(in=in[1],out=out[1]); 17 | Not(in=in[2],out=out[2]); 18 | Not(in=in[3],out=out[3]); 19 | Not(in=in[4],out=out[4]); 20 | Not(in=in[5],out=out[5]); 21 | Not(in=in[6],out=out[6]); 22 | Not(in=in[7],out=out[7]); 23 | Not(in=in[8],out=out[8]); 24 | Not(in=in[9],out=out[9]); 25 | Not(in=in[10],out=out[10]); 26 | Not(in=in[11],out=out[11]); 27 | Not(in=in[12],out=out[12]); 28 | Not(in=in[13],out=out[13]); 29 | Not(in=in[14],out=out[14]); 30 | Not(in=in[15],out=out[15]); 31 | } -------------------------------------------------------------------------------- /projects/01/Mux4Way16.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/Mux4Way16.tst 5 | 6 | load Mux4Way16.hdl, 7 | output-file Mux4Way16.out, 8 | compare-to Mux4Way16.cmp, 9 | output-list a%B1.16.1 b%B1.16.1 c%B1.16.1 d%B1.16.1 sel%B2.2.2 out%B1.16.1; 10 | 11 | set a 0, 12 | set b 0, 13 | set c 0, 14 | set d 0, 15 | set sel 0, 16 | eval, 17 | output; 18 | 19 | set sel 1, 20 | eval, 21 | output; 22 | 23 | set sel 2, 24 | eval, 25 | output; 26 | 27 | set sel 3, 28 | eval, 29 | output; 30 | 31 | set a %B0001001000110100, 32 | set b %B1001100001110110, 33 | set c %B1010101010101010, 34 | set d %B0101010101010101, 35 | set sel 0, 36 | eval, 37 | output; 38 | 39 | set sel 1, 40 | eval, 41 | output; 42 | 43 | set sel 2, 44 | eval, 45 | output; 46 | 47 | set sel 3, 48 | eval, 49 | output; 50 | -------------------------------------------------------------------------------- /projects/01/Mux4Way16.out: -------------------------------------------------------------------------------- 1 | | a | b | c | d | sel | out | 2 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 00 | 0000000000000000 | 3 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 01 | 0000000000000000 | 4 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 10 | 0000000000000000 | 5 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 11 | 0000000000000000 | 6 | | 0001001000110100 | 1001100001110110 | 1010101010101010 | 0101010101010101 | 00 | 0001001000110100 | 7 | | 0001001000110100 | 1001100001110110 | 1010101010101010 | 0101010101010101 | 01 | 1001100001110110 | 8 | | 0001001000110100 | 1001100001110110 | 1010101010101010 | 0101010101010101 | 10 | 1010101010101010 | 9 | | 0001001000110100 | 1001100001110110 | 1010101010101010 | 0101010101010101 | 11 | 0101010101010101 | 10 | -------------------------------------------------------------------------------- /projects/11/Average/Main.jack: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/11/Average/Main.jack 5 | 6 | /** Computes the average of a sequence of integers */ 7 | class Main { 8 | function void main() { 9 | var Array a; 10 | var int length; 11 | var int i, sum; 12 | 13 | let length = Keyboard.readInt("How many numbers? "); 14 | let a = Array.new(length); 15 | let i = 0; 16 | 17 | while (i < length) { 18 | let a[i] = Keyboard.readInt("Enter the next number: "); 19 | let i = i + 1; 20 | } 21 | 22 | let i = 0; 23 | let sum = 0; 24 | 25 | while (i < length) { 26 | let sum = sum + a[i]; 27 | let i = i + 1; 28 | } 29 | 30 | do Output.printString("The average is: "); 31 | do Output.printInt(sum / length); 32 | do Output.println(); 33 | 34 | return; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /projects/05/ComputerRect-external.cmp: -------------------------------------------------------------------------------- 1 | | time | 2 | | 0 | 3 | | 1 | 4 | | 2 | 5 | | 3 | 6 | | 4 | 7 | | 5 | 8 | | 6 | 9 | | 7 | 10 | | 8 | 11 | | 9 | 12 | | 10 | 13 | | 11 | 14 | | 12 | 15 | | 13 | 16 | | 14 | 17 | | 15 | 18 | | 16 | 19 | | 17 | 20 | | 18 | 21 | | 19 | 22 | | 20 | 23 | | 21 | 24 | | 22 | 25 | | 23 | 26 | | 24 | 27 | | 25 | 28 | | 26 | 29 | | 27 | 30 | | 28 | 31 | | 29 | 32 | | 30 | 33 | | 31 | 34 | | 32 | 35 | | 33 | 36 | | 34 | 37 | | 35 | 38 | | 36 | 39 | | 37 | 40 | | 38 | 41 | | 39 | 42 | | 40 | 43 | | 41 | 44 | | 42 | 45 | | 43 | 46 | | 44 | 47 | | 45 | 48 | | 46 | 49 | | 47 | 50 | | 48 | 51 | | 49 | 52 | | 50 | 53 | | 51 | 54 | | 52 | 55 | | 53 | 56 | | 54 | 57 | | 55 | 58 | | 56 | 59 | | 57 | 60 | | 58 | 61 | | 59 | 62 | | 60 | 63 | | 61 | 64 | | 62 | 65 | | 63 | 66 | -------------------------------------------------------------------------------- /projects/10/ArrayTest/Main.jack: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/10/ArrayTest/Main.jack 5 | 6 | /** Computes the average of a sequence of integers. */ 7 | class Main { 8 | function void main() { 9 | var Array a; 10 | var int length; 11 | var int i, sum; 12 | 13 | let length = Keyboard.readInt("HOW MANY NUMBERS? "); 14 | let a = Array.new(length); 15 | let i = 0; 16 | 17 | while (i < length) { 18 | let a[i] = Keyboard.readInt("ENTER THE NEXT NUMBER: "); 19 | let i = i + 1; 20 | } 21 | 22 | let i = 0; 23 | let sum = 0; 24 | 25 | while (i < length) { 26 | let sum = sum + a[i]; 27 | let i = i + 1; 28 | } 29 | 30 | do Output.printString("THE AVERAGE IS: "); 31 | do Output.printInt(sum / length); 32 | do Output.println(); 33 | 34 | return; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /projects/01/Mux4Way16.cmp: -------------------------------------------------------------------------------- 1 | | a | b | c | d | sel | out | 2 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 00 | 0000000000000000 | 3 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 01 | 0000000000000000 | 4 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 10 | 0000000000000000 | 5 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 11 | 0000000000000000 | 6 | | 0001001000110100 | 1001100001110110 | 1010101010101010 | 0101010101010101 | 00 | 0001001000110100 | 7 | | 0001001000110100 | 1001100001110110 | 1010101010101010 | 0101010101010101 | 01 | 1001100001110110 | 8 | | 0001001000110100 | 1001100001110110 | 1010101010101010 | 0101010101010101 | 10 | 1010101010101010 | 9 | | 0001001000110100 | 1001100001110110 | 1010101010101010 | 0101010101010101 | 11 | 0101010101010101 | 10 | -------------------------------------------------------------------------------- /projects/05/ComputerAdd-external.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/05/ComputerAdd-external.tst 5 | 6 | load Computer.hdl, 7 | output-file ComputerAdd-external.out, 8 | compare-to ComputerAdd-external.cmp, 9 | output-list time%S1.4.1 reset%B2.1.2 RAM16K[0]%D1.7.1 RAM16K[1]%D1.7.1 RAM16K[2]%D1.7.1; 10 | 11 | // Load a program written in the Hack machine language. 12 | // The program adds the two constants 2 and 3 and writes the result in RAM[0]. 13 | ROM32K load Add.hack, 14 | output; 15 | 16 | // First run (at the beginning PC=0) 17 | repeat 6 { 18 | tick, tock, output; 19 | } 20 | 21 | // Reset the PC 22 | set reset 1, 23 | set RAM16K[0] 0, 24 | tick, tock, output; 25 | 26 | 27 | // Second run, to check that the PC was reset correctly. 28 | set reset 0, 29 | 30 | repeat 6 { 31 | tick, tock, output; 32 | } 33 | -------------------------------------------------------------------------------- /projects/05/ComputerAdd.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/05/ComputerAdd.tst 5 | 6 | load Computer.hdl, 7 | output-file ComputerAdd.out, 8 | compare-to ComputerAdd.cmp, 9 | output-list time%S1.4.1 reset%B2.1.2 ARegister[0]%D1.7.1 DRegister[0]%D1.7.1 PC[]%D0.4.0 RAM16K[0]%D1.7.1 RAM16K[1]%D1.7.1 RAM16K[2]%D1.7.1; 10 | 11 | // Load a program written in the Hack machine language. 12 | // The program adds the two constants 2 and 3 and writes the result in RAM[0]. 13 | ROM32K load Add.hack, 14 | output; 15 | 16 | // First run (at the beginning PC=0) 17 | repeat 6 { 18 | tick, tock, output; 19 | } 20 | 21 | // Reset the PC 22 | set reset 1, 23 | set RAM16K[0] 0, 24 | tick, tock, output; 25 | 26 | 27 | // Second run, to check that the PC was reset correctly. 28 | set reset 0, 29 | 30 | repeat 6 { 31 | tick, tock, output; 32 | } 33 | -------------------------------------------------------------------------------- /projects/05/ComputerRect-external.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/05/ComputerRect-external.tst 5 | 6 | load Computer.hdl, 7 | output-file ComputerRect-external.out, 8 | compare-to ComputerRect-external.cmp, 9 | output-list time%S1.4.1; 10 | 11 | // Load a program written in the Hack machine language. 12 | // The program draws a rectangle of width 16 pixels and 13 | // length RAM[0] at the top left of the screen. 14 | ROM32K load Rect.hack, 15 | 16 | echo "Before you run this script, select the 'Screen' option from the 'View' menu"; 17 | 18 | echo "A small rectangle should be drawn at the top left of the screen (the 'Screen' option of the 'View' menu should be selected.)"; 19 | 20 | // draw a rectangle 16 pixels wide and 4 pixels long 21 | set RAM16K[0] 4, 22 | output; 23 | 24 | repeat 63 { 25 | tick, tock, output; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /projects/10/Complier/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 1.7 15 | 16 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /projects/07/StackArithmetic/StackTest/StackTest.vm: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/07/StackArithmetic/StackTest/StackTest.vm 5 | 6 | // Executes a sequence of arithmetic and logical operations 7 | // on the stack. 8 | push constant 17 9 | push constant 17 10 | eq 11 | push constant 17 12 | push constant 16 13 | eq 14 | push constant 16 15 | push constant 17 16 | eq 17 | push constant 892 18 | push constant 891 19 | lt 20 | push constant 891 21 | push constant 892 22 | lt 23 | push constant 891 24 | push constant 891 25 | lt 26 | push constant 32767 27 | push constant 32766 28 | gt 29 | push constant 32766 30 | push constant 32767 31 | gt 32 | push constant 32766 33 | push constant 32766 34 | gt 35 | push constant 57 36 | push constant 31 37 | push constant 53 38 | add 39 | push constant 112 40 | sub 41 | neg 42 | and 43 | push constant 82 44 | or 45 | not 46 | -------------------------------------------------------------------------------- /projects/12/SysTest/Main.jack: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/12/SysTest/Main.jack 5 | 6 | /** Test program for the OS Sys class. */ 7 | class Main { 8 | 9 | /** tests the wait method of the Sys class. */ 10 | function void main() { 11 | var char key; 12 | 13 | do Output.printString("Wait test:"); 14 | do Output.println(); 15 | do Output.printString("Press any key. After 2 seconds, another message will be printed:"); 16 | 17 | while (key = 0) { 18 | let key = Keyboard.keyPressed(); 19 | } 20 | while (~(key = 0)) { 21 | let key = Keyboard.keyPressed(); 22 | } 23 | 24 | do Sys.wait(2000); 25 | 26 | do Output.println(); 27 | do Output.printString("Time is up. Make sure that 2 seconds had passed."); 28 | 29 | return; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /projects/01/Or16.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/Or16.hdl 5 | 6 | 7 | 8 | /** 9 | * 16-bit bitwise Or gate: for i=0..15 out[i] = a[i] or b[i]. 10 | */ 11 | 12 | CHIP Or16 { 13 | IN a[16], b[16]; 14 | OUT out[16]; 15 | 16 | PARTS: 17 | Or(a=a[0],b=b[0],out=out[0]); 18 | Or(a=a[1],b=b[1],out=out[1]); 19 | Or(a=a[2],b=b[2],out=out[2]); 20 | Or(a=a[3],b=b[3],out=out[3]); 21 | Or(a=a[4],b=b[4],out=out[4]); 22 | Or(a=a[5],b=b[5],out=out[5]); 23 | Or(a=a[6],b=b[6],out=out[6]); 24 | Or(a=a[7],b=b[7],out=out[7]); 25 | Or(a=a[8],b=b[8],out=out[8]); 26 | Or(a=a[9],b=b[9],out=out[9]); 27 | Or(a=a[10],b=b[10],out=out[10]); 28 | Or(a=a[11],b=b[11],out=out[11]); 29 | Or(a=a[12],b=b[12],out=out[12]); 30 | Or(a=a[13],b=b[13],out=out[13]); 31 | Or(a=a[14],b=b[14],out=out[14]); 32 | Or(a=a[15],b=b[15],out=out[15]); 33 | } -------------------------------------------------------------------------------- /projects/01/And16.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/And16.hdl 5 | 6 | /** 7 | * 16-bit-wise and gate: for i = 0..15: out[i] = a[i] and b[i] 8 | */ 9 | 10 | CHIP And16 { 11 | IN a[16], b[16]; 12 | OUT out[16]; 13 | 14 | PARTS: 15 | And(a=a[0],b=b[0],out=out[0]); 16 | And(a=a[1],b=b[1],out=out[1]); 17 | And(a=a[2],b=b[2],out=out[2]); 18 | And(a=a[3],b=b[3],out=out[3]); 19 | And(a=a[4],b=b[4],out=out[4]); 20 | And(a=a[5],b=b[5],out=out[5]); 21 | And(a=a[6],b=b[6],out=out[6]); 22 | And(a=a[7],b=b[7],out=out[7]); 23 | And(a=a[8],b=b[8],out=out[8]); 24 | And(a=a[9],b=b[9],out=out[9]); 25 | And(a=a[10],b=b[10],out=out[10]); 26 | And(a=a[11],b=b[11],out=out[11]); 27 | And(a=a[12],b=b[12],out=out[12]); 28 | And(a=a[13],b=b[13],out=out[13]); 29 | And(a=a[14],b=b[14],out=out[14]); 30 | And(a=a[15],b=b[15],out=out[15]); 31 | } -------------------------------------------------------------------------------- /projects/03/PC.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/03/a/PC.hdl 5 | 6 | /** 7 | * A 16-bit counter with load and reset control bits. 8 | * if (reset[t]==1) out[t+1] = 0 9 | * else if (load[t]==1) out[t+1] = in[t] 10 | * else if (inc[t]==1) out[t+1] = out[t] + 1 (integer addition) 11 | * else out[t+1] = out[t] 12 | */ 13 | 14 | CHIP PC { 15 | IN in[16],load,inc,reset; 16 | OUT out[16]; 17 | 18 | PARTS: 19 | Inc16(in=out5,out=out1); 20 | Mux16(a=out5,b=out1,sel=inc,out=out2); 21 | Mux16(a=out2,b=in,sel=load,out=out3); 22 | Mux16(a=out3,b=false,sel=reset,out=out4); 23 | Register(in=out4,load=true,out=out5,out=out); 24 | 25 | } 26 | 27 | /** 28 | * Reference: http://nand2tetris-questions-and-answers-forum.32033.n3.nabble.com/PC-Hdl-td4026543.html 29 | * User cadet1620's answer really inspired me 30 | * 31 | */ 32 | 33 | 34 | -------------------------------------------------------------------------------- /projects/03/a/PC.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/03/a/PC.hdl 5 | 6 | /** 7 | * A 16-bit counter with load and reset control bits. 8 | * if (reset[t]==1) out[t+1] = 0 9 | * else if (load[t]==1) out[t+1] = in[t] 10 | * else if (inc[t]==1) out[t+1] = out[t] + 1 (integer addition) 11 | * else out[t+1] = out[t] 12 | */ 13 | 14 | CHIP PC { 15 | IN in[16],load,inc,reset; 16 | OUT out[16]; 17 | 18 | PARTS: 19 | Inc16(in=out5,out=out1); 20 | Mux16(a=out5,b=out1,sel=inc,out=out2); 21 | Mux16(a=out2,b=in,sel=load,out=out3); 22 | Mux16(a=out3,b=false,sel=reset,out=out4); 23 | Register(in=out4,load=true,out=out5,out=out); 24 | 25 | } 26 | 27 | /** 28 | * Reference: http://nand2tetris-questions-and-answers-forum.32033.n3.nabble.com/PC-Hdl-td4026543.html 29 | * User cadet1620's answer really inspired me 30 | * 31 | */ 32 | 33 | 34 | -------------------------------------------------------------------------------- /projects/03/RAM16K.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/03/b/RAM16K.hdl 5 | 6 | /** 7 | * Memory of 16K registers, each 16 bit-wide. Out holds the value 8 | * stored at the memory location specified by address. If load=1, then 9 | * the in value is loaded into the memory location specified by address 10 | * (the loaded value will be emitted to out after the next time step.) 11 | */ 12 | 13 | CHIP RAM16K { 14 | IN in[16], load, address[14]; 15 | OUT out[16]; 16 | 17 | PARTS: 18 | DMux4Way(in=load,sel=address[12..13],a=load0,b=load1,c=load2,d=load3); 19 | RAM4K(in=in,load=load0,address=address[0..11],out=out0); 20 | RAM4K(in=in,load=load1,address=address[0..11],out=out1); 21 | RAM4K(in=in,load=load2,address=address[0..11],out=out2); 22 | RAM4K(in=in,load=load3,address=address[0..11],out=out3); 23 | Mux4Way16(a=out0,b=out1,c=out2,d=out3,sel=address[12..13],out=out); 24 | } -------------------------------------------------------------------------------- /projects/03/b/RAM16K.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/03/b/RAM16K.hdl 5 | 6 | /** 7 | * Memory of 16K registers, each 16 bit-wide. Out holds the value 8 | * stored at the memory location specified by address. If load=1, then 9 | * the in value is loaded into the memory location specified by address 10 | * (the loaded value will be emitted to out after the next time step.) 11 | */ 12 | 13 | CHIP RAM16K { 14 | IN in[16], load, address[14]; 15 | OUT out[16]; 16 | 17 | PARTS: 18 | DMux4Way(in=load,sel=address[12..13],a=load0,b=load1,c=load2,d=load3); 19 | RAM4K(in=in,load=load0,address=address[0..11],out=out0); 20 | RAM4K(in=in,load=load1,address=address[0..11],out=out1); 21 | RAM4K(in=in,load=load2,address=address[0..11],out=out2); 22 | RAM4K(in=in,load=load3,address=address[0..11],out=out3); 23 | Mux4Way16(a=out0,b=out1,c=out2,d=out3,sel=address[12..13],out=out); 24 | } -------------------------------------------------------------------------------- /projects/05/ComputerRect.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/05/ComputerRect.tst 5 | 6 | load Computer.hdl, 7 | output-file ComputerRect.out, 8 | compare-to ComputerRect.cmp, 9 | output-list time%S1.4.1 ARegister[]%D1.7.1 DRegister[]%D1.7.1 PC[]%D0.4.0 RAM16K[0]%D1.7.1 RAM16K[1]%D1.7.1 RAM16K[2]%D1.7.1; 10 | 11 | // Load a program written in the Hack machine language. 12 | // The program draws a rectangle of width 16 pixels and 13 | // length RAM[0] at the top left of the screen. 14 | ROM32K load Rect.hack, 15 | 16 | echo "Before you run this script, select the 'Screen' option from the 'View' menu"; 17 | 18 | echo "A small rectangle should be drawn at the top left of the screen (the 'Screen' option of the 'View' menu should be selected.)"; 19 | 20 | // Draws a rectangle 16 pixels wide and 4 pixels long 21 | set RAM16K[0] 4, 22 | output; 23 | 24 | repeat 63 { 25 | tick, tock, output; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /projects/05/Computer.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/05/Computer.hdl 5 | 6 | /** 7 | * The HACK computer, including CPU, ROM and RAM. 8 | * When reset is 0, the program stored in the computer's ROM executes. 9 | * When reset is 1, the execution of the program restarts. 10 | * Thus, to start a program's execution, reset must be pushed "up" (1) 11 | * and "down" (0). From this point onward the user is at the mercy of 12 | * the software. In particular, depending on the program's code, the 13 | * screen may show some output and the user may be able to interact 14 | * with the computer via the keyboard. 15 | */ 16 | 17 | CHIP Computer { 18 | 19 | IN reset; 20 | 21 | PARTS: 22 | CPU(instruction=instruction,reset=reset,inM=outMemo,outM=CPUoutM,writeM=wM,addressM=adM,pc=PC); 23 | Memory(in=CPUoutM,load=wM,address=adM,out=outMemo); 24 | ROM32K(address=PC,out=instruction); 25 | } 26 | -------------------------------------------------------------------------------- /projects/05/ComputerAdd.out: -------------------------------------------------------------------------------- 1 | | time |reset|ARegister|DRegister|PC[]|RAM16K[0]|RAM16K[1]|RAM16K[2]| 2 | | 0 | 0 | 0 | 0 | 0| 0 | 0 | 0 | 3 | | 1 | 0 | 2 | 0 | 1| 0 | 0 | 0 | 4 | | 2 | 0 | 2 | 2 | 2| 0 | 0 | 0 | 5 | | 3 | 0 | 3 | 2 | 3| 0 | 0 | 0 | 6 | | 4 | 0 | 3 | 5 | 4| 0 | 0 | 0 | 7 | | 5 | 0 | 0 | 5 | 5| 0 | 0 | 0 | 8 | | 6 | 0 | 0 | 5 | 6| 5 | 0 | 0 | 9 | | 7 | 1 | 0 | 5 | 0| 0 | 0 | 0 | 10 | | 8 | 0 | 2 | 5 | 1| 0 | 0 | 0 | 11 | | 9 | 0 | 2 | 2 | 2| 0 | 0 | 0 | 12 | | 10 | 0 | 3 | 2 | 3| 0 | 0 | 0 | 13 | | 11 | 0 | 3 | 5 | 4| 0 | 0 | 0 | 14 | | 12 | 0 | 0 | 5 | 5| 0 | 0 | 0 | 15 | | 13 | 0 | 0 | 5 | 6| 5 | 0 | 0 | 16 | -------------------------------------------------------------------------------- /projects/08/FunctionCalls/FibonacciElement/Main.vm: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/08/FunctionCalls/FibonacciElement/Main.vm 5 | 6 | // Computes the n'th element of the Fibonacci series, recursively. 7 | // n is given in argument[0]. Called by the Sys.init function 8 | // (part of the Sys.vm file), which also pushes the argument[0] 9 | // parameter before this code starts running. 10 | 11 | function Main.fibonacci 0 12 | push argument 0 13 | push constant 2 14 | lt // check if n < 2 15 | if-goto IF_TRUE 16 | goto IF_FALSE 17 | label IF_TRUE // if n<2, return n 18 | push argument 0 19 | return 20 | label IF_FALSE // if n>=2, return fib(n-2)+fib(n-1) 21 | push argument 0 22 | push constant 2 23 | sub 24 | call Main.fibonacci 1 // compute fib(n-2) 25 | push argument 0 26 | push constant 1 27 | sub 28 | call Main.fibonacci 1 // compute fib(n-1) 29 | add // return fib(n-1) + fib(n-2) 30 | return 31 | -------------------------------------------------------------------------------- /projects/05/ComputerAdd.cmp: -------------------------------------------------------------------------------- 1 | | time |reset|ARegister|DRegister|PC[]|RAM16K[0]|RAM16K[1]|RAM16K[2]| 2 | | 0 | 0 | 0 | 0 | 0| 0 | 0 | 0 | 3 | | 1 | 0 | 2 | 0 | 1| 0 | 0 | 0 | 4 | | 2 | 0 | 2 | 2 | 2| 0 | 0 | 0 | 5 | | 3 | 0 | 3 | 2 | 3| 0 | 0 | 0 | 6 | | 4 | 0 | 3 | 5 | 4| 0 | 0 | 0 | 7 | | 5 | 0 | 0 | 5 | 5| 0 | 0 | 0 | 8 | | 6 | 0 | 0 | 5 | 6| 5 | 0 | 0 | 9 | | 7 | 1 | 0 | 5 | 0| 0 | 0 | 0 | 10 | | 8 | 0 | 2 | 5 | 1| 0 | 0 | 0 | 11 | | 9 | 0 | 2 | 2 | 2| 0 | 0 | 0 | 12 | | 10 | 0 | 3 | 2 | 3| 0 | 0 | 0 | 13 | | 11 | 0 | 3 | 5 | 4| 0 | 0 | 0 | 14 | | 12 | 0 | 0 | 5 | 5| 0 | 0 | 0 | 15 | | 13 | 0 | 0 | 5 | 6| 5 | 0 | 0 | 16 | -------------------------------------------------------------------------------- /projects/01/DMux8Way.out: -------------------------------------------------------------------------------- 1 | | in | sel | a | b | c | d | e | f | g | h | 2 | | 0 | 00 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 3 | | 0 | 01 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 4 | | 0 | 10 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 5 | | 0 | 11 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 6 | | 0 | 00 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 7 | | 0 | 01 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 8 | | 0 | 10 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 9 | | 0 | 11 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 10 | | 1 | 00 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 11 | | 1 | 01 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 12 | | 1 | 10 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 13 | | 1 | 11 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 14 | | 1 | 00 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 15 | | 1 | 01 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 16 | | 1 | 10 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 17 | | 1 | 11 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 18 | -------------------------------------------------------------------------------- /projects/05/ComputerMax-external.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/05/ComputerMax-external.tst 5 | 6 | load Computer.hdl, 7 | output-file ComputerMax-external.out, 8 | compare-to ComputerMax-external.cmp, 9 | output-list time%S1.4.1 reset%B2.1.2 RAM16K[0]%D1.7.1 RAM16K[1]%D1.7.1 RAM16K[2]%D1.7.1; 10 | 11 | // Load a program written in the Hack machine language. 12 | // The program computes the maximum of RAM[0] and RAM[1] 13 | // and writes the result in RAM[2]. 14 | ROM32K load Max.hack, 15 | 16 | // first run: compute max(3,5) 17 | set RAM16K[0] 3, 18 | set RAM16K[1] 5, 19 | output; 20 | 21 | repeat 14 { 22 | tick, tock, output; 23 | } 24 | 25 | // reset the PC 26 | set reset 1, 27 | tick, tock, output; 28 | 29 | // second run: compute max(23456,12345) 30 | set reset 0, 31 | set RAM16K[0] 23456, 32 | set RAM16K[1] 12345, 33 | output; 34 | 35 | // The run on these inputs needs less cycles (different branching) 36 | repeat 10 { 37 | tick, tock, output; 38 | } 39 | -------------------------------------------------------------------------------- /projects/01/DMux8Way.cmp: -------------------------------------------------------------------------------- 1 | | in | sel | a | b | c | d | e | f | g | h | 2 | | 0 | 00 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 3 | | 0 | 01 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 4 | | 0 | 10 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 5 | | 0 | 11 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 6 | | 0 | 00 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 7 | | 0 | 01 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 8 | | 0 | 10 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 9 | | 0 | 11 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 10 | | 1 | 00 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 11 | | 1 | 01 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 12 | | 1 | 10 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 13 | | 1 | 11 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 14 | | 1 | 00 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 15 | | 1 | 01 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 16 | | 1 | 10 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 17 | | 1 | 11 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 18 | -------------------------------------------------------------------------------- /projects/05/ComputerMax.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/05/ComputerMax.tst 5 | 6 | load Computer.hdl, 7 | output-file ComputerMax.out, 8 | compare-to ComputerMax.cmp, 9 | output-list time%S1.4.1 reset%B2.1.2 ARegister[]%D1.7.1 DRegister[]%D1.7.1 PC[]%D0.4.0 RAM16K[0]%D1.7.1 RAM16K[1]%D1.7.1 RAM16K[2]%D1.7.1; 10 | 11 | // Load a program written in the Hack machine language. 12 | // The program computes the maximum of RAM[0] and RAM[1] 13 | // and writes the result in RAM[2]. 14 | 15 | ROM32K load Max.hack, 16 | 17 | // first run: compute max(3,5) 18 | set RAM16K[0] 3, 19 | set RAM16K[1] 5, 20 | output; 21 | 22 | repeat 14 { 23 | tick, tock, output; 24 | } 25 | 26 | // reset the PC 27 | set reset 1, 28 | tick, tock, output; 29 | 30 | // second run: compute max(23456,12345) 31 | set reset 0, 32 | set RAM16K[0] 23456, 33 | set RAM16K[1] 12345, 34 | output; 35 | 36 | // The run on these inputs needs less cycles (different branching) 37 | repeat 10 { 38 | tick, tock, output; 39 | } 40 | -------------------------------------------------------------------------------- /projects/10/ExpressionlessSquare/MainT.xml: -------------------------------------------------------------------------------- 1 | 2 | class 3 | Main 4 | { 5 | function 6 | void 7 | main 8 | ( 9 | ) 10 | { 11 | var 12 | SquareGame 13 | game 14 | ; 15 | let 16 | game 17 | = 18 | game 19 | ; 20 | do 21 | game 22 | . 23 | run 24 | ( 25 | ) 26 | ; 27 | do 28 | game 29 | . 30 | dispose 31 | ( 32 | ) 33 | ; 34 | return 35 | ; 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /projects/03/Register.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/03/a/Register.hdl 5 | 6 | /** 7 | * 16-bit register. 8 | * If load[t]=1 then out[t+1] = in[t] 9 | * else out does not change 10 | */ 11 | 12 | CHIP Register { 13 | IN in[16], load; 14 | OUT out[16]; 15 | 16 | PARTS: 17 | Bit(in=in[0],load=load,out=out[0]); 18 | Bit(in=in[1],load=load,out=out[1]); 19 | Bit(in=in[2],load=load,out=out[2]); 20 | Bit(in=in[3],load=load,out=out[3]); 21 | Bit(in=in[4],load=load,out=out[4]); 22 | Bit(in=in[5],load=load,out=out[5]); 23 | Bit(in=in[6],load=load,out=out[6]); 24 | Bit(in=in[7],load=load,out=out[7]); 25 | Bit(in=in[8],load=load,out=out[8]); 26 | Bit(in=in[9],load=load,out=out[9]); 27 | Bit(in=in[10],load=load,out=out[10]); 28 | Bit(in=in[11],load=load,out=out[11]); 29 | Bit(in=in[12],load=load,out=out[12]); 30 | Bit(in=in[13],load=load,out=out[13]); 31 | Bit(in=in[14],load=load,out=out[14]); 32 | Bit(in=in[15],load=load,out=out[15]); 33 | } 34 | -------------------------------------------------------------------------------- /projects/03/a/Register.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/03/a/Register.hdl 5 | 6 | /** 7 | * 16-bit register. 8 | * If load[t]=1 then out[t+1] = in[t] 9 | * else out does not change 10 | */ 11 | 12 | CHIP Register { 13 | IN in[16], load; 14 | OUT out[16]; 15 | 16 | PARTS: 17 | Bit(in=in[0],load=load,out=out[0]); 18 | Bit(in=in[1],load=load,out=out[1]); 19 | Bit(in=in[2],load=load,out=out[2]); 20 | Bit(in=in[3],load=load,out=out[3]); 21 | Bit(in=in[4],load=load,out=out[4]); 22 | Bit(in=in[5],load=load,out=out[5]); 23 | Bit(in=in[6],load=load,out=out[6]); 24 | Bit(in=in[7],load=load,out=out[7]); 25 | Bit(in=in[8],load=load,out=out[8]); 26 | Bit(in=in[9],load=load,out=out[9]); 27 | Bit(in=in[10],load=load,out=out[10]); 28 | Bit(in=in[11],load=load,out=out[11]); 29 | Bit(in=in[12],load=load,out=out[12]); 30 | Bit(in=in[13],load=load,out=out[13]); 31 | Bit(in=in[14],load=load,out=out[14]); 32 | Bit(in=in[15],load=load,out=out[15]); 33 | } 34 | -------------------------------------------------------------------------------- /projects/03/RAM8.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/03/a/RAM8.hdl 5 | 6 | /** 7 | * Memory of 8 registers, each 16 bit-wide. Out holds the value 8 | * stored at the memory location specified by address. If load=1, then 9 | * the in value is loaded into the memory location specified by address 10 | * (the loaded value will be emitted to out after the next time step.) 11 | */ 12 | 13 | CHIP RAM8 { 14 | IN in[16], load, address[3]; 15 | OUT out[16]; 16 | 17 | PARTS: 18 | DMux8Way(in=load,sel=address,a=load0,b=load1,c=load2,d=load3,e=load4,f=load5,g=load6,h=load7); 19 | Register(in=in,load=load0,out=out0); 20 | Register(in=in,load=load1,out=out1); 21 | Register(in=in,load=load2,out=out2); 22 | Register(in=in,load=load3,out=out3); 23 | Register(in=in,load=load4,out=out4); 24 | Register(in=in,load=load5,out=out5); 25 | Register(in=in,load=load6,out=out6); 26 | Register(in=in,load=load7,out=out7); 27 | Mux8Way16(a=out0,b=out1,c=out2,d=out3,e=out4,f=out5,g=out6,h=out7,sel=address,out=out); 28 | } -------------------------------------------------------------------------------- /projects/00/RAM8.cmp: -------------------------------------------------------------------------------- 1 | | time | in |load |address| out | 2 | | 0+ | 0 | 0 | 0 | 0 | 3 | | 1 | 0 | 0 | 0 | 0 | 4 | | 1+ | 0 | 1 | 0 | 0 | 5 | | 2 | 0 | 1 | 0 | 0 | 6 | | 2+ | 11111 | 0 | 0 | 0 | 7 | | 3 | 11111 | 0 | 0 | 0 | 8 | | 3+ | 11111 | 1 | 1 | 0 | 9 | | 4 | 11111 | 1 | 1 | 11111 | 10 | | 4+ | 11111 | 0 | 0 | 0 | 11 | | 5 | 11111 | 0 | 0 | 0 | 12 | | 5+ | 3333 | 0 | 3 | 0 | 13 | | 6 | 3333 | 0 | 3 | 0 | 14 | | 6+ | 3333 | 1 | 3 | 0 | 15 | | 7 | 3333 | 1 | 3 | 3333 | 16 | | 7+ | 3333 | 0 | 3 | 3333 | 17 | | 8 | 3333 | 0 | 3 | 3333 | 18 | | 8 | 3333 | 0 | 1 | 11111 | 19 | | 8+ | 7777 | 0 | 1 | 11111 | 20 | | 9 | 7777 | 0 | 1 | 11111 | 21 | | 9+ | 7777 | 1 | 7 | 0 | 22 | | 10 | 7777 | 1 | 7 | 7777 | 23 | | 10+ | 7777 | 0 | 7 | 7777 | 24 | | 11 | 7777 | 0 | 7 | 7777 | 25 | | 11 | 7777 | 0 | 3 | 3333 | 26 | | 11 | 7777 | 0 | 7 | 7777 | 27 | -------------------------------------------------------------------------------- /projects/01/Mux16.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/Mux16.hdl 5 | 6 | /** 7 | * 16 bit multiplexor. If sel==1 then out=b else out=a. 8 | */ 9 | 10 | CHIP Mux16 { 11 | IN a[16], b[16], sel; 12 | OUT out[16]; 13 | 14 | PARTS: 15 | Mux(a=a[0],b=b[0],sel=sel,out=out[0]); 16 | Mux(a=a[1],b=b[1],sel=sel,out=out[1]); 17 | Mux(a=a[2],b=b[2],sel=sel,out=out[2]); 18 | Mux(a=a[3],b=b[3],sel=sel,out=out[3]); 19 | Mux(a=a[4],b=b[4],sel=sel,out=out[4]); 20 | Mux(a=a[5],b=b[5],sel=sel,out=out[5]); 21 | Mux(a=a[6],b=b[6],sel=sel,out=out[6]); 22 | Mux(a=a[7],b=b[7],sel=sel,out=out[7]); 23 | Mux(a=a[8],b=b[8],sel=sel,out=out[8]); 24 | Mux(a=a[9],b=b[9],sel=sel,out=out[9]); 25 | Mux(a=a[10],b=b[10],sel=sel,out=out[10]); 26 | Mux(a=a[11],b=b[11],sel=sel,out=out[11]); 27 | Mux(a=a[12],b=b[12],sel=sel,out=out[12]); 28 | Mux(a=a[13],b=b[13],sel=sel,out=out[13]); 29 | Mux(a=a[14],b=b[14],sel=sel,out=out[14]); 30 | Mux(a=a[15],b=b[15],sel=sel,out=out[15]); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /projects/03/a/RAM8.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/03/a/RAM8.hdl 5 | 6 | /** 7 | * Memory of 8 registers, each 16 bit-wide. Out holds the value 8 | * stored at the memory location specified by address. If load=1, then 9 | * the in value is loaded into the memory location specified by address 10 | * (the loaded value will be emitted to out after the next time step.) 11 | */ 12 | 13 | CHIP RAM8 { 14 | IN in[16], load, address[3]; 15 | OUT out[16]; 16 | 17 | PARTS: 18 | DMux8Way(in=load,sel=address,a=load0,b=load1,c=load2,d=load3,e=load4,f=load5,g=load6,h=load7); 19 | Register(in=in,load=load0,out=out0); 20 | Register(in=in,load=load1,out=out1); 21 | Register(in=in,load=load2,out=out2); 22 | Register(in=in,load=load3,out=out3); 23 | Register(in=in,load=load4,out=out4); 24 | Register(in=in,load=load5,out=out5); 25 | Register(in=in,load=load6,out=out6); 26 | Register(in=in,load=load7,out=out7); 27 | Mux8Way16(a=out0,b=out1,c=out2,d=out3,e=out4,f=out5,g=out6,h=out7,sel=address,out=out); 28 | } -------------------------------------------------------------------------------- /projects/08/ProgramFlow/BasicLoop/BasicLoop.asm: -------------------------------------------------------------------------------- 1 | @0 2 | D=A 3 | @SP 4 | A=M 5 | M=D 6 | @SP 7 | M=M+1 8 | @LCL 9 | D=M 10 | @0 11 | D=D+A 12 | @R13 13 | M=D 14 | @SP 15 | AM=M-1 16 | D=M 17 | @R13 18 | A=M 19 | M=D 20 | (LOOP_START) 21 | @ARG 22 | D=M 23 | @0 24 | A=D+A 25 | D=M 26 | @SP 27 | A=M 28 | M=D 29 | @SP 30 | M=M+1 31 | @LCL 32 | D=M 33 | @0 34 | A=D+A 35 | D=M 36 | @SP 37 | A=M 38 | M=D 39 | @SP 40 | M=M+1 41 | @SP 42 | AM=M-1 43 | D=M 44 | A=A-1 45 | M=M+D 46 | @LCL 47 | D=M 48 | @0 49 | D=D+A 50 | @R13 51 | M=D 52 | @SP 53 | AM=M-1 54 | D=M 55 | @R13 56 | A=M 57 | M=D 58 | @ARG 59 | D=M 60 | @0 61 | A=D+A 62 | D=M 63 | @SP 64 | A=M 65 | M=D 66 | @SP 67 | M=M+1 68 | @1 69 | D=A 70 | @SP 71 | A=M 72 | M=D 73 | @SP 74 | M=M+1 75 | @SP 76 | AM=M-1 77 | D=M 78 | A=A-1 79 | M=M-D 80 | @ARG 81 | D=M 82 | @0 83 | D=D+A 84 | @R13 85 | M=D 86 | @SP 87 | AM=M-1 88 | D=M 89 | @R13 90 | A=M 91 | M=D 92 | @ARG 93 | D=M 94 | @0 95 | A=D+A 96 | D=M 97 | @SP 98 | A=M 99 | M=D 100 | @SP 101 | M=M+1 102 | @SP 103 | AM=M-1 104 | D=M 105 | A=A-1 106 | @LOOP_START 107 | D;JNE 108 | @LCL 109 | D=M 110 | @0 111 | A=D+A 112 | D=M 113 | @SP 114 | A=M 115 | M=D 116 | @SP 117 | M=M+1 118 | -------------------------------------------------------------------------------- /projects/10/Square/MainT.xml: -------------------------------------------------------------------------------- 1 | 2 | class 3 | Main 4 | { 5 | function 6 | void 7 | main 8 | ( 9 | ) 10 | { 11 | var 12 | SquareGame 13 | game 14 | ; 15 | let 16 | game 17 | = 18 | SquareGame 19 | . 20 | new 21 | ( 22 | ) 23 | ; 24 | do 25 | game 26 | . 27 | run 28 | ( 29 | ) 30 | ; 31 | do 32 | game 33 | . 34 | dispose 35 | ( 36 | ) 37 | ; 38 | return 39 | ; 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /projects/01/DMux8Way.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/DMux8Way.tst 5 | 6 | load DMux8Way.hdl, 7 | output-file DMux8Way.out, 8 | compare-to DMux8Way.cmp, 9 | output-list in%B2.1.2 sel%B2.2.2 a%B2.1.2 b%B2.1.2 c%B2.1.2 d%B2.1.2 e%B2.1.2 f%B2.1.2 g%B2.1.2 h%B2.1.2; 10 | 11 | set in 0, 12 | set sel %B000, 13 | eval, 14 | output; 15 | 16 | set sel %B001, 17 | eval, 18 | output; 19 | 20 | set sel %B010, 21 | eval, 22 | output; 23 | 24 | set sel %B011, 25 | eval, 26 | output; 27 | 28 | set sel %B100, 29 | eval, 30 | output; 31 | 32 | set sel %B101, 33 | eval, 34 | output; 35 | 36 | set sel %B110, 37 | eval, 38 | output; 39 | 40 | set sel %B111, 41 | eval, 42 | output; 43 | 44 | set in 1, 45 | set sel %B000, 46 | eval, 47 | output; 48 | 49 | set sel %B001, 50 | eval, 51 | output; 52 | 53 | set sel %B010, 54 | eval, 55 | output; 56 | 57 | set sel %B011, 58 | eval, 59 | output; 60 | 61 | set sel %B100, 62 | eval, 63 | output; 64 | 65 | set sel %B101, 66 | eval, 67 | output; 68 | 69 | set sel %B110, 70 | eval, 71 | output; 72 | 73 | set sel %B111, 74 | eval, 75 | output; 76 | -------------------------------------------------------------------------------- /projects/00/Mux8Way16.cmp: -------------------------------------------------------------------------------- 1 | | a | b | c | d | e | f | g | h | sel | out | 2 | | 0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 0 | 0000 | 3 | | 0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 1 | 0000 | 4 | | 0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 2 | 0000 | 5 | | 0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 3 | 0000 | 6 | | 0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 4 | 0000 | 7 | | 0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 5 | 0000 | 8 | | 0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 6 | 0000 | 9 | | 0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 0000 | 7 | 0000 | 10 | | 1234 | 2345 | 3456 | 4567 | 5678 | 6789 | 789a | 89ab | 0 | 1234 | 11 | | 1234 | 2345 | 3456 | 4567 | 5678 | 6789 | 789a | 89ab | 1 | 2345 | 12 | | 1234 | 2345 | 3456 | 4567 | 5678 | 6789 | 789a | 89ab | 2 | 3456 | 13 | | 1234 | 2345 | 3456 | 4567 | 5678 | 6789 | 789a | 89ab | 3 | 4567 | 14 | | 1234 | 2345 | 3456 | 4567 | 5678 | 6789 | 789a | 89ab | 4 | 5678 | 15 | | 1234 | 2345 | 3456 | 4567 | 5678 | 6789 | 789a | 89ab | 5 | 6789 | 16 | | 1234 | 2345 | 3456 | 4567 | 5678 | 6789 | 789a | 89ab | 6 | 789a | 17 | | 1234 | 2345 | 3456 | 4567 | 5678 | 6789 | 789a | 89ab | 7 | 89ab | 18 | -------------------------------------------------------------------------------- /projects/07/MemoryAccess/PointerTest/PointerTest.asm: -------------------------------------------------------------------------------- 1 | @3030 2 | D=A 3 | @SP 4 | A=M 5 | M=D 6 | @SP 7 | M=M+1 8 | @THIS 9 | D=A 10 | @R13 11 | M=D 12 | @SP 13 | AM=M-1 14 | D=M 15 | @R13 16 | A=M 17 | M=D 18 | @3040 19 | D=A 20 | @SP 21 | A=M 22 | M=D 23 | @SP 24 | M=M+1 25 | @THAT 26 | D=A 27 | @R13 28 | M=D 29 | @SP 30 | AM=M-1 31 | D=M 32 | @R13 33 | A=M 34 | M=D 35 | @32 36 | D=A 37 | @SP 38 | A=M 39 | M=D 40 | @SP 41 | M=M+1 42 | @THIS 43 | D=M 44 | @2 45 | D=D+A 46 | @R13 47 | M=D 48 | @SP 49 | AM=M-1 50 | D=M 51 | @R13 52 | A=M 53 | M=D 54 | @46 55 | D=A 56 | @SP 57 | A=M 58 | M=D 59 | @SP 60 | M=M+1 61 | @THAT 62 | D=M 63 | @6 64 | D=D+A 65 | @R13 66 | M=D 67 | @SP 68 | AM=M-1 69 | D=M 70 | @R13 71 | A=M 72 | M=D 73 | @THIS 74 | D=M 75 | @SP 76 | A=M 77 | M=D 78 | @SP 79 | M=M+1 80 | @THAT 81 | D=M 82 | @SP 83 | A=M 84 | M=D 85 | @SP 86 | M=M+1 87 | @SP 88 | AM=M-1 89 | D=M 90 | A=A-1 91 | M=M+D 92 | @THIS 93 | D=M 94 | @2 95 | A=D+A 96 | D=M 97 | @SP 98 | A=M 99 | M=D 100 | @SP 101 | M=M+1 102 | @SP 103 | AM=M-1 104 | D=M 105 | A=A-1 106 | M=M-D 107 | @THAT 108 | D=M 109 | @6 110 | A=D+A 111 | D=M 112 | @SP 113 | A=M 114 | M=D 115 | @SP 116 | M=M+1 117 | @SP 118 | AM=M-1 119 | D=M 120 | A=A-1 121 | M=M+D 122 | -------------------------------------------------------------------------------- /projects/09/PigDice/Player.vm: -------------------------------------------------------------------------------- 1 | function Player.new 0 2 | push constant 2 3 | call Memory.alloc 1 4 | pop pointer 0 5 | push constant 0 6 | call Random.new 1 7 | pop this 1 8 | push constant 0 9 | pop this 0 10 | push pointer 0 11 | return 12 | function Player.roll 1 13 | push argument 0 14 | pop pointer 0 15 | push this 1 16 | push constant 5 17 | call Random.randRange 2 18 | push constant 1 19 | add 20 | pop local 0 21 | push local 0 22 | push constant 1 23 | eq 24 | if-goto IF_TRUE0 25 | goto IF_FALSE0 26 | label IF_TRUE0 27 | push constant 0 28 | pop this 0 29 | label IF_FALSE0 30 | push local 0 31 | push constant 1 32 | eq 33 | not 34 | if-goto IF_TRUE1 35 | goto IF_FALSE1 36 | label IF_TRUE1 37 | push this 0 38 | push local 0 39 | add 40 | pop this 0 41 | label IF_FALSE1 42 | push local 0 43 | return 44 | function Player.getScore 0 45 | push argument 0 46 | pop pointer 0 47 | push this 0 48 | return 49 | function Player.changeRandom 0 50 | push argument 0 51 | pop pointer 0 52 | push this 1 53 | push argument 1 54 | call Random.addCallTime 2 55 | pop temp 0 56 | push constant 0 57 | return 58 | function Player.dispose 0 59 | push argument 0 60 | pop pointer 0 61 | push this 1 62 | call Random.dispose 1 63 | pop temp 0 64 | push pointer 0 65 | call Memory.deAlloc 1 66 | pop temp 0 67 | push constant 0 68 | return 69 | -------------------------------------------------------------------------------- /projects/12/ArrayTest/Main.jack: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/12/ArrayTest/Main.jack 5 | 6 | /** Test program for the OS Array class. */ 7 | class Main { 8 | 9 | /** Performs several Array manipulations. */ 10 | function void main() { 11 | var Array r; // stores test results 12 | var Array a, b, c; 13 | 14 | let r = 8000; 15 | 16 | let a = Array.new(3); 17 | let a[2] = 222; 18 | let r[0] = a[2]; // RAM[8000] = 222 19 | 20 | let b = Array.new(3); 21 | let b[1] = a[2] - 100; 22 | let r[1] = b[1]; // RAM[8001] = 122 23 | 24 | let c = Array.new(500); 25 | let c[499] = a[2] - b[1]; 26 | let r[2] = c[499]; // RAM[8002] = 100 27 | 28 | do a.dispose(); 29 | do b.dispose(); 30 | 31 | let b = Array.new(3); 32 | let b[0] = c[499] - 90; 33 | let r[3] = b[0]; // RAM[8003] = 10 34 | 35 | do c.dispose(); 36 | do b.dispose(); 37 | 38 | return; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /projects/03/RAM64.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/03/a/RAM64.hdl 5 | 6 | /** 7 | * Memory of 64 registers, each 16 bit-wide. Out hold the value 8 | * stored at the memory location specified by address. If load=1, then 9 | * the in value is loaded into the memory location specified by address 10 | * (the loaded value will be emitted to out after the next time step.) 11 | */ 12 | 13 | CHIP RAM64 { 14 | IN in[16], load, address[6]; 15 | OUT out[16]; 16 | 17 | PARTS: 18 | DMux8Way(in=load,sel=address[3..5],a=load0,b=load1,c=load2,d=load3,e=load4,f=load5,g=load6,h=load7); 19 | RAM8(in=in,load=load0,address=address[0..2],out=out0); 20 | RAM8(in=in,load=load1,address=address[0..2],out=out1); 21 | RAM8(in=in,load=load2,address=address[0..2],out=out2); 22 | RAM8(in=in,load=load3,address=address[0..2],out=out3); 23 | RAM8(in=in,load=load4,address=address[0..2],out=out4); 24 | RAM8(in=in,load=load5,address=address[0..2],out=out5); 25 | RAM8(in=in,load=load6,address=address[0..2],out=out6); 26 | RAM8(in=in,load=load7,address=address[0..2],out=out7); 27 | Mux8Way16(a=out0,b=out1,c=out2,d=out3,e=out4,f=out5,g=out6,h=out7,sel=address[3..5],out=out); 28 | } -------------------------------------------------------------------------------- /projects/03/a/RAM64.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/03/a/RAM64.hdl 5 | 6 | /** 7 | * Memory of 64 registers, each 16 bit-wide. Out hold the value 8 | * stored at the memory location specified by address. If load=1, then 9 | * the in value is loaded into the memory location specified by address 10 | * (the loaded value will be emitted to out after the next time step.) 11 | */ 12 | 13 | CHIP RAM64 { 14 | IN in[16], load, address[6]; 15 | OUT out[16]; 16 | 17 | PARTS: 18 | DMux8Way(in=load,sel=address[3..5],a=load0,b=load1,c=load2,d=load3,e=load4,f=load5,g=load6,h=load7); 19 | RAM8(in=in,load=load0,address=address[0..2],out=out0); 20 | RAM8(in=in,load=load1,address=address[0..2],out=out1); 21 | RAM8(in=in,load=load2,address=address[0..2],out=out2); 22 | RAM8(in=in,load=load3,address=address[0..2],out=out3); 23 | RAM8(in=in,load=load4,address=address[0..2],out=out4); 24 | RAM8(in=in,load=load5,address=address[0..2],out=out5); 25 | RAM8(in=in,load=load6,address=address[0..2],out=out6); 26 | RAM8(in=in,load=load7,address=address[0..2],out=out7); 27 | Mux8Way16(a=out0,b=out1,c=out2,d=out3,e=out4,f=out5,g=out6,h=out7,sel=address[3..5],out=out); 28 | } -------------------------------------------------------------------------------- /projects/06/README.txt: -------------------------------------------------------------------------------- 1 | How to compile the code 2 | 3 | 1.Go to folder src/ 4 | 5 | 2.Open terminal in current path 6 | 7 | 3.Run command “javac Assembler.java” and “javac FileHelper.java”(java is required to be installed) 8 | 9 | 4.After compiling, there will be an “Assembler.class” and a “FileHelper.class” file in current path 10 | 11 | How to run the code 12 | 13 | 1.Command format is “java Assembler path”. There is no need to place the .asm file in the same path with .class files. But if the .asm is in src/, you should add “./” before the filename 14 | 15 | Example: 16 | java Assembler ./Fill.asm (if Fill.asm in src/) 17 | java Assembler ../Fill.asm (if Fill.asm in the parent directory of src/) 18 | java Assembler (absolute path of the file) 19 | 20 | After finishing translation, there will be a .hack file with same name in the same path of the .asm file. 21 | If you run “java Assembler /Users/Fill.asm”, there will be a Fill.hack in /Users/ 22 | 23 | About Exceptions: 24 | 25 | 1.all translation errors will be presented by throwing Exceptions. There will be four types of error: 26 | a.more than 16384 user-defined symbols 27 | b.illegal user-defined symbol 28 | c.illegal instruction format 29 | d.wrong file type(not .asm) 30 | abc errors will give an error message and the line number in the .asm file. 31 | d error will only give an error message. 32 | -------------------------------------------------------------------------------- /projects/03/RAM4K.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/03/b/RAM4K.hdl 5 | 6 | /** 7 | * Memory of 4K registers, each 16 bit-wide. Out hold the value 8 | * stored at the memory location specified by address. If load=1, then 9 | * the in value is loaded into the memory location specified by address 10 | * (the loaded value will be emitted to out after the next time step.) 11 | */ 12 | 13 | CHIP RAM4K { 14 | IN in[16], load, address[12]; 15 | OUT out[16]; 16 | 17 | PARTS: 18 | DMux8Way(in=load,sel=address[9..11],a=load0,b=load1,c=load2,d=load3,e=load4,f=load5,g=load6,h=load7); 19 | RAM512(in=in,load=load0,address=address[0..8],out=out0); 20 | RAM512(in=in,load=load1,address=address[0..8],out=out1); 21 | RAM512(in=in,load=load2,address=address[0..8],out=out2); 22 | RAM512(in=in,load=load3,address=address[0..8],out=out3); 23 | RAM512(in=in,load=load4,address=address[0..8],out=out4); 24 | RAM512(in=in,load=load5,address=address[0..8],out=out5); 25 | RAM512(in=in,load=load6,address=address[0..8],out=out6); 26 | RAM512(in=in,load=load7,address=address[0..8],out=out7); 27 | Mux8Way16(a=out0,b=out1,c=out2,d=out3,e=out4,f=out5,g=out6,h=out7,sel=address[9..11],out=out); 28 | } -------------------------------------------------------------------------------- /projects/03/b/RAM4K.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/03/b/RAM4K.hdl 5 | 6 | /** 7 | * Memory of 4K registers, each 16 bit-wide. Out hold the value 8 | * stored at the memory location specified by address. If load=1, then 9 | * the in value is loaded into the memory location specified by address 10 | * (the loaded value will be emitted to out after the next time step.) 11 | */ 12 | 13 | CHIP RAM4K { 14 | IN in[16], load, address[12]; 15 | OUT out[16]; 16 | 17 | PARTS: 18 | DMux8Way(in=load,sel=address[9..11],a=load0,b=load1,c=load2,d=load3,e=load4,f=load5,g=load6,h=load7); 19 | RAM512(in=in,load=load0,address=address[0..8],out=out0); 20 | RAM512(in=in,load=load1,address=address[0..8],out=out1); 21 | RAM512(in=in,load=load2,address=address[0..8],out=out2); 22 | RAM512(in=in,load=load3,address=address[0..8],out=out3); 23 | RAM512(in=in,load=load4,address=address[0..8],out=out4); 24 | RAM512(in=in,load=load5,address=address[0..8],out=out5); 25 | RAM512(in=in,load=load6,address=address[0..8],out=out6); 26 | RAM512(in=in,load=load7,address=address[0..8],out=out7); 27 | Mux8Way16(a=out0,b=out1,c=out2,d=out3,e=out4,f=out5,g=out6,h=out7,sel=address[9..11],out=out); 28 | } -------------------------------------------------------------------------------- /projects/05/ComputerMax-external.out: -------------------------------------------------------------------------------- 1 | | time |reset|RAM16K[0]|RAM16K[1]|RAM16K[2]| 2 | | 0 | 0 | 3 | 5 | 0 | 3 | | 1 | 0 | 3 | 5 | 0 | 4 | | 2 | 0 | 3 | 5 | 0 | 5 | | 3 | 0 | 3 | 5 | 0 | 6 | | 4 | 0 | 3 | 5 | 0 | 7 | | 5 | 0 | 3 | 5 | 0 | 8 | | 6 | 0 | 3 | 5 | 0 | 9 | | 7 | 0 | 3 | 5 | 0 | 10 | | 8 | 0 | 3 | 5 | 0 | 11 | | 9 | 0 | 3 | 5 | 0 | 12 | | 10 | 0 | 3 | 5 | 0 | 13 | | 11 | 0 | 3 | 5 | 0 | 14 | | 12 | 0 | 3 | 5 | 5 | 15 | | 13 | 0 | 3 | 5 | 5 | 16 | | 14 | 0 | 3 | 5 | 5 | 17 | | 15 | 1 | 3 | 5 | 5 | 18 | | 15 | 0 | 23456 | 12345 | 5 | 19 | | 16 | 0 | 23456 | 12345 | 5 | 20 | | 17 | 0 | 23456 | 12345 | 5 | 21 | | 18 | 0 | 23456 | 12345 | 5 | 22 | | 19 | 0 | 23456 | 12345 | 5 | 23 | | 20 | 0 | 23456 | 12345 | 5 | 24 | | 21 | 0 | 23456 | 12345 | 5 | 25 | | 22 | 0 | 23456 | 12345 | 5 | 26 | | 23 | 0 | 23456 | 12345 | 5 | 27 | | 24 | 0 | 23456 | 12345 | 5 | 28 | | 25 | 0 | 23456 | 12345 | 23456 | 29 | -------------------------------------------------------------------------------- /projects/00/Register.tst: -------------------------------------------------------------------------------- 1 | // This file is part of the materials accompanying the book 2 | // "The Elements of Computing Systems" by Nisan and Schocken, 3 | // MIT Press. Book site: www.nand2tetris.org 4 | // File name: projects/00/Register.tst 5 | 6 | load Register.hdl, 7 | output-file Register.out, 8 | compare-to Register.cmp, 9 | output-list time%S1.4.1 in%D1.6.1 load%B2.1.2 out%D1.6.1; 10 | 11 | set in 0, 12 | set load 0, 13 | tick, 14 | output; 15 | 16 | tock, 17 | output; 18 | 19 | set in 0, 20 | set load 1, 21 | tick, 22 | output; 23 | 24 | tock, 25 | output; 26 | 27 | set in -32123, 28 | set load 0, 29 | tick, 30 | output; 31 | 32 | tock, 33 | output; 34 | 35 | set in 11111, 36 | set load 0, 37 | tick, 38 | output; 39 | 40 | tock, 41 | output; 42 | 43 | set in -32123, 44 | set load 1, 45 | tick, 46 | output; 47 | 48 | tock, 49 | output; 50 | 51 | set in -32123, 52 | set load 1, 53 | tick, 54 | output; 55 | 56 | tock, 57 | output; 58 | 59 | set in -32123, 60 | set load 0, 61 | tick, 62 | output; 63 | 64 | tock, 65 | output; 66 | 67 | set in 12345, 68 | set load 1, 69 | tick, 70 | output; 71 | 72 | tock, 73 | output; 74 | 75 | set in 0, 76 | set load 0, 77 | tick, 78 | output; 79 | 80 | tock, 81 | output; 82 | 83 | set in 0, 84 | set load 1, 85 | tick, 86 | output; 87 | 88 | tock, 89 | output; 90 | -------------------------------------------------------------------------------- /projects/03/RAM512.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of the materials accompanying the book 2 | // "The Elements of Computing Systems" by Nisan and Schocken, 3 | // MIT Press. Book site: www.idc.ac.il/tecs 4 | // File name: projects/03/b/RAM512.hdl 5 | 6 | /** 7 | * Memory of 512 registers, each 16 bit-wide. Out holds the value 8 | * stored at the memory location specified by address. If load=1, then 9 | * the in value is loaded into the memory location specified by address 10 | * (the loaded value will be emitted to out after the next time step.) 11 | */ 12 | 13 | CHIP RAM512 { 14 | IN in[16], load, address[9]; 15 | OUT out[16]; 16 | 17 | PARTS: 18 | DMux8Way(in=load,sel=address[6..8],a=load0,b=load1,c=load2,d=load3,e=load4,f=load5,g=load6,h=load7); 19 | RAM64(in=in,load=load0,address=address[0..5],out=out0); 20 | RAM64(in=in,load=load1,address=address[0..5],out=out1); 21 | RAM64(in=in,load=load2,address=address[0..5],out=out2); 22 | RAM64(in=in,load=load3,address=address[0..5],out=out3); 23 | RAM64(in=in,load=load4,address=address[0..5],out=out4); 24 | RAM64(in=in,load=load5,address=address[0..5],out=out5); 25 | RAM64(in=in,load=load6,address=address[0..5],out=out6); 26 | RAM64(in=in,load=load7,address=address[0..5],out=out7); 27 | Mux8Way16(a=out0,b=out1,c=out2,d=out3,e=out4,f=out5,g=out6,h=out7,sel=address[6..8],out=out); 28 | } -------------------------------------------------------------------------------- /projects/03/b/RAM512.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of the materials accompanying the book 2 | // "The Elements of Computing Systems" by Nisan and Schocken, 3 | // MIT Press. Book site: www.idc.ac.il/tecs 4 | // File name: projects/03/b/RAM512.hdl 5 | 6 | /** 7 | * Memory of 512 registers, each 16 bit-wide. Out holds the value 8 | * stored at the memory location specified by address. If load=1, then 9 | * the in value is loaded into the memory location specified by address 10 | * (the loaded value will be emitted to out after the next time step.) 11 | */ 12 | 13 | CHIP RAM512 { 14 | IN in[16], load, address[9]; 15 | OUT out[16]; 16 | 17 | PARTS: 18 | DMux8Way(in=load,sel=address[6..8],a=load0,b=load1,c=load2,d=load3,e=load4,f=load5,g=load6,h=load7); 19 | RAM64(in=in,load=load0,address=address[0..5],out=out0); 20 | RAM64(in=in,load=load1,address=address[0..5],out=out1); 21 | RAM64(in=in,load=load2,address=address[0..5],out=out2); 22 | RAM64(in=in,load=load3,address=address[0..5],out=out3); 23 | RAM64(in=in,load=load4,address=address[0..5],out=out4); 24 | RAM64(in=in,load=load5,address=address[0..5],out=out5); 25 | RAM64(in=in,load=load6,address=address[0..5],out=out6); 26 | RAM64(in=in,load=load7,address=address[0..5],out=out7); 27 | Mux8Way16(a=out0,b=out1,c=out2,d=out3,e=out4,f=out5,g=out6,h=out7,sel=address[6..8],out=out); 28 | } -------------------------------------------------------------------------------- /projects/07/README.txt: -------------------------------------------------------------------------------- 1 | How to compile the code 2 | 3 | 1.Go to folder src/ 4 | 5 | 2.Open terminal in current path 6 | 7 | 3.Run command “javac CodeWriter.java” 8 | “javac Parser.java” 9 | “javac VMtranslator.java” 10 | (java is required to be installed) 11 | 12 | 4.After compiling, there will be a “CodeWriter.class”, a “Parser.class” and a “VMtranslator.class” file in current path 13 | 14 | How to run the code 15 | 16 | 1.Command format is “java VMtranslator directory”. 17 | 18 | Example: 19 | a.java VMtranslator /home/xuc/system/VMtranslator 20 | 21 | If the argument is a directory, after translation finishing, there will be a VMtranslator.asm file which is named by the directory name and this file will be in this directory. 22 | 23 | And there will be a printed message in the terminal: “File created : /home/xuc/system/VMtranslator/VMtranslator.asm” 24 | 25 | b.java VMtranslator /home/xuc/system/VMtranslator/StaticTest.vm 26 | 27 | If the argument is a file, after translation finishing, there will be a StaticTest.asm file which is named by the file name and this file will be in the same directory as the vm file. 28 | 29 | And there will be a printed message in the terminal: “File created : /home/xuc/system/VMtranslator/StaticTest.asm” 30 | 31 | About Exceptions: 32 | 33 | 1.all translation errors will be presented by throwing Exceptions. 34 | 35 | -------------------------------------------------------------------------------- /projects/05/ComputerMax-external.cmp: -------------------------------------------------------------------------------- 1 | | time |reset|RAM16K[0]|RAM16K[1]|RAM16K[2]| 2 | | 0 | 0 | 3 | 5 | 0 | 3 | | 1 | 0 | 3 | 5 | 0 | 4 | | 2 | 0 | 3 | 5 | 0 | 5 | | 3 | 0 | 3 | 5 | 0 | 6 | | 4 | 0 | 3 | 5 | 0 | 7 | | 5 | 0 | 3 | 5 | 0 | 8 | | 6 | 0 | 3 | 5 | 0 | 9 | | 7 | 0 | 3 | 5 | 0 | 10 | | 8 | 0 | 3 | 5 | 0 | 11 | | 9 | 0 | 3 | 5 | 0 | 12 | | 10 | 0 | 3 | 5 | 0 | 13 | | 11 | 0 | 3 | 5 | 0 | 14 | | 12 | 0 | 3 | 5 | 5 | 15 | | 13 | 0 | 3 | 5 | 5 | 16 | | 14 | 0 | 3 | 5 | 5 | 17 | | 15 | 1 | 3 | 5 | 5 | 18 | | 15 | 0 | 23456 | 12345 | 5 | 19 | | 16 | 0 | 23456 | 12345 | 5 | 20 | | 17 | 0 | 23456 | 12345 | 5 | 21 | | 18 | 0 | 23456 | 12345 | 5 | 22 | | 19 | 0 | 23456 | 12345 | 5 | 23 | | 20 | 0 | 23456 | 12345 | 5 | 24 | | 21 | 0 | 23456 | 12345 | 5 | 25 | | 22 | 0 | 23456 | 12345 | 5 | 26 | | 23 | 0 | 23456 | 12345 | 5 | 27 | | 24 | 0 | 23456 | 12345 | 5 | 28 | | 25 | 0 | 23456 | 12345 | 23456 | 29 | -------------------------------------------------------------------------------- /projects/12/MathTest/Main.jack: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/12/MathTest/Main.jack 5 | 6 | /** Test program for the OS Math class. */ 7 | class Main { 8 | 9 | /** Performs several mathematical operations from the Math class. */ 10 | function void main() { 11 | var Array r; // stores test results; 12 | 13 | let r = 8000; 14 | 15 | let r[0] = 2 * 3; // 6 16 | let r[1] = r[0] * (-30); // 6 * (-30) = -180 17 | let r[2] = r[1] * 100; // (-180) * 100 = -18000 18 | let r[3] = 1 * r[2]; // 1 * (-18000) = -18000 19 | let r[4] = r[3] * 0; // 0 20 | 21 | let r[5] = 9 / 3; // 3 22 | let r[6] = -18000 / 6; // -3000 23 | let r[7] = 32766 / (-32767); // 0 24 | 25 | let r[8] = Math.sqrt(9); // 3 26 | let r[9] = Math.sqrt(32767); // 181 27 | 28 | let r[10] = Math.min(345, 123); // 123 29 | let r[11] = Math.max(123, -345); // 123 30 | let r[12] = Math.abs(27); // 27 31 | let r[13] = Math.abs(-32767); // 32767 32 | 33 | return; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /projects/12/SysTest/Sys.vm: -------------------------------------------------------------------------------- 1 | function Sys.init 0 2 | call Math.init 0 3 | pop temp 0 4 | call Output.init 0 5 | pop temp 0 6 | call Screen.init 0 7 | pop temp 0 8 | call Keyboard.init 0 9 | pop temp 0 10 | call Memory.init 0 11 | pop temp 0 12 | call Main.main 0 13 | pop temp 0 14 | call Sys.halt 0 15 | pop temp 0 16 | push constant 0 17 | return 18 | function Sys.halt 0 19 | label WHILE_EXP0 20 | push constant 0 21 | not 22 | not 23 | if-goto WHILE_END0 24 | goto WHILE_EXP0 25 | label WHILE_END0 26 | push constant 0 27 | return 28 | function Sys.wait 2 29 | push constant 0 30 | pop local 0 31 | label WHILE_EXP0 32 | push local 0 33 | push argument 0 34 | lt 35 | not 36 | if-goto WHILE_END0 37 | push constant 0 38 | pop local 1 39 | label WHILE_EXP1 40 | push local 1 41 | push constant 100 42 | lt 43 | not 44 | if-goto WHILE_END1 45 | push local 1 46 | push constant 1 47 | add 48 | pop local 1 49 | goto WHILE_EXP1 50 | label WHILE_END1 51 | push local 0 52 | push constant 1 53 | add 54 | pop local 0 55 | goto WHILE_EXP0 56 | label WHILE_END0 57 | push constant 0 58 | return 59 | function Sys.error 0 60 | push constant 3 61 | call String.new 1 62 | push constant 69 63 | call String.appendChar 2 64 | push constant 114 65 | call String.appendChar 2 66 | push constant 114 67 | call String.appendChar 2 68 | call Output.printString 1 69 | pop temp 0 70 | push argument 0 71 | call Output.printInt 1 72 | pop temp 0 73 | call Sys.halt 0 74 | pop temp 0 75 | push constant 0 76 | return 77 | -------------------------------------------------------------------------------- /projects/12/Sys.jack: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/12/Sys.jack 5 | 6 | /** 7 | * A library of basic system services. 8 | */ 9 | class Sys { 10 | 11 | /** Performs all the initializations required by the OS. */ 12 | function void init() { 13 | do Math.init(); 14 | do Output.init(); 15 | do Screen.init(); 16 | do Keyboard.init(); 17 | do Memory.init(); 18 | do Main.main(); 19 | do Sys.halt(); 20 | return; 21 | } 22 | 23 | /** Halts execution. */ 24 | function void halt() { 25 | while(true){} 26 | return; 27 | } 28 | 29 | /** Waits approximately duration milliseconds and then returns. */ 30 | function void wait(int duration) { 31 | var int i, j; 32 | let i = 0; 33 | while(i < duration) { 34 | let j = 0; 35 | while(j < 100){ 36 | let j = j + 1; 37 | } 38 | let i = i + 1; 39 | } 40 | return; 41 | } 42 | 43 | /** Prints the given error code in the form "ERR", and halts. */ 44 | function void error(int errorCode) { 45 | do Output.printString("Err"); 46 | do Output.printInt(errCode); 47 | do Sys.halt(); 48 | return; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /projects/02/Add16.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/02/Adder16.hdl 5 | 6 | /* 7 | * Adds two 16-bit values. 8 | * The most-significant carry bit is ignored. 9 | */ 10 | 11 | CHIP Add16 { 12 | IN a[16], b[16]; 13 | OUT out[16]; 14 | 15 | PARTS: 16 | HalfAdder(a=a[0],b=b[0],sum=out[0],carry=carry1); 17 | FullAdder(a=a[1],b=b[1],c=carry1,sum=out[1],carry=carry2); 18 | FullAdder(a=a[2],b=b[2],c=carry2,sum=out[2],carry=carry3); 19 | FullAdder(a=a[3],b=b[3],c=carry3,sum=out[3],carry=carry4); 20 | FullAdder(a=a[4],b=b[4],c=carry4,sum=out[4],carry=carry5); 21 | FullAdder(a=a[5],b=b[5],c=carry5,sum=out[5],carry=carry6); 22 | FullAdder(a=a[6],b=b[6],c=carry6,sum=out[6],carry=carry7); 23 | FullAdder(a=a[7],b=b[7],c=carry7,sum=out[7],carry=carry8); 24 | FullAdder(a=a[8],b=b[8],c=carry8,sum=out[8],carry=carry9); 25 | FullAdder(a=a[9],b=b[9],c=carry9,sum=out[9],carry=carry10); 26 | FullAdder(a=a[10],b=b[10],c=carry10,sum=out[10],carry=carry11); 27 | FullAdder(a=a[11],b=b[11],c=carry11,sum=out[11],carry=carry12); 28 | FullAdder(a=a[12],b=b[12],c=carry12,sum=out[12],carry=carry13); 29 | FullAdder(a=a[13],b=b[13],c=carry13,sum=out[13],carry=carry14); 30 | FullAdder(a=a[14],b=b[14],c=carry14,sum=out[14],carry=carry15); 31 | FullAdder(a=a[15],b=b[15],c=carry15,sum=out[15],carry=carry16); 32 | } -------------------------------------------------------------------------------- /projects/12/OutputTest/Main.jack: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/12/OutputTest/Main.jack 5 | 6 | /** Test program for the OS Output class. */ 7 | class Main { 8 | 9 | /** Outputs the entire character set to the screen using all the 10 | * methods of the Output class. */ 11 | function void main() { 12 | var String s; 13 | 14 | let s = String.new(1); 15 | do s.appendChar(String.doubleQuote()); 16 | 17 | do Output.moveCursor(0, 63); 18 | do Output.printChar(66); 19 | do Output.moveCursor(22, 0); 20 | do Output.printChar(67); 21 | do Output.moveCursor(22, 63); 22 | do Output.printChar(68); 23 | do Output.printChar(65); 24 | 25 | do Output.moveCursor(2, 0); 26 | do Output.printString("0123456789"); 27 | do Output.println(); 28 | 29 | do Output.printString("ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz"); 30 | do Output.println(); 31 | 32 | do Output.printString("!#$%&'()*+,-./:;<=>?@[\]^_`{|}~"); 33 | do Output.printString(s); 34 | do Output.println(); 35 | 36 | do Output.printInt(-12345); 37 | do Output.backSpace(); 38 | do Output.printInt(6789); 39 | 40 | return; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /projects/00/Mux8Way16.tst: -------------------------------------------------------------------------------- 1 | // This file is part of the materials accompanying the book 2 | // "The Elements of Computing Systems" by Nisan and Schocken, 3 | // MIT Press. Book site: www.nand2tetris.org 4 | // File name: projects/00/Mux8Way16.tst 5 | 6 | load Mux8Way16.hdl, 7 | output-file Mux8Way16.out, 8 | compare-to Mux8Way16.cmp, 9 | output-list a%X1.4.1 b%X1.4.1 c%X1.4.1 d%X1.4.1 e%X1.4.1 f%X1.4.1 g%X1.4.1 h%X1.4.1 sel%D2.1.2 out%X1.4.1; 10 | 11 | set a 0, 12 | set b 0, 13 | set c 0, 14 | set d 0, 15 | set e 0, 16 | set f 0, 17 | set g 0, 18 | set h 0, 19 | set sel 0, 20 | eval, 21 | output; 22 | 23 | set sel 1, 24 | eval, 25 | output; 26 | 27 | set sel 2, 28 | eval, 29 | output; 30 | 31 | set sel 3, 32 | eval, 33 | output; 34 | 35 | set sel 4, 36 | eval, 37 | output; 38 | 39 | set sel 5, 40 | eval, 41 | output; 42 | 43 | set sel 6, 44 | eval, 45 | output; 46 | 47 | set sel 7, 48 | eval, 49 | output; 50 | 51 | set a %X1234, 52 | set b %X2345, 53 | set c %X3456, 54 | set d %X4567, 55 | set e %X5678, 56 | set f %X6789, 57 | set g %X789a, 58 | set h %X89ab, 59 | set sel 0, 60 | eval, 61 | output; 62 | 63 | set sel 1, 64 | eval, 65 | output; 66 | 67 | set sel 2, 68 | eval, 69 | output; 70 | 71 | set sel 3, 72 | eval, 73 | output; 74 | 75 | set sel 4, 76 | eval, 77 | output; 78 | 79 | set sel 5, 80 | eval, 81 | output; 82 | 83 | set sel 6, 84 | eval, 85 | output; 86 | 87 | set sel 7, 88 | eval, 89 | output; 90 | -------------------------------------------------------------------------------- /projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries.vm: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries.vm 5 | 6 | // Puts the first argument[0] elements of the Fibonacci series 7 | // in the memory, starting in the address given in argument[1]. 8 | // Argument[0] and argument[1] are initialized by the test script 9 | // before this code starts running. 10 | 11 | push argument 1 12 | pop pointer 1 // that = argument[1] 13 | 14 | push constant 0 15 | pop that 0 // first element = 0 16 | push constant 1 17 | pop that 1 // second element = 1 18 | 19 | push argument 0 20 | push constant 2 21 | sub 22 | pop argument 0 // num_of_elements -= 2 (first 2 elements are set) 23 | 24 | label MAIN_LOOP_START 25 | 26 | push argument 0 27 | if-goto COMPUTE_ELEMENT // if num_of_elements > 0, goto COMPUTE_ELEMENT 28 | goto END_PROGRAM // otherwise, goto END_PROGRAM 29 | 30 | label COMPUTE_ELEMENT 31 | 32 | push that 0 33 | push that 1 34 | add 35 | pop that 2 // that[2] = that[0] + that[1] 36 | 37 | push pointer 1 38 | push constant 1 39 | add 40 | pop pointer 1 // that += 1 41 | 42 | push argument 0 43 | push constant 1 44 | sub 45 | pop argument 0 // num_of_elements-- 46 | 47 | goto MAIN_LOOP_START 48 | 49 | label END_PROGRAM 50 | -------------------------------------------------------------------------------- /projects/00/RAM8.tst: -------------------------------------------------------------------------------- 1 | // This file is part of the materials accompanying the book 2 | // "The Elements of Computing Systems" by Nisan and Schocken, 3 | // MIT Press. Book site: www.nand2tetris.org 4 | // File name: projects/00/RAM8.tst 5 | 6 | load RAM8.hdl, 7 | output-file RAM8.output, 8 | compare-to RAM8.cmp, 9 | output-list time%S1.4.1 in%D1.6.1 load%B2.1.2 address%D3.1.3 out%D1.6.1; 10 | 11 | set in 0, 12 | set load 0, 13 | set address 0, 14 | tick, 15 | output; 16 | tock, 17 | output; 18 | 19 | set load 1, 20 | tick, 21 | output; 22 | tock, 23 | output; 24 | 25 | set in 11111, 26 | set load 0, 27 | tick, 28 | output; 29 | tock, 30 | output; 31 | 32 | set load 1, 33 | set address 1, 34 | tick, 35 | output; 36 | tock, 37 | output; 38 | 39 | set load 0, 40 | set address 0, 41 | tick, 42 | output; 43 | tock, 44 | output; 45 | 46 | set in 3333, 47 | set address 3, 48 | tick, 49 | output; 50 | tock, 51 | output; 52 | 53 | set load 1, 54 | tick, 55 | output; 56 | tock, 57 | output; 58 | 59 | set load 0, 60 | tick, 61 | output; 62 | tock, 63 | output; 64 | 65 | set address 1, 66 | eval, 67 | output; 68 | 69 | set in 7777, 70 | tick, 71 | output; 72 | tock, 73 | output; 74 | 75 | set load 1, 76 | set address 7, 77 | tick, 78 | output; 79 | tock, 80 | output; 81 | 82 | set load 0, 83 | tick, 84 | output; 85 | tock, 86 | output; 87 | 88 | set address 3, 89 | eval, 90 | output; 91 | 92 | set address 7, 93 | eval, 94 | output; 95 | -------------------------------------------------------------------------------- /projects/12/MemoryTest/Main.jack: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/12/MemoryTest/Main.jack 5 | 6 | /** Test program for the OS Memory class. */ 7 | class Main { 8 | 9 | /** Performs several memory manipulations. */ 10 | function void main() { 11 | var int temp; 12 | var Array a, b, c; 13 | 14 | do Memory.poke(8000, 333); // RAM[8000] = 333 15 | let temp = Memory.peek(8000); 16 | do Memory.poke(8001, temp + 1); // RAM[8001] = 334 17 | 18 | let a = Array.new(3); // uses Memory.alloc 19 | let a[2] = 222; 20 | do Memory.poke(8002, a[2]); // RAM[8002] = 222 21 | 22 | let b = Array.new(3); 23 | let b[1] = a[2] - 100; 24 | do Memory.poke(8003, b[1]); // RAM[8003] = 122 25 | 26 | let c = Array.new(500); 27 | let c[499] = a[2] - b[1]; 28 | do Memory.poke(8004, c[499]); // RAM[8004] = 100 29 | 30 | do a.dispose(); // uses Memory.deAlloc 31 | do b.dispose(); 32 | 33 | let b = Array.new(3); 34 | let b[0] = c[499] - 90; 35 | do Memory.poke(8005, b[0]); // RAM[8005] = 10 36 | 37 | do c.dispose(); 38 | do b.dispose(); 39 | 40 | return; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /projects/03/a/PC.out: -------------------------------------------------------------------------------- 1 | | time | in |reset|load | inc | out | 2 | | 0+ | 0 | 0 | 0 | 0 | 0 | 3 | | 1 | 0 | 0 | 0 | 0 | 0 | 4 | | 1+ | 0 | 0 | 0 | 1 | 0 | 5 | | 2 | 0 | 0 | 0 | 1 | 1 | 6 | | 2+ | -32123 | 0 | 0 | 1 | 1 | 7 | | 3 | -32123 | 0 | 0 | 1 | 2 | 8 | | 3+ | -32123 | 0 | 1 | 1 | 2 | 9 | | 4 | -32123 | 0 | 1 | 1 | -32123 | 10 | | 4+ | -32123 | 0 | 0 | 1 | -32123 | 11 | | 5 | -32123 | 0 | 0 | 1 | -32122 | 12 | | 5+ | -32123 | 0 | 0 | 1 | -32122 | 13 | | 6 | -32123 | 0 | 0 | 1 | -32121 | 14 | | 6+ | 12345 | 0 | 1 | 0 | -32121 | 15 | | 7 | 12345 | 0 | 1 | 0 | 12345 | 16 | | 7+ | 12345 | 1 | 1 | 0 | 12345 | 17 | | 8 | 12345 | 1 | 1 | 0 | 0 | 18 | | 8+ | 12345 | 0 | 1 | 1 | 0 | 19 | | 9 | 12345 | 0 | 1 | 1 | 12345 | 20 | | 9+ | 12345 | 1 | 1 | 1 | 12345 | 21 | | 10 | 12345 | 1 | 1 | 1 | 0 | 22 | | 10+ | 12345 | 0 | 0 | 1 | 0 | 23 | | 11 | 12345 | 0 | 0 | 1 | 1 | 24 | | 11+ | 12345 | 1 | 0 | 1 | 1 | 25 | | 12 | 12345 | 1 | 0 | 1 | 0 | 26 | | 12+ | 0 | 0 | 1 | 1 | 0 | 27 | | 13 | 0 | 0 | 1 | 1 | 0 | 28 | | 13+ | 0 | 0 | 0 | 1 | 0 | 29 | | 14 | 0 | 0 | 0 | 1 | 1 | 30 | | 14+ | 22222 | 1 | 0 | 0 | 1 | 31 | | 15 | 22222 | 1 | 0 | 0 | 0 | 32 | -------------------------------------------------------------------------------- /projects/03/a/PC.cmp: -------------------------------------------------------------------------------- 1 | | time | in |reset|load | inc | out | 2 | | 0+ | 0 | 0 | 0 | 0 | 0 | 3 | | 1 | 0 | 0 | 0 | 0 | 0 | 4 | | 1+ | 0 | 0 | 0 | 1 | 0 | 5 | | 2 | 0 | 0 | 0 | 1 | 1 | 6 | | 2+ | -32123 | 0 | 0 | 1 | 1 | 7 | | 3 | -32123 | 0 | 0 | 1 | 2 | 8 | | 3+ | -32123 | 0 | 1 | 1 | 2 | 9 | | 4 | -32123 | 0 | 1 | 1 | -32123 | 10 | | 4+ | -32123 | 0 | 0 | 1 | -32123 | 11 | | 5 | -32123 | 0 | 0 | 1 | -32122 | 12 | | 5+ | -32123 | 0 | 0 | 1 | -32122 | 13 | | 6 | -32123 | 0 | 0 | 1 | -32121 | 14 | | 6+ | 12345 | 0 | 1 | 0 | -32121 | 15 | | 7 | 12345 | 0 | 1 | 0 | 12345 | 16 | | 7+ | 12345 | 1 | 1 | 0 | 12345 | 17 | | 8 | 12345 | 1 | 1 | 0 | 0 | 18 | | 8+ | 12345 | 0 | 1 | 1 | 0 | 19 | | 9 | 12345 | 0 | 1 | 1 | 12345 | 20 | | 9+ | 12345 | 1 | 1 | 1 | 12345 | 21 | | 10 | 12345 | 1 | 1 | 1 | 0 | 22 | | 10+ | 12345 | 0 | 0 | 1 | 0 | 23 | | 11 | 12345 | 0 | 0 | 1 | 1 | 24 | | 11+ | 12345 | 1 | 0 | 1 | 1 | 25 | | 12 | 12345 | 1 | 0 | 1 | 0 | 26 | | 12+ | 0 | 0 | 1 | 1 | 0 | 27 | | 13 | 0 | 0 | 1 | 1 | 0 | 28 | | 13+ | 0 | 0 | 0 | 1 | 0 | 29 | | 14 | 0 | 0 | 0 | 1 | 1 | 30 | | 14+ | 22222 | 1 | 0 | 0 | 1 | 31 | | 15 | 22222 | 1 | 0 | 0 | 0 | 32 | -------------------------------------------------------------------------------- /projects/11/README.txt: -------------------------------------------------------------------------------- 1 | How to compile the code 2 | 3 | 1.Go to folder src/ 4 | 5 | 2.Open terminal in current path 6 | 7 | 3.Run command “javac CompilationEngine.java JackCompiler.java JackTokenizer.java Symbol.java SymbolTable.java VMWriter.java”(java is required to be installed) 8 | 9 | 4.After compiling, there will be a “CompilationEngine.class”, a “JackCompiler.class”, a “JackTokenizer.class”, a “Symbol.class”, a “SymbolTable.class” and a “VMWriter.class” file in current path 10 | 11 | How to run the code 12 | 13 | 1.Command format is “java JackCompiler directory”. 14 | 15 | Example: 16 | a.single file: java JackCompiler /home/xuc/system/Average/Main.jack 17 | 18 | If the argument is a directory, JackAnalyzer will generate a Main.xml(result of parsing the class) and a MainT.xml(result of tokenizing) in the same directory of Main.jack. 19 | 20 | And there will be output message in the terminal: 21 | File created : /home/xuc/system/Average/Main.vm 22 | 23 | 24 | b.directory: java JackCompiler /home/xuc/system/Square/ 25 | 26 | If the argument is a directory, JackAnalyzer will generate several .xml files according to the number of .jack files named by filename.vm in the same directory. 27 | 28 | And there will be output message in the terminal: 29 | File created : /home/xuc/system/Square/Main.vm 30 | File created : /home/xuc/system/Square/Square.vm 31 | File created : /home/xuc/system/Square/SquareGame.vm 32 | 33 | About Exceptions: 34 | 35 | 1.all translation errors will be presented by throwing Exceptions. 36 | 37 | -------------------------------------------------------------------------------- /projects/12/ScreenTest/Main.jack: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/12/ScreenTest/Main.jack 5 | 6 | /** Test program for the OS Screen class. */ 7 | class Main { 8 | 9 | /** Draws a sample pictue on the screen using lines and circles. */ 10 | function void main() { 11 | 12 | do Screen.drawLine(0,220,511,220); // base line 13 | do Screen.drawRectangle(280,90,410,220); // house 14 | 15 | do Screen.setColor(false); 16 | do Screen.drawRectangle(350,120,390,219); // door 17 | do Screen.drawRectangle(292,120,332,150); // window 18 | 19 | do Screen.setColor(true); 20 | do Screen.drawCircle(360,170,3); // door handle 21 | do Screen.drawLine(280,90,345,35); // roof 22 | do Screen.drawLine(345,35,410,90); // roof 23 | 24 | do Screen.drawCircle(140,60,30); // sun 25 | do Screen.drawLine(140,26, 140, 6); 26 | do Screen.drawLine(163,35,178,20); 27 | do Screen.drawLine(174,60,194,60); 28 | do Screen.drawLine(163,85,178,100); 29 | do Screen.drawLine(140,94,140,114); 30 | do Screen.drawLine(117,85,102,100); 31 | do Screen.drawLine(106,60,86,60); 32 | do Screen.drawLine(117,35,102,20); 33 | 34 | return; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /projects/01/Mux8Way16.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/01/Mux8Way16.tst 5 | 6 | load Mux8Way16.hdl, 7 | output-file Mux8Way16.out, 8 | compare-to Mux8Way16.cmp, 9 | output-list a%B1.16.1 b%B1.16.1 c%B1.16.1 d%B1.16.1 e%B1.16.1 f%B1.16.1 g%B1.16.1 h%B1.16.1 sel%B2.3.2 out%B1.16.1; 10 | 11 | set a 0, 12 | set b 0, 13 | set c 0, 14 | set d 0, 15 | set e 0, 16 | set f 0, 17 | set g 0, 18 | set h 0, 19 | set sel 0, 20 | eval, 21 | output; 22 | 23 | set sel 1, 24 | eval, 25 | output; 26 | 27 | set sel 2, 28 | eval, 29 | output; 30 | 31 | set sel 3, 32 | eval, 33 | output; 34 | 35 | set sel 4, 36 | eval, 37 | output; 38 | 39 | set sel 5, 40 | eval, 41 | output; 42 | 43 | set sel 6, 44 | eval, 45 | output; 46 | 47 | set sel 7, 48 | eval, 49 | output; 50 | 51 | set a %B0001001000110100, 52 | set b %B0010001101000101, 53 | set c %B0011010001010110, 54 | set d %B0100010101100111, 55 | set e %B0101011001111000, 56 | set f %B0110011110001001, 57 | set g %B0111100010011010, 58 | set h %B1000100110101011, 59 | set sel 0, 60 | eval, 61 | output; 62 | 63 | set sel 1, 64 | eval, 65 | output; 66 | 67 | set sel 2, 68 | eval, 69 | output; 70 | 71 | set sel 3, 72 | eval, 73 | output; 74 | 75 | set sel 4, 76 | eval, 77 | output; 78 | 79 | set sel 5, 80 | eval, 81 | output; 82 | 83 | set sel 6, 84 | eval, 85 | output; 86 | 87 | set sel 7, 88 | eval, 89 | output; 90 | -------------------------------------------------------------------------------- /projects/09/Readme.txt: -------------------------------------------------------------------------------- 1 | How to compile and run the code 2 | 1. use jackComplier to compile the src/PigDice dir 3 | 2. use VMEmulator to run complied VM code(open src/PigDice folder) 4 | 5 | Game introduction 6 | 7 | The user will play against the computer. 8 | On a turn, a player rolls the die repeatedly until either: 9 | 10 | > A 1 is rolled 11 | > The player chooses to hold (stop rolling) 12 | 13 | If a 1 is rolled, that player's turn ends and no points are earned. 14 | If the player chooses to hold, all of the points rolled during that turn are added to his or her score. 15 | 16 | Scoring Examples 17 | 18 | Example 1: User rolls a 3 and decides to continue. He then chooses to roll seven more times (6, 6, 6, 4, 5, 6, 1). 19 | Because he rolled a 1, the user's turn ends and he earns 0 points. 20 | 21 | Example 2: The computer rolls a random number of times before holding. For example, the computer rolls 4 times. 22 | (6, 3, 4, 2) and then holds. The computer accumulates 15 points (6+3+4+2=15). 23 | 24 | Game play is returned to the user, who must roll as least once, and so on. 25 | 26 | Game End 27 | 28 | When a player reaches a total of 20 or more points, the game ends and that player is the winner. (I change the upper bound of the score fro 100 to 20, because if roll too many times, the heap of hack machine will overflow) 29 | 30 | For a more detailed description of the game, see: http://en.wikipedia.org/wiki/Pig_(dice_game) 31 | 32 | P.S. 33 | I write a random class to generate pseudo random number, but it is not that random because the seed is hard coded in the code. 34 | -------------------------------------------------------------------------------- /projects/08/README.txt: -------------------------------------------------------------------------------- 1 | How to compile the code 2 | 3 | 1.Go to folder src/ 4 | 5 | 2.Open terminal in current path 6 | 7 | 3.Run command “javac CodeWriter.java” 8 | “javac Parser.java” 9 | “javac VMtranslator.java” 10 | (java is required to be installed) 11 | 12 | 4.After compiling, there will be a “CodeWriter.class”, a “Parser.class” and a “VMtranslator.class” file in current path 13 | 14 | How to run the code 15 | 16 | 1.Command format is “java VMtranslator directory”. 17 | 18 | Example: 19 | a.java VMtranslator /home/xuc/system/VMtranslator 20 | 21 | If the argument is a directory, after translation finishing, there will be a VMtranslator.asm file which is named by the directory name and this file will be in this directory. 22 | 23 | And there will be a printed message in the terminal: “File created : /home/xuc/system/VMtranslator/VMtranslator.asm” 24 | 25 | b.java VMtranslator /home/xuc/system/VMtranslator/StaticTest.vm 26 | 27 | If the argument is a file, after translation finishing, there will be a StaticTest.asm file which is named by the file name and this file will be in the same directory as the vm file. 28 | 29 | And there will be a printed message in the terminal: “File created : /home/xuc/system/VMtranslator/StaticTest.asm” 30 | 31 | About Exceptions: 32 | 33 | 1.all translation errors will be presented by throwing Exceptions. 34 | 35 | 36 | About Sys.init 37 | 1.According to the requirement of Project08, there will be a bootstrap code at the begin of the output asm file. So if there is no Sys.init function in vm files, there may be mistakes when running the asm code. -------------------------------------------------------------------------------- /projects/12/KeyboardTest/Keyboard.vm: -------------------------------------------------------------------------------- 1 | function Keyboard.init 0 2 | push constant 24576 3 | pop static 0 4 | push constant 0 5 | return 6 | function Keyboard.keyPressed 0 7 | push constant 0 8 | push static 0 9 | add 10 | pop pointer 1 11 | push that 0 12 | return 13 | function Keyboard.readChar 1 14 | label WHILE_EXP0 15 | call Keyboard.keyPressed 0 16 | push constant 0 17 | eq 18 | not 19 | if-goto WHILE_END0 20 | goto WHILE_EXP0 21 | label WHILE_END0 22 | call Keyboard.keyPressed 0 23 | pop local 0 24 | label WHILE_EXP1 25 | call Keyboard.keyPressed 0 26 | push constant 0 27 | eq 28 | not 29 | not 30 | if-goto WHILE_END1 31 | goto WHILE_EXP1 32 | label WHILE_END1 33 | push local 0 34 | call Output.printChar 1 35 | pop temp 0 36 | push local 0 37 | return 38 | function Keyboard.readLine 2 39 | push argument 0 40 | call Output.printString 1 41 | pop temp 0 42 | push constant 50 43 | call String.new 1 44 | pop local 0 45 | call Keyboard.readChar 0 46 | pop local 1 47 | label WHILE_EXP0 48 | push local 1 49 | call String.newLine 0 50 | eq 51 | not 52 | not 53 | if-goto WHILE_END0 54 | push local 1 55 | call String.backSpace 0 56 | eq 57 | if-goto IF_TRUE0 58 | goto IF_FALSE0 59 | label IF_TRUE0 60 | push local 0 61 | call String.eraseLastChar 1 62 | pop temp 0 63 | goto IF_END0 64 | label IF_FALSE0 65 | push local 0 66 | push local 1 67 | call String.appendChar 2 68 | pop temp 0 69 | label IF_END0 70 | call Keyboard.readChar 0 71 | pop local 1 72 | goto WHILE_EXP0 73 | label WHILE_END0 74 | push local 0 75 | return 76 | function Keyboard.readInt 1 77 | push argument 0 78 | call Keyboard.readLine 1 79 | pop local 0 80 | push local 0 81 | call String.intValue 1 82 | return 83 | -------------------------------------------------------------------------------- /projects/10/README.txt: -------------------------------------------------------------------------------- 1 | How to compile the code 2 | 3 | 1.Go to folder src/ 4 | 5 | 2.Open terminal in current path 6 | 7 | 3.Run command “javac CompilationEngine.java JackAnalyzer.java JackTokenizer.java” 8 | (java is required to be installed) 9 | 10 | 4.After compiling, there will be a “CompilationEngine.class”, a “JackAnalyzer.class” and a “JackTokenizer.class” file in current path 11 | 12 | How to run the code 13 | 14 | 1.Command format is “java JackAnalyzer directory”. 15 | 16 | Example: 17 | a.single file: java JackAnalyzer /home/xuc/system/ArrayTest/Main.jack 18 | 19 | If the argument is a directory, JackAnalyzer will generate a Main.xml(result of parsing the class) and a MainT.xml(result of tokenizing) in the same directory of Main.jack. 20 | 21 | And there will be output message in the terminal: 22 | File created : /home/xuc/system/ArrayTest/Main.xml 23 | File created : /home/xuc/system/ArrayTest/MainT.xml 24 | 25 | 26 | b.directory: java JackAnalyzer /home/xuc/system/Square/ 27 | 28 | If the argument is a directory, JackAnalyzer will generate several .xml files according to the number of .jack files named by filename.xml(result of parsing the class) and a filenameT.xml(result of tokenizing) in the same directory. 29 | 30 | And there will be output message in the terminal: 31 | File created : /home/xuc/system/Square/Main.xml 32 | File created : /home/xuc/system/Square/MainT.xml 33 | File created : /home/xuc/system/Square/Square.xml 34 | File created : /home/xuc/system/Square/SquareT.xml 35 | File created : /home/xuc/system/Square/SquareGame.xml 36 | File created : /home/xuc/system/Square/SquareGameT.xml 37 | 38 | About Exceptions: 39 | 40 | 1.all translation errors will be presented by throwing Exceptions. 41 | 42 | -------------------------------------------------------------------------------- /projects/04/fill/Fill.asm: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/04/Fill.asm 5 | 6 | // Runs an infinite loop that listens to the keyboard input. 7 | // When a key is pressed (any key), the program blackens the screen, 8 | // i.e. writes "black" in every pixel. When no key is pressed, the 9 | // program clears the screen, i.e. writes "white" in every pixel. 10 | 11 | // Put your code here. 12 | @SCREEN 13 | D=A 14 | @Position //initial position to -1, if to 0 there will be a bug when whiten the screen 15 | M=D-1 16 | 17 | (CHECK) 18 | @KBD 19 | D=M //Read keyboard input 20 | @BLACKEN //if the key value is greater than 0, go to blacken 21 | D;JGT 22 | @WHITEN //else go to whiten 23 | 0;JMP 24 | 25 | //Start blacken the Screen 26 | (BLACKEN) 27 | //if position is at max of the screen, do nothing 28 | @24576 29 | D=M 30 | @Position 31 | D=D-M 32 | @CHECK 33 | D;JEQ 34 | 35 | //else start blacken 36 | @Position 37 | A=M 38 | M=-1 39 | 40 | //go to next position 41 | @Position 42 | D=M+1 43 | @Position 44 | M=D 45 | 46 | //go to check if user is still hold the key 47 | @CHECK 48 | 0;JMP 49 | 50 | 51 | //Start whiten the Screen 52 | (WHITEN) 53 | 54 | //if position is at left top of the screen, do nothing 55 | @SCREEN 56 | D=A-1 57 | //if it is D=A there will be a line on the left top of the screen 58 | //that can not be removed 59 | @Position 60 | D=D-M 61 | @CHECK 62 | D;JEQ 63 | 64 | //else start whiten 65 | @Position 66 | A=M 67 | M=0 68 | 69 | //draw back position 70 | @Position 71 | D=M-1 72 | @Position 73 | M=D 74 | 75 | //check if there is any key pressed by user 76 | @CHECK 77 | 0;JMP -------------------------------------------------------------------------------- /projects/03/a/PC.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/03/a/PC.tst 5 | 6 | load PC.hdl, 7 | output-file PC.out, 8 | compare-to PC.cmp, 9 | output-list time%S1.4.1 in%D1.6.1 reset%B2.1.2 load%B2.1.2 inc%B2.1.2 out%D1.6.1; 10 | 11 | set in 0, 12 | set reset 0, 13 | set load 0, 14 | set inc 0, 15 | tick, 16 | output; 17 | 18 | tock, 19 | output; 20 | 21 | set inc 1, 22 | tick, 23 | output; 24 | 25 | tock, 26 | output; 27 | 28 | set in -32123, 29 | tick, 30 | output; 31 | 32 | tock, 33 | output; 34 | 35 | set load 1, 36 | tick, 37 | output; 38 | 39 | tock, 40 | output; 41 | 42 | set load 0, 43 | tick, 44 | output; 45 | 46 | tock, 47 | output; 48 | 49 | tick, 50 | output; 51 | 52 | tock, 53 | output; 54 | 55 | set in 12345, 56 | set load 1, 57 | set inc 0, 58 | tick, 59 | output; 60 | 61 | tock, 62 | output; 63 | 64 | set reset 1, 65 | tick, 66 | output; 67 | 68 | tock, 69 | output; 70 | 71 | set reset 0, 72 | set inc 1, 73 | tick, 74 | output; 75 | 76 | tock, 77 | output; 78 | 79 | set reset 1, 80 | tick, 81 | output; 82 | 83 | tock, 84 | output; 85 | 86 | set reset 0, 87 | set load 0, 88 | tick, 89 | output; 90 | 91 | tock, 92 | output; 93 | 94 | set reset 1, 95 | tick, 96 | output; 97 | 98 | tock, 99 | output; 100 | 101 | set in 0, 102 | set reset 0, 103 | set load 1, 104 | tick, 105 | output; 106 | 107 | tock, 108 | output; 109 | 110 | set load 0, 111 | set inc 1, 112 | tick, 113 | output; 114 | 115 | tock, 116 | output; 117 | 118 | set in 22222, 119 | set reset 1, 120 | set inc 0, 121 | tick, 122 | output; 123 | 124 | tock, 125 | output; 126 | -------------------------------------------------------------------------------- /projects/08/FunctionCalls/SimpleFunction/SimpleFunction.asm: -------------------------------------------------------------------------------- 1 | @256 2 | D=A 3 | @SP 4 | M=D 5 | @RETURN_LABEL0 6 | D=A 7 | @SP 8 | A=M 9 | M=D 10 | @SP 11 | M=M+1 12 | @LCL 13 | D=M 14 | @SP 15 | A=M 16 | M=D 17 | @SP 18 | M=M+1 19 | @ARG 20 | D=M 21 | @SP 22 | A=M 23 | M=D 24 | @SP 25 | M=M+1 26 | @THIS 27 | D=M 28 | @SP 29 | A=M 30 | M=D 31 | @SP 32 | M=M+1 33 | @THAT 34 | D=M 35 | @SP 36 | A=M 37 | M=D 38 | @SP 39 | M=M+1 40 | @SP 41 | D=M 42 | @5 43 | D=D-A 44 | @0 45 | D=D-A 46 | @ARG 47 | M=D 48 | @SP 49 | D=M 50 | @LCL 51 | M=D 52 | @Sys.init 53 | 0;JMP 54 | (RETURN_LABEL0) 55 | (SimpleFunction.test) 56 | @0 57 | D=A 58 | @SP 59 | A=M 60 | M=D 61 | @SP 62 | M=M+1 63 | @0 64 | D=A 65 | @SP 66 | A=M 67 | M=D 68 | @SP 69 | M=M+1 70 | @LCL 71 | D=M 72 | @0 73 | A=D+A 74 | D=M 75 | @SP 76 | A=M 77 | M=D 78 | @SP 79 | M=M+1 80 | @LCL 81 | D=M 82 | @1 83 | A=D+A 84 | D=M 85 | @SP 86 | A=M 87 | M=D 88 | @SP 89 | M=M+1 90 | @SP 91 | AM=M-1 92 | D=M 93 | A=A-1 94 | M=M+D 95 | @SP 96 | A=M-1 97 | M=!M 98 | @ARG 99 | D=M 100 | @0 101 | A=D+A 102 | D=M 103 | @SP 104 | A=M 105 | M=D 106 | @SP 107 | M=M+1 108 | @SP 109 | AM=M-1 110 | D=M 111 | A=A-1 112 | M=M+D 113 | @ARG 114 | D=M 115 | @1 116 | A=D+A 117 | D=M 118 | @SP 119 | A=M 120 | M=D 121 | @SP 122 | M=M+1 123 | @SP 124 | AM=M-1 125 | D=M 126 | A=A-1 127 | M=M-D 128 | @LCL 129 | D=M 130 | @R11 131 | M=D 132 | @5 133 | A=D-A 134 | D=M 135 | @R12 136 | M=D 137 | @ARG 138 | D=M 139 | @0 140 | D=D+A 141 | @R13 142 | M=D 143 | @SP 144 | AM=M-1 145 | D=M 146 | @R13 147 | A=M 148 | M=D 149 | @ARG 150 | D=M 151 | @SP 152 | M=D+1 153 | @R11 154 | D=M-1 155 | AM=D 156 | D=M 157 | @THAT 158 | M=D 159 | @R11 160 | D=M-1 161 | AM=D 162 | D=M 163 | @THIS 164 | M=D 165 | @R11 166 | D=M-1 167 | AM=D 168 | D=M 169 | @ARG 170 | M=D 171 | @R11 172 | D=M-1 173 | AM=D 174 | D=M 175 | @LCL 176 | M=D 177 | @R12 178 | A=M 179 | 0;JMP 180 | -------------------------------------------------------------------------------- /projects/06/src/FileHelper.java: -------------------------------------------------------------------------------- 1 | import java.io.File; 2 | 3 | /** 4 | * Created by xuchen on 10/25/14. 5 | */ 6 | public class FileHelper { 7 | 8 | /** 9 | * Delete comments(String after "//") from a String 10 | * @param strIn 11 | * @return 12 | */ 13 | public static String noComments(String strIn){ 14 | 15 | int position = strIn.indexOf("//"); 16 | 17 | if (position != -1){ 18 | 19 | strIn = strIn.substring(0, position); 20 | 21 | } 22 | 23 | return strIn; 24 | } 25 | 26 | /** 27 | * Delete spaces from a String 28 | * @param strIn 29 | * @return 30 | */ 31 | public static String noSpaces(String strIn){ 32 | String result = ""; 33 | 34 | if (strIn.length() != 0){ 35 | 36 | String[] segs = strIn.split(" "); 37 | 38 | for (String s: segs){ 39 | result += s; 40 | } 41 | } 42 | 43 | return result; 44 | } 45 | 46 | /** 47 | * return whether a file is an .asm file 48 | * @param fileIn 49 | * @return 50 | */ 51 | public static boolean isAsm(File fileIn){ 52 | 53 | String filename = fileIn.getName(); 54 | int position = filename.lastIndexOf("."); 55 | 56 | if (position != -1) { 57 | 58 | String ext = filename.substring(position); 59 | 60 | if (ext.toLowerCase().equals(".asm")) { 61 | return true; 62 | } 63 | } 64 | 65 | return false; 66 | } 67 | 68 | /** 69 | * pad 0 to the input string on the left until the length is equal to the input length 70 | * @param strIn 71 | * @param len 72 | * @return 73 | */ 74 | public static String padLeftZero(String strIn, int len){ 75 | 76 | for (int i = strIn.length(); i < len; i++){ 77 | strIn = "0" + strIn; 78 | } 79 | 80 | return strIn; 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /projects/05/ComputerMax.out: -------------------------------------------------------------------------------- 1 | | time |reset|ARegister|DRegister|PC[]|RAM16K[0]|RAM16K[1]|RAM16K[2]| 2 | | 0 | 0 | 0 | 0 | 0| 3 | 5 | 0 | 3 | | 1 | 0 | 0 | 0 | 1| 3 | 5 | 0 | 4 | | 2 | 0 | 0 | 3 | 2| 3 | 5 | 0 | 5 | | 3 | 0 | 1 | 3 | 3| 3 | 5 | 0 | 6 | | 4 | 0 | 1 | -2 | 4| 3 | 5 | 0 | 7 | | 5 | 0 | 10 | -2 | 5| 3 | 5 | 0 | 8 | | 6 | 0 | 10 | -2 | 6| 3 | 5 | 0 | 9 | | 7 | 0 | 1 | -2 | 7| 3 | 5 | 0 | 10 | | 8 | 0 | 1 | 5 | 8| 3 | 5 | 0 | 11 | | 9 | 0 | 12 | 5 | 9| 3 | 5 | 0 | 12 | | 10 | 0 | 12 | 5 | 12| 3 | 5 | 0 | 13 | | 11 | 0 | 2 | 5 | 13| 3 | 5 | 0 | 14 | | 12 | 0 | 2 | 5 | 14| 3 | 5 | 5 | 15 | | 13 | 0 | 14 | 5 | 15| 3 | 5 | 5 | 16 | | 14 | 0 | 14 | 5 | 14| 3 | 5 | 5 | 17 | | 15 | 1 | 14 | 5 | 0| 3 | 5 | 5 | 18 | | 15 | 0 | 14 | 5 | 0| 23456 | 12345 | 5 | 19 | | 16 | 0 | 0 | 5 | 1| 23456 | 12345 | 5 | 20 | | 17 | 0 | 0 | 23456 | 2| 23456 | 12345 | 5 | 21 | | 18 | 0 | 1 | 23456 | 3| 23456 | 12345 | 5 | 22 | | 19 | 0 | 1 | 11111 | 4| 23456 | 12345 | 5 | 23 | | 20 | 0 | 10 | 11111 | 5| 23456 | 12345 | 5 | 24 | | 21 | 0 | 10 | 11111 | 10| 23456 | 12345 | 5 | 25 | | 22 | 0 | 0 | 11111 | 11| 23456 | 12345 | 5 | 26 | | 23 | 0 | 0 | 23456 | 12| 23456 | 12345 | 5 | 27 | | 24 | 0 | 2 | 23456 | 13| 23456 | 12345 | 5 | 28 | | 25 | 0 | 2 | 23456 | 14| 23456 | 12345 | 23456 | 29 | -------------------------------------------------------------------------------- /projects/05/ComputerMax.cmp: -------------------------------------------------------------------------------- 1 | | time |reset|ARegister|DRegister|PC[]|RAM16K[0]|RAM16K[1]|RAM16K[2]| 2 | | 0 | 0 | 0 | 0 | 0| 3 | 5 | 0 | 3 | | 1 | 0 | 0 | 0 | 1| 3 | 5 | 0 | 4 | | 2 | 0 | 0 | 3 | 2| 3 | 5 | 0 | 5 | | 3 | 0 | 1 | 3 | 3| 3 | 5 | 0 | 6 | | 4 | 0 | 1 | -2 | 4| 3 | 5 | 0 | 7 | | 5 | 0 | 10 | -2 | 5| 3 | 5 | 0 | 8 | | 6 | 0 | 10 | -2 | 6| 3 | 5 | 0 | 9 | | 7 | 0 | 1 | -2 | 7| 3 | 5 | 0 | 10 | | 8 | 0 | 1 | 5 | 8| 3 | 5 | 0 | 11 | | 9 | 0 | 12 | 5 | 9| 3 | 5 | 0 | 12 | | 10 | 0 | 12 | 5 | 12| 3 | 5 | 0 | 13 | | 11 | 0 | 2 | 5 | 13| 3 | 5 | 0 | 14 | | 12 | 0 | 2 | 5 | 14| 3 | 5 | 5 | 15 | | 13 | 0 | 14 | 5 | 15| 3 | 5 | 5 | 16 | | 14 | 0 | 14 | 5 | 14| 3 | 5 | 5 | 17 | | 15 | 1 | 14 | 5 | 0| 3 | 5 | 5 | 18 | | 15 | 0 | 14 | 5 | 0| 23456 | 12345 | 5 | 19 | | 16 | 0 | 0 | 5 | 1| 23456 | 12345 | 5 | 20 | | 17 | 0 | 0 | 23456 | 2| 23456 | 12345 | 5 | 21 | | 18 | 0 | 1 | 23456 | 3| 23456 | 12345 | 5 | 22 | | 19 | 0 | 1 | 11111 | 4| 23456 | 12345 | 5 | 23 | | 20 | 0 | 10 | 11111 | 5| 23456 | 12345 | 5 | 24 | | 21 | 0 | 10 | 11111 | 10| 23456 | 12345 | 5 | 25 | | 22 | 0 | 0 | 11111 | 11| 23456 | 12345 | 5 | 26 | | 23 | 0 | 0 | 23456 | 12| 23456 | 12345 | 5 | 27 | | 24 | 0 | 2 | 23456 | 13| 23456 | 12345 | 5 | 28 | | 25 | 0 | 2 | 23456 | 14| 23456 | 12345 | 23456 | 29 | -------------------------------------------------------------------------------- /projects/10/ExpressionlessSquare/Main.xml: -------------------------------------------------------------------------------- 1 | 2 | class 3 | Main 4 | { 5 | 6 | function 7 | void 8 | main 9 | ( 10 | 11 | 12 | ) 13 | 14 | { 15 | 16 | var 17 | SquareGame 18 | game 19 | ; 20 | 21 | 22 | 23 | let 24 | game 25 | = 26 | 27 | 28 | game 29 | 30 | 31 | ; 32 | 33 | 34 | do 35 | game 36 | . 37 | run 38 | ( 39 | 40 | 41 | ) 42 | ; 43 | 44 | 45 | do 46 | game 47 | . 48 | dispose 49 | ( 50 | 51 | 52 | ) 53 | ; 54 | 55 | 56 | return 57 | ; 58 | 59 | 60 | } 61 | 62 | 63 | } 64 | 65 | -------------------------------------------------------------------------------- /projects/05/Memory.hdl: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/05/Memory.hdl 5 | 6 | /** 7 | * The complete address space of the computer's memory, 8 | * including RAM and memory mapped I/O. 9 | * The chip facilitates read and write operations, as follows: 10 | * Read: out(t) = Memory[address(t)](t) 11 | * Write: If load(t-1) then Memory[address(t-1)](t) = in(t-1) 12 | * In words: the chip always outputs the value stored at the memory 13 | * location specified by address. If load=1, the in value is loaded 14 | * into the memory location specified by address. This value becomes 15 | * available through the out output in the next time step. 16 | * Address space rules: 17 | * Only the upper 16K+8K+1 words of the Memory chip are used. 18 | * Access to address>0x6000 is invalid. Access to any address in 19 | * the range 0x4000-0x5FFF results in accessing the screen memory 20 | * map. Access to address 0x6000 results in accessing the keyboard 21 | * memory map. The behavior in these addresses is described in the 22 | * Screen and Keyboard chip specifications given in the book. 23 | */ 24 | 25 | CHIP Memory { 26 | IN in[16], load, address[15]; 27 | OUT out[16]; 28 | 29 | PARTS: 30 | //determine which one to load 31 | DMux(in=load,sel=address[14],a=ramload,b=skload); 32 | DMux(in=skload,sel=address[13],a=sload,b=nothing); 33 | 34 | //load ram and screen 35 | RAM16K(in=in,load=ramload,address=address[0..13],out=ramout); 36 | Screen(in=in,load=sload,address=address[0..12],out=screenout); 37 | 38 | //Deal with keyboard, make sure that all bits in [0..12] are 0 39 | Keyboard(out=kbd); 40 | Or8Way(in=address[0..7], out=notkbd1); 41 | Or8Way(in[0..4]=address[8..12], in[5..7]=false, out=notkbd2); 42 | Or(a=notkbd1,b=notkbd2,out=notkbd); 43 | Mux16(a=kbd,b=false,sel=notkbd,out=kbdout); 44 | 45 | //determine which is the output 46 | Mux16(a=ramout,b=outsk,sel=address[14],out=out); 47 | Mux16(a=screenout,b=kbdout,sel=address[13],out=outsk); 48 | 49 | } -------------------------------------------------------------------------------- /projects/04/mult/Mult.tst: -------------------------------------------------------------------------------- 1 | // This file is part of www.nand2tetris.org 2 | // and the book "The Elements of Computing Systems" 3 | // by Nisan and Schocken, MIT Press. 4 | // File name: projects/04/mult/Mult.tst 5 | 6 | load Mult.hack, 7 | output-file Mult.out, 8 | compare-to Mult.cmp, 9 | output-list RAM[0]%D2.6.2 RAM[1]%D2.6.2 RAM[2]%D2.6.2; 10 | 11 | set RAM[0] 0, // Set test arguments 12 | set RAM[1] 0, 13 | set RAM[2] -1; // Test that program initialized product to 0 14 | repeat 20 { 15 | ticktock; 16 | } 17 | set RAM[0] 0, // Restore arguments in case program used them as loop counter 18 | set RAM[1] 0, 19 | output; 20 | 21 | set PC 0, 22 | set RAM[0] 1, // Set test arguments 23 | set RAM[1] 0, 24 | set RAM[2] -1; // Ensure that program initialized product to 0 25 | repeat 50 { 26 | ticktock; 27 | } 28 | set RAM[0] 1, // Restore arguments in case program used them as loop counter 29 | set RAM[1] 0, 30 | output; 31 | 32 | set PC 0, 33 | set RAM[0] 0, // Set test arguments 34 | set RAM[1] 2, 35 | set RAM[2] -1; // Ensure that program initialized product to 0 36 | repeat 80 { 37 | ticktock; 38 | } 39 | set RAM[0] 0, // Restore arguments in case program used them as loop counter 40 | set RAM[1] 2, 41 | output; 42 | 43 | set PC 0, 44 | set RAM[0] 3, // Set test arguments 45 | set RAM[1] 1, 46 | set RAM[2] -1; // Ensure that program initialized product to 0 47 | repeat 120 { 48 | ticktock; 49 | } 50 | set RAM[0] 3, // Restore arguments in case program used them as loop counter 51 | set RAM[1] 1, 52 | output; 53 | 54 | set PC 0, 55 | set RAM[0] 2, // Set test arguments 56 | set RAM[1] 4, 57 | set RAM[2] -1; // Ensure that program initialized product to 0 58 | repeat 150 { 59 | ticktock; 60 | } 61 | set RAM[0] 2, // Restore arguments in case program used them as loop counter 62 | set RAM[1] 4, 63 | output; 64 | 65 | set PC 0, 66 | set RAM[0] 6, // Set test arguments 67 | set RAM[1] 7, 68 | set RAM[2] -1; // Ensure that program initialized product to 0 69 | repeat 210 { 70 | ticktock; 71 | } 72 | set RAM[0] 6, // Restore arguments in case program used them as loop counter 73 | set RAM[1] 7, 74 | output; 75 | -------------------------------------------------------------------------------- /projects/09/PigDice/Random.jack: -------------------------------------------------------------------------------- 1 | /** 2 | Reference: http://nand2tetris-questions-and-answers-forum.32033.n3.nabble.com/Random-number-generator-for-hack-cpu-td4025503.html#a4025511 3 | 4 | There is no random generator in Jack 5 | So we need to generate pseudo random number by ourself 6 | This class is quite simple random number generator 7 | 8 | Improvement: let the number be more random by adding shift number 9 | */ 10 | 11 | class Random { 12 | field int seed; //random seed 13 | field int calledTime; 14 | 15 | constructor Random new(int seedIn) { 16 | 17 | let seed = seedIn; 18 | let seed = rand(); 19 | let calledTime = 0; 20 | 21 | return this; 22 | } 23 | 24 | method int rand() { 25 | /** return a random number in the range 0..32767 */ 26 | 27 | let seed = seed + 20251; 28 | 29 | if (seed < 0) { 30 | let seed = seed - 32767 - 1; 31 | } 32 | 33 | return seed; 34 | } 35 | 36 | method int randRange(int range) { 37 | /** return a random number in the range 0..range */ 38 | var int mask; 39 | var int ret; 40 | 41 | let mask = 1; 42 | while (mask < range) { 43 | let mask = mask * 2 + 1; 44 | } 45 | 46 | let ret = rand() & mask; 47 | let calledTime = calledTime + 1; 48 | 49 | while (ret > range) { 50 | let ret = rand() & mask; 51 | let calledTime = calledTime + 1; 52 | let ret = ret & getShift(); 53 | } 54 | 55 | return ret; 56 | } 57 | 58 | //get new shift number according to called times 59 | method int getShift() { 60 | var int temp; 61 | 62 | let calledTime = calledTime + 1; 63 | let temp = calledTime; 64 | 65 | while(temp > 9){ 66 | 67 | let temp = temp - 9; 68 | 69 | } 70 | 71 | return temp; 72 | } 73 | 74 | //dispose the Oject 75 | method void dispose(){ 76 | do Memory.deAlloc(this); 77 | return; 78 | } 79 | 80 | method void addCallTime(int times){ 81 | 82 | let calledTime = calledTime + times; 83 | return; 84 | 85 | } 86 | 87 | } --------------------------------------------------------------------------------