summaryrefslogtreecommitdiff
path: root/projects/04/fill/Fill.asm
blob: 1b69de110cb4f8e21c3c67742ad032c76a686792 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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