From 04b283cceca94acc034cc02ad9f39768003f2d4d Mon Sep 17 00:00:00 2001 From: Konarak Date: Tue, 7 Dec 2021 19:09:03 +0530 Subject: [PATCH] add Fill.asm --- projects/04/fill/Fill.asm | 128 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 projects/04/fill/Fill.asm diff --git a/projects/04/fill/Fill.asm b/projects/04/fill/Fill.asm new file mode 100644 index 0000000..6cc2244 --- /dev/null +++ b/projects/04/fill/Fill.asm @@ -0,0 +1,128 @@ +// This file is part of www.nand2tetris.org +// and the book "The Elements of Computing Systems" +// by Nisan and Schocken, MIT Press. +// File name: projects/04/Fill.asm + +// Runs an infinite loop that listens to the keyboard input. +// When a key is pressed (any key), the program blackens the screen, +// i.e. writes "black" in every pixel; +// the screen should remain fully black as long as the key is pressed. +// When no key is pressed, the program clears the screen, i.e. writes +// "white" in every pixel; +// the screen should remain fully clear as long as no key is pressed. + +// Put your code here. + +// addr = SCREEN map start address +@SCREEN +D=A +@addr +M=D + +// reg = SCREEN map register total count +@8192 +D=A +@reg +M=D + +// itr = current register count +@itr +M=0 + +// black if pix = -1 +// white if pix = 0 +@pix +M=-1 + +// loop declaration +// D = reg - itr +// if 0 jump to @END +// else continue + (LOOP) +@itr +D=M +@reg +D=D-M +@END +D;JEQ + +// addr = 16384..24575 +// @16384 = pix +// @16385 = pix +// . +// . +// @24575 = pix +@pix +D=M +@addr +A=M +M=D + +// itr = itr + 1 +// addr = addr + 1 +@itr +M=M+1 +@addr +M=M+1 + +//back to loop declaration +@LOOP +0;JMP + + +// clear the screen if no key pressed: +// pix = 0 +// itr = 0 +// addr = 16384 +// back to loop declaration + (CLEAR) +@pix +M=0 + +@itr +M=0 + +@SCREEN +D=A +@addr +M=D + +@LOOP +0;JMP + +// paint the screen if no key pressed: +// pix = -1 +// itr = 0 +// addr = 16384 +// back to loop declaration + (PAINT) +@pix +M=-1 + +@itr +M=0 + +@SCREEN +D=A +@addr +M=D + +@LOOP +0;JMP + +// main feedback loop +// jump to @CLEAR if no key pressed +// jump to @PAINT if any key pressed + (END) +@KBD +D=M +@CLEAR +D;JEQ + +@KBD +D=M +@PAINT +D;JGT + +@END +0;JMP