-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasm_header.js
231 lines (215 loc) · 6.08 KB
/
asm_header.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
if (typeof require !== 'undefined')
parser = require('./asm_parser.js');
function removeSpaces(str) {
while (true) {
var next = str.replace(' ', '');
if (next == str)
return str;
str = next;
}
}
function bin2dec(str) {
var result = 0;
for (var i = 0; i < str.length; i++) {
result *= 2;
result += str.charAt(i) == '1';
}
return result;
}
// Represents an encoded instruction of 1-3 bytes in size.
function Frame(size) {
this.size = size;
this.bytes = [];
for (var i = 0; i < size; i++)
this.bytes.push(0);
};
function Bits(n, size, offset) {
this.n = n;
this.size = size;
this.offset = offset;
}
Bits.prototype.toByte = function() {
return this.n << this.offset;
};
Bits.prototype.addToFrame = function(frame) {
frame.bytes[0] |= this.toByte();
};
function Immediate(n, size) {
this.n = n;
this.size = size;
};
Immediate.prototype.addToFrame = function(frame) {
var lb = this.n & 0xff;
frame.bytes[1] = lb;
if (this.size == 16) {
var hb = (this.n >> 8) & 0xff;
frame.bytes[2] = hb;
}
};
function Arg(name) {
this.name = name;
}
Arg.prototype.encodeRegMem = function(op, offset) {
var regs = ['b', 'c', 'd', 'e', 'h', 'l', 'm', 'a'];
var i = regs.indexOf(op);
if (i == -1)
return null;
return new Bits(i, 3, offset);
};
Arg.prototype.encodeRegPair = function(op) {
if (op == 'af')
op = 'sp';
var rps = ['bc', 'de', 'hl', 'sp'];
var i = rps.indexOf(op);
if (i == -1)
return null;
return new Bits(i, 2, 4);
};
Arg.prototype.encodeImmediate = function(op, size) {
return new Immediate(op, size);
};
Arg.prototype.encodeCondition = function(op) {
var conditions = ['nz', 'z', 'nc', 'c', 'po', 'pe', 'p', 'm'];
var i = conditions.indexOf(op.toLowerCase());
if (i < 0)
return null;
return new Bits(i, 3, 3);
};
Arg.prototype.encode = function(op) {
switch (this.name) {
// Registers / [hl]
case 'DDD':
case 'SSS':
var offset = this.name == 'DDD' ? 3 : 0;
return this.encodeRegMem(op, offset);
case 'RP':
case 'bc': // Constrained version of RP.
case 'de': // Constrained version of RP.
return this.encodeRegPair(op);
case 'arg16':
case 'addr':
return this.encodeImmediate(op, 16);
case 'db':
return this.encodeImmediate(op, 8);
case 'CCC':
return this.encodeCondition(op);
default:
return null;
}
};
function Instruction(name, bitTemplate, mnemonicTemplate, size) {
this.name = name;
this.bitTemplate = removeSpaces(bitTemplate);
this.mnemonicTemplate = mnemonicTemplate;
this.size = size;
this.args = this.parseMnemonicTemplate(mnemonicTemplate);
};
Instruction.prototype.parseMnemonicTemplate = function(template) {
var i = template.indexOf(' ');
if (i == -1)
return [];
var argListString = template.substring(i + 1);
var args = argListString.replace(' ', '').split(',');
return args.map(function(argStr, i) { return new Arg(argStr); });
};
Instruction.prototype.matches = function(asmStatement) {
return asmStatement.name == this.name;
};
Instruction.prototype.getTemplateBits = function() {
};
Instruction.prototype.encode = function(asmStatement) {
// bin2dec treats non-1 characters as zero.
var base = bin2dec(this.bitTemplate);
var frame = new Frame(this.size);
frame.bytes[0] = base;
if (this.args.length != asmStatement.ops.length)
throw this.name + " xpected " + this.args.length + " args, got " + asmStatement.ops.length;
this.args.forEach(function(arg, i) {
var encodedArg = arg.encode(asmStatement.ops[i]);
if (!encodedArg)
throw this.name + " couldn't encode arg " + i + " - " + asmStatement.ops[i];
arg.encode(asmStatement.ops[i]).addToFrame(frame);
});
return frame.bytes;
};
var mov = new Instruction('MOV', '01 DDD SSS', 'MOV DDD, SSS', 1);
var dad = new Instruction('DAD', '00 RP1 001', 'DAD RP', 1);
var mvi = new Instruction('MVI', '00 DDD 110', 'MVI DDD, db', 2);
asmStatement = {type: 'i', name: 'MOV', ops: ['a', 'b']};
asmStatement = {type: 'i', name: 'DAD', ops: ['hl']};
asmStatement = {type: 'i', name: 'MVI', ops: ['b', 7]};
// console.log(mov.encode(asmStatement));
// console.log(mvi.encode(asmStatement));
function i(name, template, disas, size) {
return new Instruction(name, template, disas, size);
}
///var instructionList;
function lookupInstruction(name) {
name = name.toUpperCase();
for (var i = 0; i < instructionList.length; i++) {
var ins = instructionList[i];
if (ins.name == name)
return ins;
}
return null;
}
function Assembler(parsed) {
this.parsed = JSON.parse(JSON.stringify(parsed));
this.bytes = [];
this.labelPositions = {};
};
Assembler.prototype.locateLabels = function() {
var pc = 0;
for (var i = 0; i < this.parsed.length; i++) {
var p = this.parsed[i];
if (p.type == 'label') {
this.labelPositions[p.name] = pc;
} else {
var inst = lookupInstruction(p.name);
if (!inst)
console.log("unknown instruction: " + p.name);
else
pc += inst.size;
}
}
};
Assembler.prototype.replaceLabelsWithPositions = function(ops) {
for (var i = 0; i < ops.length; i++) {
if (typeof ops[i] == 'string') {
if (this.labelPositions[ops[i]])
ops[i] = this.labelPositions[ops[i]];
}
}
};
Assembler.prototype.encodeInstructions = function() {
for (var i = 0; i < this.parsed.length; i++) {
var p = this.parsed[i];
if (p.type == 'i') {
this.replaceLabelsWithPositions(p.ops);
this.handleInstruction(p);
}
}
};
Assembler.prototype.assemble = function() {
this.locateLabels();
this.encodeInstructions();
return this.bytes;
};
Assembler.prototype.handleInstruction = function(p) {
var i = lookupInstruction(p.name);
if (!i)
throw "Unknown instruction: " + p.name;
this.addBytes(i.encode(p));
};
Assembler.prototype.addBytes = function(bs) {
for (var i = 0; i < bs.length; i++)
this.bytes.push(bs[i]);
};
(function() {
var program = 'mov a, b';
console.log('assembler.js test');
console.log('assembling program "' + program + '"');
var a = new Assembler(parser.parse('mov a, b'));
var bytes = a.assemble();
console.log(bytes);
})();