summaryrefslogtreecommitdiff
path: root/projects/04/fill/Fill.asm
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2022-08-16 11:54:23 +0800
committerFrederick Yin <fkfd@fkfd.me>2022-08-16 11:54:23 +0800
commit9542deeb483a00b6fabed7574720926ce97d7511 (patch)
tree0f2c1f72c03dd4693fd59df67544d2a4dddc5494 /projects/04/fill/Fill.asm
parent9c0cb1d1c32724fc95ac9548e4f8d873d3adaccc (diff)
Projects, 01-06 completed
Diffstat (limited to 'projects/04/fill/Fill.asm')
-rw-r--r--projects/04/fill/Fill.asm60
1 files changed, 60 insertions, 0 deletions
diff --git a/projects/04/fill/Fill.asm b/projects/04/fill/Fill.asm
new file mode 100644
index 0000000..1b69de1
--- /dev/null
+++ b/projects/04/fill/Fill.asm
@@ -0,0 +1,60 @@
+// 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.
+
+@8192
+D=A
+@words
+M=D // words = 8192
+
+(KBD_LOOP)
+ @KBD
+ D=M
+ @FILL_WHITE
+ D;JEQ // if (KBD == 0) goto FILL_WHITE
+ @color
+ M=-1 // color = 0xffff (black)
+ @FILL
+ 0;JMP
+
+(FILL_WHITE)
+ @color
+ M=0 // color = 0x0000 (white)
+ @FILL
+ 0;JMP
+
+(FILL)
+ @i
+ M=0 // i = 0
+ @SCREEN
+ D=A
+ @pos
+ M=D // pos = &SCREEN
+ (DRAW)
+ @color
+ D=M
+ @pos
+ A=M
+ M=D // *pos = color
+ @pos
+ M=M+1 // pos++
+ @i
+ M=M+1 // i++
+ D=M // D = i
+ @words
+ D=D-M // D = i - words
+ @KBD_LOOP
+ D;JEQ // if (i - words == 0) break
+ @DRAW
+ 0;JMP