|
|
@ -140,44 +140,49 @@ func TestNewton2(t *testing.T) { |
|
|
|
input := []string{ |
|
|
|
input := []string{ |
|
|
|
"01 START", |
|
|
|
"01 START", |
|
|
|
"02 LOADNUM 1", |
|
|
|
"02 LOADNUM 1", |
|
|
|
"03 STORE 10", |
|
|
|
"03 STORE 10", // Every x is saved to reg 10
|
|
|
|
|
|
|
|
|
|
|
|
"10 MUL 10", |
|
|
|
// f(x)
|
|
|
|
"11 STORE 11", |
|
|
|
"10 MUL 10", // x²
|
|
|
|
"12 MUL 10", |
|
|
|
"11 STORE 11", // Save x² to reg 11 for later use since we need that in the second part of the function
|
|
|
|
"13 MUL 10", |
|
|
|
"12 MUL 10", // x³
|
|
|
|
"14 MUL 10", |
|
|
|
"13 MUL 10", // x⁴
|
|
|
|
"15 STORE 20", |
|
|
|
"14 MUL 10", // x⁵
|
|
|
|
"16 MUL 10", |
|
|
|
"15 STORE 20", // This is x⁵ which we save here to save a few instructions
|
|
|
|
"17 MULNUM 8", |
|
|
|
"16 MUL 10", // x⁶
|
|
|
|
"18 STORE 12", |
|
|
|
"17 MULNUM 8", // x⁶ * 8
|
|
|
|
"19 LOAD 11", |
|
|
|
"18 STORE 12", // Reg 12 now contains the result of the first part of f(x)
|
|
|
|
"20 MULNUM 3", |
|
|
|
"19 LOAD 11", // Put x² from earlier back in the accumulator
|
|
|
|
"21 ADD 12", |
|
|
|
"20 MULNUM 3", // x² * 3
|
|
|
|
"22 SUBNUM 3", |
|
|
|
"21 ADD 12", // Add the first and second part of the function together, basically 8x⁶ + 3x²
|
|
|
|
"23 STORE 15", |
|
|
|
"22 SUBNUM 3", // (8x⁶ + 3x²) - 3
|
|
|
|
|
|
|
|
"23 STORE 15", // Result in reg 15
|
|
|
|
"24 SUB 50", |
|
|
|
|
|
|
|
"25 JUMPNULL 45", |
|
|
|
// This is our exit condition:
|
|
|
|
"26 LOAD 15", |
|
|
|
// 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", |
|
|
|
"27 STORE 50", |
|
|
|
|
|
|
|
|
|
|
|
"30 LOAD 20", |
|
|
|
// f'(x)
|
|
|
|
"31 MULNUM 48", |
|
|
|
"30 LOAD 20", // x⁵ which we saved earlier while calculating x⁶
|
|
|
|
"32 STORE 21", |
|
|
|
"31 MULNUM 48", // x⁵ * 48
|
|
|
|
"33 LOAD 10", |
|
|
|
"32 STORE 21", // Store the result in 21
|
|
|
|
"34 MULNUM 6", |
|
|
|
"33 LOAD 10", // Load our x
|
|
|
|
"35 ADD 21", |
|
|
|
"34 MULNUM 6", // x * 6
|
|
|
|
"36 STORE 25", |
|
|
|
"35 ADD 21", // (x * 6) + 48x⁵
|
|
|
|
|
|
|
|
"36 STORE 25", // Store the result, which is the total result of f'(x) in reg 25
|
|
|
|
"40 LOAD 15", |
|
|
|
|
|
|
|
"41 DIV 25", |
|
|
|
// x - f(x) / f'(x)
|
|
|
|
"42 MULNUM -1", |
|
|
|
"40 LOAD 15", // load f(x)
|
|
|
|
"43 ADD 10", |
|
|
|
"41 DIV 25", // f(x) / f'(x)
|
|
|
|
"44 JUMP 03", |
|
|
|
"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
|
|
|
|
"45 LOAD 10", |
|
|
|
"44 JUMP 03", // Go to the beginning
|
|
|
|
"46 OUT 1", |
|
|
|
|
|
|
|
|
|
|
|
"45 LOAD 10", // Load the result
|
|
|
|
|
|
|
|
"46 OUT 1", // Print t
|
|
|
|
"47 STOP", |
|
|
|
"47 STOP", |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|