Hacker News new | ask | show | jobs
by KeriWarr 4254 days ago
I was attempting to create the most simple infinite loop that I could, and found that the instructions do not work in the way that I thought they would. Here are a couple programs I wrote. If someone could explain why they work the way they do..?

00000011

00000100

00000000

00000001

Looking at this, I expected that it would read line one, then move the value addressed by line two to the value addressed by line three. i.e. set the instruction pointer to 00000001. This didn't work. What did was:

00000000

00000000

00000000

00000011

00000111

00000000

00000001

This program advances to line 4 then remains there. Could someone explain why program two is an infinite loop, but program one isn't? Thanks!

4 comments

Your first program does as you expect, but then the instruction pointer gets incremented by 3, so it goes on.

If you modify the last line to "11111101" then when the instruction pointer is increased by 3 it will overflow back to 00000001 and you will get your infinite loop.

The CPU runs the opcode(instruction), then adds 3 to the IP. Therefore, your first program sets IP to 1, then it gets 3 added to it, setting IP to 4. Your second program is exactly the same, but setting IP to 4 means infinite loop.
Hi, I made this site.

The problem is that it moves that value into the instruction pointer, then the instruction pointer increments itself by 3.

the instruction pointer always increments itself by 3 after every instruction.

Thanks all, for the explanations :)
As nothing is moving visually it seems to be doing nothing. 0001: 0000 0010: 0000 0011: 0000 0100: 0000 0101: 0000 0110: 0000 0111: 0011 1000: 1010 1001: 0000 1010: 0001