The prime, and only, directive.



Previous post in topic
Next post in topic
First post in topic
Table of contents

There seems to be a tradeoff.

On the one hand, you can have a complicated machine, and easy programming.

On the other, you can have a simple machine, but the programming gets weird and obscure.

I’m shooting for a simple machine. It will just do the same thing over and over again.

In modern computers, you send data to where it will be processed, and an instruction to tell it what to do with that data. instructions might include things like, jump to this line, add these two numbers, are these things equal, is this greater than that, and so on. The more instructions you have, the more complicated the machine has to be. To keep things simple, let’s just have one instruction.

Our numbers are stored in an array. Picture a long ribbon that has been divided up into slots. Each slot or little square has its address, and inside each slot is a number.

Instead of using a, b, and c, let’s run through one with actual numbers.

0 3
1 4
2 6
3 1
4 3
5 6
6 0
7 0
8 -1

I’ll use a * to act as our pointer. Screen reader users might want to turn on punctuation.

We start at address 0.

0 3 *
1 4
2 6
3 1
4 3
5 6
6 0
7 0
8 -1

The first number is our address, the number in the slot is 3. We look down at slot three, and save that number.

0 3 *
1 4
2 6
3 1 this number
4 3
5 6
6 0
7 0
8 -1

In slot 3 is 1. We write that number down and move to slot 1.

0 3
1 4 *
2 6
3 1
4 3
5 6
6 0
7 0
8 -1

We look down at slot 4, and save that number

0 3
1 4 *
2 6
3 1
4 3 this number
5 6
6 0
7 0
8 -1

In slot 4 is the number 3.

Now we subtract the first number we got from the second number we got and replace the second number with the answer

The first number was 1, the second number was 3.

3-1=2

We put that answer where the second number came from. This time, we shove it back in slot 4.

0 3
1 4 *
2 6
3 1
4 2 where we replaced the old number “3” with the answer “2”
5 6
6 0
7 0
8 -1

Now e look at our answer. Depending upon what it is, we will move the pointer to a new spot and start over. As long as the answer is more than zero, we just move the pointer ahead two spots.

0 3
1 4 where the pointer was
2 6
3 1 *
4 2
5 6
6 0
7 0
8 -1

Next, we’ll do it again.

This time, the numbers we used for the subtraction came from slots lower down than the pointer. But, they don’t have to. The numbers can just as easily come from slots above the pointer.

Let’s pick it up where we left off.

0 3
1 4
2 6
3 1 *
4 2
5 6
6 0
7 0
8 -1

0 3
1 4 this number
2 6
3 1 *
4 2
5 6
6 0
7 0
8 -1

0 3
1 4
2 6 this number
3 1
4 2 *
5 6
6 0
7 0
8 -1

6-4=2

0 3
1 4
2 2 answer replaces 6
3 1
4 2 *
5 6
6 0
7 0
8 -1

Our answer is still greater than zero, so we move the pointer the same way.

0 3
1 4
2 2
3 1
4 2 where the pointer was
5 6
6 0 *
7 0
8 -1

Let’s go again.

0 3 this number
1 4
2 2
3 1
4 2
5 6
6 0 *
7 0
8 -1

0 3 this number
1 4
2 2
3 1
4 2
5 6
6 0
7 0 *
8 -1

This time, both numbers came from the same place—slot 0. So, we store the result of 3-3 in slot 0. Since the answer happens to be zero, we put zero in zero.

0 0 replace 3 with 0 in slot 0
1 4
2 2
3 1
4 2
5 6
6 0
7 0 *
8 -1

This time the answer is 0, so we have to do something different. We move the pointer ahead once.

0 0
1 4
2 2
3 1
4 2
5 6
6 0
7 0
8 -1 *

We’re not quite done. We take the number in that slot, and move our pointer there. Since the number in slot 8 is -1, it would look like this.

*
0 0
1 4
2 2
3 1
4 2
5 6
6 0
7 0
8 -1

As it happens, there are no negative numbered slots. Putting in a -1 is a handy way to tell the machine to stop.

Let’s start again, only we’ll change the first value held in slot 4 from 3 to 1.

0 3 *
1 4
2 6
3 1 this number
4 1
5 6
6 0
7 0
8 -1

0 3
1 4 *
2 6
3 1
4 1 this number
5 6
6 0
7 0
8 -1

This time, both slots are holding the same number, instead of one slot getting called twice. Still, just like 3-3, 1-1=0

0 3
1 4 *
2 6
3 1
4 0 replaced 1
5 6
6 0
7 0
8 -1

Our answer is zero, we move the pointer ahead by one.

0 3
1 4
2 6 *
3 1
4 1
5 6
6 0
7 0
8 -1

In slot 2 is 6, so that’s where we should move the pointer to.

0 3
1 4
2 6 where the pointer was
3 1
4 0
5 6
6 0 *
7 0
8 -1

We’ve been here before. We’re about to subtract 3 from 3, and then halt the program with a negative one, but this time, we got there by a slightly different path. We only did two commands, instead of three, skipping the middle numbers completely.

You’ll notice that running the program tends to change it. If you take our first run, from beginning until it finally reached the -1, our program changed from:

0 3
1 4
2 6
3 1
4 3
5 6
6 0
7 0
8 -1

Into:

0 0
1 4
2 2
3 1
4 2
5 6
6 0
7 0
8 -1

If you’re into math for fun, you can, for fun, figure out what would happen if we started over with those numbers.

Next time, we’ll get subtraction to add.

Previous post in topic
Next post in topic
First post in topic
Table of contents


Leave a Reply