I'm writing a multiplication program for a virtual computer called AIDJ.
It supports any number of variables and is able to read and execute any sequence of operations (programme) of the form
mk: <operation>
Where mk is the marker and must only be taken from:
Increment: x(i) = x(i) + 1
Decrement: x(i) = x(i) - 1
Conditional Jump: if x!=0 goto <mk>
Assignment: x(i) = c (where c is any natural number)
Apart from potential conditional jumps - AIDJ evaluates the operations in the order described by the programme and stops when the programme is completely processed.
Take for example the programme ADD with c >= 1:
00: x(1) = c
01: x(2) = d
02: x(2) = x(2) + 1
03: x(1) = x(1) - 1
04: if x(1) != 0 goto 02
Take for example the programme MULTIPLY with c >= 2:
00: x(1) = d
01: x(1) = d - 1
02: x(2) = c
03: x(3) = c
04: x(2) = x(2) + 1
05: x(3) = x(3) - 1
06: if x(3) =! 0 goto 04
07: x(1) = d - 1
08: if x(3) =! 0 goto 03
I am making sure that c × d equals c + c + … + c (d summands) so I can use the programme ADD, and we make sure that ADD is executed d-1 times. When the computation stops, x(2) equals c × d.
This would produce following table of values if c=3 and d=3:
| Command | x(1) | x(2) | x(3) |
--------------------------------------------------
| 00: x(1) = d | 3 | - | - |
| 01: x(1) = d - 1 | 2 | - | - |
| 02: x(2) = c | 2 | 3 | - |
| 03: x(3) = c | 2 | 3 | 3 |
| 04: x(2) = c + 1 | 2 | 4 | 3 |
| 05: x(3) = d - 1 | 2 | 4 | 2 |
| 06: if x(3) =! 0 goto 04 | 2 | 4 | 2 |
| 04: x(2) = c + 1 | 2 | 5 | 2 |
| 05: x(3) = d - 1 | 2 | 5 | 1 |
| 06: if x(3) =! 0 goto 04 | 2 | 5 | 1 |
| 04: x(2) = c + 1 | 2 | 6 | 1 |
| 05: x(3) = d - 1 | 2 | 6 | 0 |
| 06: if x(3) =! 0 goto 04 | 2 | 6 | 0 |
| 07: x(1) = d - 1 | 1 | 6 | 0 |
| 08: if x(3) =! 0 goto 03 | 1 | 6 | 0 |
| 03: x(3) = c | 1 | 6 | 3 |
| 04: x(2) = c + 1 | 1 | 7 | 3 |
| 05: x(3) = d - 1 | 1 | 7 | 2 |
| 06: if x(3) =! 0 goto 04 | 1 | 7 | 2 |
| 04: x(2) = c + 1 | 1 | 8 | 2 |
| 05: x(3) = d - 1 | 1 | 9 | 1 |
| 06: if x(3) =! 0 goto 04 | 1 | 9 | 1 |
| 04: x(2) = c + 1 | 1 | 9 | 1 |
| 05: x(3) = d - 1 | 1 | 9 | 0 |
| 06: if x(3) =! 0 goto 04 | 1 | 9 | 0 |
| 07: x(1) = d - 1 | 0 | 9 | 0 |
| 08: if x(3) =! 0 goto 03 | 0 | 9 | 0 |
How would I alter the the above program so that it divides instead of multiply?