aboutsummaryrefslogtreecommitdiff
path: root/projects/12/MathTest
diff options
context:
space:
mode:
Diffstat (limited to 'projects/12/MathTest')
-rw-r--r--projects/12/MathTest/Main.jack35
-rw-r--r--projects/12/MathTest/Main.vm162
-rw-r--r--projects/12/MathTest/Math.jack158
-rw-r--r--projects/12/MathTest/Math.vm472
-rw-r--r--projects/12/MathTest/MathTest.cmp2
-rw-r--r--projects/12/MathTest/MathTest.out2
-rw-r--r--projects/12/MathTest/MathTest.tst15
7 files changed, 0 insertions, 846 deletions
diff --git a/projects/12/MathTest/Main.jack b/projects/12/MathTest/Main.jack
deleted file mode 100644
index de5cec2..0000000
--- a/projects/12/MathTest/Main.jack
+++ /dev/null
@@ -1,35 +0,0 @@
-// 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/12/MathTest/Main.jack
-
-/** Test program for the OS Math class. */
-class Main {
-
- /** Performs various mathematical operations, using calls to the Math class methods. */
- function void main() {
- var Array r; // stores the test results;
-
- let r = 8000;
-
- let r[0] = 2 * 3; // 6
- let r[1] = r[0] * (-30); // 6 * (-30) = -180
- let r[2] = r[1] * 100; // (-180) * 100 = -18000
- let r[3] = 1 * r[2]; // 1 * (-18000) = -18000
- let r[4] = r[3] * 0; // 0
-
- let r[5] = 9 / 3; // 3
- let r[6] = (-18000) / 6; // -3000
- let r[7] = 32766 / (-32767); // 0
-
- let r[8] = Math.sqrt(9); // 3
- let r[9] = Math.sqrt(32767); // 181
-
- let r[10] = Math.min(345, 123); // 123
- let r[11] = Math.max(123, -345); // 123
- let r[12] = Math.abs(27); // 27
- let r[13] = Math.abs(-32767); // 32767
-
- return;
- }
-}
diff --git a/projects/12/MathTest/Main.vm b/projects/12/MathTest/Main.vm
deleted file mode 100644
index 025f20c..0000000
--- a/projects/12/MathTest/Main.vm
+++ /dev/null
@@ -1,162 +0,0 @@
-function Main.main 1
-push constant 8000
-pop local 0
-push constant 0
-push local 0
-add
-push constant 2
-push constant 3
-call Math.multiply 2
-pop temp 0
-pop pointer 1
-push temp 0
-pop that 0
-push constant 1
-push local 0
-add
-push constant 0
-push local 0
-add
-pop pointer 1
-push that 0
-push constant 30
-neg
-call Math.multiply 2
-pop temp 0
-pop pointer 1
-push temp 0
-pop that 0
-push constant 2
-push local 0
-add
-push constant 1
-push local 0
-add
-pop pointer 1
-push that 0
-push constant 100
-call Math.multiply 2
-pop temp 0
-pop pointer 1
-push temp 0
-pop that 0
-push constant 3
-push local 0
-add
-push constant 1
-push constant 2
-push local 0
-add
-pop pointer 1
-push that 0
-call Math.multiply 2
-pop temp 0
-pop pointer 1
-push temp 0
-pop that 0
-push constant 4
-push local 0
-add
-push constant 3
-push local 0
-add
-pop pointer 1
-push that 0
-push constant 0
-call Math.multiply 2
-pop temp 0
-pop pointer 1
-push temp 0
-pop that 0
-push constant 5
-push local 0
-add
-push constant 9
-push constant 3
-call Math.divide 2
-pop temp 0
-pop pointer 1
-push temp 0
-pop that 0
-push constant 6
-push local 0
-add
-push constant 18000
-neg
-push constant 6
-call Math.divide 2
-pop temp 0
-pop pointer 1
-push temp 0
-pop that 0
-push constant 7
-push local 0
-add
-push constant 32766
-push constant 32767
-neg
-call Math.divide 2
-pop temp 0
-pop pointer 1
-push temp 0
-pop that 0
-push constant 8
-push local 0
-add
-push constant 9
-call Math.sqrt 1
-pop temp 0
-pop pointer 1
-push temp 0
-pop that 0
-push constant 9
-push local 0
-add
-push constant 32767
-call Math.sqrt 1
-pop temp 0
-pop pointer 1
-push temp 0
-pop that 0
-push constant 10
-push local 0
-add
-push constant 345
-push constant 123
-call Math.min 2
-pop temp 0
-pop pointer 1
-push temp 0
-pop that 0
-push constant 11
-push local 0
-add
-push constant 123
-push constant 345
-neg
-call Math.max 2
-pop temp 0
-pop pointer 1
-push temp 0
-pop that 0
-push constant 12
-push local 0
-add
-push constant 27
-call Math.abs 1
-pop temp 0
-pop pointer 1
-push temp 0
-pop that 0
-push constant 13
-push local 0
-add
-push constant 32767
-neg
-call Math.abs 1
-pop temp 0
-pop pointer 1
-push temp 0
-pop that 0
-push constant 0
-return
diff --git a/projects/12/MathTest/Math.jack b/projects/12/MathTest/Math.jack
deleted file mode 100644
index 01bce8f..0000000
--- a/projects/12/MathTest/Math.jack
+++ /dev/null
@@ -1,158 +0,0 @@
-// 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/12/Math.jack
-
-/**
- * A library of commonly used mathematical functions.
- * Note: Jack compilers implement multiplication and division using OS method calls.
- */
-class Math {
- static Array twoToThe;
-
- /** Initializes the library. */
- function void init() {
- var int i, x;
- let x = 1;
- let i = 0;
- let twoToThe = Array.new(16);
- while (i < 16){
- let twoToThe[i] = x;
- let x = x + x;
- let i = i + 1;
- }
- return;
- }
-
- function boolean bit(int x, int i) {
- return ~(x & twoToThe[i] = 0);
- }
-
- function int sign(int x) {
- if (x > 0) {
- return 1;
- } else {
- if (x < 0) {
- return -1;
- } else {
- return 0;
- }
- }
- }
-
- /** Returns the absolute value of x. */
- function int abs(int x) {
- if (x > 0){
- return x;
- } else {
- return -x;
- }
- }
-
- /** Returns the product of x and y.
- * When a Jack compiler detects the multiplication operator '*' in the
- * program's code, it handles it by invoking this method. In other words,
- * the Jack expressions x*y and multiply(x,y) return the same value.
- */
- function int multiply(int x, int y) {
- var int res, shiftedX, i;
- let shiftedX = x;
- let i = 0;
- let res = 0;
- //while ((~(twoToThe[i] > y)) & (i < 16)) {
- while (i < 16) {
- if (Math.bit(y, i)){
- let res = res + shiftedX;
- }
- let shiftedX = shiftedX + shiftedX;
- let i = i + 1;
- }
- return res;
- }
-
- /** Returns the integer part of x/y.
- * When a Jack compiler detects the multiplication operator '/' in the
- * program's code, it handles it by invoking this method. In other words,
- * the Jack expressions x/y and divide(x,y) return the same value.
- */
- function int divide(int x, int y) {
- var int ax, ay;
- if (x > 0) {
- if (y > 0) {
- return Math.divide1(x, y);
- } else {
- return -Math.divide1(x, -y);
- }
- } else {
- if (y > 0) {
- return -Math.divide1(-x, y);
- } else {
- return Math.divide1(-x, -y);
- }
- }
- }
-
- function int divide1(int x, int y) {
- var int q;
- if (y = 0) {
- do Output.printString("Error: division by zero.");
- do Sys.error(0);
- }
- if ((y > x) | (y < 0)) {
- return 0;
- }
- let q = Math.divide1(x, y * 2);
- if (x - (2 * q * y) < y) {
- return 2 * q;
- } else {
- return 2 * q + 1;
- }
- }
-
- function int length(int x){
- var int n;
- let n = 14;
- while (twoToThe[n] > x){
- let n = n - 1;
- }
- return n;
- }
-
- /** Returns the integer part of the square root of x. */
- function int sqrt(int x) {
- var int y, k, z, w;
- if (x < 0) {
- do Output.printString("Error: square rooting a negative number.");
- do Sys.error(0);
- }
- let k = Math.length(x) / 2;
- let y = 0;
- while (~(k < 0)) {
- let z = y + twoToThe[k];
- let w = z * z;
- if ((~(w > x)) & (w > 0)) {
- let y = z;
- }
- let k = k - 1;
- }
- return y;
- }
-
- /** Returns the greater number. */
- function int max(int a, int b) {
- if (a > b) {
- return a;
- } else {
- return b;
- }
- }
-
- /** Returns the smaller number. */
- function int min(int a, int b) {
- if (a < b) {
- return a;
- } else {
- return b;
- }
- }
-}
diff --git a/projects/12/MathTest/Math.vm b/projects/12/MathTest/Math.vm
deleted file mode 100644
index 513fc1a..0000000
--- a/projects/12/MathTest/Math.vm
+++ /dev/null
@@ -1,472 +0,0 @@
-function Math.init 2
-push constant 1
-pop local 1
-push constant 0
-pop local 0
-push constant 16
-call Array.new 1
-pop static 0
-label WHILE_EXP0
-push local 0
-push constant 16
-lt
-not
-if-goto WHILE_END0
-push local 0
-push static 0
-add
-push local 1
-pop temp 0
-pop pointer 1
-push temp 0
-pop that 0
-push local 1
-push local 1
-add
-pop local 1
-push local 0
-push constant 1
-add
-pop local 0
-goto WHILE_EXP0
-label WHILE_END0
-push constant 0
-return
-function Math.bit 0
-push argument 0
-push argument 1
-push static 0
-add
-pop pointer 1
-push that 0
-and
-push constant 0
-eq
-not
-return
-function Math.sign 0
-push argument 0
-push constant 0
-gt
-if-goto IF_TRUE0
-goto IF_FALSE0
-label IF_TRUE0
-push constant 1
-return
-goto IF_END0
-label IF_FALSE0
-push argument 0
-push constant 0
-lt
-if-goto IF_TRUE1
-goto IF_FALSE1
-label IF_TRUE1
-push constant 1
-neg
-return
-goto IF_END1
-label IF_FALSE1
-push constant 0
-return
-label IF_END1
-label IF_END0
-function Math.abs 0
-push argument 0
-push constant 0
-gt
-if-goto IF_TRUE0
-goto IF_FALSE0
-label IF_TRUE0
-push argument 0
-return
-goto IF_END0
-label IF_FALSE0
-push argument 0
-neg
-return
-label IF_END0
-function Math.multiply 3
-push argument 0
-pop local 1
-push constant 0
-pop local 2
-push constant 0
-pop local 0
-label WHILE_EXP0
-push local 2
-push constant 16
-lt
-not
-if-goto WHILE_END0
-push argument 1
-push local 2
-call Math.bit 2
-if-goto IF_TRUE0
-goto IF_FALSE0
-label IF_TRUE0
-push local 0
-push local 1
-add
-pop local 0
-label IF_FALSE0
-push local 1
-push local 1
-add
-pop local 1
-push local 2
-push constant 1
-add
-pop local 2
-goto WHILE_EXP0
-label WHILE_END0
-push local 0
-return
-function Math.divide 2
-push argument 0
-push constant 0
-gt
-if-goto IF_TRUE0
-goto IF_FALSE0
-label IF_TRUE0
-push argument 1
-push constant 0
-gt
-if-goto IF_TRUE1
-goto IF_FALSE1
-label IF_TRUE1
-push argument 0
-push argument 1
-call Math.divide1 2
-return
-goto IF_END1
-label IF_FALSE1
-push argument 0
-push argument 1
-neg
-call Math.divide1 2
-neg
-return
-label IF_END1
-goto IF_END0
-label IF_FALSE0
-push argument 1
-push constant 0
-gt
-if-goto IF_TRUE2
-goto IF_FALSE2
-label IF_TRUE2
-push argument 0
-neg
-push argument 1
-call Math.divide1 2
-neg
-return
-goto IF_END2
-label IF_FALSE2
-push argument 0
-neg
-push argument 1
-neg
-call Math.divide1 2
-return
-label IF_END2
-label IF_END0
-function Math.divide1 1
-push argument 1
-push constant 0
-eq
-if-goto IF_TRUE0
-goto IF_FALSE0
-label IF_TRUE0
-push constant 24
-call String.new 1
-push constant 69
-call String.appendChar 2
-push constant 114
-call String.appendChar 2
-push constant 114
-call String.appendChar 2
-push constant 111
-call String.appendChar 2
-push constant 114
-call String.appendChar 2
-push constant 58
-call String.appendChar 2
-push constant 32
-call String.appendChar 2
-push constant 100
-call String.appendChar 2
-push constant 105
-call String.appendChar 2
-push constant 118
-call String.appendChar 2
-push constant 105
-call String.appendChar 2
-push constant 115
-call String.appendChar 2
-push constant 105
-call String.appendChar 2
-push constant 111
-call String.appendChar 2
-push constant 110
-call String.appendChar 2
-push constant 32
-call String.appendChar 2
-push constant 98
-call String.appendChar 2
-push constant 121
-call String.appendChar 2
-push constant 32
-call String.appendChar 2
-push constant 122
-call String.appendChar 2
-push constant 101
-call String.appendChar 2
-push constant 114
-call String.appendChar 2
-push constant 111
-call String.appendChar 2
-push constant 46
-call String.appendChar 2
-call Output.printString 1
-pop temp 0
-push constant 0
-call Sys.error 1
-pop temp 0
-label IF_FALSE0
-push argument 1
-push argument 0
-gt
-push argument 1
-push constant 0
-lt
-or
-if-goto IF_TRUE1
-goto IF_FALSE1
-label IF_TRUE1
-push constant 0
-return
-label IF_FALSE1
-push argument 0
-push argument 1
-push constant 2
-call Math.multiply 2
-call Math.divide1 2
-pop local 0
-push argument 0
-push constant 2
-push local 0
-call Math.multiply 2
-push argument 1
-call Math.multiply 2
-sub
-push argument 1
-lt
-if-goto IF_TRUE2
-goto IF_FALSE2
-label IF_TRUE2
-push constant 2
-push local 0
-call Math.multiply 2
-return
-goto IF_END2
-label IF_FALSE2
-push constant 2
-push local 0
-call Math.multiply 2
-push constant 1
-add
-return
-label IF_END2
-function Math.length 1
-push constant 14
-pop local 0
-label WHILE_EXP0
-push local 0
-push static 0
-add
-pop pointer 1
-push that 0
-push argument 0
-gt
-not
-if-goto WHILE_END0
-push local 0
-push constant 1
-sub
-pop local 0
-goto WHILE_EXP0
-label WHILE_END0
-push local 0
-return
-function Math.sqrt 4
-push argument 0
-push constant 0
-lt
-if-goto IF_TRUE0
-goto IF_FALSE0
-label IF_TRUE0
-push constant 40
-call String.new 1
-push constant 69
-call String.appendChar 2
-push constant 114
-call String.appendChar 2
-push constant 114
-call String.appendChar 2
-push constant 111
-call String.appendChar 2
-push constant 114
-call String.appendChar 2
-push constant 58
-call String.appendChar 2
-push constant 32
-call String.appendChar 2
-push constant 115
-call String.appendChar 2
-push constant 113
-call String.appendChar 2
-push constant 117
-call String.appendChar 2
-push constant 97
-call String.appendChar 2
-push constant 114
-call String.appendChar 2
-push constant 101
-call String.appendChar 2
-push constant 32
-call String.appendChar 2
-push constant 114
-call String.appendChar 2
-push constant 111
-call String.appendChar 2
-push constant 111
-call String.appendChar 2
-push constant 116
-call String.appendChar 2
-push constant 105
-call String.appendChar 2
-push constant 110
-call String.appendChar 2
-push constant 103
-call String.appendChar 2
-push constant 32
-call String.appendChar 2
-push constant 97
-call String.appendChar 2
-push constant 32
-call String.appendChar 2
-push constant 110
-call String.appendChar 2
-push constant 101
-call String.appendChar 2
-push constant 103
-call String.appendChar 2
-push constant 97
-call String.appendChar 2
-push constant 116
-call String.appendChar 2
-push constant 105
-call String.appendChar 2
-push constant 118
-call String.appendChar 2
-push constant 101
-call String.appendChar 2
-push constant 32
-call String.appendChar 2
-push constant 110
-call String.appendChar 2
-push constant 117
-call String.appendChar 2
-push constant 109
-call String.appendChar 2
-push constant 98
-call String.appendChar 2
-push constant 101
-call String.appendChar 2
-push constant 114
-call String.appendChar 2
-push constant 46
-call String.appendChar 2
-call Output.printString 1
-pop temp 0
-push constant 0
-call Sys.error 1
-pop temp 0
-label IF_FALSE0
-push argument 0
-call Math.length 1
-push constant 2
-call Math.divide 2
-pop local 1
-push constant 0
-pop local 0
-label WHILE_EXP0
-push local 1
-push constant 0
-lt
-not
-not
-if-goto WHILE_END0
-push local 0
-push local 1
-push static 0
-add
-pop pointer 1
-push that 0
-add
-pop local 2
-push local 2
-push local 2
-call Math.multiply 2
-pop local 3
-push local 3
-push argument 0
-gt
-not
-push local 3
-push constant 0
-gt
-and
-if-goto IF_TRUE1
-goto IF_FALSE1
-label IF_TRUE1
-push local 2
-pop local 0
-label IF_FALSE1
-push local 1
-push constant 1
-sub
-pop local 1
-goto WHILE_EXP0
-label WHILE_END0
-push local 0
-return
-function Math.max 0
-push argument 0
-push argument 1
-gt
-if-goto IF_TRUE0
-goto IF_FALSE0
-label IF_TRUE0
-push argument 0
-return
-goto IF_END0
-label IF_FALSE0
-push argument 1
-return
-label IF_END0
-function Math.min 0
-push argument 0
-push argument 1
-lt
-if-goto IF_TRUE0
-goto IF_FALSE0
-label IF_TRUE0
-push argument 0
-return
-goto IF_END0
-label IF_FALSE0
-push argument 1
-return
-label IF_END0
diff --git a/projects/12/MathTest/MathTest.cmp b/projects/12/MathTest/MathTest.cmp
deleted file mode 100644
index 703c1be..0000000
--- a/projects/12/MathTest/MathTest.cmp
+++ /dev/null
@@ -1,2 +0,0 @@
-|RAM[8000]|RAM[8001]|RAM[8002]|RAM[8003]|RAM[8004]|RAM[8005]|RAM[8006]|RAM[8007]|RAM[8008]|RAM[8009]|RAM[8010]|RAM[8011]|RAM[8012]|RAM[8013]|
-| 6 | -180 | -18000 | -18000 | 0 | 3 | -3000 | 0 | 3 | 181 | 123 | 123 | 27 | 32767 |
diff --git a/projects/12/MathTest/MathTest.out b/projects/12/MathTest/MathTest.out
deleted file mode 100644
index 8b5a504..0000000
--- a/projects/12/MathTest/MathTest.out
+++ /dev/null
@@ -1,2 +0,0 @@
-|RAM[8000]|RAM[8001]|RAM[8002]|RAM[8003]|RAM[8004]|RAM[8005]|RAM[8006]|RAM[8007]|RAM[8008]|RAM[8009]|RAM[8010]|RAM[8011]|RAM[8012]|RAM[8013]|
-| 6 | -180 | -18000 | -18000 | 0 | 3 | -3000 | 0 | 3 | 181 | 123 | 123 | 27 | 32767 |
diff --git a/projects/12/MathTest/MathTest.tst b/projects/12/MathTest/MathTest.tst
deleted file mode 100644
index 127dbb4..0000000
--- a/projects/12/MathTest/MathTest.tst
+++ /dev/null
@@ -1,15 +0,0 @@
-// 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/12/MathTest/MathTest.tst
-
-load,
-output-file MathTest.out,
-compare-to MathTest.cmp,
-output-list RAM[8000]%D2.6.1 RAM[8001]%D2.6.1 RAM[8002]%D2.6.1 RAM[8003]%D2.6.1 RAM[8004]%D2.6.1 RAM[8005]%D2.6.1 RAM[8006]%D2.6.1 RAM[8007]%D2.6.1 RAM[8008]%D2.6.1 RAM[8009]%D2.6.1 RAM[8010]%D2.6.1 RAM[8011]%D2.6.1 RAM[8012]%D2.6.1 RAM[8013]%D2.6.1;
-
-repeat 1000000 {
- vmstep;
-}
-
-output;