From e33a33d4a4fbfbef5bb076c4f98630ba0d55cf0f Mon Sep 17 00:00:00 2001 From: kolaente Date: Mon, 25 May 2020 18:15:15 +0200 Subject: [PATCH] Comment all the things --- hal_test.go | 77 ++++++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/hal_test.go b/hal_test.go index 4bb4fb3..2cf02d5 100644 --- a/hal_test.go +++ b/hal_test.go @@ -140,44 +140,49 @@ func TestNewton2(t *testing.T) { input := []string{ "01 START", "02 LOADNUM 1", - "03 STORE 10", - - "10 MUL 10", - "11 STORE 11", - "12 MUL 10", - "13 MUL 10", - "14 MUL 10", - "15 STORE 20", - "16 MUL 10", - "17 MULNUM 8", - "18 STORE 12", - "19 LOAD 11", - "20 MULNUM 3", - "21 ADD 12", - "22 SUBNUM 3", - "23 STORE 15", - - "24 SUB 50", - "25 JUMPNULL 45", - "26 LOAD 15", + "03 STORE 10", // Every x is saved to reg 10 + + // f(x) + "10 MUL 10", // x² + "11 STORE 11", // Save x² to reg 11 for later use since we need that in the second part of the function + "12 MUL 10", // x³ + "13 MUL 10", // x⁴ + "14 MUL 10", // x⁵ + "15 STORE 20", // This is x⁵ which we save here to save a few instructions + "16 MUL 10", // x⁶ + "17 MULNUM 8", // x⁶ * 8 + "18 STORE 12", // Reg 12 now contains the result of the first part of f(x) + "19 LOAD 11", // Put x² from earlier back in the accumulator + "20 MULNUM 3", // x² * 3 + "21 ADD 12", // Add the first and second part of the function together, basically 8x⁶ + 3x² + "22 SUBNUM 3", // (8x⁶ + 3x²) - 3 + "23 STORE 15", // Result in reg 15 + + // This is our exit condition: + // We compare the current result with the last calculated result, if both are equal, we exit + "24 SUB 50", // 50 contains the last result + "25 JUMPNULL 45", // If both are equal, we exit + "26 LOAD 15", // Otherwise load reg 15 again, store it as the current last result and continue "27 STORE 50", - "30 LOAD 20", - "31 MULNUM 48", - "32 STORE 21", - "33 LOAD 10", - "34 MULNUM 6", - "35 ADD 21", - "36 STORE 25", - - "40 LOAD 15", - "41 DIV 25", - "42 MULNUM -1", - "43 ADD 10", - "44 JUMP 03", - - "45 LOAD 10", - "46 OUT 1", + // f'(x) + "30 LOAD 20", // x⁵ which we saved earlier while calculating x⁶ + "31 MULNUM 48", // x⁵ * 48 + "32 STORE 21", // Store the result in 21 + "33 LOAD 10", // Load our x + "34 MULNUM 6", // x * 6 + "35 ADD 21", // (x * 6) + 48x⁵ + "36 STORE 25", // Store the result, which is the total result of f'(x) in reg 25 + + // x - f(x) / f'(x) + "40 LOAD 15", // load f(x) + "41 DIV 25", // f(x) / f'(x) + "42 MULNUM -1", // Invert the result + "43 ADD 10", // Add x: Because we inverted the result before, we can do this instead of SUB - lets us reuse the content of the accumulator + "44 JUMP 03", // Go to the beginning + + "45 LOAD 10", // Load the result + "46 OUT 1", // Print t "47 STOP", }