-
Notifications
You must be signed in to change notification settings - Fork 50
/
kaush.cpp
814 lines (774 loc) · 16.8 KB
/
kaush.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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
// Anurag Deshmane
// 2001CS08
#include <bits/stdc++.h>
using namespace std;
// SYMBOLS AND LABELS explanation after pass 1
struct Mnemonic
{
string opcode; // type = 0 means no operand
int type; // type = 1 means argument is given
// type = 2 means offset to be given
};
struct symb
{
int address; // type = 0 means varicable or data is declared here
int type; // type = 1 means Label name or block name
};
struct threepc
{
string label_name;
string ins;
string op;
threepc(string &a, string &b, string &c)
{
label_name = a;
ins = b;
op = c;
}
friend ostream &operator<<(ostream &os, const threepc &H)
{
os << H.label_name << " " << H.ins << " " << H.op << " ";
return os;
}
};
vector<string> MachineCode; // final machine code generated ;
vector<string> ListingFile; // final Listing file in strings ;
vector<pair<int, threepc>> p1Inst; // cleaned Instructions for pass1
vector<pair<int, threepc>> Inst; // Processed Instructions for pass2
map<string, Mnemonic> Optab; // Opcode Table
map<string, symb> declared; // Declared Labels
map<string, symb> used; // Used Labels
vector<pair<int, string>> Warning; // All the warnings
vector<pair<int, string>> Errors; // All The errors ;
int pc = 0; // Program counter
set<string> datadeclared; // labels declared with data
set<string> setdeclared; // labels declared with set instruction
vector<pair<int, threepc>> dataseg; // Data Segment
bool isHaltPresent = false;
// Parsing functions
// Removing Comments and unnecessary space from beginning from a line
string ProcessRawInput(string s)
{
string ans;
int i = 0;
while (i < s.size() && s[i] == ' ')
i++;
for (; i < s.size(); i++)
{
char c = s[i];
if (c == ';')
break;
if (c >= 'A' && c <= 'Z')
c = tolower(c);
ans.push_back(c);
}
return ans;
}
bool CheckifNum(char x)
{
return (x >= '0' && x <= '9');
}
bool isOctal(string s)
{
if (s.size() < 2)
return false;
bool ok = true;
ok &= (s[0] == '0');
for (int i = 1; i < s.size(); i++)
{
ok &= (s[i] >= '0' && s[i] <= '7');
}
return ok;
}
bool isHexadecimal(string s)
{
bool ok = true;
if (s.size() < 3)
return false;
ok &= (s[0] == '0');
ok &= (s[1] == 'x');
for (int i = 2; i < s.size(); i++)
{
bool check = (s[i] >= '0' && s[i] <= '9');
check |= (s[i] >= 'a' && s[i] <= 'f');
ok &= check;
}
return ok;
}
int Octal2Dec(string s)
{
int ans = 0, x = 1;
for (int i = s.size() - 1; i > 0; i--)
{
ans += (s[i] - '0') * x;
x *= 8;
}
return ans;
}
int Hex2Dec(string s)
{
int ans = 0, x = 1;
for (int i = s.size() - 1; i > 1; i--)
{
if (CheckifNum(s[i]))
{
ans += (s[i] - '0') * x;
}
else
{
ans += ((s[i] - 'a') + 10) * x;
}
x *= 16;
}
return ans;
}
bool isValidName(string name)
{
if (name.empty())
return false;
char x = name[0];
return ((x >= 'A' && x <= 'Z') || (x >= 'a' && x <= 'z')); //check if char
bool ans = true;
for (int i = 0; i < name.size(); i++)
{
ans &= ( ((name[i] >= 'A' && name[i] <= 'Z') || (name[i] >= 'a' && name[i] <= 'z')) || CheckifNum(name[i]) || name[i] == '_');
}
return ans;
}
// Split the single line into 3 components label - ins - operand
void Split(string &label_name, string &ins, string &op, string &Line, int line_num)
{
int i = 0;
bool label = false;
for (; i < Line.size(); i++)
{
if (Line[i] == ':')
{
label = true;
for (int j = 0; j < i; j++)
{
label_name.push_back(Line[j]);
}
i++;
break;
}
}
if (!label)
i = 0;
while (i < Line.size() && (Line[i] == ' ' || Line[i] == '\t'))
i++;
while (i < Line.size() && !(Line[i] == ' ' || Line[i] == '\t'))
{
ins.push_back(Line[i]);
i++;
}
while (i < Line.size() && (Line[i] == ' ' || Line[i] == '\t'))
i++;
while (i < Line.size() && !(Line[i] == ' ' || Line[i] == '\t'))
{
op.push_back(Line[i]);
i++;
}
while (i < Line.size() && (Line[i] == ' ' || Line[i] == '\t'))
i++;
if (i < Line.size() && !(Line[i] == ' ' || Line[i] == '\t'))
{
string t = "Extra operand on end of line ";
Errors.push_back({line_num, t});
}
}
string Process_Operand(string s, int *err, int line_num)
{
if (isValidName(s))
{
used[s] = {line_num, 5};
return s;
}
int sign = 1;
if (s[0] == '-')
sign = -1;
if (s[0] == '+' || s[0] == '-')
s = s.substr(1);
if (isOctal(s))
return to_string(sign * Octal2Dec(s));
if (isHexadecimal(s))
return to_string(sign * Hex2Dec(s));
bool ok = true;
for (int i = 0; i < s.size(); i++)
{
ok &= CheckifNum(s[i]);
}
if (ok)
return to_string(sign * stoi(s));
*err = 1;
return s;
}
// Fill declared by data and declared by set labels
void Declaredbywho()
{
for (auto &Line : p1Inst)
{
if (Line.second.ins == "set")
{
setdeclared.insert(Line.second.label_name);
}
}
for (auto &Line : dataseg)
{
datadeclared.insert(Line.second.label_name);
}
vector<pair<int, threepc>> temper;
for (auto Line : p1Inst)
{
bool flag = false;
if (Line.second.ins == "set")
{
if (datadeclared.count(Line.second.label_name) == 0)
{
datadeclared.insert(Line.second.label_name);
Line.second.ins = "data";
dataseg.push_back(Line);
flag = true;
}
}
if (!flag)
{
temper.push_back(Line);
}
}
p1Inst.clear();
for (auto Line : temper)
{
p1Inst.push_back(Line);
}
for (auto Line : dataseg)
{
p1Inst.push_back(Line);
}
temper.clear();
dataseg.clear();
}
// Function to Implement SET Instruction
bool SETinstruction(string &label_name, string &val, int lnum, int toadd)
{
int A = 0;
if (label_name.empty())
{
string t = "Missing Label name for set Instruction";
Errors.push_back({lnum, t});
return false;
}
if (val.empty())
{
string t = "Missing operand";
Errors.push_back({lnum, t});
return false;
}
string nop = Process_Operand(val, &A, lnum);
if (((val[0] >= 'A' && val[0] <= 'Z') || (val[0] >= 'a' && val[0] <= 'z')) || A == 1)
{
string t = "Not a Number for SET Instruction";
Errors.push_back({lnum, t});
return false;
}
string ins;
if (datadeclared.count(label_name) == 0 && declared.count(label_name) == 0)
{
return true;
}
else
{
string op;
string label;
ins = "adj";
op = "2"; // increase the stack size
nop = Process_Operand(op, &A, lnum);
Inst.push_back({pc, threepc(label, ins, nop)});
pc++;
ins = "stl";
op = "-1"; // push A
nop = Process_Operand(op, &A, lnum);
Inst.push_back({pc, threepc(label, ins, nop)});
pc++;
ins = "stl";
op = "0"; // push B
nop = Process_Operand(op, &A, lnum);
Inst.push_back({pc, threepc(label, ins, nop)});
pc++;
ins = "ldc";
op = val; // load the value to set
nop = Process_Operand(op, &A, lnum);
Inst.push_back({pc, threepc(label, ins, nop)});
pc++;
ins = "ldc";
op = label_name; // load location of var
nop = Process_Operand(op, &A, lnum);
Inst.push_back({pc, threepc(label, ins, nop)});
pc++;
ins = "adc";
op = to_string(toadd);
nop = Process_Operand(op, &A, lnum);
Inst.push_back({pc, threepc(label, ins, nop)});
pc++; // for setting value in array
ins = "stnl";
op = "0"; // store value to set
nop = Process_Operand(op, &A, lnum);
Inst.push_back({pc, threepc(label, ins, nop)});
pc++;
ins = "ldl";
op = "0"; // load back B
nop = Process_Operand(op, &A, lnum);
Inst.push_back({pc, threepc(label, ins, nop)});
pc++;
ins = "ldl";
op = "-1"; // load back A
nop = Process_Operand(op, &A, lnum);
Inst.push_back({pc, threepc(label, ins, nop)});
pc++;
ins = "adj";
op = "-2"; // decrease the stack size
nop = Process_Operand(op, &A, lnum);
Inst.push_back({pc, threepc(label, ins, nop)});
pc++;
}
return false;
}
// Clean the Lines of the code ;
string InputName = "input.asm";
void clean()
{
ifstream infile;
infile.open(InputName);
if (infile.fail())
{
cout << "Error in Opening Input file";
exit(0);
}
string s;
int lnum = 0;
while (getline(infile, s))
{
string Line = ProcessRawInput(s);
lnum++;
if (Line.empty())
continue;
string ins, op, label_name;
Split(label_name, ins, op, Line, lnum);
if (!ins.empty() && ins == "data")
{
if (p1Inst.size() > 0)
{
auto temp = p1Inst.back();
if (label_name.empty() && temp.second.ins.empty())
{
dataseg.push_back(temp);
p1Inst.pop_back();
}
}
dataseg.push_back({lnum, threepc(label_name, ins, op)});
}
else
{
p1Inst.push_back({lnum, threepc(label_name, ins, op)});
}
}
}
// PASS 1
void pass1()
{
clean();
Declaredbywho();
int cnt = 0;
string prev;
for (auto &Line : p1Inst)
{
int lnum;
string ins, op, label_name;
lnum = Line.first;
label_name = Line.second.label_name;
ins = Line.second.ins;
op = Line.second.op;
bool wanttodeclare = false;
if (!label_name.empty())
{
bool checker = isValidName(label_name);
if (!checker)
{
string t = "Bogus Label Name ";
Errors.push_back({lnum, t});
}
else
{
// declare the valid Label .
wanttodeclare = true;
prev = label_name;
cnt = 0;
}
}
else
cnt++;
int use = 0;
int minus = 1, toincrease = 0;
bool needsoperand = false;
bool flag = false;
if (!ins.empty())
{
bool checker = (Optab.count(ins) > 0);
if (!checker)
{
string t = "Bogus Mnemonic";
Errors.push_back({lnum, t});
}
else
{
needsoperand |= (Optab[ins].type > 0);
if (ins == "set")
flag = SETinstruction(prev, op, lnum, cnt);
if (flag)
ins = "data";
toincrease++;
if (wanttodeclare && ins == "data")
use = 1;
/*special care for data and set instructions */
}
}
if (!flag && (ins == "set"))
{
continue;
}
if (wanttodeclare)
{
if (declared.count(label_name) > 0)
{
string t = "Duplicate label definition of \"" + label_name + "\"";
Errors.push_back({lnum, t});
}
else
{
declared[label_name] = {pc, use};
}
}
string nop;
if (!op.empty())
{
if (!needsoperand)
{
string t = "Unexpected Operand";
Errors.push_back({lnum, t});
}
else
{
// check if it is valid label or valid number
int A = 0;
nop = Process_Operand(op, &A, lnum);
if (A == 1)
{
string t = "Not a number or a valid Label";
Errors.push_back({lnum, t});
}
}
}
else
{
if (needsoperand)
{
string t = "Missing Operand";
Errors.push_back({lnum, t});
}
}
Inst.push_back({pc, threepc(label_name, ins, nop)});
pc += toincrease;
}
map<string, symb>::iterator it;
for (it = used.begin(); it != used.end(); it++)
{
string name = it->first;
symb temp = it->second;
int add = temp.address;
if (declared.count(name) == 0)
{
string t = "No such label as \"" + name + "\" has been declared";
Errors.push_back({add, t});
}
}
for (it = declared.begin(); it != declared.end(); it++)
{
string name = it->first;
if (used.count(name) == 0)
{
string t = "Label with name \"" + name + "\" not used but declared";
Warning.push_back({it->second.address, t});
}
}
}
string hexstr(int n, bool is24)
{
string ans = "00000000";
int idx = 7;
if (n < 0)
{
unsigned int x = n;
ans = "";
while (x != 0)
{
int temp = x % 16;
if (temp < 10)
{
ans = (char)(temp + 48) + ans;
}
else
{
ans = (char)(temp + 55) + ans;
}
x /= 16;
idx--;
}
string res = "00000000";
int j = 7;
for (int i = ans.size() - 1; i >= 0 && j >= 0; i--, j--)
{
res[j] = ans[i];
}
ans = res;
}
else
{
while (n != 0)
{
int temp = n % 16;
if (temp < 10)
{
ans[idx] = (char)(temp + 48);
}
else
{
ans[idx] = (char)(temp + 55);
}
n /= 16;
idx--;
}
}
if (is24)
return ans.substr(2);
return ans;
}
// PASS 2
void pass2()
{
for (auto &Line : Inst)
{
// cout << Line.second <<endl ;
int loc = Line.first;
string label_name = Line.second.label_name;
string ins = Line.second.ins;
string op = Line.second.op;
int operand_val;
string ins_val;
bool needsoffset = false;
bool isthisdataval = false;
if (!ins.empty())
{
ins_val = Optab[ins].opcode;
if (Optab[ins].type == 2)
needsoffset = true;
/*
if instruction is data special care needed
*/
if (ins == "data")
isthisdataval = true;
if (ins == "halt")
isHaltPresent = true;
}
string final;
if (!op.empty())
{
if (declared.count(op) == 0)
operand_val = stoi(op);
else
operand_val = declared[op].address;
if (needsoffset && declared.count(op) != 0)
{
operand_val -= loc + 1;
/* Take care when operand should be 0 but it comes to be -1*/
}
/*
operand value should be changed if you want offset ;
convert the integer to 24 bit or 6 len string hexadecimal
*/
if (isthisdataval)
{
/* convert to 32 bit */
final = hexstr(operand_val, false);
}
else
{
/* convert to 24 bit */
final = hexstr(operand_val, true) + ins_val;
}
}
else
final = "000000" + ins_val;
string program_counter = hexstr(loc, false);
string mcode, lcode;
lcode = program_counter;
if (!ins.empty())
{
mcode = final;
lcode += " " + final;
}
else
{
lcode += " ";
}
if (!label_name.empty())
{
lcode += " " + label_name + ":";
}
if (!ins.empty())
{
lcode += " " + ins;
}
if (!op.empty())
{
lcode += " " + op;
}
if (!mcode.empty())
MachineCode.push_back(mcode);
if (!lcode.empty())
ListingFile.push_back(lcode);
}
if (!isHaltPresent)
{
string t = "HALT instruction not found .";
Warning.push_back({12, t});
}
}
// optable is initialised with the opcodes and type
void Optable_initialisation()
{
// type = 0 means no operand
Optab["add"] = {"06", 0};
Optab["sub"] = {"07", 0};
Optab["shl"] = {"08", 0};
Optab["shr"] = {"09", 0};
Optab["a2sp"] = {"0B", 0};
Optab["sp2a"] = {"0C", 0};
Optab["return"] = {"0E", 0};
Optab["halt"] = {"12", 0};
// type = 1 means argument is given
Optab["data"] = {"", 1};
Optab["ldc"] = {"00", 1};
Optab["adc"] = {"01", 1};
Optab["set"] = {"", 1};
Optab["adj"] = {"0A", 1};
// type = 2 means offset to be given
Optab["ldl"] = {"02", 2};
Optab["stl"] = {"03", 2};
Optab["ldnl"] = {"04", 2};
Optab["stnl"] = {"05", 2};
Optab["call"] = {"0D", 2};
Optab["brz"] = {"0F", 2};
Optab["brlz"] = {"10", 2};
Optab["br"] = {"11", 2};
}
void Errors_display(const string &lgfile)
{
sort(Errors.begin(), Errors.end());
cerr << "Failed to Assemble!!" << endl;
ofstream coutLog(lgfile);
coutLog << "Failed to assemble!" << endl;
coutLog << "Errors :-" << endl;
for (auto x : Errors)
{
cerr << "Error at Line " << x.first << " : " << x.second << endl;
coutLog << "Error at Line " << x.first << " : " << x.second << endl;
}
coutLog.close();
exit(0);
}
void Warning_display(const string &lgfile)
{
sort(Warning.begin(), Warning.end());
ofstream coutLog(lgfile);
coutLog << "Warning :-" << endl;
for (auto x : Warning)
{
cerr << "Warning at Line " << x.first << " : " << x.second << endl;
coutLog << "Warning at Line " << x.first << " : " << x.second << endl;
}
coutLog.close();
}
void write_listing_file(const string &lsfile)
{
ofstream outfile(lsfile);
for (auto &x : ListingFile)
{
outfile << x << endl;
}
outfile.close();
}
void write_object_code_file(const string &mcfile)
{
ofstream noutfile;
noutfile.open(mcfile, ios::binary | ios::out);
for (auto &x : MachineCode)
{
unsigned int y;
std::stringstream ss;
ss << std::hex << x;
ss >> y;
static_cast<int>(y);
noutfile.write((const char *)&y, sizeof(unsigned));
}
noutfile.close();
}
int main(int argc, char *argv[])
{
// file format check , name extraction & Intialization
if (argc != 2)
{
cerr << "Error: Only pass the .asm file";
return 0;
}
int fname_len = strlen(argv[1]);
if (fname_len <= 3 or strcmp("asm", argv[1] + fname_len - 3))
{
cerr << "Error: Incorrect file type" << endl;
cerr << "Enter a .asm file" << endl;
return 0;
}
string filename(argv[1]);
string namewithoutasm = "";
int i = 0;
while (argv[1][i] != '.')
{
namewithoutasm += argv[1][i];
i++;
}
InputName = namewithoutasm + ".asm";
string lgfile, lsfile, mcfile;
lgfile = namewithoutasm + "log.txt";
lsfile = namewithoutasm + ".l";
mcfile = namewithoutasm + ".o";
Optable_initialisation();
/**************************************************************/
/*Assembler Passes & Error and warning genration*/
/*************************************************************/
pass1();
if (Errors.size() > 0)
Errors_display(lgfile);
pass2();
Warning_display(lgfile);
/**************************************************************/
/*Listing and Object File generation*/
/**************************************************************/
write_listing_file(lsfile);
write_object_code_file(mcfile);
cerr << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cerr << "Compiled without errors!!!" << endl;
cerr << "Logs generated in: " + namewithoutasm + "log.txt" << endl;
cerr << "Machine code generated in: " + namewithoutasm + ".o" << endl;
cerr << "Listing generated in: " + namewithoutasm + ".l" << endl;
cerr << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
return 0;
}