├── 10 ├── Square │ ├── Main.jack │ ├── MainT.xml │ ├── Main.xml │ └── SquareGame.jack ├── ExpressionlessSquare │ ├── Main.jack │ ├── MainT.xml │ ├── Main.xml │ └── SquareGame.jack └── ArrayTest │ └── Main.jack ├── 11 ├── Seven │ └── Main.jack ├── Pong │ ├── Main.jack │ └── Bat.jack ├── Square │ ├── Main.jack │ └── SquareGame.jack ├── Average │ └── Main.jack ├── ComplexArrays │ └── Main.jack └── ConvertToBin │ └── Main.jack ├── 12 ├── Output.jack ├── ArrayTest │ ├── ArrayTest.cmp │ ├── ArrayTest.tst │ └── Main.jack ├── OutputTest │ ├── OutputTestOutput.gif │ └── Main.jack ├── ScreenTest │ ├── ScreenTestOutput.gif │ └── Main.jack ├── StringTest │ ├── StringTestOutput.gif │ └── Main.jack ├── KeyboardTest │ ├── KeyboardTestOutput.gif │ └── Main.jack ├── MemoryTest │ ├── MemoryTest.cmp │ ├── MemoryTest.tst │ └── Main.jack ├── MathTest │ ├── MathTest.cmp │ ├── MathTest.tst │ └── Main.jack ├── Array.jack ├── Sys.jack ├── Math.jack ├── Memory.jack ├── SysTest │ └── Main.jack ├── Screen.jack ├── String.jack └── Keyboard.jack ├── 13 └── more fun to go.txt ├── 05 ├── Memory.out ├── Add.hack ├── Max.hack ├── Rect.hack ├── ComputerAdd-external.out ├── ComputerAdd-external.cmp ├── ComputerRect-external.out ├── ComputerAdd-external.tst ├── ComputerRect-external.cmp ├── ComputerRect-external.tst ├── ComputerAdd.tst ├── ComputerRect.tst ├── ComputerAdd.out ├── ComputerAdd.cmp ├── ComputerMax-external.tst ├── ComputerMax.tst ├── Computer.hdl ├── ComputerMax-external.out ├── ComputerMax-external.cmp ├── Memory.hdl ├── ComputerMax.out ├── ComputerMax.cmp └── Memory.cmp ├── README.md ├── 07 ├── MemoryAccess │ ├── StaticTest │ │ ├── StaticTest.cmp │ │ ├── StaticTest.tst │ │ ├── StaticTestVME.tst │ │ └── StaticTest.vm │ ├── PointerTest │ │ ├── PointerTest.cmp │ │ ├── PointerTest.tst │ │ ├── PointerTestVME.tst │ │ └── PointerTest.vm │ └── BasicTest │ │ ├── BasicTest.cmp │ │ ├── BasicTestVME.tst │ │ ├── BasicTest.tst │ │ └── BasicTest.vm └── StackArithmetic │ ├── SimpleAdd │ ├── SimpleAdd.cmp │ ├── SimpleAdd.vm │ ├── SimpleAdd.tst │ └── SimpleAddVME.tst │ └── StackTest │ ├── StackTest.cmp │ ├── StackTest.tst │ ├── StackTestVME.tst │ └── StackTest.vm ├── 01 ├── Not.out ├── Not.cmp ├── And.out ├── Or.out ├── Or8Way.out ├── Xor.out ├── And.cmp ├── Or.cmp ├── Xor.cmp ├── Or8Way.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 ├── And16.cmp ├── Or16.cmp ├── And.hdl ├── Or.hdl ├── Xor.hdl ├── Mux.hdl ├── DMux.tst ├── Or.tst ├── And.tst ├── Xor.tst ├── Or8Way.tst ├── Mux16.out ├── Mux16.cmp ├── Not16.tst ├── Or8Way.hdl ├── Mux4Way16.hdl ├── DMux4Way.hdl ├── DMux.hdl ├── DMux4Way.tst ├── Mux.tst ├── DMux8Way.hdl ├── Or16.tst ├── And16.tst ├── Mux8Way16.hdl ├── Mux16.tst ├── Mux4Way16.tst ├── Mux4Way16.out ├── Not16.hdl ├── Mux4Way16.cmp ├── Or16.hdl ├── And16.hdl ├── DMux8Way.out ├── DMux8Way.cmp ├── DMux8Way.tst ├── Mux16.hdl ├── Mux8Way16.tst ├── Mux8Way16.out └── Mux8Way16.cmp ├── 08 ├── ProgramFlow │ ├── BasicLoop │ │ ├── BasicLoop.cmp │ │ ├── BasicLoop.tst │ │ ├── BasicLoopVME.tst │ │ └── BasicLoop.vm │ └── FibonacciSeries │ │ ├── FibonacciSeries.cmp │ │ ├── FibonacciSeries.tst │ │ ├── FibonacciSeriesVME.tst │ │ └── FibonacciSeries.vm └── FunctionCalls │ ├── FibonacciElement │ ├── FibonacciElement.cmp │ ├── FibonacciElementVME.tst │ ├── FibonacciElement.tst │ ├── Sys.vm │ └── Main.vm │ ├── StaticsTest │ ├── StaticsTest.cmp │ ├── StaticsTest.tst │ ├── StaticsTestVME.tst │ ├── Class1.vm │ ├── Class2.vm │ └── Sys.vm │ ├── NestedCall │ ├── NestedCall.cmp │ ├── NestedCall.tst │ ├── NestedCallVME.tst │ └── Sys.vm │ └── SimpleFunction │ ├── SimpleFunction.cmp │ ├── SimpleFunction.vm │ ├── SimpleFunction.tst │ └── SimpleFunctionVME.tst ├── 09 ├── Jack OS API.pdf └── Square │ ├── Main.jack │ └── SquareGame.jack ├── 06 ├── add │ ├── Add.hack │ └── Add.asm ├── max │ ├── Max.hack │ ├── MaxL.hack │ ├── MaxL.asm │ └── Max.asm └── rect │ ├── RectL.asm │ ├── Rect.hack │ ├── RectL.hack │ └── Rect.asm ├── 02 ├── HalfAdder.out ├── HalfAdder.cmp ├── Inc16.out ├── Inc16.cmp ├── FullAdder.out ├── Add16.out ├── FullAdder.cmp ├── Add16.cmp ├── HalfAdder.hdl ├── FullAdder.hdl ├── HalfAdder.tst ├── Inc16.tst ├── FullAdder.tst ├── Add16.tst ├── Inc16.hdl ├── Add16.hdl ├── ALU.hdl ├── ALU-nostat.out ├── ALU-nostat.cmp └── ALU.out ├── 04 ├── mult │ ├── Mult.cmp │ ├── Mult.out │ ├── Mult.hack │ ├── Mult.asm │ └── Mult.tst └── fill │ ├── Fill.tst │ ├── Fill.hack │ └── Fill.asm ├── 00 └── Xor.hdl └── 03 ├── a ├── Bit.hdl ├── PC.hdl ├── Register.hdl ├── RAM8.hdl ├── RAM64.hdl ├── PC.out ├── PC.cmp └── PC.tst └── b ├── RAM16K.hdl ├── RAM4K.hdl └── RAM512.hdl /05/Memory.out: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Nand2Tetris memo 2 | ===================== 3 | 4 | -------------------------------------------------------------------------------- /07/MemoryAccess/StaticTest/StaticTest.cmp: -------------------------------------------------------------------------------- 1 | |RAM[256]| 2 | | 1110 | 3 | -------------------------------------------------------------------------------- /01/Not.out: -------------------------------------------------------------------------------- 1 | | in | out | 2 | | 0 | 1 | 3 | | 1 | 0 | 4 | -------------------------------------------------------------------------------- /01/Not.cmp: -------------------------------------------------------------------------------- 1 | | in | out | 2 | | 0 | 1 | 3 | | 1 | 0 | 4 | -------------------------------------------------------------------------------- /08/ProgramFlow/BasicLoop/BasicLoop.cmp: -------------------------------------------------------------------------------- 1 | | RAM[0] |RAM[256]| 2 | | 257 | 6 | 3 | -------------------------------------------------------------------------------- /12/Output.jack: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hirak/nand2tetris-memo/HEAD/12/Output.jack -------------------------------------------------------------------------------- /09/Jack OS API.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hirak/nand2tetris-memo/HEAD/09/Jack OS API.pdf -------------------------------------------------------------------------------- /07/StackArithmetic/SimpleAdd/SimpleAdd.cmp: -------------------------------------------------------------------------------- 1 | | RAM[0] | RAM[256] | 2 | | 257 | 15 | 3 | -------------------------------------------------------------------------------- /08/FunctionCalls/FibonacciElement/FibonacciElement.cmp: -------------------------------------------------------------------------------- 1 | | RAM[0] |RAM[261]| 2 | | 262 | 3 | 3 | -------------------------------------------------------------------------------- /08/FunctionCalls/StaticsTest/StaticsTest.cmp: -------------------------------------------------------------------------------- 1 | | RAM[0] |RAM[261]|RAM[262]| 2 | | 263 | -2 | 8 | 3 | -------------------------------------------------------------------------------- /12/ArrayTest/ArrayTest.cmp: -------------------------------------------------------------------------------- 1 | |RAM[8000]|RAM[8001]|RAM[8002]|RAM[8003]| 2 | | 222 | 122 | 100 | 10 | 3 | -------------------------------------------------------------------------------- /12/OutputTest/OutputTestOutput.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hirak/nand2tetris-memo/HEAD/12/OutputTest/OutputTestOutput.gif -------------------------------------------------------------------------------- /12/ScreenTest/ScreenTestOutput.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hirak/nand2tetris-memo/HEAD/12/ScreenTest/ScreenTestOutput.gif -------------------------------------------------------------------------------- /12/StringTest/StringTestOutput.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hirak/nand2tetris-memo/HEAD/12/StringTest/StringTestOutput.gif -------------------------------------------------------------------------------- /12/KeyboardTest/KeyboardTestOutput.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hirak/nand2tetris-memo/HEAD/12/KeyboardTest/KeyboardTestOutput.gif -------------------------------------------------------------------------------- /05/Add.hack: -------------------------------------------------------------------------------- 1 | 0000000000000010 2 | 1110110000010000 3 | 0000000000000011 4 | 1110000010010000 5 | 0000000000000000 6 | 1110001100001000 7 | -------------------------------------------------------------------------------- /06/add/Add.hack: -------------------------------------------------------------------------------- 1 | 0000000000000010 2 | 1110110000010000 3 | 0000000000000011 4 | 1110000010010000 5 | 0000000000000000 6 | 1110001100001000 7 | -------------------------------------------------------------------------------- /07/MemoryAccess/PointerTest/PointerTest.cmp: -------------------------------------------------------------------------------- 1 | |RAM[256]| RAM[3] | RAM[4] |RAM[3032|RAM[3046| 2 | | 6084 | 3030 | 3040 | 32 | 46 | 3 | -------------------------------------------------------------------------------- /08/FunctionCalls/NestedCall/NestedCall.cmp: -------------------------------------------------------------------------------- 1 | | RAM[0] | RAM[1] | RAM[2] | RAM[5] | RAM[6] | 2 | | 261 | 261 | 256 | 135 | 246 | 3 | -------------------------------------------------------------------------------- /01/And.out: -------------------------------------------------------------------------------- 1 | | a | b | out | 2 | | 0 | 0 | 0 | 3 | | 0 | 1 | 0 | 4 | | 1 | 0 | 0 | 5 | | 1 | 1 | 1 | 6 | -------------------------------------------------------------------------------- /01/Or.out: -------------------------------------------------------------------------------- 1 | | a | b | out | 2 | | 0 | 0 | 0 | 3 | | 0 | 1 | 1 | 4 | | 1 | 0 | 1 | 5 | | 1 | 1 | 1 | 6 | -------------------------------------------------------------------------------- /01/Or8Way.out: -------------------------------------------------------------------------------- 1 | | in | out | 2 | | 00000000 | 0 | 3 | | 11111111 | 1 | 4 | | 00010000 | 1 | 5 | | 00000001 | 1 | 6 | | 00100110 | 1 | 7 | -------------------------------------------------------------------------------- /01/Xor.out: -------------------------------------------------------------------------------- 1 | | a | b | out | 2 | | 0 | 0 | 0 | 3 | | 0 | 1 | 1 | 4 | | 1 | 0 | 1 | 5 | | 1 | 1 | 0 | 6 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /01/And.cmp: -------------------------------------------------------------------------------- 1 | | a | b | out | 2 | | 0 | 0 | 0 | 3 | | 0 | 1 | 0 | 4 | | 1 | 0 | 0 | 5 | | 1 | 1 | 1 | 6 | -------------------------------------------------------------------------------- /01/Or.cmp: -------------------------------------------------------------------------------- 1 | | a | b | out | 2 | | 0 | 0 | 0 | 3 | | 0 | 1 | 1 | 4 | | 1 | 0 | 1 | 5 | | 1 | 1 | 1 | 6 | -------------------------------------------------------------------------------- /01/Xor.cmp: -------------------------------------------------------------------------------- 1 | | a | b | out | 2 | | 0 | 0 | 0 | 3 | | 0 | 1 | 1 | 4 | | 1 | 0 | 1 | 5 | | 1 | 1 | 0 | 6 | -------------------------------------------------------------------------------- /01/Or8Way.cmp: -------------------------------------------------------------------------------- 1 | | in | out | 2 | | 00000000 | 0 | 3 | | 11111111 | 1 | 4 | | 00010000 | 1 | 5 | | 00000001 | 1 | 6 | | 00100110 | 1 | 7 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /02/Inc16.out: -------------------------------------------------------------------------------- 1 | | in | out | 2 | | 0000000000000000 | 0000000000000001 | 3 | | 1111111111111111 | 0000000000000000 | 4 | | 0000000000000101 | 0000000000000110 | 5 | | 1111111111111011 | 1111111111111100 | 6 | -------------------------------------------------------------------------------- /02/Inc16.cmp: -------------------------------------------------------------------------------- 1 | | in | out | 2 | | 0000000000000000 | 0000000000000001 | 3 | | 1111111111111111 | 0000000000000000 | 4 | | 0000000000000101 | 0000000000000110 | 5 | | 1111111111111011 | 1111111111111100 | 6 | -------------------------------------------------------------------------------- /01/Not16.out: -------------------------------------------------------------------------------- 1 | | in | out | 2 | | 0000000000000000 | 1111111111111111 | 3 | | 1111111111111111 | 0000000000000000 | 4 | | 1010101010101010 | 0101010101010101 | 5 | | 0011110011000011 | 1100001100111100 | 6 | | 0001001000110100 | 1110110111001011 | 7 | -------------------------------------------------------------------------------- /01/Not16.cmp: -------------------------------------------------------------------------------- 1 | | in | out | 2 | | 0000000000000000 | 1111111111111111 | 3 | | 1111111111111111 | 0000000000000000 | 4 | | 1010101010101010 | 0101010101010101 | 5 | | 0011110011000011 | 1100001100111100 | 6 | | 0001001000110100 | 1110110111001011 | 7 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /13/more fun to go.txt: -------------------------------------------------------------------------------- 1 | See Chapter 13 in the book. It's your call! 2 | 3 | And, if you've developed something cool, and you wish to share it with others, please let us know about it. We'll put a link to it from the nand2tetris site. 4 | 5 | We hope that you enjoyed the ride! 6 | 7 | -- Noam and Shimon 8 | 9 | www.nand2tetris.org -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /06/max/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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /04/mult/Mult.hack: -------------------------------------------------------------------------------- 1 | 0000000000000010 2 | 1110101010001000 3 | 0000000000000001 4 | 1111110000010000 5 | 0000000000001110 6 | 1110001100000110 7 | 0000000000000000 8 | 1111110000010000 9 | 0000000000000010 10 | 1111000010001000 11 | 0000000000000001 12 | 1111110010001000 13 | 0000000000000010 14 | 1110101010000111 15 | 0000000000001110 16 | 1110101010000111 17 | -------------------------------------------------------------------------------- /06/max/MaxL.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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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: 8 | * out = not in 9 | */ 10 | 11 | CHIP Not { 12 | IN in; 13 | OUT out; 14 | 15 | PARTS: 16 | Nand(a=in, b=in, out=out); 17 | } 18 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | 5 | /** 6 | * Exclusive-or gate: out = !(a == b). 7 | */ 8 | 9 | CHIP Xor { 10 | 11 | IN a, b; 12 | OUT out; 13 | 14 | PARTS: 15 | Nand (a=a, b=b, out= AnandB); 16 | Or (a=a, b=b, out= AorB); 17 | And (a=AnandB, b=AorB, out=out); 18 | } 19 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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: 8 | * out = 1 if (a == 1 and b == 1) 9 | * 0 otherwise 10 | */ 11 | 12 | CHIP And { 13 | IN a, b; 14 | OUT out; 15 | 16 | PARTS: 17 | Nand(a=a, b=b, out=nandout); 18 | Nand(a=nandout, b=nandout, out=out); 19 | } 20 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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: 8 | * out = 1 if (a == 1 or b == 1) 9 | * 0 otherwise 10 | */ 11 | 12 | CHIP Or { 13 | IN a, b; 14 | OUT out; 15 | 16 | PARTS: 17 | Nand(a=a, b=a, out=nota); 18 | Nand(a=b, b=b, out=notb); 19 | Nand(a=nota, b=notb, out=out); 20 | } 21 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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: 8 | * out = not (a == b) 9 | */ 10 | 11 | CHIP Xor { 12 | IN a, b; 13 | OUT out; 14 | 15 | PARTS: 16 | Nand(a=a, b=b, out=nandab); 17 | Nand(a=a, b=nandab, out=aa); 18 | Nand(a=nandab, b=b, out=bb); 19 | Nand(a=aa, b=bb, out=out); 20 | } 21 | -------------------------------------------------------------------------------- /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 | Xor(a=a, b=b, out=sum); 17 | And(a=a, b=b, out=carry); 18 | } 19 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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: 8 | * out = a if sel == 0 9 | * b otherwise 10 | */ 11 | 12 | CHIP Mux { 13 | IN a, b, sel; 14 | OUT out; 15 | 16 | PARTS: 17 | Nand(a=sel, b=sel, out=notsel); 18 | Nand(a=a, b=notsel, out=aa); 19 | Nand(a=sel, b=b, out=bb); 20 | Nand(a=aa, b=bb, out=out); 21 | } 22 | -------------------------------------------------------------------------------- /06/rect/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 | -------------------------------------------------------------------------------- /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 | } 14 | 15 | /** De-allocates the array and frees its space. */ 16 | method void dispose() { 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /06/rect/RectL.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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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(sel=load, a=feedback, b=in, out=selected); 18 | DFF(in=selected, out=feedback, out=out); 19 | } 20 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /08/FunctionCalls/NestedCall/NestedCall.tst: -------------------------------------------------------------------------------- 1 | // Test file for NestedCall test. 2 | 3 | load NestedCall.asm, 4 | output-file NestedCall.out, 5 | compare-to NestedCall.cmp, 6 | output-list RAM[0]%D1.6.1 RAM[1]%D1.6.1 RAM[2]%D1.6.1 RAM[5]%D1.6.1 RAM[6]%D1.6.1; 7 | 8 | set RAM[0] 261, 9 | set RAM[1] 261, 10 | set RAM[2] 256, 11 | set RAM[3] -1, 12 | set RAM[4] -1, 13 | set RAM[256] 1234, // fake RIP 14 | set RAM[257] -1, 15 | set RAM[258] -1, 16 | set RAM[259] -1, 17 | set RAM[260] -1, 18 | 19 | repeat 1000 { 20 | ticktock; 21 | } 22 | 23 | output; 24 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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; -------------------------------------------------------------------------------- /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, carry=ca1, sum=su1); 17 | HalfAdder(a=su1, b=c, carry=ca2, sum=sum); 18 | Or(a=ca1, b=ca2, out=carry); 19 | } 20 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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; -------------------------------------------------------------------------------- /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: 8 | * out = (in[0] or in[1] or ... or in[7]) 9 | */ 10 | 11 | CHIP Or8Way { 12 | IN in[8]; 13 | OUT out; 14 | 15 | PARTS: 16 | Or(a=in[0], b=in[1], out=ab); 17 | Or(a=in[2], b=in[3], out=cd); 18 | Or(a=in[4], b=in[5], out=ef); 19 | Or(a=in[6], b=in[7], out=gh); 20 | Or(a=ab, b=cd, out=abcd); 21 | Or(a=ef, b=gh, out=efgh); 22 | Or(a=abcd, b=efgh, out=out); 23 | } 24 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /08/FunctionCalls/NestedCall/NestedCallVME.tst: -------------------------------------------------------------------------------- 1 | // Test file for NestedCall test. 2 | 3 | load Sys.vm, 4 | output-file NestedCall.out, 5 | compare-to NestedCall.cmp, 6 | output-list RAM[0]%D1.6.1 RAM[1]%D1.6.1 RAM[2]%D1.6.1 RAM[5]%D1.6.1 RAM[6]%D1.6.1; 7 | 8 | set RAM[0] 261, 9 | set RAM[1] 261, 10 | set RAM[2] 256, 11 | set RAM[3] -1, 12 | set RAM[4] -1, 13 | set RAM[256] 1234, // fake RIP 14 | set RAM[257] -1, 15 | set RAM[258] -1, 16 | set RAM[259] -1, 17 | set RAM[260] -1, 18 | 19 | set sp 261, 20 | set local 261, 21 | set argument 256, 22 | set this 3000, 23 | set that 4000; 24 | 25 | repeat 50 { 26 | vmstep; 27 | } 28 | output; 29 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | // Put your code here: 20 | Mux16(a=a, b=b, sel=sel[0], out=out1); 21 | Mux16(a=c, b=d, sel=sel[0], out=out2); 22 | Mux16(a=out1, b=out2, sel=sel[1], out=out); 23 | } 24 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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[0], a=out0, b=out1); 20 | DMux(in=out0, sel=sel[1], a=a, b=c); 21 | DMux(in=out1, sel=sel[1], a=b, b=d); 22 | } 23 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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[2], respectively.) 8 | 9 | @R2 10 | M=0 // R2 <- 0 11 | 12 | (LOOP) 13 | @R1 14 | D=M // D <- R1 15 | @END 16 | D;JLE // if D(=R1) <= 0 then goto END 17 | @R0 18 | D=M // D <- R0 19 | @R2 20 | M=D+M // R2 += R0 21 | @R1 22 | M=M-1 // R1-- 23 | @LOOP 24 | 0;JMP // LOOPへ戻る 25 | (END) 26 | @END 27 | 0;JMP 28 | -------------------------------------------------------------------------------- /04/fill/Fill.hack: -------------------------------------------------------------------------------- 1 | 0100000000000000 2 | 1110110000010000 3 | 0000000000010000 4 | 1110001100001000 5 | 0110000000000000 6 | 1111110000010000 7 | 0000000000001010 8 | 1110001100000001 9 | 0000000000001110 10 | 1110101010000111 11 | 0000000000010001 12 | 1110111010001000 13 | 0000000000010000 14 | 1110101010000111 15 | 0000000000010001 16 | 1110101010001000 17 | 0000000000010001 18 | 1111110000010000 19 | 0000000000010000 20 | 1111110000100000 21 | 1110001100001000 22 | 0000000000010000 23 | 1111110111001000 24 | 0010000000000000 25 | 1110110000010000 26 | 0100000000000000 27 | 1110000010010000 28 | 0000000000010000 29 | 1111010011010000 30 | 0000000000010000 31 | 1110001100000001 32 | 0000000000000000 33 | 1110101010000111 34 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | } 14 | 15 | /** Halts execution. */ 16 | function void halt() { 17 | } 18 | 19 | /** Waits approximately duration milliseconds and then returns. */ 20 | function void wait(int duration) { 21 | } 22 | 23 | /** Prints the given error code in the form "ERR", and halts. */ 24 | function void error(int errorCode) { 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /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 | CHIP DMux { 13 | IN in, sel; 14 | OUT a, b; 15 | 16 | PARTS: 17 | Nand(a=in, b=sel, out=notb); 18 | Nand(a=notb, b=notb, out=b); 19 | Nand(a=in, b=notb, out=nota); 20 | Nand(a=nota, b=nota, out=a); 21 | /* 22 | Nand(a=sel, b=sel, out=notsel); 23 | Nand(a=notsel, b=in, out=nota); 24 | Nand(a=nota, b=nota, out=a); 25 | Nand(a=sel, b=in, out=notb); 26 | Nand(a=notb, b=notb, out=b); 27 | */ 28 | } 29 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | CHIP DMux8Way { 15 | IN in, sel[3]; 16 | OUT a, b, c, d, e, f, g, h; 17 | 18 | PARTS: 19 | DMux4Way(in=in, sel=sel[0..1], a=s0, b=s1, c=s2, d=s3); 20 | DMux(in=s0, sel=sel[2], a=a, b=e); 21 | DMux(in=s1, sel=sel[2], a=b, b=f); 22 | DMux(in=s2, sel=sel[2], a=c, b=g); 23 | DMux(in=s3, sel=sel[2], a=d, b=h); 24 | } 25 | -------------------------------------------------------------------------------- /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; -------------------------------------------------------------------------------- /08/FunctionCalls/NestedCall/Sys.vm: -------------------------------------------------------------------------------- 1 | // Sys.vm for NestedCall test. 2 | // 3 | // Copyright (C) 2013 Mark A. Armbrust. 4 | // Permission granted for educational use. 5 | 6 | // Sys.init() calls Sys.main(), stores the return value in temp 1, 7 | // and enters an infinite loop. 8 | 9 | function Sys.init 0 10 | call Sys.main 0 11 | pop temp 1 12 | label LOOP 13 | goto LOOP 14 | 15 | // Sys.main() calls Sys.add12(123) and stores return value (135) in temp 0. 16 | // Returns 456. 17 | 18 | function Sys.main 0 19 | push constant 123 20 | call Sys.add12 1 21 | pop temp 0 22 | push constant 246 23 | return 24 | 25 | // Sys.add12(int x) returns x+12. 26 | // It allocates 3 words of local storage to test the deallocation of local 27 | // storage during the return. 28 | 29 | function Sys.add12 3 30 | push argument 0 31 | push constant 12 32 | add 33 | return 34 | -------------------------------------------------------------------------------- /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; -------------------------------------------------------------------------------- /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 | Mux16(a=a, b=b, sel=sel[0], out=ab); 22 | Mux16(a=c, b=d, sel=sel[0], out=cd); 23 | Mux16(a=e, b=f, sel=sel[0], out=ef); 24 | Mux16(a=g, b=h, sel=sel[0], out=gh); 25 | Mux16(a=ab, b=cd, sel=sel[1], out=abcd); 26 | Mux16(a=ef, b=gh, sel=sel[1], out=efgh); 27 | Mux16(a=abcd, b=efgh, sel=sel[2], out=out); 28 | } 29 | -------------------------------------------------------------------------------- /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 R2 = max(R0, R1) (R0,R1,R2 refer to RAM[0],RAM[1],RAM[2]) 7 | 8 | @R0 9 | D=M // D = first number 10 | @R1 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 | @R1 15 | D=M // D = second number 16 | @OUTPUT_D 17 | 0;JMP // goto output_d 18 | (OUTPUT_FIRST) 19 | @R0 20 | D=M // D = first number 21 | (OUTPUT_D) 22 | @R2 23 | M=D // M[2] = D (greatest number) 24 | (INFINITE_LOOP) 25 | @INFINITE_LOOP 26 | 0;JMP // infinite loop 27 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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; -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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: 8 | * for i=0..15: out[i] = not in[i] 9 | */ 10 | 11 | CHIP Not16 { 12 | IN in[16]; 13 | OUT out[16]; 14 | 15 | PARTS: 16 | Not(in=in[0], out=out[0]); 17 | Not(in=in[1], out=out[1]); 18 | Not(in=in[2], out=out[2]); 19 | Not(in=in[3], out=out[3]); 20 | Not(in=in[4], out=out[4]); 21 | Not(in=in[5], out=out[5]); 22 | Not(in=in[6], out=out[6]); 23 | Not(in=in[7], out=out[7]); 24 | Not(in=in[8], out=out[8]); 25 | Not(in=in[9], out=out[9]); 26 | Not(in=in[10], out=out[10]); 27 | Not(in=in[11], out=out[11]); 28 | Not(in=in[12], out=out[12]); 29 | Not(in=in[13], out=out[13]); 30 | Not(in=in[14], out=out[14]); 31 | Not(in=in[15], out=out[15]); 32 | } 33 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /12/Math.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/Math.jack 5 | 6 | /** 7 | * A basic math library. 8 | */ 9 | class Math { 10 | 11 | /** Initializes the library. */ 12 | function void init() { 13 | } 14 | 15 | /** Returns the absolute value of x. */ 16 | function int abs(int x) { 17 | } 18 | 19 | /** Returns the product of x and y. */ 20 | function int multiply(int x, int y) { 21 | } 22 | 23 | /** Returns the integer part of x/y. */ 24 | function int divide(int x, int y) { 25 | } 26 | 27 | /** Returns the integer part of the square root of x. */ 28 | function int sqrt(int x) { 29 | } 30 | 31 | /** Returns the greater number. */ 32 | function int max(int a, int b) { 33 | } 34 | 35 | /** Returns the smaller number. */ 36 | function int min(int a, int b) { 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /12/Memory.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/Memory.jack 5 | 6 | /** 7 | * Memory operations library. 8 | */ 9 | class Memory { 10 | 11 | /** Initializes memory parameters. */ 12 | function void init() { 13 | } 14 | 15 | /** Returns the value of the main memory at the given address. */ 16 | function int peek(int address) { 17 | } 18 | 19 | /** Sets the value of the main memory at this address 20 | * to the given value. */ 21 | function void poke(int address, int value) { 22 | } 23 | 24 | /** finds and allocates from the heap a memory block of the 25 | * specified size and returns a reference to its base address. */ 26 | function int alloc(int size) { 27 | } 28 | 29 | /** De-allocates the given object and frees its space. */ 30 | function void deAlloc(int object) { 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | Register(in=regin, load=regload, out=regout, out=out); 20 | 21 | Inc16(in=regout, out=incremented); 22 | 23 | // make register-in 24 | Mux16(a=tmp2, b=false, sel=reset, out=regin); 25 | Mux16(a=tmp1, b=in, sel=load, out=tmp2); 26 | Mux16(a=false, b=incremented, sel=inc, out=tmp1); 27 | 28 | // make register-load 29 | Or(a=reset, b=inc, out=resetorinc); 30 | Or(a=load, b=resetorinc, out=regload); 31 | } 32 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | * 16-bit bitwise Or: 8 | * for i = 0..15 out[i] = (a[i] or b[i]) 9 | */ 10 | 11 | CHIP Or16 { 12 | IN a[16], b[16]; 13 | OUT out[16]; 14 | 15 | PARTS: 16 | Or(a=a[0], b=b[0], out=out[0]); 17 | Or(a=a[1], b=b[1], out=out[1]); 18 | Or(a=a[2], b=b[2], out=out[2]); 19 | Or(a=a[3], b=b[3], out=out[3]); 20 | Or(a=a[4], b=b[4], out=out[4]); 21 | Or(a=a[5], b=b[5], out=out[5]); 22 | Or(a=a[6], b=b[6], out=out[6]); 23 | Or(a=a[7], b=b[7], out=out[7]); 24 | Or(a=a[8], b=b[8], out=out[8]); 25 | Or(a=a[9], b=b[9], out=out[9]); 26 | Or(a=a[10], b=b[10], out=out[10]); 27 | Or(a=a[11], b=b[11], out=out[11]); 28 | Or(a=a[12], b=b[12], out=out[12]); 29 | Or(a=a[13], b=b[13], out=out[13]); 30 | Or(a=a[14], b=b[14], out=out[14]); 31 | Or(a=a[15], b=b[15], out=out[15]); 32 | } 33 | -------------------------------------------------------------------------------- /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 bitwise And: 8 | * for i = 0..15: out[i] = (a[i] and b[i]) 9 | */ 10 | 11 | CHIP And16 { 12 | IN a[16], b[16]; 13 | OUT out[16]; 14 | 15 | PARTS: 16 | And(a=a[0], b=b[0], out=out[0]); 17 | And(a=a[1], b=b[1], out=out[1]); 18 | And(a=a[2], b=b[2], out=out[2]); 19 | And(a=a[3], b=b[3], out=out[3]); 20 | And(a=a[4], b=b[4], out=out[4]); 21 | And(a=a[5], b=b[5], out=out[5]); 22 | And(a=a[6], b=b[6], out=out[6]); 23 | And(a=a[7], b=b[7], out=out[7]); 24 | And(a=a[8], b=b[8], out=out[8]); 25 | And(a=a[9], b=b[9], out=out[9]); 26 | And(a=a[10], b=b[10], out=out[10]); 27 | And(a=a[11], b=b[11], out=out[11]); 28 | And(a=a[12], b=b[12], out=out[12]); 29 | And(a=a[13], b=b[13], out=out[13]); 30 | And(a=a[14], b=b[14], out=out[14]); 31 | And(a=a[15], b=b[15], out=out[15]); 32 | } 33 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /01/DMux8Way.out: -------------------------------------------------------------------------------- 1 | | in | sel | a | b | c | d | e | f | g | h | 2 | | 0 | 000 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 3 | | 0 | 001 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 4 | | 0 | 010 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 5 | | 0 | 011 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 6 | | 0 | 100 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 7 | | 0 | 101 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 8 | | 0 | 110 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 9 | | 0 | 111 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 10 | | 1 | 000 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 11 | | 1 | 001 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 12 | | 1 | 010 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 13 | | 1 | 011 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 14 | | 1 | 100 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 15 | | 1 | 101 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 16 | | 1 | 110 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 17 | | 1 | 111 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 18 | -------------------------------------------------------------------------------- /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 from the next time step onward). 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[0..1], 19 | a=load0, b=load1, c=load2, d=load3); 20 | 21 | RAM4K(in=in, load=load0, address=address[2..13], out=out0); 22 | RAM4K(in=in, load=load1, address=address[2..13], out=out1); 23 | RAM4K(in=in, load=load2, address=address[2..13], out=out2); 24 | RAM4K(in=in, load=load3, address=address[2..13], out=out3); 25 | 26 | Mux4Way16( 27 | a=out0, b=out1, c=out2, d=out3, 28 | sel=address[0..1], 29 | out=out); 30 | } 31 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /01/DMux8Way.cmp: -------------------------------------------------------------------------------- 1 | | in | sel | a | b | c | d | e | f | g | h | 2 | | 0 | 000 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 3 | | 0 | 001 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 4 | | 0 | 010 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 5 | | 0 | 011 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 6 | | 0 | 100 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 7 | | 0 | 101 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 8 | | 0 | 110 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 9 | | 0 | 111 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 10 | | 1 | 000 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 11 | | 1 | 001 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 12 | | 1 | 010 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 13 | | 1 | 011 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 14 | | 1 | 100 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 15 | | 1 | 101 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 16 | | 1 | 110 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 17 | | 1 | 111 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 18 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /12/Screen.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/Screen.jack 5 | 6 | /** 7 | * Graphic screen library. 8 | */ 9 | class Screen { 10 | 11 | /** Initializes the Screen. */ 12 | function void init() { 13 | } 14 | 15 | /** Erases the whole screen. */ 16 | function void clearScreen() { 17 | } 18 | 19 | /** Sets the color to be used in further draw commands 20 | * where white = false, black = true. */ 21 | function void setColor(boolean b) { 22 | } 23 | 24 | /** Draws the (x, y) pixel. */ 25 | function void drawPixel(int x, int y) { 26 | } 27 | 28 | /** Draws a line from (x1, y1) to (x2, y2). */ 29 | function void drawLine(int x1, int y1, int x2, int y2) { 30 | } 31 | 32 | /** Draws a filled rectangle where the top left corner 33 | * is (x1, y1) and the bottom right corner is (x2, y2). */ 34 | function void drawRectangle(int x1, int y1, int x2, int y2) { 35 | } 36 | 37 | /** Draws a filled circle of radius r around (cx, cy). */ 38 | function void drawCircle(int cx, int cy, int r) { 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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.3.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 | -------------------------------------------------------------------------------- /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: 8 | * for i = 0..15 out[i] = a[i] if sel == 0 9 | * b[i] if sel == 1 10 | */ 11 | 12 | CHIP Mux16 { 13 | IN a[16], b[16], sel; 14 | OUT out[16]; 15 | 16 | PARTS: 17 | Mux(a=a[0], b=b[0], sel=sel, out=out[0]); 18 | Mux(a=a[1], b=b[1], sel=sel, out=out[1]); 19 | Mux(a=a[2], b=b[2], sel=sel, out=out[2]); 20 | Mux(a=a[3], b=b[3], sel=sel, out=out[3]); 21 | Mux(a=a[4], b=b[4], sel=sel, out=out[4]); 22 | Mux(a=a[5], b=b[5], sel=sel, out=out[5]); 23 | Mux(a=a[6], b=b[6], sel=sel, out=out[6]); 24 | Mux(a=a[7], b=b[7], sel=sel, out=out[7]); 25 | Mux(a=a[8], b=b[8], sel=sel, out=out[8]); 26 | Mux(a=a[9], b=b[9], sel=sel, out=out[9]); 27 | Mux(a=a[10], b=b[10], sel=sel, out=out[10]); 28 | Mux(a=a[11], b=b[11], sel=sel, out=out[11]); 29 | Mux(a=a[12], b=b[12], sel=sel, out=out[12]); 30 | Mux(a=a[13], b=b[13], sel=sel, out=out[13]); 31 | Mux(a=a[14], b=b[14], sel=sel, out=out[14]); 32 | Mux(a=a[15], b=b[15], sel=sel, out=out[15]); 33 | } 34 | -------------------------------------------------------------------------------- /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( 23 | inM=memoryOut, 24 | instruction=romOut, 25 | reset=reset, 26 | 27 | outM=memoryIn, 28 | writeM=memoryLoad, 29 | addressM=memoryAddress, 30 | pc=romAddress 31 | ); 32 | 33 | ROM32K(address=romAddress, out=romOut); 34 | 35 | Memory( 36 | in=memoryIn, 37 | load=memoryLoad, 38 | address=memoryAddress, 39 | 40 | out=memoryOut 41 | ); 42 | } 43 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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: 8 | * out = in + 1 (arithmetic addition) 9 | */ 10 | 11 | CHIP Inc16 { 12 | IN in[16]; 13 | OUT out[16]; 14 | 15 | PARTS: 16 | Not(in=in[0], out=out[0]); 17 | HalfAdder(a=in[1], b=in[0], sum=out[1], carry=ca1); 18 | HalfAdder(a=in[2], b=ca1, sum=out[2], carry=ca2); 19 | HalfAdder(a=in[3], b=ca2, sum=out[3], carry=ca3); 20 | HalfAdder(a=in[4], b=ca3, sum=out[4], carry=ca4); 21 | HalfAdder(a=in[5], b=ca4, sum=out[5], carry=ca5); 22 | HalfAdder(a=in[6], b=ca5, sum=out[6], carry=ca6); 23 | HalfAdder(a=in[7], b=ca6, sum=out[7], carry=ca7); 24 | HalfAdder(a=in[8], b=ca7, sum=out[8], carry=ca8); 25 | HalfAdder(a=in[9], b=ca8, sum=out[9], carry=ca9); 26 | HalfAdder(a=in[10], b=ca9, sum=out[10], carry=ca10); 27 | HalfAdder(a=in[11], b=ca10, sum=out[11], carry=ca11); 28 | HalfAdder(a=in[12], b=ca11, sum=out[12], carry=ca12); 29 | HalfAdder(a=in[13], b=ca12, sum=out[13], carry=ca13); 30 | HalfAdder(a=in[14], b=ca13, sum=out[14], carry=ca14); 31 | HalfAdder(a=in[15], b=ca14, sum=out[15], carry=ca15); 32 | } 33 | -------------------------------------------------------------------------------- /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 from the next time step onward). 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, 19 | a=load0, b=load1, c=load2, d=load3, 20 | e=load4, f=load5, g=load6, h=load7); 21 | 22 | Register(in=in, load=load0, out=out0); 23 | Register(in=in, load=load1, out=out1); 24 | Register(in=in, load=load2, out=out2); 25 | Register(in=in, load=load3, out=out3); 26 | Register(in=in, load=load4, out=out4); 27 | Register(in=in, load=load5, out=out5); 28 | Register(in=in, load=load6, out=out6); 29 | Register(in=in, load=load7, out=out7); 30 | 31 | Mux8Way16( 32 | a=out0, b=out1, c=out2, d=out3, 33 | e=out4, f=out5, g=out6, h=out7, 34 | sel=address, 35 | out=out); 36 | } 37 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | (INIT) 12 | @SCREEN 13 | D=A // D <- SCREENのアドレス 14 | @addr // 塗りつぶしたいSCREEN上のワードを表す 15 | M=D // 初期値はSCREENアドレス 16 | 17 | // キーボードの状態を見て、塗りつぶす色を選択 18 | @KBD 19 | D=M 20 | @IFPRESS 21 | D;JGT 22 | @ELSE 23 | 0;JMP 24 | (IFPRESS) 25 | @color 26 | M=-1 // color=-1 黒 27 | @ENDIF 28 | 0;JMP 29 | (ELSE) 30 | @color 31 | M=0 // color=0 白 32 | (ENDIF) 33 | 34 | (LOOP) //塗りつぶすループ 35 | @color 36 | D=M // D <- color 37 | @addr 38 | A=M 39 | M=D // *addr <- color 40 | @addr 41 | M=M+1 42 | 43 | @8192 44 | D=A 45 | @SCREEN 46 | D=D+A 47 | @addr 48 | D=D-M // D= 8192 + SCREEN - addr 49 | // 塗り残しがあれば塗りつぶすまで続ける 50 | @LOOP 51 | D;JGT 52 | 53 | // 全部塗り終わったら、最初に戻る 54 | @INIT 55 | 0;JMP 56 | 57 | // このプログラムは永久に終了しない 58 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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=ca0); 17 | FullAdder(a=a[1], b=b[1], c=ca0, sum=out[1], carry=ca1); 18 | FullAdder(a=a[2], b=b[2], c=ca1, sum=out[2], carry=ca2); 19 | FullAdder(a=a[3], b=b[3], c=ca2, sum=out[3], carry=ca3); 20 | FullAdder(a=a[4], b=b[4], c=ca3, sum=out[4], carry=ca4); 21 | FullAdder(a=a[5], b=b[5], c=ca4, sum=out[5], carry=ca5); 22 | FullAdder(a=a[6], b=b[6], c=ca5, sum=out[6], carry=ca6); 23 | FullAdder(a=a[7], b=b[7], c=ca6, sum=out[7], carry=ca7); 24 | FullAdder(a=a[8], b=b[8], c=ca7, sum=out[8], carry=ca8); 25 | FullAdder(a=a[9], b=b[9], c=ca8, sum=out[9], carry=ca9); 26 | FullAdder(a=a[10], b=b[10], c=ca9, sum=out[10], carry=ca10); 27 | FullAdder(a=a[11], b=b[11], c=ca10, sum=out[11], carry=ca11); 28 | FullAdder(a=a[12], b=b[12], c=ca11, sum=out[12], carry=ca12); 29 | FullAdder(a=a[13], b=b[13], c=ca12, sum=out[13], carry=ca13); 30 | FullAdder(a=a[14], b=b[14], c=ca13, sum=out[14], carry=ca14); 31 | FullAdder(a=a[15], b=b[15], c=ca14, sum=out[15], carry=ca15); 32 | } 33 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 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 from the next time step onward). 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[0..2], 19 | a=load0, b=load1, c=load2, d=load3, 20 | e=load4, f=load5, g=load6, h=load7); 21 | 22 | RAM8(in=in, load=load0, address=address[3..5], out=out0); 23 | RAM8(in=in, load=load1, address=address[3..5], out=out1); 24 | RAM8(in=in, load=load2, address=address[3..5], out=out2); 25 | RAM8(in=in, load=load3, address=address[3..5], out=out3); 26 | RAM8(in=in, load=load4, address=address[3..5], out=out4); 27 | RAM8(in=in, load=load5, address=address[3..5], out=out5); 28 | RAM8(in=in, load=load6, address=address[3..5], out=out6); 29 | RAM8(in=in, load=load7, address=address[3..5], out=out7); 30 | 31 | Mux8Way16( 32 | a=out0, b=out1, c=out2, d=out3, 33 | e=out4, f=out5, g=out6, h=out7, 34 | sel=address[0..2], 35 | out=out); 36 | } 37 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 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 from the next time step onward). 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[0..2], 19 | a=load0, b=load1, c=load2, d=load3, 20 | e=load4, f=load5, g=load6, h=load7); 21 | 22 | RAM512(in=in, load=load0, address=address[3..11], out=out0); 23 | RAM512(in=in, load=load1, address=address[3..11], out=out1); 24 | RAM512(in=in, load=load2, address=address[3..11], out=out2); 25 | RAM512(in=in, load=load3, address=address[3..11], out=out3); 26 | RAM512(in=in, load=load4, address=address[3..11], out=out4); 27 | RAM512(in=in, load=load5, address=address[3..11], out=out5); 28 | RAM512(in=in, load=load6, address=address[3..11], out=out6); 29 | RAM512(in=in, load=load7, address=address[3..11], out=out7); 30 | 31 | Mux8Way16( 32 | a=out0, b=out1, c=out2, d=out3, 33 | e=out4, f=out5, g=out6, h=out7, 34 | sel=address[0..2], 35 | out=out); 36 | } 37 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 from the next time step onward). 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[0..2], 19 | a=load0, b=load1, c=load2, d=load3, 20 | e=load4, f=load5, g=load6, h=load7); 21 | 22 | RAM64(in=in, load=load0, address=address[3..8], out=out0); 23 | RAM64(in=in, load=load1, address=address[3..8], out=out1); 24 | RAM64(in=in, load=load2, address=address[3..8], out=out2); 25 | RAM64(in=in, load=load3, address=address[3..8], out=out3); 26 | RAM64(in=in, load=load4, address=address[3..8], out=out4); 27 | RAM64(in=in, load=load5, address=address[3..8], out=out5); 28 | RAM64(in=in, load=load6, address=address[3..8], out=out6); 29 | RAM64(in=in, load=load7, address=address[3..8], out=out7); 30 | 31 | Mux8Way16( 32 | a=out0, b=out1, c=out2, d=out3, 33 | e=out4, f=out5, g=out6, h=out7, 34 | sel=address[0..2], 35 | out=out); 36 | } 37 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /12/String.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/String.jack 5 | 6 | /** 7 | * Represents a String object. Implements the String type. 8 | */ 9 | class String { 10 | 11 | /** Constructs a new empty String with a maximum length of maxLength. */ 12 | constructor String new(int maxLength) { 13 | } 14 | 15 | /** De-allocates the string and frees its space. */ 16 | method void dispose() { 17 | } 18 | 19 | /** Returns the current length of this String. */ 20 | method int length() { 21 | } 22 | 23 | /** Returns the character at location j. */ 24 | method char charAt(int j) { 25 | } 26 | 27 | /** Sets the j'th character of this string to be c. */ 28 | method void setCharAt(int j, char c) { 29 | } 30 | 31 | /** Appends the character c to the end of this String. 32 | * Returns this string as the return value. */ 33 | method String appendChar(char c) { 34 | } 35 | 36 | /** Erases the last character from this String. */ 37 | method void eraseLastChar() { 38 | } 39 | 40 | /** Returns the integer value of this String until the first non 41 | * numeric character. */ 42 | method int intValue() { 43 | } 44 | 45 | /** Sets this String to hold a representation of the given number. */ 46 | method void setInt(int number) { 47 | } 48 | 49 | /** Returns the new line character. */ 50 | function char newLine() { 51 | } 52 | 53 | /** Returns the backspace character. */ 54 | function char backSpace() { 55 | } 56 | 57 | /** Returns the double quote (") character. */ 58 | function char doubleQuote() { 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /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 Hack 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 from the next time step onward. 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 | DMux(in=load, sel=address[14], a=ramload, b=ioload); // 1 { 31 | DMux(in=ioload, sel=address[13], a=scrload, b=kbload); // 2 { 32 | 33 | RAM16K(in=in, load=ramload, address=address[0..13], out=ramout); 34 | Screen(in=in, load=scrload, address=address[0..12], out=scrout); 35 | Keyboard(out=kbout); 36 | 37 | Mux16(a=scrout, b=kbout, sel=address[13], out=ioout); // } 2 38 | Mux16(a=ramout, b=ioout, sel=address[14], out=out); // } 1 39 | } 40 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /12/Keyboard.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/Keyboard.jack 5 | 6 | /** 7 | * A library for handling user input from the keyboard. 8 | */ 9 | class Keyboard { 10 | 11 | /** Initializes the keyboard. */ 12 | function void init() { 13 | } 14 | 15 | /** 16 | * Returns the ASCII code (as char) of the currently pressed key, 17 | * or 0 if no key is currently pressed. 18 | * Recognizes all ASCII characters, as well as the following extension 19 | * of action keys: 20 | * New line = 128 = String.newline() 21 | * Backspace = 129 = String.backspace() 22 | * Left Arrow = 130 23 | * Up Arrow = 131 24 | * Right Arrow = 132 25 | * Down Arrow = 133 26 | * Home = 134 27 | * End = 135 28 | * Page Up = 136 29 | * Page Down = 137 30 | * Insert = 138 31 | * Delete = 139 32 | * ESC = 140 33 | * F1 - F12 = 141 - 152 34 | */ 35 | function char keyPressed() { 36 | } 37 | 38 | /** 39 | * Reads the next character from the keyboard. 40 | * waits until a key is pressed and then released, then echoes 41 | * the key to the screen, and returns the value of the pressed key. 42 | */ 43 | function char readChar() { 44 | } 45 | 46 | /** 47 | * Prints the message on the screen, reads the next line 48 | * (until a newline character) from the keyboard, and returns its value. 49 | */ 50 | function String readLine(String message) { 51 | } 52 | 53 | /** 54 | * Prints the message on the screen, reads the next line 55 | * (until a newline character) from the keyboard, and returns its 56 | * integer value (until the first non numeric character). 57 | */ 58 | function int readInt(String message) { 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /10/Square/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 | SquareGame 29 | . 30 | new 31 | ( 32 | 33 | 34 | ) 35 | 36 | 37 | ; 38 | 39 | 40 | do 41 | game 42 | . 43 | run 44 | ( 45 | 46 | 47 | ) 48 | ; 49 | 50 | 51 | do 52 | game 53 | . 54 | dispose 55 | ( 56 | 57 | 58 | ) 59 | ; 60 | 61 | 62 | return 63 | ; 64 | 65 | 66 | } 67 | 68 | 69 | } 70 | 71 | -------------------------------------------------------------------------------- /11/ComplexArrays/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/ComplexArrays/Main.jack 5 | 6 | /** 7 | * Performs several complex Array tests. 8 | * For each test, the required result is printed along with the 9 | * actual result. In each test, the two results should be equal. 10 | */ 11 | class Main { 12 | 13 | function void main() { 14 | var Array a, b, c; 15 | 16 | let a = Array.new(10); 17 | let b = Array.new(5); 18 | let c = Array.new(1); 19 | 20 | let a[3] = 2; 21 | let a[4] = 8; 22 | let a[5] = 4; 23 | let b[a[3]] = a[3] + 3; // b[2] = 5 24 | let a[b[a[3]]] = a[a[5]] * b[((7 - a[3]) - Main.double(2)) + 1]; // a[5] = 8 * 5 = 40 25 | let c[0] = null; 26 | let c = c[0]; 27 | 28 | do Output.printString("Test 1 - Required result: 5, Actual result: "); 29 | do Output.printInt(b[2]); 30 | do Output.println(); 31 | do Output.printString("Test 2 - Required result: 40, Actual result: "); 32 | do Output.printInt(a[5]); 33 | do Output.println(); 34 | do Output.printString("Test 3 - Required result: 0, Actual result: "); 35 | do Output.printInt(c); 36 | do Output.println(); 37 | 38 | let c = null; 39 | 40 | if (c = null) { 41 | do Main.fill(a, 10); 42 | let c = a[3]; 43 | let c[1] = 33; 44 | let c = a[7]; 45 | let c[1] = 77; 46 | let b = a[3]; 47 | let b[1] = b[1] + c[1]; // b[1] = 33 + 77 = 110; 48 | } 49 | 50 | do Output.printString("Test 4 - Required result: 77, Actual result: "); 51 | do Output.printInt(c[1]); 52 | do Output.println(); 53 | do Output.printString("Test 5 - Required result: 110, Actual result: "); 54 | do Output.printInt(b[1]); 55 | do Output.println(); 56 | 57 | return; 58 | } 59 | 60 | function int double(int a) { 61 | return a * 2; 62 | } 63 | 64 | function void fill(Array a, int size) { 65 | while (size > 0) { 66 | let size = size - 1; 67 | let a[size] = Array.new(3); 68 | } 69 | 70 | return; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /05/Memory.cmp: -------------------------------------------------------------------------------- 1 | | in |load | address | out | 2 | | -1 | 1 | 000000000000000 | 0 | 3 | | -1 | 1 | 000000000000000 | -1 | 4 | | 9999 | 0 | 000000000000000 | -1 | 5 | | 9999 | 0 | 000000000000000 | -1 | 6 | | 9999 | 0 | 010000000000000 | 0 | 7 | | 9999 | 0 | 100000000000000 | 0 | 8 | | 2222 | 1 | 010000000000000 | 0 | 9 | | 2222 | 1 | 010000000000000 | 2222 | 10 | | 9999 | 0 | 010000000000000 | 2222 | 11 | | 9999 | 0 | 010000000000000 | 2222 | 12 | | 9999 | 0 | 000000000000000 | -1 | 13 | | 9999 | 0 | 100000000000000 | 0 | 14 | | 9999 | 0 | 000000000000001 | 0 | 15 | | 9999 | 0 | 000000000000010 | 0 | 16 | | 9999 | 0 | 000000000000100 | 0 | 17 | | 9999 | 0 | 000000000001000 | 0 | 18 | | 9999 | 0 | 000000000010000 | 0 | 19 | | 9999 | 0 | 000000000100000 | 0 | 20 | | 9999 | 0 | 000000001000000 | 0 | 21 | | 9999 | 0 | 000000010000000 | 0 | 22 | | 9999 | 0 | 000000100000000 | 0 | 23 | | 9999 | 0 | 000001000000000 | 0 | 24 | | 9999 | 0 | 000010000000000 | 0 | 25 | | 9999 | 0 | 000100000000000 | 0 | 26 | | 9999 | 0 | 001000000000000 | 0 | 27 | | 9999 | 0 | 010000000000000 | 2222 | 28 | | 1234 | 1 | 001001000110100 | 0 | 29 | | 1234 | 1 | 001001000110100 | 1234 | 30 | | 1234 | 0 | 010001000110100 | 0 | 31 | | 1234 | 0 | 110001000110100 | 0 | 32 | | 2345 | 1 | 010001101000101 | 0 | 33 | | 2345 | 1 | 010001101000101 | 2345 | 34 | | 2345 | 0 | 000001101000101 | 0 | 35 | | 2345 | 0 | 100001101000101 | 0 | 36 | | 2345 | 0 | 110000000000000 | 75 | 37 | | -1 | 1 | 100111111001111 | -1 | 38 | | -1 | 1 | 101000001001111 | -1 | 39 | | -1 | 1 | 000111111001111 | 0 | 40 | | -1 | 1 | 010111111001111 | 0 | 41 | | -1 | 0 | 100111111001110 | 0 | 42 | | -1 | 0 | 100111111001101 | 0 | 43 | | -1 | 0 | 100111111001011 | 0 | 44 | | -1 | 0 | 100111111000111 | 0 | 45 | | -1 | 0 | 100111111011111 | 0 | 46 | | -1 | 0 | 100111111101111 | 0 | 47 | | -1 | 0 | 100111110001111 | 0 | 48 | | -1 | 0 | 100111101001111 | 0 | 49 | | -1 | 0 | 100111011001111 | 0 | 50 | | -1 | 0 | 100110111001111 | 0 | 51 | | -1 | 0 | 100101111001111 | 0 | 52 | | -1 | 0 | 100011111001111 | 0 | 53 | | -1 | 0 | 101111111001111 | 0 | 54 | | -1 | 0 | 110000000000000 | 89 | 55 | -------------------------------------------------------------------------------- /02/ALU.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/ALU.hdl 5 | 6 | /** 7 | * The ALU (Arithmetic Logic Unit). 8 | * Computes one of the following functions: 9 | * x+y, x-y, y-x, 0, 1, -1, x, y, -x, -y, !x, !y, 10 | * x+1, y+1, x-1, y-1, x&y, x|y on two 16-bit inputs, 11 | * according to 6 input bits denoted zx,nx,zy,ny,f,no. 12 | * In addition, the ALU computes two 1-bit outputs: 13 | * if the ALU output == 0, zr is set to 1; otherwise zr is set to 0; 14 | * if the ALU output < 0, ng is set to 1; otherwise ng is set to 0. 15 | */ 16 | 17 | // Implementation: the ALU logic manipulates the x and y inputs 18 | // and operates on the resulting values, as follows: 19 | // if (zx == 1) set x = 0 // 16-bit constant 20 | // if (nx == 1) set x = !x // bitwise not 21 | // if (zy == 1) set y = 0 // 16-bit constant 22 | // if (ny == 1) set y = !y // bitwise not 23 | // if (f == 1) set out = x + y // integer 2's complement addition 24 | // if (f == 0) set out = x & y // bitwise and 25 | // if (no == 1) set out = !out // bitwise not 26 | // if (out == 0) set zr = 1 27 | // if (out < 0) set ng = 1 28 | 29 | CHIP ALU { 30 | IN 31 | x[16], y[16], // 16-bit inputs 32 | zx, // zero the x input? 33 | nx, // negate the x input? 34 | zy, // zero the y input? 35 | ny, // negate the y input? 36 | f, // compute out = x + y (if 1) or x & y (if 0) 37 | no; // negate the out output? 38 | 39 | OUT 40 | out[16], // 16-bit output 41 | zr, // 1 if (out == 0), 0 otherwise 42 | ng; // 1 if (out < 0), 0 otherwise 43 | 44 | PARTS: 45 | // if (zx == 1) set x = 0 // 16-bit constant 46 | Mux16(a=x, b[0..15]=false, sel=zx, out=xx); 47 | // if (nx == 1) set x = !x // bitwise not 48 | Not16(in=xx, out=notx); 49 | Mux16(a=xx, b=notx, sel=nx, out=xxx); 50 | 51 | // if (zy == 1) set y = 0 // 16-bit constant 52 | Mux16(a=y, b[0..15]=false, sel=zy, out=yy); 53 | // if (ny == 1) set y = !y // bitwise not 54 | Not16(in=yy, out=noty); 55 | Mux16(a=yy, b=noty, sel=ny, out=yyy); 56 | 57 | // if (f == 1) set out = x + y // integer 2's complement addition 58 | // if (f == 0) set out = x & y // bitwise and 59 | Add16(a=xxx, b=yyy, out=xplusy); 60 | And16(a=xxx, b=yyy, out=xandy); 61 | Mux16(a=xandy, b=xplusy, sel=f, out=fout); 62 | 63 | // if (no == 1) set out = !out // bitwise not 64 | // if (out < 0) set ng = 1 65 | Not16(in=fout, out=notfout); 66 | Mux16(a=fout, b=notfout, sel=no, out=out, out[0..7]=aout, out[8..15]=bout, out[15]=ng); 67 | 68 | // if (out == 0) set zr = 1 69 | Or8Way(in=aout, out=zr1); 70 | Or8Way(in=bout, out=zr2); 71 | Or(a=zr1, b=zr2, out=any); 72 | Not(in=any, out=zr); 73 | } 74 | -------------------------------------------------------------------------------- /11/ConvertToBin/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/ConvertToBin/Main.jack 5 | 6 | /** 7 | * Unpacks a 16-bit number into its binary representation: 8 | * Takes the 16-bit number stored in RAM[8000] and stores its individual 9 | * bits in RAM[8001..8016] (each location will contain 0 or 1). 10 | * Before the conversion, RAM[8001]..RAM[8016] are initialized to -1. 11 | * 12 | * The program should be tested as follows: 13 | * 1) Load the program into the supplied VM Emulator 14 | * 2) Put some value in RAM[8000] 15 | * 3) Switch to "no animation" 16 | * 4) Run the program (give it enough time to run) 17 | * 5) Stop the program 18 | * 6) Check that RAM[8001]..RAM[8016] contains the correct binary result, and 19 | * that none of these memory locations contain -1. 20 | */ 21 | class Main { 22 | 23 | /** 24 | * Initializes RAM[8001]..RAM[8016] to -1, and converts the value in 25 | * RAM[8000] to binary. 26 | */ 27 | function void main() { 28 | var int result, value; 29 | 30 | do Main.fillMemory(8001, 16, -1); // sets RAM[8001]..RAM[8016] to -1 31 | let value = Memory.peek(8000); // reads a value from RAM[8000] 32 | do Main.convert(value); // perform the conversion 33 | 34 | return; 35 | } 36 | 37 | /** Converts the given decimal value to binary, and puts 38 | * the resulting bits in RAM[8001]..RAM[8016]. */ 39 | function void convert(int value) { 40 | var int mask, position; 41 | var boolean loop; 42 | 43 | let loop = true; 44 | 45 | while (loop) { 46 | let position = position + 1; 47 | let mask = Main.nextMask(mask); 48 | do Memory.poke(9000 + position, mask); 49 | 50 | if (~(position > 16)) { 51 | 52 | if (~((value & mask) = 0)) { 53 | do Memory.poke(8000 + position, 1); 54 | } 55 | else { 56 | do Memory.poke(8000 + position, 0); 57 | } 58 | } 59 | else { 60 | let loop = false; 61 | } 62 | } 63 | 64 | return; 65 | } 66 | 67 | /** Returns the next mask (the mask that should follow the given mask). */ 68 | function int nextMask(int mask) { 69 | if (mask = 0) { 70 | return 1; 71 | } 72 | else { 73 | return mask * 2; 74 | } 75 | } 76 | 77 | /** Fills 'length' consecutive memory locations with 'value', 78 | * starting at 'startAddress'. */ 79 | function void fillMemory(int startAddress, int length, int value) { 80 | while (length > 0) { 81 | do Memory.poke(startAddress, value); 82 | let length = length - 1; 83 | let startAddress = startAddress + 1; 84 | } 85 | 86 | return; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /01/Mux8Way16.out: -------------------------------------------------------------------------------- 1 | | a | b | c | d | e | f | g | h | sel | out | 2 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 000 | 0000000000000000 | 3 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 001 | 0000000000000000 | 4 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 010 | 0000000000000000 | 5 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 011 | 0000000000000000 | 6 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 100 | 0000000000000000 | 7 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 101 | 0000000000000000 | 8 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 110 | 0000000000000000 | 9 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 111 | 0000000000000000 | 10 | | 0001001000110100 | 0010001101000101 | 0011010001010110 | 0100010101100111 | 0101011001111000 | 0110011110001001 | 0111100010011010 | 1000100110101011 | 000 | 0001001000110100 | 11 | | 0001001000110100 | 0010001101000101 | 0011010001010110 | 0100010101100111 | 0101011001111000 | 0110011110001001 | 0111100010011010 | 1000100110101011 | 001 | 0010001101000101 | 12 | | 0001001000110100 | 0010001101000101 | 0011010001010110 | 0100010101100111 | 0101011001111000 | 0110011110001001 | 0111100010011010 | 1000100110101011 | 010 | 0011010001010110 | 13 | | 0001001000110100 | 0010001101000101 | 0011010001010110 | 0100010101100111 | 0101011001111000 | 0110011110001001 | 0111100010011010 | 1000100110101011 | 011 | 0100010101100111 | 14 | | 0001001000110100 | 0010001101000101 | 0011010001010110 | 0100010101100111 | 0101011001111000 | 0110011110001001 | 0111100010011010 | 1000100110101011 | 100 | 0101011001111000 | 15 | | 0001001000110100 | 0010001101000101 | 0011010001010110 | 0100010101100111 | 0101011001111000 | 0110011110001001 | 0111100010011010 | 1000100110101011 | 101 | 0110011110001001 | 16 | | 0001001000110100 | 0010001101000101 | 0011010001010110 | 0100010101100111 | 0101011001111000 | 0110011110001001 | 0111100010011010 | 1000100110101011 | 110 | 0111100010011010 | 17 | | 0001001000110100 | 0010001101000101 | 0011010001010110 | 0100010101100111 | 0101011001111000 | 0110011110001001 | 0111100010011010 | 1000100110101011 | 111 | 1000100110101011 | 18 | -------------------------------------------------------------------------------- /01/Mux8Way16.cmp: -------------------------------------------------------------------------------- 1 | | a | b | c | d | e | f | g | h | sel | out | 2 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 000 | 0000000000000000 | 3 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 001 | 0000000000000000 | 4 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 010 | 0000000000000000 | 5 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 011 | 0000000000000000 | 6 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 100 | 0000000000000000 | 7 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 101 | 0000000000000000 | 8 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 110 | 0000000000000000 | 9 | | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 111 | 0000000000000000 | 10 | | 0001001000110100 | 0010001101000101 | 0011010001010110 | 0100010101100111 | 0101011001111000 | 0110011110001001 | 0111100010011010 | 1000100110101011 | 000 | 0001001000110100 | 11 | | 0001001000110100 | 0010001101000101 | 0011010001010110 | 0100010101100111 | 0101011001111000 | 0110011110001001 | 0111100010011010 | 1000100110101011 | 001 | 0010001101000101 | 12 | | 0001001000110100 | 0010001101000101 | 0011010001010110 | 0100010101100111 | 0101011001111000 | 0110011110001001 | 0111100010011010 | 1000100110101011 | 010 | 0011010001010110 | 13 | | 0001001000110100 | 0010001101000101 | 0011010001010110 | 0100010101100111 | 0101011001111000 | 0110011110001001 | 0111100010011010 | 1000100110101011 | 011 | 0100010101100111 | 14 | | 0001001000110100 | 0010001101000101 | 0011010001010110 | 0100010101100111 | 0101011001111000 | 0110011110001001 | 0111100010011010 | 1000100110101011 | 100 | 0101011001111000 | 15 | | 0001001000110100 | 0010001101000101 | 0011010001010110 | 0100010101100111 | 0101011001111000 | 0110011110001001 | 0111100010011010 | 1000100110101011 | 101 | 0110011110001001 | 16 | | 0001001000110100 | 0010001101000101 | 0011010001010110 | 0100010101100111 | 0101011001111000 | 0110011110001001 | 0111100010011010 | 1000100110101011 | 110 | 0111100010011010 | 17 | | 0001001000110100 | 0010001101000101 | 0011010001010110 | 0100010101100111 | 0101011001111000 | 0110011110001001 | 0111100010011010 | 1000100110101011 | 111 | 1000100110101011 | 18 | -------------------------------------------------------------------------------- /12/KeyboardTest/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/KeyboardTest/Main.jack 5 | 6 | /** Test program for the OS Keyboard class. */ 7 | class Main { 8 | 9 | /** Gets input from the user and verifies its contents. */ 10 | function void main() { 11 | var char c, key; 12 | var String s; 13 | var int i; 14 | var boolean ok; 15 | 16 | let ok = false; 17 | do Output.printString("keyPressed test:"); 18 | do Output.println(); 19 | while (~ok) { 20 | do Output.printString("Please press the 'Page Down' key"); 21 | while (key = 0) { 22 | let key = Keyboard.keyPressed(); 23 | } 24 | let c = key; 25 | while (~(key = 0)) { 26 | let key = Keyboard.keyPressed(); 27 | } 28 | 29 | do Output.println(); 30 | 31 | if (c = 137) { 32 | do Output.printString("ok"); 33 | do Output.println(); 34 | let ok = true; 35 | } 36 | } 37 | 38 | let ok = false; 39 | do Output.printString("readChar test:"); 40 | do Output.println(); 41 | do Output.printString("(Verify that the pressed character is echoed to the screen)"); 42 | do Output.println(); 43 | while (~ok) { 44 | do Output.printString("Please press the number '3': "); 45 | let c = Keyboard.readChar(); 46 | 47 | do Output.println(); 48 | 49 | if (c = 51) { 50 | do Output.printString("ok"); 51 | do Output.println(); 52 | let ok = true; 53 | } 54 | } 55 | 56 | let ok = false; 57 | do Output.printString("readLine test:"); 58 | do Output.println(); 59 | do Output.printString("(Verify echo and usage of 'backspace')"); 60 | do Output.println(); 61 | while (~ok) { 62 | let s = Keyboard.readLine("Please type 'JACK' and press enter: "); 63 | 64 | if (s.length() = 4) { 65 | if ((s.charAt(0) = 74) & (s.charAt(1) = 65) & (s.charAt(2) = 67) & (s.charAt(3) = 75)) { 66 | do Output.printString("ok"); 67 | do Output.println(); 68 | let ok = true; 69 | } 70 | } 71 | } 72 | 73 | let ok = false; 74 | do Output.printString("readInt test:"); 75 | do Output.println(); 76 | do Output.printString("(Verify echo and usage of 'backspace')"); 77 | do Output.println(); 78 | while (~ok) { 79 | let i = Keyboard.readInt("Please type '-32123' and press enter: "); 80 | 81 | if (i = (-32123)) { 82 | do Output.printString("ok"); 83 | do Output.println(); 84 | let ok = true; 85 | } 86 | } 87 | 88 | do Output.println(); 89 | do Output.printString("Test completed successfully"); 90 | 91 | return; 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /02/ALU-nostat.out: -------------------------------------------------------------------------------- 1 | | x | y |zx |nx |zy |ny | f |no | out | 2 | | 0000000000000000 | 1111111111111111 | 1 | 0 | 1 | 0 | 1 | 0 | 0000000000000000 | 3 | | 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | 4 | | 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 0 | 1 | 0 | 1111111111111111 | 5 | | 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 0 | 0000000000000000 | 6 | | 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 0 | 1111111111111111 | 7 | | 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 1 | 1111111111111111 | 8 | | 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 1 | 0000000000000000 | 9 | | 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 1 | 1 | 0000000000000000 | 10 | | 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 1 | 1 | 0000000000000001 | 11 | | 0000000000000000 | 1111111111111111 | 0 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | 12 | | 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 1 | 1 | 1 | 0000000000000000 | 13 | | 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 1 | 0 | 1111111111111111 | 14 | | 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 1 | 0 | 1111111111111110 | 15 | | 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 0 | 1 | 0 | 1111111111111111 | 16 | | 0000000000000000 | 1111111111111111 | 0 | 1 | 0 | 0 | 1 | 1 | 0000000000000001 | 17 | | 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 1 | 1 | 1 | 1111111111111111 | 18 | | 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 0 | 0 | 0 | 0000000000000000 | 19 | | 0000000000000000 | 1111111111111111 | 0 | 1 | 0 | 1 | 0 | 1 | 1111111111111111 | 20 | | 0101101110100000 | 0001111011010010 | 1 | 0 | 1 | 0 | 1 | 0 | 0000000000000000 | 21 | | 0101101110100000 | 0001111011010010 | 1 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | 22 | | 0101101110100000 | 0001111011010010 | 1 | 1 | 1 | 0 | 1 | 0 | 1111111111111111 | 23 | | 0101101110100000 | 0001111011010010 | 0 | 0 | 1 | 1 | 0 | 0 | 0101101110100000 | 24 | | 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 0 | 0 | 0 | 0001111011010010 | 25 | | 0101101110100000 | 0001111011010010 | 0 | 0 | 1 | 1 | 0 | 1 | 1010010001011111 | 26 | | 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 0 | 0 | 1 | 1110000100101101 | 27 | | 0101101110100000 | 0001111011010010 | 0 | 0 | 1 | 1 | 1 | 1 | 1010010001100000 | 28 | | 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 0 | 1 | 1 | 1110000100101110 | 29 | | 0101101110100000 | 0001111011010010 | 0 | 1 | 1 | 1 | 1 | 1 | 0101101110100001 | 30 | | 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 1 | 1 | 1 | 0001111011010011 | 31 | | 0101101110100000 | 0001111011010010 | 0 | 0 | 1 | 1 | 1 | 0 | 0101101110011111 | 32 | | 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 0 | 1 | 0 | 0001111011010001 | 33 | | 0101101110100000 | 0001111011010010 | 0 | 0 | 0 | 0 | 1 | 0 | 0111101001110010 | 34 | | 0101101110100000 | 0001111011010010 | 0 | 1 | 0 | 0 | 1 | 1 | 0011110011001110 | 35 | | 0101101110100000 | 0001111011010010 | 0 | 0 | 0 | 1 | 1 | 1 | 1100001100110010 | 36 | | 0101101110100000 | 0001111011010010 | 0 | 0 | 0 | 0 | 0 | 0 | 0001101010000000 | 37 | | 0101101110100000 | 0001111011010010 | 0 | 1 | 0 | 1 | 0 | 1 | 0101111111110010 | 38 | -------------------------------------------------------------------------------- /02/ALU-nostat.cmp: -------------------------------------------------------------------------------- 1 | | x | y |zx |nx |zy |ny | f |no | out | 2 | | 0000000000000000 | 1111111111111111 | 1 | 0 | 1 | 0 | 1 | 0 | 0000000000000000 | 3 | | 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | 4 | | 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 0 | 1 | 0 | 1111111111111111 | 5 | | 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 0 | 0000000000000000 | 6 | | 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 0 | 1111111111111111 | 7 | | 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 1 | 1111111111111111 | 8 | | 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 1 | 0000000000000000 | 9 | | 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 1 | 1 | 0000000000000000 | 10 | | 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 1 | 1 | 0000000000000001 | 11 | | 0000000000000000 | 1111111111111111 | 0 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | 12 | | 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 1 | 1 | 1 | 0000000000000000 | 13 | | 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 1 | 0 | 1111111111111111 | 14 | | 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 1 | 0 | 1111111111111110 | 15 | | 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 0 | 1 | 0 | 1111111111111111 | 16 | | 0000000000000000 | 1111111111111111 | 0 | 1 | 0 | 0 | 1 | 1 | 0000000000000001 | 17 | | 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 1 | 1 | 1 | 1111111111111111 | 18 | | 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 0 | 0 | 0 | 0000000000000000 | 19 | | 0000000000000000 | 1111111111111111 | 0 | 1 | 0 | 1 | 0 | 1 | 1111111111111111 | 20 | | 0101101110100000 | 0001111011010010 | 1 | 0 | 1 | 0 | 1 | 0 | 0000000000000000 | 21 | | 0101101110100000 | 0001111011010010 | 1 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | 22 | | 0101101110100000 | 0001111011010010 | 1 | 1 | 1 | 0 | 1 | 0 | 1111111111111111 | 23 | | 0101101110100000 | 0001111011010010 | 0 | 0 | 1 | 1 | 0 | 0 | 0101101110100000 | 24 | | 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 0 | 0 | 0 | 0001111011010010 | 25 | | 0101101110100000 | 0001111011010010 | 0 | 0 | 1 | 1 | 0 | 1 | 1010010001011111 | 26 | | 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 0 | 0 | 1 | 1110000100101101 | 27 | | 0101101110100000 | 0001111011010010 | 0 | 0 | 1 | 1 | 1 | 1 | 1010010001100000 | 28 | | 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 0 | 1 | 1 | 1110000100101110 | 29 | | 0101101110100000 | 0001111011010010 | 0 | 1 | 1 | 1 | 1 | 1 | 0101101110100001 | 30 | | 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 1 | 1 | 1 | 0001111011010011 | 31 | | 0101101110100000 | 0001111011010010 | 0 | 0 | 1 | 1 | 1 | 0 | 0101101110011111 | 32 | | 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 0 | 1 | 0 | 0001111011010001 | 33 | | 0101101110100000 | 0001111011010010 | 0 | 0 | 0 | 0 | 1 | 0 | 0111101001110010 | 34 | | 0101101110100000 | 0001111011010010 | 0 | 1 | 0 | 0 | 1 | 1 | 0011110011001110 | 35 | | 0101101110100000 | 0001111011010010 | 0 | 0 | 0 | 1 | 1 | 1 | 1100001100110010 | 36 | | 0101101110100000 | 0001111011010010 | 0 | 0 | 0 | 0 | 0 | 0 | 0001101010000000 | 37 | | 0101101110100000 | 0001111011010010 | 0 | 1 | 0 | 1 | 0 | 1 | 0101111111110010 | 38 | -------------------------------------------------------------------------------- /12/StringTest/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/StringTest/Main.jack 5 | 6 | /** Test program for the OS String class. */ 7 | class Main { 8 | 9 | /** Performs several string manipulations and prints their results. */ 10 | function void main() { 11 | var String s; 12 | var String i; 13 | 14 | let s = String.new(0); // A zero-capacity string should be supported 15 | do s.dispose(); 16 | 17 | let s = String.new(6); // capacity 6, make sure length 5 will be printed 18 | let s = s.appendChar(97); 19 | let s = s.appendChar(98); 20 | let s = s.appendChar(99); 21 | let s = s.appendChar(100); 22 | let s = s.appendChar(101); 23 | do Output.printString("new,appendChar: "); 24 | do Output.printString(s); // new, appendChar: abcde 25 | do Output.println(); 26 | 27 | let i = String.new(6); 28 | do i.setInt(12345); 29 | do Output.printString("setInt: "); 30 | do Output.printString(i); // setInt: 12345 31 | do Output.println(); 32 | 33 | do i.setInt(-32767); 34 | do Output.printString("setInt: "); 35 | do Output.printString(i); // setInt: -32767 36 | do Output.println(); 37 | 38 | do Output.printString("length: "); 39 | do Output.printInt(s.length()); // length: 5 40 | do Output.println(); 41 | 42 | do Output.printString("charAt[2]: "); 43 | do Output.printInt(s.charAt(2)); // charAt[2]: 99 44 | do Output.println(); 45 | 46 | do s.setCharAt(2, 45); 47 | do Output.printString("setCharAt(2,'-'): "); 48 | do Output.printString(s); // setCharAt(2,'-'): ab-de 49 | do Output.println(); 50 | 51 | do s.eraseLastChar(); 52 | do Output.printString("eraseLastChar: "); 53 | do Output.printString(s); // eraseLastChar: ab-d 54 | do Output.println(); 55 | 56 | let s = "456"; 57 | do Output.printString("intValue: "); 58 | do Output.printInt(s.intValue()); // intValue: 456 59 | do Output.println(); 60 | 61 | let s = "-32123"; 62 | do Output.printString("intValue: "); 63 | do Output.printInt(s.intValue()); // intValue: -32123 64 | do Output.println(); 65 | 66 | do Output.printString("backSpace: "); 67 | do Output.printInt(String.backSpace()); // backSpace: 129 68 | do Output.println(); 69 | 70 | do Output.printString("doubleQuote: "); 71 | do Output.printInt(String.doubleQuote());// doubleQuote: 34 72 | do Output.println(); 73 | 74 | do Output.printString("newLine: "); 75 | do Output.printInt(String.newLine()); // newLine: 128 76 | do Output.println(); 77 | 78 | do i.dispose(); 79 | do s.dispose(); 80 | 81 | return; 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /11/Pong/Bat.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/Bat.jack. 5 | 6 | /** 7 | * A graphic Pong bat. Has a screen location, width and height. 8 | * Has methods for drawing, erasing, moving left and right on 9 | * the screen and changing the width. 10 | */ 11 | class Bat { 12 | 13 | // The screen location 14 | field int x, y; 15 | 16 | // The width and height 17 | field int width, height; 18 | 19 | // The direction of the bat's movement 20 | field int direction; // 1 = left, 2 = right 21 | 22 | /** Constructs a new bat with the given location and width. */ 23 | constructor Bat new(int Ax, int Ay, int Awidth, int Aheight) { 24 | let x = Ax; 25 | let y = Ay; 26 | let width = Awidth; 27 | let height = Aheight; 28 | let direction = 2; 29 | 30 | do show(); 31 | 32 | return this; 33 | } 34 | 35 | /** Deallocates the object's memory. */ 36 | method void dispose() { 37 | do Memory.deAlloc(this); 38 | return; 39 | } 40 | 41 | /** Draws the bat on the screen. */ 42 | method void show() { 43 | do Screen.setColor(true); 44 | do draw(); 45 | return; 46 | } 47 | 48 | /** Erases the bat from the screen. */ 49 | method void hide() { 50 | do Screen.setColor(false); 51 | do draw(); 52 | return; 53 | } 54 | 55 | /** Draws the bat. */ 56 | method void draw() { 57 | do Screen.drawRectangle(x, y, x + width, y + height); 58 | return; 59 | } 60 | 61 | /** Sets the direction of the bat (0=stop, 1=left, 2=right). */ 62 | method void setDirection(int Adirection) { 63 | let direction = Adirection; 64 | return; 65 | } 66 | 67 | /** Returns the left edge of the bat. */ 68 | method int getLeft() { 69 | return x; 70 | } 71 | 72 | /** Returns the right edge of the bat. */ 73 | method int getRight() { 74 | return x + width; 75 | } 76 | 77 | /** Sets the width. */ 78 | method void setWidth(int Awidth) { 79 | do hide(); 80 | let width = Awidth; 81 | do show(); 82 | return; 83 | } 84 | 85 | /** Moves the bat one step in its direction. */ 86 | method void move() { 87 | if (direction = 1) { 88 | let x = x - 4; 89 | if (x < 0) { 90 | let x = 0; 91 | } 92 | do Screen.setColor(false); 93 | do Screen.drawRectangle((x + width) + 1, y, (x + width) + 4, y + height); 94 | do Screen.setColor(true); 95 | do Screen.drawRectangle(x, y, x + 3, y + height); 96 | } 97 | else { 98 | let x = x + 4; 99 | if ((x + width) > 511) { 100 | let x = 511 - width; 101 | } 102 | do Screen.setColor(false); 103 | do Screen.drawRectangle(x - 4, y, x - 1, y + height); 104 | do Screen.setColor(true); 105 | do Screen.drawRectangle((x + width) - 3, y, x + width, y + height); 106 | } 107 | 108 | return; 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /10/ExpressionlessSquare/SquareGame.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/SquareGame.jack 5 | 6 | // Expressionless version of Square.jack. 7 | 8 | /** 9 | * The SquareDance class implements the Square Dance game. 10 | * In this game you can move a black square around the screen and 11 | * change its size during the movement. 12 | * In the beggining, the square is located at the top left corner. 13 | * Use the arrow keys to move the square. 14 | * Use 'z' & 'x' to decrement & increment the size. 15 | * Use 'q' to quit. 16 | */ 17 | class SquareGame { 18 | 19 | // The square 20 | field Square square; 21 | 22 | // The square's movement direction 23 | field int direction; // 0=none,1=up,2=down,3=left,4=right 24 | 25 | // Constructs a new Square Game. 26 | constructor SquareGame new() { 27 | let square = square; 28 | let direction = direction; 29 | 30 | return square; 31 | } 32 | 33 | // Deallocates the object's memory. 34 | method void dispose() { 35 | do square.dispose(); 36 | do Memory.deAlloc(square); 37 | return; 38 | } 39 | 40 | // Starts the game. Handles inputs from the user that controls 41 | // the square movement direction and size. 42 | method void run() { 43 | var char key; 44 | var boolean exit; 45 | 46 | let exit = key; 47 | 48 | while (exit) { 49 | // waits for a key to be pressed. 50 | while (key) { 51 | let key = key; 52 | do moveSquare(); 53 | } 54 | 55 | if (key) { 56 | let exit = exit; 57 | } 58 | if (key) { 59 | do square.decSize(); 60 | } 61 | if (key) { 62 | do square.incSize(); 63 | } 64 | if (key) { 65 | let direction = exit; 66 | } 67 | if (key) { 68 | let direction = key; 69 | } 70 | if (key) { 71 | let direction = square; 72 | } 73 | if (key) { 74 | let direction = direction; 75 | } 76 | 77 | // waits for the key to be released. 78 | while (key) { 79 | let key = key; 80 | do moveSquare(); 81 | } 82 | } 83 | 84 | return; 85 | } 86 | 87 | // Moves the square by 2 in the current direction. 88 | method void moveSquare() { 89 | if (direction) { 90 | do square.moveUp(); 91 | } 92 | if (direction) { 93 | do square.moveDown(); 94 | } 95 | if (direction) { 96 | do square.moveLeft(); 97 | } 98 | if (direction) { 99 | do square.moveRight(); 100 | } 101 | 102 | do Sys.wait(direction); // Delays the next movement. 103 | return; 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /09/Square/SquareGame.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/SquareGame.jack 5 | 6 | /** 7 | * Implements the Square Dance game. 8 | * In this game you can move a black square around the screen and 9 | * change its size during the movement. 10 | * In the beginning, the square is located at the top-left corner 11 | * of the screen. The arrow keys are used to move the square. 12 | * The 'z' & 'x' keys are used to decrement and increment the size. 13 | * The 'q' key is used to quit the game. 14 | */ 15 | class SquareGame { 16 | 17 | // The square 18 | field Square square; 19 | 20 | // The square's movement direction 21 | field int direction; // 0=none,1=up,2=down,3=left,4=right 22 | 23 | /** Constructs a new Square Game. */ 24 | constructor SquareGame new() { 25 | let square = Square.new(0, 0, 30); 26 | let direction = 0; 27 | 28 | return this; 29 | } 30 | 31 | /** Deallocates the object's memory. */ 32 | method void dispose() { 33 | do square.dispose(); 34 | do Memory.deAlloc(this); 35 | return; 36 | } 37 | 38 | /** Starts the game. Handles inputs from the user that control 39 | * the square's movement, direction and size. */ 40 | method void run() { 41 | var char key; 42 | var boolean exit; 43 | 44 | let exit = false; 45 | 46 | while (~exit) { 47 | // waits for a key to be pressed. 48 | while (key = 0) { 49 | let key = Keyboard.keyPressed(); 50 | do moveSquare(); 51 | } 52 | 53 | if (key = 81) { 54 | let exit = true; 55 | } 56 | if (key = 90) { 57 | do square.decSize(); 58 | } 59 | if (key = 88) { 60 | do square.incSize(); 61 | } 62 | if (key = 131) { 63 | let direction = 1; 64 | } 65 | if (key = 133) { 66 | let direction = 2; 67 | } 68 | if (key = 130) { 69 | let direction = 3; 70 | } 71 | if (key = 132) { 72 | let direction = 4; 73 | } 74 | 75 | // waits for the key to be released. 76 | while (~(key = 0)) { 77 | let key = Keyboard.keyPressed(); 78 | do moveSquare(); 79 | } 80 | } 81 | 82 | return; 83 | } 84 | 85 | /** Moves the square by 2 pixels in the current direction. */ 86 | method void moveSquare() { 87 | if (direction = 1) { 88 | do square.moveUp(); 89 | } 90 | if (direction = 2) { 91 | do square.moveDown(); 92 | } 93 | if (direction = 3) { 94 | do square.moveLeft(); 95 | } 96 | if (direction = 4) { 97 | do square.moveRight(); 98 | } 99 | 100 | do Sys.wait(5); // Delays the next movement. 101 | return; 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /10/Square/SquareGame.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/SquareGame.jack 5 | 6 | /** 7 | * Implements the Square Dance game. 8 | * In this game you can move a black square around the screen and 9 | * change its size during the movement. 10 | * In the beginning, the square is located at the top-left corner 11 | * of the screen. The arrow keys are used to move the square. 12 | * The 'z' & 'x' keys are used to decrement and increment the size. 13 | * The 'q' key is used to quit the game. 14 | */ 15 | class SquareGame { 16 | 17 | // The square 18 | field Square square; 19 | 20 | // The square's movement direction 21 | field int direction; // 0=none,1=up,2=down,3=left,4=right 22 | 23 | /** Constructs a new Square Game. */ 24 | constructor SquareGame new() { 25 | let square = Square.new(0, 0, 30); 26 | let direction = 0; 27 | 28 | return this; 29 | } 30 | 31 | /** Deallocates the object's memory. */ 32 | method void dispose() { 33 | do square.dispose(); 34 | do Memory.deAlloc(this); 35 | return; 36 | } 37 | 38 | /** Starts the game. Handles inputs from the user that control 39 | * the square's movement, direction and size. */ 40 | method void run() { 41 | var char key; 42 | var boolean exit; 43 | 44 | let exit = false; 45 | 46 | while (~exit) { 47 | // waits for a key to be pressed. 48 | while (key = 0) { 49 | let key = Keyboard.keyPressed(); 50 | do moveSquare(); 51 | } 52 | 53 | if (key = 81) { 54 | let exit = true; 55 | } 56 | if (key = 90) { 57 | do square.decSize(); 58 | } 59 | if (key = 88) { 60 | do square.incSize(); 61 | } 62 | if (key = 131) { 63 | let direction = 1; 64 | } 65 | if (key = 133) { 66 | let direction = 2; 67 | } 68 | if (key = 130) { 69 | let direction = 3; 70 | } 71 | if (key = 132) { 72 | let direction = 4; 73 | } 74 | 75 | // waits for the key to be released. 76 | while (~(key = 0)) { 77 | let key = Keyboard.keyPressed(); 78 | do moveSquare(); 79 | } 80 | } 81 | 82 | return; 83 | } 84 | 85 | /** Moves the square by 2 pixels in the current direction. */ 86 | method void moveSquare() { 87 | if (direction = 1) { 88 | do square.moveUp(); 89 | } 90 | if (direction = 2) { 91 | do square.moveDown(); 92 | } 93 | if (direction = 3) { 94 | do square.moveLeft(); 95 | } 96 | if (direction = 4) { 97 | do square.moveRight(); 98 | } 99 | 100 | do Sys.wait(5); // Delays the next movement. 101 | return; 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /11/Square/SquareGame.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/SquareGame.jack 5 | 6 | /** 7 | * Implements the Square Dance game. 8 | * In this game you can move a black square around the screen and 9 | * change its size during the movement. 10 | * In the beginning, the square is located at the top-left corner 11 | * of the screen. The arrow keys are used to move the square. 12 | * The 'z' & 'x' keys are used to decrement and increment the size. 13 | * The 'q' key is used to quit the game. 14 | */ 15 | class SquareGame { 16 | 17 | // The square 18 | field Square square; 19 | 20 | // The square's movement direction 21 | field int direction; // 0=none,1=up,2=down,3=left,4=right 22 | 23 | /** Constructs a new Square Game. */ 24 | constructor SquareGame new() { 25 | let square = Square.new(0, 0, 30); 26 | let direction = 0; 27 | 28 | return this; 29 | } 30 | 31 | /** Deallocates the object's memory. */ 32 | method void dispose() { 33 | do square.dispose(); 34 | do Memory.deAlloc(this); 35 | return; 36 | } 37 | 38 | /** Starts the game. Handles inputs from the user that control 39 | * the square's movement, direction and size. */ 40 | method void run() { 41 | var char key; 42 | var boolean exit; 43 | 44 | let exit = false; 45 | 46 | while (~exit) { 47 | // waits for a key to be pressed. 48 | while (key = 0) { 49 | let key = Keyboard.keyPressed(); 50 | do moveSquare(); 51 | } 52 | 53 | if (key = 81) { 54 | let exit = true; 55 | } 56 | if (key = 90) { 57 | do square.decSize(); 58 | } 59 | if (key = 88) { 60 | do square.incSize(); 61 | } 62 | if (key = 131) { 63 | let direction = 1; 64 | } 65 | if (key = 133) { 66 | let direction = 2; 67 | } 68 | if (key = 130) { 69 | let direction = 3; 70 | } 71 | if (key = 132) { 72 | let direction = 4; 73 | } 74 | 75 | // waits for the key to be released. 76 | while (~(key = 0)) { 77 | let key = Keyboard.keyPressed(); 78 | do moveSquare(); 79 | } 80 | } 81 | 82 | return; 83 | } 84 | 85 | /** Moves the square by 2 pixels in the current direction. */ 86 | method void moveSquare() { 87 | if (direction = 1) { 88 | do square.moveUp(); 89 | } 90 | if (direction = 2) { 91 | do square.moveDown(); 92 | } 93 | if (direction = 3) { 94 | do square.moveLeft(); 95 | } 96 | if (direction = 4) { 97 | do square.moveRight(); 98 | } 99 | 100 | do Sys.wait(5); // Delays the next movement. 101 | return; 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /02/ALU.out: -------------------------------------------------------------------------------- 1 | | x | y |zx |nx |zy |ny | f |no | out |zr |ng | 2 | | 0000000000000000 | 1111111111111111 | 1 | 0 | 1 | 0 | 1 | 0 | 0000000000000000 | 1 | 0 | 3 | | 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | 0 | 0 | 4 | | 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 0 | 1 | 0 | 1111111111111111 | 0 | 1 | 5 | | 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 0 | 0000000000000000 | 1 | 0 | 6 | | 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 0 | 1111111111111111 | 0 | 1 | 7 | | 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 1 | 1111111111111111 | 0 | 1 | 8 | | 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 1 | 0000000000000000 | 1 | 0 | 9 | | 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 1 | 1 | 0000000000000000 | 1 | 0 | 10 | | 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 1 | 1 | 0000000000000001 | 0 | 0 | 11 | | 0000000000000000 | 1111111111111111 | 0 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | 0 | 0 | 12 | | 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 1 | 1 | 1 | 0000000000000000 | 1 | 0 | 13 | | 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 1 | 0 | 1111111111111111 | 0 | 1 | 14 | | 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 1 | 0 | 1111111111111110 | 0 | 1 | 15 | | 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 0 | 1 | 0 | 1111111111111111 | 0 | 1 | 16 | | 0000000000000000 | 1111111111111111 | 0 | 1 | 0 | 0 | 1 | 1 | 0000000000000001 | 0 | 0 | 17 | | 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 1 | 1 | 1 | 1111111111111111 | 0 | 1 | 18 | | 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 0 | 0 | 0 | 0000000000000000 | 1 | 0 | 19 | | 0000000000000000 | 1111111111111111 | 0 | 1 | 0 | 1 | 0 | 1 | 1111111111111111 | 0 | 1 | 20 | | 0000000000010001 | 0000000000000011 | 1 | 0 | 1 | 0 | 1 | 0 | 0000000000000000 | 1 | 0 | 21 | | 0000000000010001 | 0000000000000011 | 1 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | 0 | 0 | 22 | | 0000000000010001 | 0000000000000011 | 1 | 1 | 1 | 0 | 1 | 0 | 1111111111111111 | 0 | 1 | 23 | | 0000000000010001 | 0000000000000011 | 0 | 0 | 1 | 1 | 0 | 0 | 0000000000010001 | 0 | 0 | 24 | | 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 0 | 0 | 0 | 0000000000000011 | 0 | 0 | 25 | | 0000000000010001 | 0000000000000011 | 0 | 0 | 1 | 1 | 0 | 1 | 1111111111101110 | 0 | 1 | 26 | | 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 0 | 0 | 1 | 1111111111111100 | 0 | 1 | 27 | | 0000000000010001 | 0000000000000011 | 0 | 0 | 1 | 1 | 1 | 1 | 1111111111101111 | 0 | 1 | 28 | | 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 0 | 1 | 1 | 1111111111111101 | 0 | 1 | 29 | | 0000000000010001 | 0000000000000011 | 0 | 1 | 1 | 1 | 1 | 1 | 0000000000010010 | 0 | 0 | 30 | | 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 1 | 1 | 1 | 0000000000000100 | 0 | 0 | 31 | | 0000000000010001 | 0000000000000011 | 0 | 0 | 1 | 1 | 1 | 0 | 0000000000010000 | 0 | 0 | 32 | | 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 0 | 1 | 0 | 0000000000000010 | 0 | 0 | 33 | | 0000000000010001 | 0000000000000011 | 0 | 0 | 0 | 0 | 1 | 0 | 0000000000010100 | 0 | 0 | 34 | | 0000000000010001 | 0000000000000011 | 0 | 1 | 0 | 0 | 1 | 1 | 0000000000001110 | 0 | 0 | 35 | | 0000000000010001 | 0000000000000011 | 0 | 0 | 0 | 1 | 1 | 1 | 1111111111110010 | 0 | 1 | 36 | | 0000000000010001 | 0000000000000011 | 0 | 0 | 0 | 0 | 0 | 0 | 0000000000000001 | 0 | 0 | 37 | | 0000000000010001 | 0000000000000011 | 0 | 1 | 0 | 1 | 0 | 1 | 0000000000010011 | 0 | 0 | 38 | --------------------------------------------------------------------------------