-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
221 lines (193 loc) · 8.05 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>nand2tetris - Hack Machine Language Disassembler</title>
<script src="projects/06/hacklib.js"></script>
<style>
* {
font-family: sans-serif;
}
textarea {
font-family: monospace;
}
.box {
display: inline-block;
vertical-align: top;
}
</style>
</head>
<body>
<h1>Hack Machine Language</h1>
<p>A simple machine language assembler/disassembler for the Hack computer from <a
href="https://www.nand2tetris.org/">nand2tetris</a>.</p>
<h2>Assembler</h2>
<div class="box">
<label for="txtAsmSrc">Program (hack assembly)</label>
<br>
<textarea id="txtAsmSrc" cols="40" rows="20"></textarea>
<br>
<select id="selectSampleAsm">
<option value="">-- Select Sample --</option>
</select>
<br>
<input type='file' id="filePickerAsm" accept=".asm">
</div>
<div class="box">
<label for="txtBinDst">Binary (hack machine code)</label>
<br>
<textarea id="txtBinDst" cols="40" rows="20"></textarea>
<br>
<button id="btnAsm">Assemble</button>
<button id="btnCopyToBin">Copy to Disassembler</button>
</div>
<h2>Disassembler</h2>
<div class="box">
<label for="txtBinSrc">Binary (machine language)</label>
<br>
<textarea id="txtBinSrc" cols="40" rows="20"></textarea>
<br>
<select id="selectsampleBin">
<option value="">-- Select Sample --</option>
</select>
<br>
<input type='file' id="filePickerHack" accept=".hack">
</div>
<div class="box">
<label for="txtAsmDst">Program (disassembled code)</label>
<br>
<textarea id="txtAsmDst" cols="40" rows="20"></textarea>
<br>
<button id="btnDisasm">Disassemble</button>
<button id="btnCopyToAsm">Copy to Assembler</button>
</div>
<script>
window.onload = function () {
// Sample paths
const sampleAsm = [
'notes/extra/add2.asm',
'notes/extra/array_fill.asm',
'notes/extra/ball.asm',
'notes/extra/ball_kbd_control.asm',
'notes/extra/ball_move.asm',
'notes/extra/bit_shift_left.asm',
'notes/extra/Fill_simple.asm',
'notes/extra/mult_neg.asm',
'notes/extra/mult_shift.asm',
'notes/extra/rectangle.asm',
'notes/extra/signum.asm',
'notes/extra/sum.asm',
'notes/extra/sum2.asm',
'notes/extra/swap.asm',
'projects/04/fill/Fill.asm',
'projects/04/mult/mult.asm',
'projects/06/add/Add.asm',
'projects/06/max/Max.asm',
'projects/06/max/MaxL.asm',
'projects/06/pong/Pong.asm',
'projects/06/pong/PongL.asm',
'projects/06/pong/Pong_dis.asm',
'projects/06/rect/Rect.asm',
'projects/06/rect/RectL.asm',
'projects/07/MemoryAccess/BasicTest/BasicTest.asm',
'projects/07/MemoryAccess/PointerTest/PointerTest.asm',
'projects/07/MemoryAccess/StaticTest/StaticTest.asm',
'projects/07/StackArithmetic/SimpleAdd/SimpleAdd.asm',
'projects/07/StackArithmetic/StackTest/StackTest.asm',
'projects/08/FunctionCalls/FibonacciElement/FibonacciElement.asm',
'projects/08/FunctionCalls/NestedCall/NestedCall.asm',
'projects/08/FunctionCalls/SimpleFunction/SimpleFunction.asm',
'projects/08/FunctionCalls/StaticsTest/StaticsTest.asm',
'projects/08/ProgramFlow/BasicLoop/BasicLoop.asm',
'projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries.asm'
];
const sampleBin = [
'projects/04/fill/Fill.hack',
'projects/04/mult/mult.hack',
'projects/05/Add.hack',
'projects/05/Max.hack',
'projects/05/Rect.hack',
'projects/06/add/Add.hack',
'projects/06/add/Add_hand.hack',
'projects/06/max/Max.hack',
'projects/06/max/MaxL_hand.hack',
'projects/06/pong/Pong.hack',
'projects/06/rect/Rect.hack',
'projects/06/rect/RectL_hand.hack'
];
// DOM elements
const selectSampleAsm = document.getElementById('selectSampleAsm');
const selectsampleBin = document.getElementById('selectsampleBin');
const btnAsm = document.getElementById('btnAsm');
const btnCopyToAsm = document.getElementById('btnCopyToAsm');
const btnCopyToBin = document.getElementById('btnCopyToBin');
const btnDisasm = document.getElementById('btnDisasm');
const filePickerAsm = document.getElementById('filePickerAsm');
const filePickerHack = document.getElementById('filePickerHack');
const txtAsmDst = document.getElementById('txtAsmDst');
const txtAsmSrc = document.getElementById('txtAsmSrc');
const txtBinDst = document.getElementById('txtBinDst');
const txtBinSrc = document.getElementById('txtBinSrc');
// Event listeners
btnCopyToAsm.addEventListener('click', function () {
txtAsmSrc.value = txtAsmDst.value;
});
btnCopyToBin.addEventListener('click', function () {
txtBinSrc.value = txtBinDst.value;
});
btnDisasm.addEventListener('click', function () {
txtAsmDst.value = hacklib.disasm(txtBinSrc.value);
});
filePickerHack.addEventListener('change', function (evt) {
if (evt.target.files.length > 0) {
var fr = new FileReader();
fr.onload = function (evt) {
txtBinSrc.value = evt.target.result;
};
fr.readAsText(evt.target.files[0]);
}
});
btnAsm.addEventListener('click', function () {
txtBinDst.value = hacklib.asm(txtAsmSrc.value);
});
filePickerAsm.addEventListener('change', function (evt) {
if (evt.target.files.length > 0) {
var fr = new FileReader();
fr.onload = function (evt) {
txtAsmSrc.value = evt.target.result;
};
fr.readAsText(evt.target.files[0]);
}
});
selectSampleAsm.addEventListener('change', function (evt) {
if (evt.target.value) {
fetch(evt.target.value)
.then(response => response.text())
.then(data => txtAsmSrc.value = data);
}
});
selectsampleBin.addEventListener('change', function (evt) {
if (evt.target.value) {
fetch(evt.target.value)
.then(response => response.text())
.then(data => txtBinSrc.value = data);
}
});
// Build select options
for (let i = 0; i < sampleAsm.length; i++) {
const opt = document.createElement('option');
opt.value = sampleAsm[i];
opt.text = sampleAsm[i].split('/').slice(-1)[0];
selectSampleAsm.add(opt);
}
for (let i = 0; i < sampleBin.length; i++) {
const opt = document.createElement('option');
opt.value = sampleBin[i];
opt.text = sampleBin[i].split('/').slice(-1)[0];
selectsampleBin.add(opt);
}
};
</script>
</body>
</html>