add Fill.asm

This commit is contained in:
Konarak 2021-12-07 19:09:03 +05:30
parent 465f3da702
commit 04b283ccec

128
projects/04/fill/Fill.asm Normal file
View File

@ -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