-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathasm.js
398 lines (323 loc) · 8.13 KB
/
asm.js
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* DEPRECATED: use asm.py instead */
/**
* Assembler for LS-8 v4.0
*
* Example code:
*
* INC R0 ; A comment
* Label1:
* DEC R2
* LDI R3,Label1
*
* DS A String that is declared
* DB 0x0a ; a hex byte
* DB 12 ; a decimal byte
* DB 0b0001 ; a binary byte
*/
const fs = require('fs');
const readline = require('readline');
// Process command line
const args = process.argv.slice(2);
let input, output;
if (args.length === 0) {
input = process.stdin;
output = process.stdout.fd;
} else if (args.length === 1) {
input = fs.createReadStream(args[0]);
output = process.stdout.fd;
} else if (args.length == 2) {
input = fs.createReadStream(args[0]);
output = fs.openSync(args[1], 'w');
} else {
console.error("usage: asm infile.asm [outfile.ls8]");
process.exit(1);
}
// Set up the readline interface
const rl = readline.createInterface({
input: input
});
// Set up the symbol table
const sym = {};
// Operands:
const ops = {
"ADD": { type: 2, code: '10100000' },
"AND": { type: 2, code: '10101000' },
"CALL": { type: 1, code: '01010000' },
"CMP": { type: 2, code: '10100111' },
"DEC": { type: 1, code: '01100110' },
"DIV": { type: 2, code: '10100011' },
"HLT": { type: 0, code: '00000001' },
"INC": { type: 1, code: '01100101' },
"INT": { type: 1, code: '01010010' },
"IRET": { type: 0, code: '00010011' },
"JEQ": { type: 1, code: '01010101' },
"JGE": { type: 1, code: '01011010' },
"JGT": { type: 1, code: '01010111' },
"JLE": { type: 1, code: '01011001' },
"JLT": { type: 1, code: '01011000' },
"JMP": { type: 1, code: '01010100' },
"JNE": { type: 1, code: '01010110' },
"LD": { type: 2, code: '10000011' },
"LDI": { type: 8, code: '10000010' },
"MOD": { type: 2, code: '10100100' },
"MUL": { type: 2, code: '10100010' },
"NOP": { type: 0, code: '00000000' },
"NOT": { type: 1, code: '01101001' },
"OR": { type: 2, code: '10101010' },
"POP": { type: 1, code: '01000110' },
"PRA": { type: 1, code: '01001000' },
"PRN": { type: 1, code: '01000111' },
"PUSH": { type: 1, code: '01000101' },
"RET": { type: 0, code: '00010001' },
"SHL": { type: 2, code: '10101100' },
"SHR": { type: 2, code: '10101101' },
"ST": { type: 2, code: '10000100' },
"SUB": { type: 2, code: '10100001' },
"XOR": { type: 2, code: '10101011' },
};
// Type to function mapping
const typeF = {
0: out0,
1: out1,
2: out2,
8: out8,
};
// Set up the machine code output
const code = [];
// Current code address (for labels)
let addr = 0;
// Source line number
let line = 0;
// Regex for matching lines
// Capturing groups: label, opcode, operandA, operandB
const regex = /(?:(\w+?):)?\s*(?:(\w+)\s*(?:(\w+)(?:\s*,\s*(\w+))?)?)?/;
// Regex for capturing DS and DB data
const regexDS = /(?:(\w+?):)?\s*DS\s*(.+)/i;
const regexDB = /(?:(\w+?):)?\s*DB\s*(.+)/i;
/**
* Pass 1
*
* Read the source code lines
* Parse labels, opcodes, and operands
* Record label offsets
* Emit machine code
*/
rl.on('line', (input) => {
line++;
// Strip comments
const commentIndex = input.indexOf(';');
if (commentIndex !== -1) {
input = input.substr(0, commentIndex);
}
// Normalize
input = input.trim();
// Ignore blank lines
if (input === '') {
return;
}
//console.log(input);
const m = input.match(regex);
if (m) {
let [, label, opcode, opA, opB] = m;
label = uppercase(label);
opcode = uppercase(opcode);
opA = uppercase(opA);
opB = uppercase(opB);
//console.log(label, opcode, opA, opB);
// Track label address
if (label) {
sym[label] = addr;
//console.log("Label " + label + ": " + addr);
code.push(`# ${label} (address ${addr}):`);
}
if (opcode !== undefined) {
switch (opcode) {
case 'DS':
handleDS(input);
break;
case 'DB':
handleDB(input);
break;
default:
{
// Check operand count
checkOps(opcode, opA, opB);
// Handle opcodes
const opInfo = ops[opcode];
const handler = typeF[opInfo.type];
handler(opcode, opA, opB, opInfo.code);
}
break;
}
}
} else {
console.log("No match: " + input);
process.exit(3);
}
});
/**
* Pass 2
*
* Output the code, substituting in any symbols
*/
rl.on('close', () => {
// Pass two
// Output result
for (let i = 0; i < code.length; i++) {
let c = code[i];
// Replace symbols
if (c.substr(0,4) == 'sym:') {
let s = c.substr(4).trim();
if (s in sym) {
c = p8(sym[s]);
} else {
console.error('unknown symbol: ' + s);
process.exit(2);
}
}
fs.writeSync(output, c + '\n');
}
});
/**
* Check operands for sanity with a particular opcode
*/
function checkOps(opcode, opA, opB) {
// Makes sure we have right operand count
function checkOpsCount(desired, found) {
if (found < desired) {
console.error(`Line ${line}: missing operand to ${opcode}`);
process.exit(1);
} else if (found > desired) {
console.error(`Line ${line}: unexpected operand to ${opcode}`);
process.exit(1);
}
}
// Make sure we know this opcode at all
if (!(opcode in ops)) {
console.error(`line ${line}: unknown opcode ${opcode}`);
process.exit(2);
}
const type = ops[opcode].type;
const totalOperands = (opA !== undefined? 1: 0) + (opB !== undefined? 1: 0);
if (type === 0 || type === 1 || type === 2) {
// 0, 1, or 2 register operands
checkOpsCount(type, totalOperands);
} else if (type === 8) {
// LDI r,i or LDI r,label
checkOpsCount(2, totalOperands);
}
}
/**
* Get a register number from a string, e.g. "R2" -> 2
*/
function getReg(op, fatal=true) {
const m = op.match(/R([0-7])/);
if (m === null) {
if (fatal) {
console.error(`Line ${line}: unknown register ${op}`);
process.exit(1);
} else {
return null;
}
}
return m[1]|0;
}
/**
* Return a value as an 8-digit binary number
*/
function p8(v) {
return v.toString(2).padStart(8, '0');
}
/**
* Helper function to uppercase a string
*/
function uppercase(s) {
if (s === undefined || s === null) {
return s;
}
return s.toUpperCase();
}
/**
* Handle opcodes with zero operands
*/
function out0(opcode, opA, opB, machineCode) {
code.push(`${machineCode} # ${opcode}`);
addr++;
}
/**
* Handle opcodes with one operand
*/
function out1(opcode, opA, opB, machineCode) {
let regA = getReg(opA);
code.push(`${machineCode} # ${opcode} ${opA}`);
code.push(p8(regA));
addr += 2;
}
/**
* Handle opcodes with two operands
*/
function out2(opcode, opA, opB, machineCode) {
let regA = getReg(opA);
let regB = getReg(opB);
code.push(`${machineCode} # ${opcode} ${opA},${opB}`);
code.push(p8(regA));
code.push(p8(regB));
addr += 3;
}
/**
* Handle LDI opcode (type 8)
*/
function out8(opcode, opA, opB, machineCode) {
let regA = getReg(opA);
let valB = parseInt(opB);
let outB;
if (isNaN(valB)) {
// If it's not a value, it might be a symbol
outB = `sym:${opB}`;
} else {
outB = p8(valB);
}
code.push(`${machineCode} # ${opcode} ${opA},${opB}`);
code.push(p8(regA));
code.push(outB);
addr += 3;
}
/**
* Handle DS pseudo-opcode
*/
function handleDS(input) {
const m = input.match(regexDS);
if (m === null || m[2] === '') {
console.error(`line ${line}: missing argument to DS`);
process.exit(2);
}
const data = m[2];
for (let i = 0; i < data.length; i++) {
let printChar = data.charAt(i);
if (printChar === ' ') {
printChar = '[space]';
}
code.push(`${p8(data.charCodeAt(i))} # ${printChar}`);
}
addr += data.length;
}
/**
* Handle the DB pseudo-opcode
*/
function handleDB(input) {
const m = input.match(regexDB);
if (m === null || m[2] === '') {
console.error(`line ${line}: missing argument to DB`);
process.exit(2);
}
const data = m[2];
let val = parseInt(data);
if (isNaN(val)) {
console.error(`line ${line}: invalid integer argument to DB`);
process.exit(2);
}
// Force to byte size
val &= 0xff;
code.push(`${p8(val)} # ${data}`);
addr += 1;
}