Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions nand2tetris/projects/4/Fill.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// 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/4/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 screen should be cleared.


// Set nscr to number of addresses on screen
@8192
D = A
@nscr
M = D


// Set pSCREEN to address of SCREEN
@SCREEN
D = A
@pSCREEN
M = D


(CHECKKBD)
// if (KBD != 0) then do
// col = -1
// lastkbd = 1
// goto COLSCREEN
// end
// else // (KBD = 0) do
// if lastkbd = 0 then
// goto CHECKKBD
// else // (lastkbd != 0) do
// lastkbd = 0
// col = 0
// i = 0
// goto COLSCREEN
// end
// end


// if (KBD != 0) then do
// col = -1
// lastkbd = 1
// goto COLSCREEN
@KBD
D = M
@KBDINACTIVE
D ; JEQ
// If KBD = 0 then go to KBDINACTIVE

// Here KBD != 0

@col
M = -1
@lastkbd
M = -1
@i
M = 0

// if first address same
// color as coloring,
// assume we have already colored.
// Then exit loop.
// if col = SCREEN then goto CHECKKBD
@col
D = M
@SCREEN
D = D - M
@CHECKKBD
D ; JEQ

@COLSCREEN
0 ; JMP


(KBDINACTIVE)
@lastkbd
D = M
@CHECKKBD
D; JEQ
// Else (lastkbd != 0)
@lastkbd
M = 0
@col
M = 0
@i
M = 0
@COLSCREEN
0 ; JMP






(initi)
// Initialize i = 0
@i
M = 0
@COLSCREEN
0; JMP

(COLSCREEN)
// if (i = nscr) goto CHECKKBD
@i
D = M
@nscr
D = D - M
@CHECKKBD
D ; JEQ
// endif (i = nscr)

// (i < nscr)
// Do instruction
// RAM[pSCREEN + i] = col
@pSCREEN
D = M
@i
D = D + M
// Store current location
@curloc
M = D
// Get pixel color to D register
@col
D = M
@curloc
A = M
// Actually color pixel
M = D


// i++
@i
M = M + 1

@COLSCREEN
0; JMP
// End colscreen loop



(END)
@END
0;JMP
73 changes: 73 additions & 0 deletions nand2tetris/projects/4/Mult.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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/4/Mult.asm

// Multiplies R0 and R1 and stores the result in R2.
// (R0, R1, R2 refer to RAM[0], RAM[1], and RAM[2], respectively.)
// The algorithm is based on repetitive addition.


// max(R0, R1) + ... + MAX(R0,R1)
// min(R0, R1) times
// Store in R2

@R2
M = 0 // Initialize sum

// Find min
@R0
D = M
@END
D; JEQ // Stop script if R0 = 0
@R1
D = M
@END
D; JEQ // Stop if R1 = 0
D = D - M
@MINR0
D ; JLT // D < 0 <=> R0 < R1

@R1
D = M
@min
M = 1 // set variable min = R1
@max
M = 0 // set var max = R0
@INITI
0; JMP


(MINR0)
@R0
D = M
@min
M = 0
@max
M = 1 // set variable min = R1 (min is a pointer)


(INITI)
@i
M = D // Initialize i = min(R0, R1) (call i = Rx)


(LOOP)
@max
A = M
D = M
@R2
M = D + M // R2 = R2 + max(R0, R1)

// i--
@i
M = M - 1
D = M
@LOOP
D ; JGT


(END)
@END
0 ; JMP

138 changes: 137 additions & 1 deletion nand2tetris/projects/4/fill/Fill.asm
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,140 @@
// i.e. writes "black" in every pixel. When no key is pressed,
// the screen should be cleared.

//// Replace this comment with your code.

// Set nscr to number of addresses on screen
@8192
D = A
@nscr
M = D


// Set pSCREEN to address of SCREEN
@SCREEN
D = A
@pSCREEN
M = D


(CHECKKBD)
// if (KBD != 0) then do
// col = -1
// lastkbd = 1
// goto COLSCREEN
// end
// else // (KBD = 0) do
// if lastkbd = 0 then
// goto CHECKKBD
// else // (lastkbd != 0) do
// lastkbd = 0
// col = 0
// i = 0
// goto COLSCREEN
// end
// end


// if (KBD != 0) then do
// col = -1
// lastkbd = 1
// goto COLSCREEN
@KBD
D = M
@KBDINACTIVE
D ; JEQ
// If KBD = 0 then go to KBDINACTIVE

// Here KBD != 0

@col
M = -1
@lastkbd
M = -1
@i
M = 0

// if first address same
// color as coloring,
// assume we have already colored.
// Then exit loop.
// if col = SCREEN then goto CHECKKBD
@col
D = M
@SCREEN
D = D - M
@CHECKKBD
D ; JEQ

@COLSCREEN
0 ; JMP


(KBDINACTIVE)
@lastkbd
D = M
@CHECKKBD
D; JEQ
// Else (lastkbd != 0)
@lastkbd
M = 0
@col
M = 0
@i
M = 0
@COLSCREEN
0 ; JMP






(initi)
// Initialize i = 0
@i
M = 0
@COLSCREEN
0; JMP

(COLSCREEN)
// if (i = nscr) goto CHECKKBD
@i
D = M
@nscr
D = D - M
@CHECKKBD
D ; JEQ
// endif (i = nscr)

// (i < nscr)
// Do instruction
// RAM[pSCREEN + i] = col
@pSCREEN
D = M
@i
D = D + M
// Store current location
@curloc
M = D
// Get pixel color to D register
@col
D = M
@curloc
A = M
// Actually color pixel
M = D


// i++
@i
M = M + 1

@COLSCREEN
0; JMP
// End colscreen loop



(END)
@END
0;JMP
4 changes: 4 additions & 0 deletions nand2tetris/projects/4/fill/FillAutomatic.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
|RAM[16384]|RAM[17648]|RAM[18349]|RAM[19444]|RAM[20771]|RAM[21031]|RAM[22596]|RAM[23754]|RAM[24575]|
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| -1 | -1 | -1 | -1 | -1 | -1 | -1 | -1 | -1 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
17 changes: 17 additions & 0 deletions nand2tetris/projects/4/mult/Init.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Init multiply


// Test
@2000
D = A
@R0
M = D
@2
D = A
@R1
M = D
// End test

(END)
@END
0 ; JMP
Loading