-
Notifications
You must be signed in to change notification settings - Fork 295
/
instruction-set.html
269 lines (269 loc) · 10.5 KB
/
instruction-set.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Simple 8-bit Assembler - Instruction Set Help</title>
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" href="assets/style.css">
</head>
<body>
<a href="https://github.com/Schweigi/assembler-simulator"><img style="z-index:1001;position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png" alt="Fork me on GitHub"></a>
<nav class="navbar navbar-inverse" role="navigation" style="background-color:#428BCA;border:0px;border-radius:0px;">
<div class="container">
<div class="navbar-header">
<a type="button" class="btn btn-default navbar-btn" href="index.html">Simulator</a>
</div>
<div class="navbar-header navbar-right">
<a class="navbar-brand" style="color:#FFFFFF">Simple 8-bit Assembler Simulator</a>
</div>
</div>
</nav>
<div class="container">
<h4>Introduction</h4>
<p>This simulator provides a simplified assembler syntax (based on <a href="http://www.nasm.us" target="_blank">NASM</a>) and is simulating a x86 like cpu. In depth documentation and introduction to assembler can be found on the following websites:</p>
<ul>
<li><a href="http://en.wikipedia.org/wiki/Assembly_language" target="_blank">Assembly - Wikipedia</a></li>
<li><a href="http://cs.smith.edu/~thiebaut/ArtOfAssembly/artofasm.html" target="_blank">The Art of Assembly Language Programming</a></li>
<li><a href="http://www.nasm.us/xdoc/2.10.09/html/nasmdoc3.html" target="_blank">NASM Language Documentation</a></li>
</ul>
<p>The simulator consists of a 8-bit cpu and 256 bytes of memory. All instructions (code) and variables (data) needs to fit inside the memory. For simplicity every instruction (and operand) is 1 byte. Therefore a MOV instruction will use 3 bytes of memory. The simulator provides a console output which is memory mapped from 0xE8 to 0xFF. Memory mapped means that every value written to this memory block is visible on the console.</p>
<h4>Syntax</h4>
<p>The syntax is similar as most assemblers are using. Every instruction must be on their own line. Labels are optional and must either start with a letter or a dot (.) and end with a colon.</p>
<pre>label: instruction operands ; Comment</pre>
<p>Valid number formats for constants are:</p>
<pre>
Decimal: 200
Decimal: 200d
Hex: 0xA4
Octal: 0o48
Binary: 101b
</pre>
<p>It is possible to define a number using a character or multiple numbers (see instruction <i>DB</i>) by using a string.</p>
<pre>
Character: 'A'
String: "Hello World!"
</pre>
<p>Operands can either be one of the four general purpose registers, stack pointer register, a memory address or a constant.
Stack pointer register can only be used as operand in MOV, ADD, SUB, CMP, INC and DEC instructions.
Instead of defining an address as a constant or by using a register you can use labels. The assembler will then replace the label with the corresponding constant.</p>
<pre>
General purpose (GP) register: A, B, C, D
Stack pointer register: SP
Address using a GP register: [A]
Address using a GP register and offset: [D-3]
Address using SP register and offset: [SP+2]
Address using a constant: [100]
Address using a label: label
Constant: Any number between 0..255 (8bit unsigned)
Offset for indirect addressing: Integer between -16..+15 (sign is mandatory)
</pre>
<h4>MOV - Copy a value</h4>
<p>Copies a value from <i>src</i> to <i>dest</i>. The MOV instruction is the only one able to directly modify the memory. SP can be used as operand with MOV.</p>
<pre>
MOV reg, reg
MOV reg, address
MOV reg, constant
MOV address, reg
MOV address, constant
</pre>
<h4>DB - Variable</h4>
<p>Defines a variable. A variable can either be a single number, character or a string.</p>
<pre>
DB constant
</pre>
<h4>Math operations</h4>
<b>Addition and Subtraction</b>
<p>Adds two numbers together or subtract one number form another. This operations will modify the carry and zero flag. SP can be used as operand with ADD and SUB.</p>
<pre>
ADD reg, reg
ADD reg, address
ADD reg, constant
SUB reg, reg
SUB reg, address
SUB reg, constant
</pre>
<b>Increment and Decrement</b>
<p>Increments or decrements a register by one. This operations will modify the carry and zero flag. SP can be used as operand with INC and DEC.</p>
<pre>
INC reg
DEC reg
</pre>
<b>Multiplication and division</b>
<p>Multiplies or divides the <i>A</i> register with the given value. This operations will modify the carry and zero flag.</p>
<pre>
MUL reg
MUL address
MUL constant
DIV reg
DIV address
DIV constant
</pre>
<b>Logical instructions</b>
<p>The following logical instructions are supported: AND, OR, XOR, NOT. This operations will modify the carry and zero flag.</p>
<pre>
AND reg, reg
AND reg, address
AND reg, constant
OR reg, reg
OR reg, address
OR reg, constant
XOR reg, reg
XOR reg, address
XOR reg, constant
NOT reg
</pre>
<b>Shift instructions</b>
<p>The following shift instructions are supported: SHL/SAL and SHR/SAR. As this simulator only supports unsigned numbers SHR and SAR yield the same result. This operations will modify the carry and zero flag.</p>
<pre>
SHL reg, reg
SHL reg, address
SHL reg, constant
SHR reg, reg
SHR reg, address
SHR reg, constant
</pre>
<h4>CMP - Compare</h4>
<p>Compares two values and sets the zero flag to true if they are equal. SP can be used as operand with CMP. Use this instruction before a conditional jump.</p>
<pre>
CMP reg, reg
CMP reg, address
CMP reg, constant
</pre>
<h4>Jumps</h4>
<b>JMP - Unconditional jump</b>
<p>Let the instruction pointer do a unconditional jump to the defined address.</p>
<pre>
JMP address
</pre>
<b>Conditional jumps</b>
<p>Let the instruction pointer do a conditional jump to the defined address. See the table below for the available conditions.</p>
<table class="table table-condensed table-striped">
<thead>
<tr>
<th>Instruction</th>
<th>Description</th>
<th>Condition</th>
<th>Alternatives</th>
</tr>
</thead>
<tbody>
<tr>
<td>JC</td>
<td>Jump if carry</td>
<td>Carry = TRUE</td>
<td>JB, JNAE</td>
</tr>
<tr>
<td>JNC</td>
<td>Jump if no carry</td>
<td>Carry = FALSE</td>
<td>JNB, JAE</td>
</tr>
<tr>
<td>JZ</td>
<td>Jump if zero</td>
<td>Zero = TRUE</td>
<td>JB, JE</td>
</tr>
<tr>
<td>JNZ</td>
<td>Jump if no zero</td>
<td>Zero = FALSE</td>
<td>JNE</td>
</tr>
<tr>
<td>JA</td>
<td>></td>
<td>Carry = FALSE && Zero = FALSE</td>
<td>JNBE</td>
</tr>
<tr>
<td>JNBE</td>
<td>not <=</td>
<td>Carry = FALSE && Zero = FALSE</td>
<td>JA</td>
</tr>
<tr>
<td>JAE</td>
<td>>=</td>
<td>Carry = FALSE</td>
<td>JNC, JNB</td>
</tr>
<tr>
<td>JNB</td>
<td>not <</td>
<td>Carry = FALSE</td>
<td>JNC, JAE</td>
</tr>
<tr>
<td>JB</td>
<td><</td>
<td>Carry = TRUE</td>
<td>JC, JNAE</td>
</tr>
<tr>
<td>JNAE</td>
<td>not >=</td>
<td>Carry = TRUE</td>
<td>JC, JB</td>
</tr>
<tr>
<td>JBE</td>
<td><=</td>
<td>C = TRUE or Z = TRUE</td>
<td>JNA</td>
</tr>
<tr>
<td>JNA</td>
<td>not ></td>
<td>C = TRUE or Z = TRUE</td>
<td>JBE</td>
</tr>
<tr>
<td>JE</td>
<td>=</td>
<td>Z = TRUE</td>
<td>JZ</td>
</tr>
<tr>
<td>JNE</td>
<td>!=</td>
<td>Z = FALSE</td>
<td>JNZ</td>
</tr>
</tbody>
</table>
<b>CALL - Function call</b>
<p>Call can be used to jump into a subroutine (function). Pushes the instruction address of the next instruction to the stack and jumps to the specified address.</p>
<pre>
CALL address
</pre>
<b>RET - Exit a subroutine</b>
<p>Exits a subroutines by popping the return address previously pushed by the CALL instruction. Make sure the SP is balanced before calling RET otherwise the instruction pointer will have an ambiguous value.</p>
<pre>
RET
</pre>
<h4>Stack instructions</h4>
<b>PUSH - Push to stack</b>
<p>Pushes a value to the stack. The stack grows down and the current position is available in the stack pointer register (SP). This instruction will decrease the SP.</p>
<pre>
PUSH reg
PUSH address
PUSH constant
</pre>
<b>POP - Pop from stack</b>
<p>Pops a value from the stack to a register. This instruction will increase the SP.</p>
<pre>
POP reg
</pre>
<h4>Other instructions</h4>
<b>HLT - Stops the processor.</b>
<p>Stops operation of the processor. Hit Reset button to reset IP before restarting.</p>
<pre>
HLT
</pre>
<hr style="margin-bottom:10px;"/>
<p><small>by Marco Schweighauser (2015) | MIT License | <a href="https://www.mschweighauser.com/make-your-own-assembler-simulator-in-javascript-part1/" target="_blank">Blog</a></small></p>
</div>
</body>
</html>