-
Notifications
You must be signed in to change notification settings - Fork 3
/
assem.html
522 lines (500 loc) · 15.6 KB
/
assem.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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
<!DOCTYPE html>
<html>
<head>
<title>
Assembler by Mark Craig (youtube.com/MrMcSoftware) Copyright © 2023
</title>
<style>
body
{
background-color: black;
color: white;
/* Uncomment transition line for gradual dark/light mode change */
/* transition: color 300ms, background-color 300ms; */
}
body.dark-mode
{
background-color: black;
color: white;
}
body.dark-mode a:link { color: #f0f000; }
body.dark-mode a:visited { color: #00ff00; }
body.dark-mode code { background-color: #404040; }
body.light-mode
{
background-color: #d0d0d0;
color: black;
}
body.light-mode a:link { color: #0000f0; }
body.light-mode a:visited { color: #a000a0; }
body.light-mode code { background-color: #ffffff; }
/* tab stops of 8 are a bit much for my tastes, adjust to your prefs. */
textarea, pre { -moz-tab-size: 4; -o-tab-size: 4; tab-size: 4; }
.modal
{
display: none;
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0,0.4);
}
.modal-content
{
position: relative;
background-color: #171717;
margin: auto;
padding: 0;
border: 2px solid #6797ff;
width: 40%;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
}
.close
{
color: white;
float: right;
font-size: 18px;
font-weight: bold;
}
.close:hover,
.close:focus
{
color: #ff4040;
text-decoration: none;
cursor: pointer;
}
.modal-header
{
padding: 2px 16px;
background-color: #1022e4;
color: white;
font-size: 14px;
font-weight: bold;
}
.modal-body { padding: 2px 16px; color: white; font-size: 14px; }
</style>
<body>
<div style="float:left">
<textarea id="assemID" rows="34" cols="82" value="" style="background-color:#000050; color:#fff;" spellcheck="false" wrap="off" onkeydown='assemInput(event)'></textarea> <br>
<button onclick="runAssem()">Assemble</button>
<button onclick="saveAssem()">Save As: </button>
<input id="saveNameID" type="text" value="" maxlength="32" size=8 title="Filename to use for saving your assembly source code" style="background-color:#d0d000; color:#000;"/>
<input id="assemFileID" name="files" type="file"/>
</div>
<div style="float:left">
<textarea id="assemConsoleID" rows="34" cols="42" value="" style="background-color:#000050; color:#fff;" readonly></textarea> <br>
<select id="assemVerboseID" onchange="setAssemVerbose()" title="Set how verbose level of assembly errors, warnings, information, and statistics">
<option value="0">Just error messages</option>
<option value="1" selected>Also general messages</option>
<option value="2">Also parsed strings</option>
<option value="3">Also final machine code</option>
</select>
</div>
<div>
<button onclick="clearAssem()" title="Clear/Erase everything in the assembly source edit window" style="width:180px">Erase Assembly Source</button><br><br>
<button onclick="saveBin()" title="Export/Save assembled machine code to a Logisim RAM file (which also can be loaded into this simulator)" style="width:180px">Save Machine Code As:</button>
<input id="saveExportNameID" type="text" value="" maxlength="32" size=8 title="Filename to use for saving your assembled machine code" style="background-color:#e0c000; color:#000;"/><br><br>
</div>
<script>
//const RAMSIZE=65536;
//const RAMSIZE=16777216;
const RAMSIZE=8388608;
const assem=document.getElementById("assemID");
const acon=document.getElementById("assemConsoleID");
var RAM=new Array(RAMSIZE);
var pos=0,len,pass=0;
var equs={};
var labels={};
var addr=0,out=1,out2=0,v,dnl=1,verba=1;
var sourceName="untitled.asm";
function setAssemVerbose()
{
verba=parseInt(document.getElementById("assemVerboseID").value);
}
// won't work in some browsers
function assemInput(event)
{
if (event.keyCode===9)
{
var startPos=assem.selectionStart;
var endPos=assem.selectionEnd;
assem.value=assem.value.substring(0,startPos)+"\t"+assem.value.substring(endPos);
assem.selectionStart=assem.selectionEnd=startPos+1;
if (event.preventDefault) { event.preventDefault(); }
else { event.returnValue=false; }
return(false);
}
}
function saveAssem()
{
var name=document.getElementById("saveNameID").value;
if (name!=="") { sourceName=name; }
blob=new Blob([assem.value],{ type:'text/plain'});
const blobUrl=window.URL.createObjectURL(blob);
var link=window.document.createElement("a");
link.href=blobUrl;
link.download=sourceName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
// despite what someone said, don't revoke
//window.URL.revokeObjectURL(blobUrl);
}
function saveBin()
{
var name=document.getElementById("saveExportNameID").value;
if (name!=="") { binName=name; }
var machinecode="v2.0 raw\n";
for (i=0;i<addr;i++)
{
var opcode=(RAM[i]&0x000000ff).toString(16);
opcode="0".repeat((n=2-opcode.length)>0?n:0)+opcode;
machinecode+=(RAM[i]>>>8).toString(16)+opcode+" ";
if (((i+1)%8)==0) { machinecode+="\n"; }
}
blob=new Blob([machinecode],{ type:'text/plain'});
const blobUrl=window.URL.createObjectURL(blob);
var link=window.document.createElement("a");
link.href=blobUrl;
link.download=binName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
// despite what someone said, don't revoke
//window.URL.revokeObjectURL(blobUrl);
}
function clearAssem()
{
assem.value="";
}
function INST1(x)
{
var str=getStr();
val=lookupVal(str);
RAM[addr]=(val<<8)|x;
if ((out2)&&(pass===2))
{
var opcode=(RAM[addr]&0x000000ff).toString(16);
opcode="0".repeat((n=2-opcode.length)>0?n:0)+opcode;
acon.value+="** "+(RAM[addr]>>>8).toString(16)+opcode+"\n";
}
addr++;
}
function INST2(x)
{
RAM[addr]=x;
if ((out2)&&(pass===2))
{
var opcode=(RAM[addr]&0x000000ff).toString(16);
opcode="0".repeat((n=2-opcode.length)>0?n:0)+opcode;
acon.value+="** "+(RAM[addr]>>>8).toString(16)+opcode+"\n";
}
addr++;
}
function isAlpha(c)
{
return(((c>='a')&&(c<='z'))||((c>='A')&&(c<='Z')));
}
function lookupVal(str)
{
var val,valf;
var c=str.charAt(0);
if (isAlpha(c))
{
var l=equs[str];
if (typeof(l)!=='undefined') { val=l; return(val); }
var l=labels[str];
if (typeof(l)!=='undefined') { val=l; return(val); }
if (pass===2) { acon.value+=" **** ERROR: "+str+" not found\n"; return(-1); }
return(-1);
}
else if (c==='\'') { val=str.charCodeAt(1); }
else if (c==='$') { val=parseInt(str.substring(1),16); }
else if (c==='~') { valf=parseFloat(str.substring(1)); val=FloatToIEEE(valf); }
else if (c==='%') { val=parseInt(str.substring(1),2); }
else { val=parseInt(str); }
return(val);
}
function printLabels()
{
acon.value+="Labels:\n";
var keys=Object.keys(labels);
for (i=0;i<keys.length;i++) { acon.value+=" "+(keys[i]+" "+labels[keys[i]].toString(16)+" ("+labels[keys[i]].toString()+")\n"); }
}
function runAssem()
{
acon.value="";
equs={};
labels={};
astr=assem.value;
//console.log(astr);
len=astr.length;
//console.log(len);
// Two passes to get addresses of labels that are used before they are defined
addr=0; pos=0; pass=1;
while (parse()) { }
addr=0; pos=0; pass=2;
while (parse()) { }
if (verba>=1) { acon.value+="\nAddress space: "+addr+"\n"; printLabels(); }
if (verba===3)
{
acon.value+='\nMachine Code ("v2.0 raw" if Logisim compatibility desired):\n\n';
acon.value+="v2.0 raw\n";
for (i=0;i<addr;i++)
{
var opcode=(RAM[i]&0x000000ff).toString(16);
opcode="0".repeat((n=2-opcode.length)>0?n:0)+opcode;
acon.value+=(RAM[i]>>>8).toString(16)+opcode+" ";
}
}
}
function outputTTY(v,aReg)
{
if (aReg) { RAM[addr]=(v<<8)|0x16; RAM[addr+1]=0x8000001f; }
else { RAM[addr]=(v<<8); RAM[addr+1]=0x80000002; }
}
function scanStringForEither(str1,str2)
{
var var1;
while (1)
{
var1=getStr();
if (var1===str1) return(0);
else if (var1===str2) return(1);
else if (pos>=len) return(-1);
else if (var1.charAt(0)==='#') { while (((astr.charAt(pos))!=='\n')&&(pos<len)) { pos++; } }
}
}
function scanNested(str1,str2)
{
var c,i=1;
while (i>0)
{
c=scanStringForEither(str1,str2);
if (c===0) i--;
else if (c===1) i++;
else break;
}
}
function getStr()
{
while (((astr.charAt(pos)===" ")||(astr.charAt(pos)==="\n")||(astr.charAt(pos)==='\t'))&&(pos<len)) { pos++; }
var ns=pos;
while ((astr.charAt(pos)!==' ')&&(astr.charAt(pos)!=='\n')&&(astr.charAt(pos)!=='\t')&&(pos<len)) { pos++; }
var str=astr.substring(ns,pos);
//if (astr.charCodeAt(pos)===32) { pos++; }
//if (astr.charCodeAt(pos)===13) { pos++; }
//if (astr.charCodeAt(pos)===10) { pos++; }
return(str);
}
function parse()
{
var val,i,nl,eos,inline0,areg;
var str;
str=getStr();
//console.log("parse: "+str);
if ((verba>=2)&&(pass===2)) { acon.value+="> "+str+"\n"; }
if (str.charAt(0)==='#') { while (((astr.charAt(pos))!=='\n')&&(pos<len)) { pos++; } }
var strU=str.toUpperCase();
if (strU==="EQU")
{
var name=getStr();
var value=lookupVal(getStr());
equs[name]=value;
}
else if (strU===":")
{
var name=getStr();
labels[name]=addr;
if ((verba>=2)&&(pass===2)) { acon.value+="Label: '"+name+"' "+addr+"\n"; }
}
else if (strU==="LOADI") { INST1(0); }
else if (strU==="LOAD") { INST1(1); }
else if (strU==="STOREI") { INST1(2); }
else if (strU==="STORE") { INST1(3); if ((out)&&(pass===2)) { acon.value+=" **** WARNING: Instruction 'STORE' doesn't work\n"; } }
else if (strU==="ADDI") { INST1(4); }
else if (strU==="ADD") { INST1(5); }
else if (strU==="SUBI") { INST1(6); }
else if (strU==="SUB") { INST1(7); }
else if (strU==="JUMP") { INST1(8); }
else if (strU==="JUMPI") { INST1(9); }
else if (strU==="JUMPII") { INST1(10); if ((out)&&(pass===2)) { acon.value+=" **** WARNING: Instruction 'JUMPII' doesn't work\n"; } }
else if (strU==="DATAIN") { INST2(11); }
else if (strU==="HALT") { INST2(12); }
else if (strU==="SKIPNEG") { INST2(13); }
else if (strU==="STROBE") { INST2(14); }
else if (strU==="NOOP") { INST2(15); }
else if (strU==="MULI") { INST1(16); }
else if (strU==="MUL") { INST1(17); }
else if (strU==="DIVI") { INST1(18); }
else if (strU==="DIV") { INST1(19); }
else if (strU==="MODI") { INST1(20); }
else if (strU==="MOD") { INST1(21); }
else if (strU==="LOADAI") { INST1(22); }
else if (strU==="LOADA") { INST1(23); }
else if (strU==="JUMPEQI") { INST1(24); }
else if (strU==="JUMPGTI") { INST1(25); }
else if (strU==="JUMPLTI") { INST1(26); }
else if (strU==="JUMPNEQI") { INST1(27); }
else if (strU==="JUMPGTEI") { INST1(28); }
else if (strU==="JUMPLTEI") { INST1(29); }
else if (strU==="NEGY") { INST2(30); }
else if (strU==="STOREAI") { INST1(31); }
else if (strU==="LOADAIND") { INST2(32); }
else if (strU==="ANDI") { INST1(33); }
else if (strU==="AND") { INST1(34); }
else if (strU==="ORI") { INST1(35); }
else if (strU==="OR") { INST1(36); }
else if (strU==="NOT") { INST2(37); }
else if (strU==="XORI") { INST1(38); }
else if (strU==="XOR") { INST1(39); }
else if (strU==="JUMPEQ") { INST1(40); }
else if (strU==="JUMPGT") { INST1(41); }
else if (strU==="JUMPLT") { INST1(42); }
else if (strU==="JUMPNEQ") { INST1(43); }
else if (strU==="JUMPGTE") { INST1(44); }
else if (strU==="JUMPLTE") { INST1(45); }
else if (strU==="STOREAIND") { INST2(46); }
else if (strU==="FADDA") { INST2(47); }
else if (strU==="FADD") { INST1(48); }
else if (strU==="FSUBA") { INST2(49); }
else if (strU==="FSUB") { INST1(50); }
else if (strU==="FMULA") { INST2(51); }
else if (strU==="FMUL") { INST1(52); }
else if (strU==="FDIVA") { INST2(53); }
else if (strU==="FDIV") { INST1(54); }
else if (strU==="FMODA") { INST2(55); }
else if (strU==="FMOD") { INST1(56); }
else if (strU==="FNEGY") { INST2(57); }
else if (strU==="FSQRTY") { INST2(58); }
else if (strU==="FSQRT") { INST1(59); }
else if (strU==="FSINY") { INST2(60); }
else if (strU==="FSIN") { INST1(61); }
else if (strU==="FCOSY") { INST2(62); }
else if (strU==="FCOS") { INST1(63); }
else if (strU==="FTANY") { INST2(64); }
else if (strU==="FTAN") { INST1(65); }
else if (strU==="FASINY") { INST2(66); }
else if (strU==="FASIN") { INST1(67); }
else if (strU==="FACOSY") { INST2(68); }
else if (strU==="FACOS") { INST1(69); }
else if (strU==="FATANY") { INST2(70); }
else if (strU==="FATAN") { INST1(71); }
else if (strU==="FATAN2A") { INST2(72); }
else if (strU==="FATAN2") { INST1(73); }
else if (strU==="FPOWA") { INST2(74); }
else if (strU==="FPOW") { INST1(75); }
else if (strU==="FLNY") { INST2(76); }
else if (strU==="FLN") { INST1(77); }
else if (strU==="FLOGY") { INST2(78); }
else if (strU==="FLOG") { INST1(79); }
else if (strU==="FABSY") { INST2(80); }
else if (strU==="FABS") { INST1(81); }
else if (strU==="FEXPY") { INST2(82); }
else if (strU==="FEXP") { INST1(83); }
else if (strU==="FPTOINTY") { INST2(84); }
else if (strU==="FPTOINT") { INST1(85); }
else if (strU==="INTTOFPY") { INST2(86); }
else if (strU==="INTTOFP") { INST1(87); }
else if (strU==="FJUMPEQI") { INST1(88); }
else if (strU==="FJUMPGTI") { INST1(89); }
else if (strU==="FJUMPLTI") { INST1(90); }
else if (strU==="FJUMPNEQI") { INST1(91); }
else if (strU==="FJUMPGTEI") { INST1(92); }
else if (strU==="FJUMPLTEI") { INST1(93); }
else if (strU==="FJUMPEQ") { INST1(94); }
else if (strU==="FJUMPGT") { INST1(95); }
else if (strU==="FJUMPLT") { INST1(96); }
else if (strU==="FJUMPNEQ") { INST1(97); }
else if (strU==="FJUMPGTE") { INST1(98); }
else if (strU==="FJUMPLTE") { INST1(99); }
else if (strU==="CALL") { INST1(100); }
else if (strU==="RETURN") { INST2(101); }
else if (strU==="PUSH") { INST2(102); }
else if (strU==="POP") { INST2(103); }
else if (strU==="SREAD") { INST1(104); }
else if (strU==="FASTINTR") { INST2(253); }
else if (strU==="BREAKPOINTS") { INST1(254); }
else if (strU==="BREAKPOINT") { INST1(255); }
else if (strU==="\*\/") { if (pass===2) { acon.value+=" **** ERROR: \*\/ without \/\*\n"; } }
else if (strU.substring(0,2)==="\/\*") { scanNested("\*\/","\/\*"); }
else if (strU==="DC")
{
str=getStr();
var i,j,dcarr=str.split(",");
for (i=0;i<dcarr.length;i++)
{
var f=dcarr[i].charAt(0);
if (f==='$') { RAM[addr]=parseInt(dcarr[i].substring(1),16); addr++; }
else if (f==='\'') { for (j=1;j<dcarr[i].length-1;j++) { RAM[addr]=dcarr[i].charCodeAt(j); addr++; } }
else { RAM[addr]=lookupVal(dcarr[i]); addr++; }
}
}
else if (strU.substring(0,3)==="DCS")
{
var nl=eos=inline0=areg=0;
for (i=3;i<strU.length;i++)
{
var f=strU.charAt(i);
if (f==='N') { nl=1; }
else if (f==='0') { eos=1; }
else if (f==='I') { inline0=1; }
else if (f==='A') { areg=1; }
else if (pass===2) { acon.value+=" **** ERROR: Unknown Instr.: "+str+"\n"; }
}
pos++;
var p=pos;
while ((astr.charAt(pos)!=='\n')&&(pos<len)) { pos++; }
str=astr.substring(p,pos);
if (inline0)
{
for (i=0;i<str.length;i++) { outputTTY(str.charCodeAt(i),areg); addr+=2; }
if (nl) { if (dnl) { outputTTY(13,areg); addr+=2; } else { outputTTY(10,areg); addr+=2; } }
}
else
{
for (i=0;i<str.length;i++) { RAM[addr]=str.charCodeAt(i); addr++; }
if (nl)
{
if (dnl) { RAM[addr]=13; addr++; } else { RAM[addr+1]=10; addr++; }
} // 13 or 10 works
if (eos) { RAM[addr]=0; addr++; }
}
}
else if (strU==="DS")
{
str=getStr();
val=lookupVal(str);
for (i=0;i<val;i++) { RAM[i+addr]=0; } addr+=val;
}
else if (strU==="OUTPUT") { out2=parseInt(getStr()); }
else if ((strU.charAt(0)!=='#')&&(strU!=="")&&(pass===2)) { acon.value+=" **** ERROR: Unknown Instr.: "+str+"\n"; }
if (pos>=len) { return(0); }
return(1);
}
function FloatToIEEE(f)
{
var buf=new ArrayBuffer(4);
(new Float32Array(buf))[0]=f;
return((new Uint32Array(buf))[0]);
}
document.getElementById("assemFileID").onchange=function(event)
{
if (!window.FileReader) { return; } // Browser is not compatible
var reader=new FileReader();
reader.onload=function(event)
{
if (event.target.readyState!=2) { return; }
if (event.target.error) { alert('Error while reading file'); return; }
assem.value=event.target.result;
};
reader.readAsText(event.target.files[0]);
};
</script>
</body>
</html>