From 10c374721e0535427bd754badc5e668af63724af Mon Sep 17 00:00:00 2001 From: ATherkel Date: Mon, 6 May 2024 11:59:38 +0200 Subject: [PATCH 1/4] Create .asm files --- nand2tetris/projects/4/fill/Fill.asm | 138 ++++++++++++++++++++++++++- nand2tetris/projects/4/mult/Init.asm | 17 ++++ nand2tetris/projects/4/mult/Mult.asm | 65 ++++++++++++- 3 files changed, 218 insertions(+), 2 deletions(-) create mode 100644 nand2tetris/projects/4/mult/Init.asm 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/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 + From c40abe56eeaeecdd5340b5c636a668aecbec48ab Mon Sep 17 00:00:00 2001 From: ATherkel Date: Mon, 6 May 2024 11:59:53 +0200 Subject: [PATCH 2/4] .asm files in main project dir --- nand2tetris/projects/4/Fill.asm | 147 ++++++++++++++++++++++++++++++++ nand2tetris/projects/4/Mult.asm | 73 ++++++++++++++++ 2 files changed, 220 insertions(+) create mode 100644 nand2tetris/projects/4/Fill.asm create mode 100644 nand2tetris/projects/4/Mult.asm 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 + From 2f5389ad7356f8c5283eba6ef47a9569aa6d1368 Mon Sep 17 00:00:00 2001 From: ATherkel Date: Mon, 6 May 2024 12:00:01 +0200 Subject: [PATCH 3/4] Out files --- nand2tetris/projects/4/fill/FillAutomatic.out | 4 ++++ nand2tetris/projects/4/mult/Mult.out | 7 +++++++ 2 files changed, 11 insertions(+) create mode 100644 nand2tetris/projects/4/fill/FillAutomatic.out create mode 100644 nand2tetris/projects/4/mult/Mult.out 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/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 | From 3f183145d6bf4e0dbf29647fc4b983088221e782 Mon Sep 17 00:00:00 2001 From: ATherkel Date: Mon, 6 May 2024 12:00:07 +0200 Subject: [PATCH 4/4] Create project4.zip --- nand2tetris/projects/4/project4.zip | Bin 0 -> 1681 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 nand2tetris/projects/4/project4.zip diff --git a/nand2tetris/projects/4/project4.zip b/nand2tetris/projects/4/project4.zip new file mode 100644 index 0000000000000000000000000000000000000000..e4fd9b539107352bed902734b197dd880e9a1e8a GIT binary patch literal 1681 zcmWIWW@Zs#U|`^2n6-Ff1n0cWgCWce3_+YA5e5dg%$yv(#Nynop}xVl4FvY4tNl-W zIE5#H>9X_w8H&D_CS3~Pn)&U*OUI4ool@?nJh&A9evPfY(UtsSOZzOwC);=D&kwux z^`&Od^O(a^o^yPuKRSicvh{4*WDd1W#m#Pob3IJ6%}&fzFeq;6oWgA(b2)3mW1-fI zW~mcA8TwI(b@Z zUpJQ?ljr8bpP$}bt=xREo%_Y5*}Yk2ZeOmg->~*wb$RnaUO_RFX&0MCo{3LX+Yvq^ zYVT^t_rHF{?tdH6=)cf4zo39|u9?K{*FIL0tAl0+zd0G{Qnc)B(j>uM%;!`*U)BD5 zB3vcoDkiqmZ9(hl$Cr-q_&F?jW5K%AutO}d%^zCl` zJNItLF#DHosaxi`r7pGGqU*tjWw)jI_w#W*mi}S!4{b&km|b|k5H z#|58ZOFSnh5V3ATYV`A5>GhASw@Dv!&%4v&`E5x{v&OB?n$_2Jb{#i5a>;$M`idJb zzb|>T`MBhH4W@-n)#~>)<}JQ`VY~J5YwPB_P6_kB$$#_EJN7FU)v=%N+$@{7i)*g> zx!?L`0e`l7Hm8>c^6dr2sx+fT*Gyz%zc%ep~VxWw7=->SS; zZT0ef%d_Ii=e9#Ry!m?t7muimh#-gmHv%;?W4IjM1z*UiQ2Q| z&9bz!pKMB2r>|dnPg~mFYSsNV!CZr$IrjIttFOAIeg3(AjWo+sU70&yy0iUHD@|gm zXAXepJpXMIBO*MyHmfr+Fsx^RkU4bcgsOwukuI!#d91OlLP#7S2(nW zgcvStb6&ctomK3{v3pzhcys*ywJ%aydTFS%_cxx+`Sj%<=*8_Idgs9 z_WDQ7&s?|Wf?f4a%Nb`UUyDGb8?;@MTR@V4O=GC3*-MqJa?~55b9Byp6 z`9a~|%1thw-i0rIKH6t~O){V|cZ0*kOCtPSufk-?7>-yUcnfVl`m6tt+!QaGS`SuIZbq zX6I>gFx4#nXx^t7;xJ{Mg5T0hzg|p|)|!>DN^o{Zn}`r!x>TymqB_q_$4}?Hn|jYE zaCG{+illC3(|#ol#Ft`^utTZrA^BMdsXoen*Qnf4f(A z+UF<%Bl)lsE!FL9@7m-8#osNxXMZaE??;_qXA+k#PM@f~@z}oy5+A)Eaa>-1@uBFf z_pOc7{n9_*%nJLVc*T|b$J4`?b1Q$o&D!+;qUg1!_m!qaeW+N_4XO@mYqyzrXuic3|?f|LN)<7hJP>nCoi$?=jEM;_AH@ z!lxgVxcYXQ$f~rC4b1gB-NZ_^^3D%m8jvHN@miJpsvCah*VPueCsL!vG_&VVh<1K3mFV2a#7;`IXJwY_G$(RWh+2kr<#Q{sU$R_`@ koBxmlXwsxB)-E6ulUr9fLri96138EZ2)6*~