Add solutions for part 1
This commit is contained in:
97
projects/04/fill/Fill.asm
Normal file
97
projects/04/fill/Fill.asm
Normal file
@@ -0,0 +1,97 @@
|
||||
// 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.
|
||||
|
||||
// const_num_screen_words = 32 * 256 = 8192
|
||||
@8192
|
||||
D = A
|
||||
@const_num_screen_words
|
||||
M = D
|
||||
|
||||
// pixel_value = 0
|
||||
@pixel_value
|
||||
M = 0
|
||||
|
||||
(MAINLOOP)
|
||||
// if RAM[KBD] == 0:
|
||||
// pixel_value = 0
|
||||
// else:
|
||||
// pixel_value = -1
|
||||
@KBD
|
||||
D = M
|
||||
@IF_TRUE
|
||||
D;JEQ
|
||||
@IF_FALSE
|
||||
D;JMP
|
||||
|
||||
(IF_TRUE)
|
||||
@pixel_value
|
||||
M = 0
|
||||
@END_IF
|
||||
0;JMP
|
||||
(IF_FALSE)
|
||||
@pixel_value
|
||||
M = -1
|
||||
(END_IF)
|
||||
|
||||
@BLACKSCREEN
|
||||
0;JMP
|
||||
|
||||
|
||||
(BLACKSCREEN)
|
||||
// i = 0
|
||||
@i
|
||||
M = 0
|
||||
|
||||
// current_word = 0
|
||||
@current_word
|
||||
M = 0
|
||||
|
||||
// while i < const_num_screen_words
|
||||
(BLACKSCREEN_LOOP)
|
||||
// if (i - const_num_screen_words >= 0) goto BLACKSCREEN_LOOP_END
|
||||
@i
|
||||
D = M
|
||||
@const_num_screen_words
|
||||
D = D - M
|
||||
@BLACKSCREEN_LOOP_END
|
||||
D;JGE
|
||||
|
||||
// current_word = SCREEN + 1
|
||||
@i
|
||||
D = M
|
||||
@SCREEN
|
||||
D = D + A
|
||||
@current_word
|
||||
M = D
|
||||
|
||||
// RAM[current_word] = pixel_value
|
||||
@pixel_value
|
||||
D = M
|
||||
@current_word
|
||||
A = M
|
||||
M = D
|
||||
|
||||
// i++
|
||||
@i
|
||||
M = M + 1
|
||||
|
||||
// goto BLACKSCREEN_LOOP
|
||||
@BLACKSCREEN_LOOP
|
||||
0;JMP
|
||||
(BLACKSCREEN_LOOP_END)
|
||||
|
||||
// goto MAINLOOP
|
||||
@MAINLOOP
|
||||
0;JMP
|
||||
Reference in New Issue
Block a user