Adding by subtracting



Previous post in topic
First post in topic
Table of contents

Adding by subtracting

There won’t be posts after this one on this thing for a couple of weeks. I must navigate the dangerous, relative infested waters of the holidays.

I need to be able to show that the way we program our machine can be made to run any program, at least in principle. On the other hand, I’d very much like to switch gear, and get to talking about gear soon.

Here’s one more post on using subleq. I think that will do for now.

Have a look at a subleq program that subtracts one number from the other.

0, 0, 5, 4, 3, 4, 3, 8, 0, 0, -1

Starting with:
0 0
1 0
2 5
3 4 This number
4 3 minus this one
5 4
6 3
7 8
8 0
9 0
10 -1

the answer will go in slot 3.

0 0
1 0
2 5
3 4 this one
4 3
5 4
6 3
7 8
8 0
9 0
10 -1

starting pass 1. Pointer at address 0.
memory= 0, 0, 5, 4, 3, 4, 3, 8, 0, 0, -1
looking up commands. position 0=0, position 1=0, position 2=5
values 0, 0.
result 0-0=0. storing result in address 0.
Moving pointer to address 5.
After instruction 1:
memory= 0, 0, 5, 4, 3, 4, 3, 8, 0, 0, -1

starting pass 2. Pointer at address 5.
looking up commands. position 5=4, position 6=3, position 7=8
values 3, 4.
result 4-3=1. storing result in address 3.
Moving pointer to address 8.
After instruction 2:
memory= 0, 0, 5, 1, 3, 4, 3, 8, 0, 0, -1

starting pass 3. Pointer at address 8.
looking up commands. position 8=0, position 9=0, position 10=-1
values 0, 0.
result 0-0=0. storing result in address 0.
Moving pointer to address -1.
After instruction 3:
memory= 0, 0, 5, 1, 3, 4, 3, 8, 0, 0, -1

End

0 0
1 0
2 5
3 1 answer here
4 3
5 4
6 3
7 8
8 0
9 0
10 -1

Watch what happens if we turn the 3 held in slot 4 into a negative number—a -3 instead.

0, 0, 5, 4, -3, 4, 3, 8, 0, 0, -1

Starting with:
0 0
1 0
2 5
3 4 this number
4 -3 minus this one?
5 4
6 3
7 8
8 0
9 0
10 -1

starting pass 1. Pointer at address 0.
memory= 0, 0, 5, 4, -3, 4, 3, 8, 0, 0, -1
looking up commands. position 0=0, position 1=0, position 2=5
values 0, 0.
result 0-0=0. storing result in address 0.
Moving pointer to address 5.
After instruction 1:
memory= 0, 0, 5, 4, -3, 4, 3, 8, 0, 0, -1

starting pass 2. Pointer at address 5.
looking up commands. position 5=4, position 6=3, position 7=8
values -3, 4.
result 4 – -3=7. storing result in address 3.
Moving pointer to address 8.
After instruction 2:
memory= 0, 0, 5, 7, -3, 4, 3, 8, 0, 0, -1

starting pass 3. Pointer at address 8.
looking up commands. position 8=0, position 9=0, position 10=-1
values 0, 0.
result 0-0=0. storing result in address 0.
Moving pointer to address -1.
After instruction 3:
memory= 0, 0, 5, 7, -3, 4, 3, 8, 0, 0, -1

End

0 0
1 0
2 5
3 7 answer
4 -3
5 4
6 3
7 8
8 0
9 0
10 -1

As you can see, subtracting a negative three, 4 – -3, is the same as adding a positive three, 4+3.

We can use the fact that subtracting a negative is the same as adding a positive to write a program that adds two numbers, even though what makes it work, subleq, is always subtracting. We just subtract one number from 0, and then subtract that answer from the other number.

Starting with:
0 0
1 0
2 5
3 4 this number
4 3 plus this one
5 4
6 0
7 8
8 0
9 3
10 11
11 0
12 0
13 -1

0 0
1 0
2 5
3 4 answer will go here
4 3
5 4
6 0
7 8
8 0
9 3
10 11
11 0
12 0
13 -1

starting pass 1. Pointer at address 0.
memory= 0, 0, 5, 4, 3, 4, 0, 8, 0, 3, 11, 0, 0, -1
looking up commands. position 0=0, position 1=0, position 2=5
values 0, 0.
result 0-0=0. storing result in address 0.
Moving pointer to address 5.
After instruction 1:
memory= 0, 0, 5, 4, 3, 4, 0, 8, 0, 3, 11, 0, 0, -1

the first three numbers, (0, 0, 5) subtract 0 from 0 and keep the answer in 0. Nothing changes, but when subleq has a number that is zero or lower, it will jump to the address held in that third slot. This lets us skip the places where we are holding our variables, and go to address five instead of 3.

starting pass 2. Pointer at address 5.
memory= 0, 0, 5, 4, 3, 4, 0, 8, 0, 3, 11, 0, 0, -1
looking up commands. position 5=4, position 6=0, position 7=8
values 3, 0.
result 0-3=-3. storing result in address 0.
Moving pointer to address 8.
After instruction 2:
memory= -3, 0, 5, 4, 3, 4, 0, 8, 0, 3, 11, 0, 0, -1

This command (4, 0, 8) subtracted 3 from zero, and you can see the -3 that’s left in the memory array. It’s the very first number. The third number just moves us to address 8 if the answer is zero or less. That’s the same line we’d go to if the answer was one or more, so that no matter what the answer of this command is, we always go to the same place, address 8. That’s the way we can avoid branching when we don’t want to.

starting pass 3. Pointer at address 8.
memory= -3, 0, 5, 4, 3, 4, 0, 8, 0, 3, 11, 0, 0, -1
looking up commands. position 8=0, position 9=3, position 10=11
values -3, 4.
result 4 – -3=7. storing result in address 3.
Moving pointer to address 11.
After instruction 3:
memory= -3, 0, 5, 7, 3, 4, 0, 8, 0, 3, 11, 0, 0, -1

Now we’ve got the answer. However, the program hasn’t stopped, and we’ve got that -3 at the start. If we can clean the code, we might be able to reuse this section somewhere in a larger program. Besides, we need to stop the machine anyway.

starting pass 4. Pointer at address 11.
memory= -3, 0, 5, 7, 3, 4, 0, 8, 0, 3, 11, 0, 0, -1
looking up commands. position 11=0, position 12=0, position 13=-1
values -3, -3.
result -3 – -3=0. storing result in address 0.
Moving pointer to address -1.
After instruction 4:
memory= 0, 0, 5, 7, 3, 4, 0, 8, 0, 3, 11, 0, 0, -1

End

There, that’s cleaner, and we’re left with:

0 0
1 0
2 5
3 7 answer
4 3
5 4
6 0
7 8
8 0
9 3
10 11
11 0
12 0
13 -1

Maybe it would be even cleaner if we could keep the answer in one of those two first slots, and leave the numbers we just added the same. That can be done, but it’s a longer program.

Let me try and write that program. I’ll let you see it run, and then I’ll say “Good night, and happy holidays.”

HA! It worked on the first try. that never happens.

Starting with:
0 0
1 0 The answer lands here
2 5
3 12002000 this number
4 250017 plus this one, and have a merry Christmas!
5 4
6 0
7 8
8 0
9 1
10 11
11 0
12 0
13 14
14 3
15 0
16 17
17 0
18 1
19 20
20 0
21 0
22 -1

starting pass 1. Pointer at address 0.
memory= 0, 0, 5, 12002000, 250017, 4, 0, 8, 0, 1, 11, 0, 0, 14, 3, 0, 17, 0, 1,
20, 0, 0, -1
looking up commands. position 0=0, position 1=0, position 2=5
values 0, 0.
result 0-0=0. storing result in address 0.
Moving pointer to address 5.
After instruction 1:
memory= 0, 0, 5, 12002000, 250017, 4, 0, 8, 0, 1, 11, 0, 0, 14, 3, 0, 17, 0, 1,
20, 0, 0, -1

starting pass 2. Pointer at address 5.
looking up commands. position 5=4, position 6=0, position 7=8
values 250017, 0.
result 0-250017=-250017. storing result in address 0.
Moving pointer to address 8.
After instruction 2:
memory= -250017, 0, 5, 12002000, 250017, 4, 0, 8, 0, 1, 11, 0, 0, 14, 3, 0, 17,
0, 1, 20, 0, 0, -1

starting pass 3. Pointer at address 8.
looking up commands. position 8=0, position 9=1, position 10=11
values -250017, 0.
result 0 – -250017=250017. storing result in address 1.
Moving pointer to address 11.
After instruction 3:
memory= -250017, 250017, 5, 12002000, 250017, 4, 0, 8, 0, 1, 11, 0, 0, 14, 3, 0,
17, 0, 1, 20, 0, 0, -1

starting pass 4. Pointer at address 11.
looking up commands. position 11=0, position 12=0, position 13=14
values -250017, -250017.
result -250017 – -250017=0. storing result in address 0.
Moving pointer to address 14.
After instruction 4:
memory= 0, 250017, 5, 12002000, 250017, 4, 0, 8, 0, 1, 11, 0, 0, 14, 3, 0, 17, 0
, 1, 20, 0, 0, -1

starting pass 5. Pointer at address 14.
looking up commands. position 14=3, position 15=0, position 16=17
values 12002000, 0.
result 0-12002000=-12002000. storing result in address 0.
Moving pointer to address 17.
After instruction 5:
memory= -12002000, 250017, 5, 12002000, 250017, 4, 0, 8, 0, 1, 11, 0, 0, 14, 3,
0, 17, 0, 1, 20, 0, 0, -1

starting pass 6. Pointer at address 17.
looking up commands. position 17=0, position 18=1, position 19=20
values -12002000, 250017.
result 250017 – -12002000=12252017. storing result in address 1.
Moving pointer to address 20.
After instruction 6:
memory= -12002000, 12252017, 5, 12002000, 250017, 4, 0, 8, 0, 1, 11, 0, 0, 14, 3
, 0, 17, 0, 1, 20, 0, 0, -1

starting pass 7. Pointer at address 20.
looking up commands. position 20=0, position 21=0, position 22=-1
values -12002000, -12002000.
result -12002000 – -12002000=0. storing result in address 0.
Moving pointer to address -1.
After instruction 7:
memory= 0, 12252017, 5, 12002000, 250017, 4, 0, 8, 0, 1, 11, 0, 0, 14, 3, 0, 17,
0, 1, 20, 0, 0, -1

End

0 0
1 12252017 Ho Ho HO!
2 5
3 12002000
4 250017
5 4
6 0
7 8
8 0
9 1
10 11
11 0
12 0
13 14
14 3
15 0
16 17
17 0
18 1
19 20
20 0
21 0
22 -1

Good night, and happy holidays.

Previous post in topic
First post in topic
Table of contents


Leave a Reply