The only bug was in my Brain!



Previous post in topic
Next post in topic

In the previous post, I talked very briefly about a one instruction set computer. I didn’t go into details because I hadn’t had a chance to play with one yet. Well, I’ve played with it, and it isn’t as difficult as I was making it.

The one command I’m playing with is called subleq. That short for, “subtract and branch if less than or equal to 0.” Don’t worry if this doesn’t make sense yet; I’ll explain in painful detail in future posts.

B=A-b
If B<=0 jump to address c
Else go to next line

Commands and numbers are stored in the same array. All commands consist of three addresses. When you first start, the three address command is stored in addresses 0, 1, 2.

In address 0, another address is stored. A is first set to be equal to the value stored in the address pointed to in address 0.
B is set to be the value stored in the address stored in address 1.
C is set to be the address stored at address 2.
B is set to be equal to B-A, and then stored in the address pointed to at address 1.

If B isn’t a positive number, if it is zero or a negative number, the program jumps to C—the address stored in address 2.

Here’s where I screwed up. Somehow, I got it in my head that if B was a positive number, one or more, the program should do the same set of actions, but now using addresses 2, 3, 4. What should happen, is that the program repeats, but using addresses 3, 4, 5.

I was using a version of subleq I got from a blog post by Nicola Malizia. It wasn’t acting the way I thought, so his nice clean code got scrambled up as I put in various flags and messages to figure out why. Eventually I found what I thought was a bug. I “fixed” it. The only trouble was, it wasn’t a bug. the way I thought it should act was wrong.

The way it really should act makes so much more sense. things like an unconditional branch are so simple now.

The only bug was in my brain!

Previous post in topic
Next post in topic


Leave a Reply