forked from schuylerdaddy/Assembler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssembler.cpp
More file actions
327 lines (301 loc) · 11.3 KB
/
Assembler.cpp
File metadata and controls
327 lines (301 loc) · 11.3 KB
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
//
// Assembler.cpp
// Assembler
//
// Created by GregMac on 10/29/14.
// Copyright (c) 2014 Mac. All rights reserved.
//
#include "Assembler.h"
void Assembler::initInstructions()
{
instructions["TRP"] = TRP;
instructions["JMP"] = JMP;
instructions["JMR"] = JMR;
instructions["BNZ"] = BNZ;
instructions["BGT"] = BGT;
instructions["BLT"] = BLT;
instructions["BRZ"] = BRZ;
instructions["MOV"] = MOV;
instructions["LDA"] = LDA;
instructions["STR"] = STR;
instructions["LDR"] = LDR;
instructions["STB"] = STB;
instructions["LDB"] = LDB;
instructions["ADD"] = ADD;
instructions["ADI"] = ADI;
instructions["SUB"] = SUB;
instructions["MUL"] = MUL;
instructions["DIV"] = DIV;
instructions["AND"] = AND;
instructions["OR"] = OR;
instructions["CMP"] = CMP;
instructions["STRI"] = STRI;
instructions["LDRI"] = LDRI;
instructions["STBI"] = STBI;
instructions["LDBI"] = LDBI;
instructions["RUN"] = RUN;
instructions["END"]= END;
instructions["BLK"] = BLK;
instructions["LCK"] =LCK;
instructions["ULK"] = ULK;
}
char* Assembler::AssembleCode(string fileName, int size)
{
mem = new char[size];
//open and test file
ifstream inFile(fileName, ifstream::in);
if(!inFile)
throw runtime_error("Error in opening file: " + string(fileName));
int PC = 0;
int memctr = 0;
int instructionCount =0;
//initialize the instructions to values, built in the code statically
initInstructions();
//process first pass
string line = "";
int lineCtr = 0;
string previousDirective = "";
string previousDirectiveType = "";
while (getline(inFile, line))
{
++lineCtr;
++lineCtr;
int linetype = TokenUtil::validateLineType(line);
//if not directive ensure validity of code, increment memctrt
if (linetype == invalid)
{
throw runtime_error("Invalid syntax ln" + to_string(lineCtr) + ": " + line);
}
else if (linetype == instruction)
{
++instructionCount;
InstructionLine il = TokenUtil::instructionLine(line);
//check for label on instruction line
if(il.label != "")
{
if (tokenAddresses.find(il.label) != tokenAddresses.end())
{
throw runtime_error("Multiple definition ln" + to_string(lineCtr) + ": label '" + il.label + "'");
}
else
{
tokenAddresses[il.label] = instructionCount*12;
gotoLabels.insert(il.label);
}
}
}
else if (linetype == directive)
{
DirectiveLine directive = TokenUtil::directiveLine(line);
//empty labels should mean an array
if (directive.label == "")
{
if(previousDirective == "") //thorw an exception if no array started
throw runtime_error("Unlabeled data unbindable to a label: " + to_string(lineCtr));
else //process as an array member bound to last declared directive
{
if(previousDirectiveType == "")
previousDirectiveType = directive.type;
else if(previousDirectiveType != directive.type) //change in data in bound array, throw err
throw runtime_error("Change in previously bound array type from " +previousDirectiveType+" to " + directive.type +": line " + to_string(lineCtr));
}
}
else if (tokenAddresses.find(directive.label) != tokenAddresses.end())
{
throw runtime_error("Multiple definition ln" + to_string(lineCtr) + ": label '" + directive.label + "'");
}
else
{
previousDirective = directive.label;
previousDirectiveType = directive.type;
}
if (directive.type == ".INT")
{
memcpy(mem + memctr, directive.value.bytes, 4);
tokenAddresses[directive.label] = memctr;
memctr += 4;
}
else if (directive.type == ".BYT")
{
mem[memctr] = directive.value.byte;
tokenAddresses[directive.label] = memctr;
++memctr;
}
else
{
throw runtime_error("Unsupported datatype ln-" + to_string(lineCtr) + ": " + directive.type);
}
}
}
//move all memory behind what will be instruction space and it in all the addresses of mapped tokens
memcpy(mem + instructionCount*12, mem, memctr);
for (map<string,int>::iterator itr = tokenAddresses.begin(); itr!=tokenAddresses.end(); ++itr)
{
if(gotoLabels.find(itr->first) == gotoLabels.end())
itr->second += instructionCount*12;
}
//offset final memory Segment with padding and push it back for instruction space
memctr += 3 + (instructionCount*12);
//implement second pass
inFile.clear();
inFile.seekg(0, ios::beg);
lineCtr = 0;
while (getline(inFile, line))
{
++lineCtr;
InstructionLine instr = TokenUtil::instructionLine(line);
if (instr.instruction != "")
{
if (instructions.find(instr.instruction) != instructions.end())
{
int val = instructions[instr.instruction];
memcpy(mem + PC, reinterpret_cast<char*>(&val), 4);
PC += 4;
//add second arg
if (instr.source.mode == reg)
{
int offset = stoi(instr.source.value);
memcpy(mem + PC, &offset, 4);
PC += 4;
}
else if (instr.source.mode == immediate)
{
int val = stoi(instr.source.value);
memcpy(mem + PC, reinterpret_cast<char*>(&val), 4);
PC += 4;
}
else if (instr.source.mode == indirect)
{
if (tokenAddresses.find(instr.source.value) != tokenAddresses.end())
{
tokenAddresses[instr.source.value] = memctr;
}
int num = stoi(instr.source.value);
memcpy(mem + memctr, &num, 4);
memctr += 4;
PC += 4;
}
else if (instr.source.mode == label)
{
//validate symbol
if (tokenAddresses.find(instr.source.value) != tokenAddresses.end())
{
int addr = tokenAddresses[instr.source.value];
memcpy(mem + PC, reinterpret_cast<char*>(&addr), 4);
}
else
{
throw runtime_error("Encountered unkown label on line" + to_string(lineCtr) + ": " + instr.source.value);
}
PC += 4;
}
else
{
throw runtime_error("Invalid source argument on line " + to_string(lineCtr) + ": " + instr.source.value);
}
//add third arg, source
if (instr.destination.mode != none)
{
if (instr.destination.mode == reg)
{
int offset = stoi(instr.destination.value);
memcpy(mem + PC, &offset, 4);
PC += 4;
}
else if (instr.destination.mode == immediate)
{
int val = stoi(instr.destination.value);
memcpy(mem + PC, reinterpret_cast<char*>(&val), 4);
PC += 4;
}
else if (instr.destination.mode == indirect)
{
if (tokenAddresses.find(instr.destination.value) != tokenAddresses.end())
{
tokenAddresses[instr.destination.value] = memctr;
}
int num = stoi(instr.destination.value);
memcpy(mem + memctr, &num, 4);
memctr += 4;
PC += 4;
}
else if (instr.destination.mode == label)
{
//validate symbol
if (tokenAddresses.find(instr.destination.value) != tokenAddresses.end())
{
RawData data = RawData();
data.num = tokenAddresses[instr.destination.value];
memcpy(mem + PC, data.bytes, 4);
}
else
{
throw runtime_error("Encountered unkown label on line " + to_string(lineCtr) + ": " + instr.destination.value);
}
PC += 4;
}
else
{
throw runtime_error("Invalid source argument on line " + to_string(lineCtr) + ": " + instr.destination.value);
}
}
else{
PC += 4;
}
}
else
{
string err = "Unknown instruction on line " + std::to_string(lineCtr) + ": " + line;
throw runtime_error(err);
}
//codeDump(PC-12);
}
}
//assign end of code and data segment as heap start
heapStart = memctr;
return mem;
}
void Assembler::MakePC_InstructionTable(string inF, string outFile)
{
//open and test file
map<int,string> instructions;
map<int,string> data;
ifstream inFile(inF, ifstream::in);
if(!inFile)
throw runtime_error("Error in opening file: " + string(inF));
int memctr = 6;
int instructionCount =0;
//initialize the instructions to values, built in the code statically
initInstructions();
//process first pass
string line = "";
while (getline(inFile, line))
{
int linetype = TokenUtil::validateLineType(line);
if (linetype == instruction)
{
instructions[instructionCount] = line;
++instructionCount;
}
else if (linetype == directive)
{
data[memctr] = line;
if(regex_match(line,std::regex(".*\\.BYT.*")))
memctr++;
else if(regex_match(line,std::regex(".*\\.INT.*")))
memctr += 4;
}
}
ofstream ot(outFile, ofstream::out);
ot<<"INSTRUCTIONS: "<<endl;
for (map<int,string>::iterator itr = instructions.begin(); itr!=instructions.end(); ++itr)
{
ot << itr->first * 12 << ": " << itr->second << endl;
}
ot<<"\nDATA: "<<endl;
for (map<int,string>::iterator itr = data.begin(); itr!=data.end(); ++itr)
{
ot << itr->first + ( instructionCount * 12) << ": " << itr->second << endl;
}
ot.close();
}