From 4f39392c809cc5bd4b9df24bd11c36573f37de33 Mon Sep 17 00:00:00 2001 From: Jonas Date: Fri, 30 Jan 2026 15:08:32 +0100 Subject: [PATCH] feat: unit 04 writing .asm --- .DS_Store | Bin 8196 -> 8196 bytes project-4-machine-language/.DS_Store | Bin 0 -> 6148 bytes project-4-machine-language/asm/mult.asm | 51 ++++++++++++++++ project-4-machine-language/asm/my-add.asm | 16 +++++ project-4-machine-language/asm/my-max.asm | 68 ++++++++++++++++++++++ 5 files changed, 135 insertions(+) create mode 100644 project-4-machine-language/.DS_Store create mode 100644 project-4-machine-language/asm/mult.asm create mode 100644 project-4-machine-language/asm/my-add.asm create mode 100644 project-4-machine-language/asm/my-max.asm diff --git a/.DS_Store b/.DS_Store index e8c27c538488a471841e10075e2795519db4fd71..dd8d0a90a3a4ccd1c1824d42c5b69976ef0ae4db 100644 GIT binary patch delta 52 zcmZp1XmOa}&nU7nU^hRb$mH_^Mw{&f%vm_oQi_w4^7C^TCtnn?*jy?S$h?_d;v386 IJtFMP0L*R?EC2ui delta 44 zcmZp1XmOa}&nUbxU^hRb@Z|FXMw{&f%vm;X6A5IV*xjOOer^*~fC^9nDnJFOz=#yc zgM2n0(KGQ;r~nlhhXVF}C~#v zw}U0GtH~COcF`O@H1Di7#lSS$MGF#`RtEzWpaKI0y2yJw|F`gO^Z%fQDHWgsf2M#= zH@nRmFO_HO$Lm>rpH*8oIOvxny!`|qv7>kacf)?M1z3|Uhzg890xkms75J$FFCQim AZvX%Q literal 0 HcmV?d00001 diff --git a/project-4-machine-language/asm/mult.asm b/project-4-machine-language/asm/mult.asm new file mode 100644 index 0000000..28c90a4 --- /dev/null +++ b/project-4-machine-language/asm/mult.asm @@ -0,0 +1,51 @@ +// mult.asm +// +// Create a program that multiplies R0 & R1 and puts the result in R2 +// Algorithm is based on repetetive action. +// +// + +// okay let's go. + +// Get R0 +// Create a loop based on the number of R0. I.e loop 10 times if R0 is 10. +// Everytime around sum of R2 should be incremented by R1 +// Need variable @i which is the index. If that reaches 0. Loop end + + // Variables + @i + M=1 + @R2 + M=0 + // Check if loop is needed. + @R0 + D=M + @END // Jump to End if R0 is 0. Nothing to add. + D;JEQ + @R1 + D=M + @END // Jump to End if R1 is 0. Nothing to add. + D;JEQ + +(LOOP) + // get R1 + @R1 + D=M + // Add R1 to R2 + @R2 + M=D+M + // add one to i + @i + M=M+1 + D=M + @R0 + D=D-M + // check if R0 - i is greater or equal than 0 + @LOOP + D;JLE + // Then jump to loop + // else just move on + +(END) // End of Program + @END + 0;JMP diff --git a/project-4-machine-language/asm/my-add.asm b/project-4-machine-language/asm/my-add.asm new file mode 100644 index 0000000..e5b2686 --- /dev/null +++ b/project-4-machine-language/asm/my-add.asm @@ -0,0 +1,16 @@ +// Add two numbers which will compute the result in R2 +// R0 and R1 will be the numbers to add +// Remember to set them in the assember program before running it. +// + +// +// + @R0 + D=M + @R1 + D=D+M // Add playground (R0 + M(R1)) to new playground D || D = R0 + R1 + @R2 + M=D +(END) + @END + 0;JMP diff --git a/project-4-machine-language/asm/my-max.asm b/project-4-machine-language/asm/my-max.asm new file mode 100644 index 0000000..72f45e6 --- /dev/null +++ b/project-4-machine-language/asm/my-max.asm @@ -0,0 +1,68 @@ + +// 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/6/max/Max.asm + +// Computes R2 = max(R0, R1) (R0,R1,R2 refer to RAM[0],RAM[1],RAM[2]) +// Usage: Before executing, put two values in R0 and R1. + + + // D = R0 - R1 + +/* + @R0 + D=M + @R1 + D=D-M + // If (D > 0) goto ITSR0 + @ITSR0 + D;JGT + // Its R1 + @R1 + D=M + @OUTPUT_D + 0;JMP +(ITSR0) + @R0 + D=M +(OUTPUT_D) + @R2 + M=D +(END) + @END + 0;JMP + +*/ + +// Max +// Takes two numbers and saves the highest of them +// Get the sum of R0 & R1 +// Save it in R2 +// Get Value of R0 +// Compare it value of R1 +// Remember that the conditional can only be compared to zero. +// GT. LS, EQ etc.. All to zero +// Create a label and jump to it + +@R0 +D=M // Value of R0 +@R1 +D=D-M // New D(playground) is Old D(Value of R0) Minus Value of R1. Basically R0 - R1 +@ITSR0 +D;JGT // If D (R0 - R1) is greater than 0, then R0 is biger. Otherwise R1 or equal. +// Else it R1 +@R1 // Get The Value of R1 +D=M +@OUTPUT_D // And send it to the output section which is R2 variable +0;JMP +// The end Statements +(ITSR0) // Jump here if R0 +@R0 +D=M // Set value of R0 to the playground variable. +(OUTPUT_D) // End up here either way. Depending on value. +@R2 +M=D // Set R2 to D either way. Just with or without the ITSR0 iteration +(END) // Infinite loop - prevents CPU from executing undefined instructions. +@END +0;JMP // Infinite Jumping to end the program