Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.5 kB
1
Indexable
Never
// 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.

    @SCREEN
    D=A
    @SCREENADDRESS
    M=D

    @SCREENCOLOR
    M=0

    @INITIALIZESCREENADDRESS
    0;JMP



(CHANGESCREENWHITE)
    @24576 // Get keyboard reg
    D=M // Sets keyboard reg to D
    @CHANGESCREENBLACK
    D;JNE

    @SCREENCOLOR
    M=0

    @COLORCHANGED
    0;JMP

(CHANGESCREENBLACK)
    @-1 
    D=A
    @SCREENCOLOR
    M=D

    @COLORCHANGED
    0;JMP

(INITIALIZESCREENADDRESS)
    @SCREEN
    D=A
    @SCREENADDRESS
    M=D
    @LOOP
    0;JMP

(LOOP)

    @CHANGESCREENWHITE 
    0;JMP // Loops

    (COLORCHANGED)

    @SCREENADDRESS
    D=M
    @24575
    D=A-D
    @INITIALIZESCREENADDRESS
    D;JLT

    @SCREENCOLOR // Setting the value in memory to this will change 1/16th of a row
    D=M
    @SCREENADDRESS
    A=M
    M=D
    @SCREENADDRESS
    M=M+1
    @LOOP 
    0;JMP // Loops