-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fill.asm
121 lines (92 loc) · 2 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// 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.
// while (1){
// input = keyboard[];
//
// if (input > 0)
// {
// color = -1; (black)
//
// }
// else {
// color = 0; (white)
// }
//
// rows = 16
// cols = 32
// counter = 0
// for (i = 0; i < rows; i++) {
// for (j = 0; i < cols; j++) {
// Screen[counter] = color;
// counter += j + i
// }
// }
//}
//
(LOOP) // while (1)
@KBD // input = key
D=M
@input
M=D
@SCREEN // counter = screen[0]
D=A
@counter
M=D
@input
D=M
@IF
M=0
@IF // if input > 0
D;JEQ
@ELSE
0;JMP
(IF)
@color // color = 0 (white)
M=0
@ENDIF
0;JMP
(ELSE) // color = -1 (black)
@color
M=-1
@ENDIF
0;JMP
(ENDIF)
@i // i = 0
M=0
@8160 // rows = 8160
D=A
@rows
M=D
@ENDFOR
M=1
(FOR) // if i == rows end loop
@i
D=M
@rows
D=D-M
@ENDFOR
D;JEQ
@color
D=M
@counter
A=M
M=D
@i // i++
M=M+1
@counter
M=M+1
@FOR
0;JMP
(ENDFOR)
@LOOP // END
0;JMP