-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fill.asm
65 lines (54 loc) · 1.16 KB
/
Fill.asm
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
61
62
63
64
65
// 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. When no key is pressed, the
// program clears the screen, i.e. writes "white" in every pixel.
// Put your code here.
(RESTART)
@SCREEN
D=A
@0
M=D //PUT SCREEN START LOCATION IN RAM0
///////////////////////////
(KBDCHECK)
@KBD
D=M
@BLACK
D;JGT //JUMP IF ANY KBD KEYS ARE PRESSED
@WHITE
D;JEQ //ELSE JUMP TO WHITEN
@KBDCHECK
0;JMP
///////////////////////////
(BLACK)
@1
M=-1 //WHAT TO FILL SCREEN WITH (-1=11111111111111)
@CHANGE
0;JMP
(WHITE)
@1
M=0 //WHAT TO FILL SCREEN WITH
@CHANGE
0;JMP
//////////////////////////
(CHANGE)
@1 //CHECK WHAT TO FILL SCREEN WITH
D=M //D CONTAINS BLACK OR WHITE
@0
A=M //GET ADRESS OF SCREEN PIXEL TO FILL
M=D //FILL IT
@0
D=M+1 //INC TO NEXT PIXEL
@KBD
D=A-D //KBD-SCREEN=A
@0
M=M+1 //INC TO NEXT PIXEL
A=M
@CHANGE
D;JGT //IF A=0 EXIT AS THE WHOLE SCREEN IS BLACK
/////////////////////////
@RESTART
0;JMP