When you see the <code>0:</code> at the front of the line that's the line number. It's not code, just me being lazy and not wanting to implement line numbers in fancy CSS.
<p>To make a computer we need a way to repeatedly run code so we can do many calculations. <code>BUTTONS</code> has
the operation <code>JUMP</code> that will jump to the line you give, then keep running. If I want to run a loop that counts from 1 to 128 I can do this in <code>BUTTONS</code>:
<pre>
<code>
0: PUSH 1
1: PUSH 1
2: ADD
3: JUMP 1
</code>
</pre>
<p>You really should <b>study</b> this simple little program because inside it's simplicity is an incredibly powerful idea. Here's some questions to ask while you study:</p>
<ol>
<li>Why do I do <code>JUMP 1</code> instead of <code>JUMP 2</code> to reach the <b>second</b> line?</li>
<li>Step through the code and write down what the top of the stack is at each step.</li>
<li>If you want to go up by a different count what do you do?</li>
<li>How does this work with <code>SUB, DIV, MUL, and MOD</code> operations?</li>
</ol>
<p>Play with this code in <code>BUTTONS</code> until you understand what's going on because if you don't get
<code>JUMP</code> then you <b>definitely</b> won't understand the next section.</p>
You can also <code>JUMP</code> one line <b>past</b> the end of your code to end your program. That means if you have 4 lines of code, then you can do <code>JUMP 5</code> and end your program. This becomes important later so write this down in your notebook too.
</blockquote>
<h2>Logic with JZ/JNZ</h2>
<p>You now know how to make <code>BUTTONS</code> do math, use the <code>STACK</code>, and do a loop to do lots of math. There's <b>one</b> last thing we need before we have a real working computer and that's the idea of a <code>test</code> and jumping only if that test is true (or false).</p>
<p>Let's say you have the following code that counts <b>down</b> from 10 to 0:</p>
<pre>
<code>
0: PUSH 10
1: PUSH 1
2: SUB
3: JUMP 1
</code>
</pre>
<p>You run it, thinking it will stop at zero, and instead
<code>BUTTONS</code> does exactly what you told it to do and keeps going
<p>The reason is you have no way to tell <code>BUTTONS</code> when to stop. You can tell it to do the math and where to <code>JUMP</code> but you have no way to tell buttons "when you reach 0 on the <code>STACK</code> you should stop." You do this with the <code>JZ</code> operation which means "<code>JUMP if Zero</code>". It simply looks at the top of the <code>STACK</code> and if that's 0 then it does a <code>JUMP</code> to where you want. This is doing a test of the top of stack, and a jump. Now we can rewrite our program like this:</p>
<pre>
<code>
0: PUSH 10
1: PUSH 1
2: SUB
3: JZ 5
4: JUMP 1
</code>
</pre>
<p>See how line 3 is now <code>JZ 5</code>, but right after that on line
4 we have <code>JUMP 1</code>? This seems very backwards, but we have to do this because we want
line 3 to <b>only</b><code>JUMP 1</code> to the end (line 5 is the end) when the <code>STACK</code>
is 0. It's working as a "guard" that prevents line 4 <code>JUMP 1</code> from running when the <code>STACK</code>
is 0. Another way to see this is it will "jump over" line 4 <code>JUMP 1</code> when the <code>STACK</code> is 0.</p>
<p>We can simplify this by using another operation called <code>JNZ</code> which does the inverse of <code>JZ</code> and "jumps if <b>NOT</b> zero". Using this operation we can now have:</p>
<pre>
<code>
0: PUSH 10
1: PUSH 1
2: SUB
3: JNZ 1
</code>
</pre>
<p>Instead of that weird "jump over the next line that's a jump if the
stack is zero" in the previous program, we have "jump to line 1 if stack
is not zero". Way easier to do, but keep in mind you many times need
both. Both effectively do the same thing but it's sometimes easier to use
one or the other (<code>JZ</code> vs. <code>JNZ</code>) depending on how
you're doing the operation.</p>
<h2>STOR/RSTOR Registers</h2>
<p>You technically can do all of the computation you need with just that (minus input and output), but it's really annoying and hard to do much more than add some numbers in loops. To make it possible to implement larger more complex prgrams in <code>BUTTONS</code> you have four <code>REGISTERS</code> where you can keep temporary variables for later: <code>AX, BX, CX, DX</code>. I named them this way just for old time's sake, and to keep things simple. You put things into these registers (and take them out) using the <code>STOR</code> and <code>RSTOR</code> (for "restore") operations:</p>
<tableclass="table">
<thead>
<tr>
<th>OPERATION</th><th>DESCRIPTION</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>STOR</code></td><td><b>Copies</b> the <code>STACK</code> top to the named register.</td>
</tr>
<tr>
<td><code>RSTOR</code></td><td><code>PUSH</code> the number in the named register onto the <code>STACK</code>.</td>
</tr>
</tbody>
</table>
<p>Let's say I want to count down like before, but I want to keep track of the previous number as you go. I don't know why. You just do. Ok, here's how:</p>
<pre>
<code>
0: PUSH 10
1: STOR AX
2: PUSH 1
3: SUB
4: JNZ 1
</code>
</pre>
<p>Now as you step through this program you can watch the <code>AX</code> register keep the previous stack top as it goes. Let's say you want to keep the initial value you started with for after the loop:</p>
<pre>
<code>
0: PUSH 10
1: STOR AX
2: PUSH 1
3: SUB
4: JNZ 2
5: RSTOR AX
</code>
</pre>
<p>Now I do <code>JNZ 2</code> instead so that I <b>avoid</b> the <code>01: STOR AX</code> line, that does the loop like before but now the <code>AX</code> register has my starting number. After line <code>4: JNZ 2</code> passes I then use <code>RSTOR AX</code> to get that number back, and the program ends with two numbers on the stack: <code>10</code> and <code>0</code>.</p>
<h2>Other Operations</h2>
<p>You can also use these operations to do other things:</p>
<tableclass="table">
<thead>
<tr>
<th>OPERATION</th><th>DESCRIPTION</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>CLR</code></td><td>Clears <code>BUTTONS</code>. Good for debugging.</td>
</tr>
<tr>
<td><code>HALT</code></td><td>Stop the computer with a message.</td>
</tr>
</tbody>
</table>
<h2>What About Input and Output?</h2>
<p>Astute readers will notice I have no way to input a number from the world, and output a number (or character) to the screen. That's because I did this whole project in about 3 days while playing video games and I'm not sure how to make a fancy looking retro CRT screen. I'll get to that part eventually.</p>