-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbasics.html
228 lines (221 loc) · 8.67 KB
/
basics.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<link href="ie10-viewport-bug-workaround.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="scrolling-nav.css" rel="stylesheet">
<link href="https://bootswatch.com/flatly/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body id="page-top" data-target=".navbar-fixed-top">
<nav class="navbar navbar-inverse navbar-fixed-top top-nav-collapse">
<div class="container">
<div class="navbar-header page-scroll">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
</button>
<a class="navbar-brand page-scroll" href="index.html">Quine8</a>
<a class="navbar-brand page-scroll" href="index.html">Previous</a>
<a class="navbar-brand page-scroll" href="q8.html#challenge0">Next</a>
</div>
</div>
</nav>
<section style="margin-top: 5em; max-width: 50em;">
<h1>CPU Fundamentals</h1>
<p>At a basic level, a computer can only do three things:</p>
<h4><span class="info-tag">STORE</span> information in memory</h4>
<p>In Q8, the main memory area is that gray grid.</p>
<h4><span class="info-tag">MOVE</span> information around in memory</h4>
<p>(Copying the number from one box into another box)<br>This is done with CPU instructions,
commonly referred to as operations (ops), which we'll get to in a moment.</p>
<h4><span class="info-tag">WORK</span> with information</h4>
<p>This can't happen in the main memory area,
it has to happen in a working memory area called a register (the two special boxes at the bottom of
the main memory grid).</p>
<p>To do work on information, you have to move it into the registers, do the work there, and then
store the results of that work back into main memory, so that you can free up the registers to do
more work.</p>
<h1>Common Quine8 Structures</h1>
<p>Q8 is designed a little differently than the standard x86 or ARM processors commonly
seen today. It only has 2 registers, and doesn't provide a general purpose stack. This
greatly simplifies the instruction table, but requires the programmer to change their
method of interaction with the system slightly.</p>
<p>To keep things clear as we talk about the memory grid, we'll use the words tile or cell
to refer to specific byte-wide boxes in memory.</p>
<p>To efficiently write code for Q8 without diving into runtime modification of
bytecode, the programmer must use general memory as external registers. The platform provides a few
of instructions that help to make that interaction as painless as possible.</p>
</section>
<section style="max-width: 50em;">
<h1>Q8 Assembly Primer</h1>
<p>To improve legibility, I've provided a little asssembly block next to each grid image
Q8's assembly is simple, but it's still best to describe the notation ahead of time.</p>
<h3>Instruction Layout</h3>
<h4>Instructions <code>LOAD A 9</code></h4>
<p>Instructions are positioned op and then argument. An instruction like <code>SET A 9</code>
can be read as "set register A to the value 9"</p>
<h3>Labels and Data</h3>
<h4>Labels <code>exit:</code></h4>
<p>To tell the assembler that you want to use a name to refer to a spot in assembly, all you need to
do is plop your name where you want it, and then append a <code>:</code> to the end, like <code>loop:</code></p>
<h4>Data Definition <code>y: $55</code></h4>
<p>To define some data that'll live in the grid, you can use a label, and then prefix the value you want to
use with a <code>$</code>. For example, to define a variable, x, and to set it to 1 initially, you'd
write this: <code>x: $1</code></p>
<h3>Position Shifters</h3>
<h4>Shifters <code>> 25</code></h4>
<p>To place the next piece of code at a particular position, position shifters can be used. These are mostly useful
for asthetic reasons, allowing the user to neatly separate op blocks from data. Another possible use, putting a
catchall halt at the end of the grid so that the program counter doesn't wrap around from 255 to 0 on an unforseen
failure state </p>
<h3>Code Comments</h3>
<h4>Comments <code>; Hello</code></h4>
<p> Comments are quite useful for program legibility. Assembly can be hard to follow, comments are there to help
<code>ADD A x ; Adds the value at labelled position x to A</code>
</p>
<h3>Assembly Example</h3>
<p>This program sets <code>A</code> to 1, <code>B</code> to the value at label x, 1, and then adds them. The result, 2, is left in <code>A</code>.
<pre>
SET A 1
LOAD B x
ADD A B
> 32
x: $1</pre>
</section>
<section style="max-width: 50em;">
<h2>I/O</h2>
<h3>The Direct</h3>
<p>A great little debugging buddy, the <code>SET</code> instruction.
<code>SET</code> is excellent for debugging and cramming single-use constants into registers.
This mechanism proves remarkably useful in some of the example programs for tracking boolean
data, and drawing specific block values to the screen.</p>
<div class="img_container">
<div>
<img src="basics_constant.png">
</div>
<div>
<h4>Assembly</h4>
<pre>
JMP 33
> 33
SET A 9
SET B 2</pre>
</div>
</div>
<h3>The Indirect</h3>
<p>The indirect accessors, <code>LOAD</code> and <code>STORE</code> are the meat and potatoes of the Q8 I/O family.
Most of the time to keep the limited register space available for ops, you'll wind up using these.</p>
<div class="img_container">
<div>
<img src="basics_direct.png">
</div>
<div>
<h4>Assembly</h4>
<pre>
JMP 26
> 33
LOAD A x
LOAD B y
STORE A x
STORE B y
> 64
x: $1
y: $2</pre>
</div>
</div>
<h3>The Double Indirect</h3>
<p>Double indirect accessors are used a little less frequently, but when you need them, you need them. They've proved Very useful
for tracking indices into an array, or setting up jump tables. You can <code>LOADI</code> to get the value,
and <code>LOAD</code> to get the index. When you want to change your index, you <code>LOAD</code> the index,
make your modification, and then <code>STORE</code> it back again. Changing the value at the tile pointed to by the index
is a <code>STOREI</code></p>
<br style="clear: both;">
<div class="img_container">
<div>
<img src="basics_indirect.png">
</div>
<div>
<h4>Assembly</h4>
<pre>
JMP 26
> 33
LOADI A x_loc
LOADI B y_loc
STOREI A x_loc
STOREI B y_loc
> 64
x_loc: $80
y_loc: $81
> 80
x: $1
y: $2</pre>
</div>
</div>
</section>
<section style="max-width: 50em;">
<h2>Loops</h2>
<h3>The Register Loop</h3>
<p>This doesn't prove to be very useful in practice, as most of the time you'll
need to keep both registers available, but it does take fewer cpu cycles than a tile loop,
not needing the additional blocks to load and store the external tile. It's also good for
reducing the loop's tile footprint, as it doesn't need to use an extra tile for state.</p>
<div class="img_container">
<div>
<img src="basics_register_loop.png">
</div>
<div>
<h4>Assembly</h4>
<pre>
SET A 5
JMP loop_start
> 34
loop_start:
ISZERO A
JZ 255
DEC A
JMP loop_start
> 255
HALT</pre>
</div>
</div>
<h3>The Memory Tile Loop</h3>
<p>This is the loop you'll see frequently in the example projects. It's
a little longer than a register loop, but far more practical, as it keeps the two registers
available for the code the loop jumps into. It isn't perfect. The loop overwrites a register,
so you'll need to save it if you want to come back to that value.</p>
<div class="img_container">
<div>
<img src="basics_tile_loop.png">
</div>
<div>
<h4>Assembly</h4>
<pre>
JMP loop_start
> 34
loop_start:
LOAD A idx
ISZERO A
JZ 255
DEC A
STORE A idx
JMP loop_start
> 255
HALT
> 80
idx: $5</pre>
</div>
</div>
</section>
<link href="tutorial_style.css" rel="stylesheet">
</body>
<footer style="margin-top: 4em; padding-bottom: 4em; background-color: #555"></footer>
</html>