diff --git a/nand2tetris/projects/4/Fill.asm b/nand2tetris/projects/4/Fill.asm new file mode 100644 index 0000000..04bac6b --- /dev/null +++ b/nand2tetris/projects/4/Fill.asm @@ -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 \ No newline at end of file diff --git a/nand2tetris/projects/4/Mult.asm b/nand2tetris/projects/4/Mult.asm new file mode 100644 index 0000000..a9119b8 --- /dev/null +++ b/nand2tetris/projects/4/Mult.asm @@ -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 + diff --git a/nand2tetris/projects/4/fill/Fill.asm b/nand2tetris/projects/4/fill/Fill.asm index ef94346..04bac6b 100644 --- a/nand2tetris/projects/4/fill/Fill.asm +++ b/nand2tetris/projects/4/fill/Fill.asm @@ -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 \ No newline at end of file diff --git a/nand2tetris/projects/4/fill/FillAutomatic.out b/nand2tetris/projects/4/fill/FillAutomatic.out new file mode 100644 index 0000000..a71db27 --- /dev/null +++ b/nand2tetris/projects/4/fill/FillAutomatic.out @@ -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 | diff --git a/nand2tetris/projects/4/mult/Init.asm b/nand2tetris/projects/4/mult/Init.asm new file mode 100644 index 0000000..fcd9bae --- /dev/null +++ b/nand2tetris/projects/4/mult/Init.asm @@ -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 \ No newline at end of file diff --git a/nand2tetris/projects/4/mult/Mult.asm b/nand2tetris/projects/4/mult/Mult.asm index 18eb3ec..a9119b8 100644 --- a/nand2tetris/projects/4/mult/Mult.asm +++ b/nand2tetris/projects/4/mult/Mult.asm @@ -7,4 +7,67 @@ // (R0, R1, R2 refer to RAM[0], RAM[1], and RAM[2], respectively.) // The algorithm is based on repetitive addition. -//// Replace this comment with your code. + +// 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 + diff --git a/nand2tetris/projects/4/mult/Mult.out b/nand2tetris/projects/4/mult/Mult.out new file mode 100644 index 0000000..e31b5df --- /dev/null +++ b/nand2tetris/projects/4/mult/Mult.out @@ -0,0 +1,7 @@ +| RAM[0] | RAM[1] | RAM[2] | +| 0 | 0 | 0 | +| 1 | 0 | 0 | +| 0 | 2 | 0 | +| 3 | 1 | 3 | +| 2 | 4 | 8 | +| 6 | 7 | 42 | diff --git a/nand2tetris/projects/4/project4.zip b/nand2tetris/projects/4/project4.zip new file mode 100644 index 0000000..e4fd9b5 Binary files /dev/null and b/nand2tetris/projects/4/project4.zip differ