-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdlx_assembler.cpp
581 lines (501 loc) · 18.7 KB
/
dlx_assembler.cpp
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*
* Author: Ehiremen Ekore
* Started: 03/2019
*/
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <bits/stdc++.h>
#include <vector>
#include "tempMap.h"
#define OPCODE_LENGTH 6
#define FUNC_LENGTH 11
#define IMM_LENGTH 16
#define OFFSET_LENGTH 26
// Returns '0' for '1' and '1' for '0'
char flip(char c) {return (c == '0')? '1': '0';}
string getFileName();
void convertToLowerCaseAndClean(const string &fileName);
string registerToBinary(const string ®Name);
string intToBinary(int numberOfBits, int convertInt);
string immToBinary(const string &immediate);
string negativeToBinary(int numberOfBits, int convertInt);
void splitString(string inputString, string* regString, string* immString);
void sortingLabels(const string &iFile, vector<string> &vecOfCodeLine, vector<string> &vecOfLabelsAdr);
void convertAndWriteInstr(vector<string> vecOfCodeLine, vector<string> vecOfLabelsAdr);
class R_Instr{ //for all R-type ALU instr
public:
const int opCode = 0;
string opcodeStr = intToBinary(OPCODE_LENGTH, opCode);
string rs1, rs2, rd, func, finalInstr;
//code comes: func rd rs1 rs2; e.g. add r2 r3 r4 = r2 <- r3 ADD r4
explicit R_Instr(string &strng){
istringstream instrString(strng);
int tempIndex = 0;
int funcInt;
do {
// Read a word
string tempWord;
instrString >> tempWord;
if (tempIndex == 0){
auto funcCode = AluFunc.find(tempWord); //iterator for AluFunc map
funcInt = funcCode->second; //get the number mapped to the funcCode
func = intToBinary(FUNC_LENGTH, funcInt);
}
else if (tempIndex == 1){
rd = registerToBinary(tempWord);
}
else if (tempIndex == 2){
rs1 = registerToBinary(tempWord);
}
else if (tempIndex == 3){
rs2 = registerToBinary(tempWord);
}
tempIndex++;
// While there is more to read
} while (instrString);
finalInstr = opcodeStr + rs1 + rs2 + rd + func;
}
};
class I_LHI_Instr{ //for LHI (load high immediate) instr
public:
int opcodeValue;
string opcodeStr, rs1, immStr, finalInstr;
const string rs2 = "00000";
explicit I_LHI_Instr(string &strng){
istringstream instrString(strng);
int tempIndex = 0;
//instr format: opcode rs1 imm
do {
string tempWord;
instrString >> tempWord;
if (tempIndex == 0){
auto opCode = Opcode.find(tempWord); //iterator for Opcode map
opcodeValue = opCode->second; //get the number mapped to the opCode
opcodeStr = intToBinary(OPCODE_LENGTH, opcodeValue);
}
else if (tempIndex == 1){
rs1 = registerToBinary(tempWord);
}
else if (tempIndex == 2){
immStr = immToBinary(tempWord);
}
tempIndex++;
} while (instrString);
finalInstr = opcodeStr + rs1 + rs2 + immStr;
}
};
class I_Load_Instr{ //for LOAD instrs in format: [instr] [rs2] [imm]([rs1])
public:
int opcodeValue;
string opcodeStr;
string rs1, rs2, immStr, finalInstr;
explicit I_Load_Instr(string &strng){
istringstream instrString(strng);
int tempIndex = 0;
string immWithReg, immTemp, rs1Temp;
//instr format: opcode rs2 immRs1
do {
string tempWord;
instrString >> tempWord;
if (tempIndex == 0){
auto opCode = Opcode.find(tempWord); //iterator for Opcode map
opcodeValue = opCode->second; //get the number mapped to the opCode
opcodeStr = intToBinary(OPCODE_LENGTH, opcodeValue);
}
else if (tempIndex == 1){
rs2 = registerToBinary(tempWord);
}
else if (tempIndex == 2){
immWithReg = tempWord;
splitString(immWithReg, &rs1Temp, &immTemp);
rs1 = registerToBinary(rs1Temp);
immStr = immToBinary(immTemp);
}
tempIndex++;
} while (instrString);
finalInstr = opcodeStr + rs1 + rs2 + immStr;
}
};
class I_Store_Instr{ //for STORE instr
public:
int opcodeValue;
string opcodeStr;
string rs1, rs2, immStr, finalInstr;
explicit I_Store_Instr(string &strng){
istringstream instrString(strng);
int tempIndex = 0;
string immWithReg, immTemp, rs1Temp;
//instr format: opcode immRs1 rs2
do {
string tempWord;
instrString >> tempWord;
if (tempIndex == 0){
auto opCode = Opcode.find(tempWord); //iterator for Opcode map
opcodeValue = opCode->second; //get the number mapped to the opCode
opcodeStr = intToBinary(OPCODE_LENGTH, opcodeValue);
}
else if (tempIndex == 1){
immWithReg = tempWord;
splitString(immWithReg, &rs1Temp, &immTemp);
rs1 = registerToBinary(rs1Temp);
immStr = immToBinary(immTemp);
}
else if (tempIndex == 2){
rs2 = registerToBinary(tempWord);
}
tempIndex++;
} while (instrString);
finalInstr = opcodeStr + rs1 + rs2 + immStr;
}
};
class I_Math_Instr{ //for I-type ALU instructions
public:
int opcodeValue;
string opcodeStr, rs1, rs2, immStr, finalInstr;
explicit I_Math_Instr(string &strng){
istringstream instrString(strng);
int tempIndex = 0;
//instr format: opcode rs2 rs1 imm
do {
string tempWord;
instrString >> tempWord;
if (tempIndex == 0){
auto opCode = Opcode.find(tempWord); //iterator for Opcode map
opcodeValue = opCode->second; //get the number mapped to the opCode
opcodeStr = intToBinary(OPCODE_LENGTH, opcodeValue);
}
else if (tempIndex == 1){
rs2 = registerToBinary(tempWord);
}
else if (tempIndex == 2){
rs1 = registerToBinary(tempWord);
}
else if (tempIndex == 3){
immStr = immToBinary(tempWord);
}
tempIndex++;
} while (instrString);
finalInstr = opcodeStr + rs1 + rs2 + immStr;
}
};
class I_Jump_Instr{ //for JR and JALR instructions
public:
int opcodeValue;
string opcodeStr, rs1, finalInstr;
const string rs2 = "00000";
const string immStr = immToBinary("0");
explicit I_Jump_Instr(string &strng){
istringstream instrString(strng);
int tempIndex = 0;
//instr format: opcode rs2 rs1 imm
do {
string tempWord;
instrString >> tempWord;
if (tempIndex == 0){
auto opCode = Opcode.find(tempWord); //iterator for Opcode map
opcodeValue = opCode->second; //get the number mapped to the opCode
opcodeStr = intToBinary(OPCODE_LENGTH, opcodeValue);
}
else if (tempIndex == 1){
rs1 = registerToBinary(tempWord);
}
tempIndex++;
} while (instrString);
finalInstr = opcodeStr + rs1 + rs2 + immStr;
}
};
class J_OR_Branch_Instr{ //for regular Jump and Branch instr
public:
int opcode;
int offset;
string rs1;
string rs2 = "00000";
string labelName, offsetStr, opcodeStr, finalInstr;
explicit J_OR_Branch_Instr(string &strng, int currentInstrLine, vector<string> &vecOfLabelsAdr) {
istringstream instrString(strng);
int tempIndex = 0;
string word1, word2;
do {
string tempWord;
instrString >> tempWord;
if (tempIndex == 0) {
auto opCode = Opcode.find(tempWord); //iterator for Opcode map
opcode = opCode->second; //get the number mapped to the opCode
opcodeStr = intToBinary(OPCODE_LENGTH, opcode);
} else if (tempIndex == 1) {
word1 = tempWord;
} else if (tempIndex == 2) {
word2 = tempWord;
}
tempIndex++;
// While there is more to read
} while (instrString);
if (tempIndex == 3) {//J-type
labelName = word1;
auto iteratorJ = find(vecOfLabelsAdr.begin(), vecOfLabelsAdr.end(), labelName);
if (iteratorJ != vecOfLabelsAdr.end()){
int tempInt = (iteratorJ - vecOfLabelsAdr.begin());
offset = 4*(tempInt - currentInstrLine - 1);
}
if (offset < 0){
offsetStr = negativeToBinary(OFFSET_LENGTH, offset);
}
else{
offsetStr = intToBinary(OFFSET_LENGTH, offset);
}
finalInstr = opcodeStr + offsetStr;
}
if (tempIndex == 4) {//I-type: Branch
rs1 = registerToBinary(word1);
labelName = word2;
auto iteratorJ = find(vecOfLabelsAdr.begin(), vecOfLabelsAdr.end(), labelName);
if (iteratorJ != vecOfLabelsAdr.end()) {
int tempInt = (iteratorJ - vecOfLabelsAdr.begin());
offset = 4 * (tempInt - currentInstrLine - 1);
}
if (offset < 0) {
offsetStr = negativeToBinary(IMM_LENGTH, offset);
} else {
offsetStr = intToBinary(IMM_LENGTH, offset);
}
finalInstr = opcodeStr + rs1 + rs2 + offsetStr;
}
}
};
//Everything is broken into classes and functions, so the project can readily be split into smaller source files
int main() {
vector<string> vecOfCodeLine;
vector<string> vecOfLabelsAdr;
string vecFile = "tk.txt";
string inputFileName = getFileName();
convertToLowerCaseAndClean(inputFileName);
sortingLabels(vecFile, vecOfCodeLine, vecOfLabelsAdr);
convertAndWriteInstr(vecOfCodeLine, vecOfLabelsAdr);
return 0;
}
string getFileName(){
string inputFileName;
while(true){
cout << "Please input the name of your file, including the extension" << endl;
cout << "If the file is not in the same folder as this program's executable--or if you're not sure--\n"
"you are free to use an explicit file name" << endl;
ifstream inputFile;
getline(cin, inputFileName);
inputFile.open(inputFileName);
if (inputFile){
//checks if the file exists
break;
}
else {
cout << "FILE: " << inputFileName << " NOT FOUND\n" << endl;
continue;
}
}
return inputFileName;
}
void convertToLowerCaseAndClean(const string &fileName){
char inputBuffer;
char* inputString = (char*) calloc(20, sizeof(char));
FILE *bufferFile = fopen("lower.txt", "w"); //lowercase file
FILE *tkFile = fopen("tk.txt", "w");
ifstream minifyThisFile (fileName);
while ((inputBuffer = minifyThisFile.get()) != EOF) {
inputBuffer = tolower(inputBuffer);
fprintf(bufferFile, "%c", inputBuffer);
}
fclose(bufferFile);
FILE* bufferFile2 = fopen("lower.txt", "r");
int index = 0;
while ((inputBuffer = getc(bufferFile2)) != EOF) {
inputString[index] = inputBuffer;
index++;
if ((inputBuffer == '\n') || (EOF)){
char* token = strtok(inputString, ",:()");
while (token){
fprintf(tkFile, "%s", token);
token = strtok(NULL, ",:()");
}
index = 0;
realloc(inputString, 20);
}
}
fclose(tkFile);
minifyThisFile.close();
}
string registerToBinary(const string ®Name){
string registerName = regName;
string registerBinaryString;
registerName.erase(0, 1); //delete "r"
int registerNo = stoi(registerName);
int binaryNum[5] = {0, 0, 0, 0, 0};
int i = 5;
while (registerNo > 0) {
// storing remainder in binary array
binaryNum[i-1] = registerNo % 2;
registerNo = registerNo / 2;
i--;
}
for(auto x : binaryNum){
registerBinaryString.push_back(x + '0');
}
return registerBinaryString;
}
string intToBinary(const int numberOfBits, const int convertInt){
string binaryString;
int tempDecimal = convertInt;
int* binaryNum = (int*) calloc(numberOfBits, sizeof(int)); //use calloc to initialize each bit to '0'
int i = numberOfBits;
while (tempDecimal > 0) {
binaryNum[i-1] = tempDecimal % 2;
tempDecimal = tempDecimal / 2;
i--;
}
for(int x=0; x<numberOfBits; x++){
binaryString.push_back(binaryNum[x] + '0');
}
return binaryString;
}
string negativeToBinary(int numberOfBits, int convertInt){
string negativeString, tempBinary;
string onesComplement, twosComplement;
int tempDecimal = abs(convertInt);
tempBinary = intToBinary(numberOfBits, tempDecimal);
int i;
for (i = 0; i < numberOfBits; i++){
onesComplement += flip(tempBinary[i]);
}
// for two's complement go from right to left in
// ones complement and if we get 1 , we make
// them 0 and keep going left when we get first
// 0, make that 1 and go out of loop
twosComplement = onesComplement;
for (i = numberOfBits - 1; i >= 0; i--)
{
if (onesComplement[i] == '1')
twosComplement[i] = '0';
else
{
twosComplement[i] = '1';
break;
}
}
// If No break : all are 1 as in 111 or 11111;
// in such case, add extra 1 at beginning
if (i == -1){
twosComplement = "";
for(int index=0; index <numberOfBits; index++){
twosComplement += '1';
}
}
negativeString = twosComplement;
return negativeString;
}
string immToBinary(const string &immediate){
string immName = immediate;
string immBinaryString;
int immDecimal = stoi(immName);
if (immDecimal < 0){
immBinaryString = negativeToBinary(IMM_LENGTH, immDecimal);
}
else {
immBinaryString = intToBinary(IMM_LENGTH, immDecimal);
}
return immBinaryString;
}
void splitString(string inputString, string* regString, string* immString){
//this separates [imm] from [reg] when register direct with displacement is used
int lengthOfString = inputString.length();
int i=0, tempTest=0; //tempTest is used to know when the imm ends
string tempImmStr, tempRegStr;
while (i < lengthOfString){
if (isalpha(inputString[i]) != 0) tempTest = 1;
if ((isalpha(inputString[i]) != 1) && (tempTest == 0)){
tempImmStr.push_back(inputString[i]);
}
else{
tempRegStr.push_back(inputString[i]);
}
i++;
}
*regString = tempRegStr;
*immString = tempImmStr;
}
void sortingLabels(const string &iFile, vector<string> &vecOfCodeLine, vector<string> &vecOfLabelsAdr){
int index = 0;
char inputBuffer;
char* inputString = (char*) calloc(20, sizeof(char));
ifstream unsortedLabels (iFile);
string tempString;
while (getline(unsortedLabels, tempString)) {
vecOfCodeLine.emplace_back(tempString);
}
vector<string> tempVecLabels(vecOfCodeLine.size(), "");
int instrLine = 0;
for(auto x: vecOfCodeLine){
istringstream tempStr(x);
// Read a word (the first line of the string)
string word;
tempStr >> word;
auto opcodeCheck = Opcode.find(word); //iterator for Opcode map
auto funcCodeCheck = AluFunc.find(word); //iterator for AluFunc map
//if the first word isn't in either map, store it as a label
if (opcodeCheck == Opcode.end()){
if (funcCodeCheck == AluFunc.end()){
tempVecLabels.at(instrLine) = word;
x.erase(0, word.length()+1); //remove the label and following space from the stored instr
vecOfCodeLine.at(instrLine) = x;
}
}
instrLine++;
}
vecOfLabelsAdr = tempVecLabels;
}
void convertAndWriteInstr(vector<string> vecOfCodeLine, vector<string> vecOfLabelsAdr){
int instrLine = 0;
int tempValue;
ofstream instrFile("instr.txt");
for(auto x: vecOfCodeLine){
istringstream tempStr(x);
// Read a word (the first word of the string)
string word;
tempStr >> word;
auto opcodeCheck = Opcode.find(word); //iterator for Opcode map
auto funcCodeCheck = AluFunc.find(word); //iterator for AluFunc map
if (opcodeCheck != Opcode.end()){
tempValue = opcodeCheck->second;
if ((tempValue >= 2) && (tempValue <= 5)){
J_OR_Branch_Instr testBranchInstr(x, instrLine, vecOfLabelsAdr);
instrFile << testBranchInstr.finalInstr << endl;
}
else if (((tempValue >= 8) && (tempValue <= 14)) || ((tempValue >= 24) && (tempValue <= 29))
|| (tempValue >= 48)){
I_Math_Instr testMathInstr(x);
instrFile << testMathInstr.finalInstr << endl;
}
else if (tempValue == 15){
I_LHI_Instr testLhiInstr(x);
instrFile << testLhiInstr.finalInstr << endl;
}
else if ((tempValue == 18) || (tempValue == 19)){
I_Jump_Instr testJumpInstr(x);
instrFile << testJumpInstr.finalInstr << endl;
}
else if ((tempValue >= 32) && (tempValue <= 37)){
I_Load_Instr testLoadInstr(x);
instrFile << testLoadInstr.finalInstr << endl;
}
else if ((tempValue >= 40) && (tempValue <= 43)){
I_Store_Instr testStoreInstr(x);
instrFile << testStoreInstr.finalInstr << endl;
}
}
else if (funcCodeCheck != AluFunc.end()){
R_Instr testRInstr(x);
instrFile << testRInstr.finalInstr << endl;
}
instrLine++;
}
instrFile.close();
}