diff --git a/hal_test.go b/hal_test.go index 642320c..0f72bd1 100644 --- a/hal_test.go +++ b/hal_test.go @@ -72,39 +72,49 @@ func TestNewton1(t *testing.T) { input := []string{ "01 START", "02 LOADNUM 1", - "03 STORE 10", - "10 MUL 10", - "11 MUL 10", - "12 STORE 12", - "13 MUL 10", - "14 MUL 10", - "15 STORE 11", - "21 LOAD 12", - "22 MULNUM 5", - "30 ADD 11", - "31 SUBNUM 5", - "32 STORE 20", - "33 SUB 50", - "34 JUMPNULL 66", - "35 LOAD 20", - "36 STORE 50", - "40 LOAD 10", - "41 MUL 10", - "42 STORE 21", - "43 MUL 10", - "44 MUL 10", - "45 MULNUM 5", - "46 STORE 22", - "47 LOAD 21", - "48 MULNUM 15", - "49 ADD 22", - "50 STORE 30", - "60 LOAD 20", + "03 STORE 10", // Every x is saved to reg 10 + + // f(x) + "10 MUL 10", // x² + "11 MUL 10", // x³ + "12 STORE 12", // Saving x³ for later + "13 MUL 10", // x⁴ + "14 MUL 10", // x⁵ + "15 STORE 11", // Save x⁵ in reg 11 + "21 LOAD 12", // Load x³ + "22 MULNUM 5", // x³ * 5 + "30 ADD 11", // (x³ * 5) + x⁵ + "31 SUBNUM 5", // ((x³ * 5) + x⁵) - 5 + "32 STORE 20", // Store the result of f(x) in 20 + + // Exit condition + "33 SUB 50", // reg 50 contains the last result which we compare with the last one + "34 JUMPNULL 66", // If both are equal, we go to line 66 + "35 LOAD 20", // Otherwise save the last result of f(x) as last result + "36 STORE 50", // and continue + + // f'(x) + "40 LOAD 10", // Load x + "41 MUL 10", // x² + "42 STORE 21", // Save x² for later + "43 MUL 10", // x³ + "44 MUL 10", // x⁴ + "45 MULNUM 5", // x⁴ * 5 + "46 STORE 22", // Save 5x⁴ to reg 22 + "47 LOAD 21", // load x² from earlier + "48 MULNUM 15", // x² * 15 + "49 ADD 22", // 15x² + 5x⁴ from earlier + "50 STORE 30", // Save the result of f'(x) to reg 30 + + // x - (f(x) / f'(x)) + "60 LOAD 20", // f(x) / f'(x) "61 DIV 30", - "62 STORE 40", - "63 LOAD 10", - "64 SUB 40", - "65 JUMP 03", + "62 STORE 40", // Store the result of it in reg 40 + "63 LOAD 10", // Load the x again + "64 SUB 40", // x - (f(x) / f'(x)) + "65 JUMP 03", // Start at the beginning + + // Load the result and print it "66 LOAD 10", "67 OUT 0", "68 STOP",