32 Difference Engine 0000 NUM 0 top: Temporary storage location 0001 LDN 30 Get 3rd order variable 0002 SUB 31 Add 4th order variable 0003 STO 0 recomplement 0004 LDN 0 through temp 0005 STO 31 Store updated 4th order variable 0006 LDN 29 Get 2nd order variable 0007 SUB 30 Add 3rd order variable 0008 STO 0 recomplement 0009 LDN 0 through temp 0010 STO 30 Store updated 3rd order variable 0011 LDN 28 Get 1st order variable 0012 SUB 29 Add 2nd order variable 0013 STO 0 recomplement 0014 LDN 0 through temp 0015 STO 29 Store updated 2nd order variable 0016 LDN 27 Get 0th order variable (constant -1) 0017 SUB 28 Add 1st order variable 0018 STO 0 recomplement 0019 LDN 0 through temp 0020 STO 28 Store updated 1st order variable 0021 SUB 26 Subtract final value 0022 CMP Test for equality 0023 JMP 25 Jump back to top if still greater 0024 STP Stop 0025 NUM 0 Indirect jump value 0026 NUM -669 Final value of 1st order variable 0027 NUM -1 0th order variable (constant -1) 0028 NUM 669 1st order variable 0029 NUM -189363 2nd order variable 0030 NUM 26199102 3rd order variable 0031 NUM -600000000 4th order variable
32 Artillery Game 0000 NUM 0 Standard filler, constant 0 0001 LDN 0 top: Load 0 0002 STO 30 Initialize Y (vertical) position 0003 LDN 19 Load negative range 0004 STO 31 Initialize X (horizontal) position 0005 LDN 31 loop: Get X position 0006 SUB 29 Add X velocity 0007 STO 26 recomplement 0008 LDN 26 through temp 0009 STO 31 Store updated X position 0010 LDN 30 Get Y position 0011 SUB 28 Add Y velocity 0012 STO 26 recomplement 0013 LDN 26 through temp 0014 STO 30 Store updated Y position 0015 CMP Test for negative (fell back through 0) 0016 JMP 19 Jump ahead if still going 0017 STP Stop 0018 JMP 0 Jump to top for another round 0019 NUM 8388627 Initial range and jump constant 19 0020 LDN 28 Get Y velocity 0021 STO 26 recomplement 0022 LDN 26 through temp 0023 SUB 27 Subtract gravity constant 0024 STO 28 Store updated Y velocity 0025 JMP 27 Jump back to loop 0026 NUM 0 Temporary variable 0027 NUM 4 Gravity constant and jump constant for loop 0028 NUM 4095 Y Velocity - adjust between rounds to aim 0029 NUM 4095 X Velocity - adjust between rounds to aim 0030 NUM 0 Y Position 0031 NUM 0 X Position
The program uses several tricks to fit in memory. The indirect jump value at 0 for the inner loop is strategically located so that the jump instruction at 15 is used four different ways - as an instruction, as the indirect jump value for the outer loop, as the decrement for the outer loop counter and as unity (virtual "one") for all the arithmetic in the program. The outer loop jump "JMP 15" at 28 also holds the outer loop count. When the count expires the opcode changes from jump to stop. Finally, the scaling constant at 25 is encoded in the "LDN 28" instruction there, including the op code.
With other scaling constants the program can achive higher precision and accuracy as well as handle larger angles at the cost of significantly increased run time. The parameters here are a compromise to show interesting angles within a reasonable run time. Source: sine.txt.
32 Sin(x) 0000 NUM 3 0001 LDN 30 major: Get cosine 0002 STO 29 Put in dividend 0003 STO 30 Store cosine positive 0004 LDN 31 minor: Get sine 0005 SUB 15 Increment using u 0006 STO 31 recomplement 0007 LDN 31 in place 0008 STO 31 Store incremented sine 0009 LDN 29 Get dividend 0010 STO 29 recomplement 0011 LDN 29 in place 0012 SUB 25 Subtract divisor 0013 STO 29 Store updated dividend 0014 CMP Skip if division complete 0015 NUM 128 u: JMP 0 to minor loop if not (3 other uses) 0016 LDN 31 Get sine 0017 STO 29 recomplement 0018 LDN 29 in dividend (temporary) 0019 SUB 25 Subtract divisor to rezero 0020 STO 31 Store adjusted sine 0021 SUB 30 Negative subtract cosine 0022 STO 30 Store updated cosine negative 0023 LDN 28 Get x 0024 STO 28 recomplement 0025 NUM 16796 in place (this LDN 28 also used as divisor) 0026 SUB 15 Decrement x using u 0027 STO 28 Store updated x, x changes to STP on underflow 0028 NUM 3343 x: JMP 15 through u to major loop, also contains x 0029 NUM 0 Dividend for scaling down cosine variable 0030 NUM -3695104 Cosine variable (negative) 0031 NUM 0 Sine variable