-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassembler.cpp
More file actions
1255 lines (1202 loc) · 38.6 KB
/
assembler.cpp
File metadata and controls
1255 lines (1202 loc) · 38.6 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
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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Environment - Linux g++ (PLEASE USE C++17)
Running instructions:
cd to the folder containing assembler.cpp, input.txt and linking_loader.cpp
1. g++ assembler.txt
2. ./a.out input.txt
The output will be available in object.txt. The intermediate file is intermediate.txt
Harsh Motwani
180101028
!!!IMPORTANT - LINE FORMAT
Every line should have between 21 and 80 columns (inclusive).
First 10 columns for LABEL, next 10 columns for OPCODE and (at max) the next 60 for OPERAND
*/
#include <bits/stdc++.h>
using namespace std;
//Mapping from operation assempler codes to machine codes
static unordered_map<string,string> OPTAB = {
{"LDA","00"},
{"LDL","08"},
{"LDX","04"},
{"LDB","68"},
{"LDT","74"},
{"STA","0C"},
{"STL","14"},
{"STX","10"},
{"LDCH","50"},
{"STCH","54"},
{"ADD","18"},
{"SUB","1C"},
{"MUL","20"},
{"DIV","24"},
{"COMP","28"},
{"COMPR","A0"},
{"CLEAR","B4"},
{"J","3C"},
{"JLT","38"},
{"JEQ","30"},
{"JGT","34"},
{"JSUB","48"},
{"RSUB","4C"},
{"TIX","2C"},
{"TIXR","B8"},
{"TD","E0"},
{"RD","D8"},
{"WD","DC"}
};
//Error/Warning flags and history
//DUPLICATE SYMBOL HANDLED IN SYMTAB
bool SYNTAX_ERROR=false;
string SYNTAX_ERROR_LINES;
bool UNDEFINED_SYMBOL=false;
string UNDEFINED_SYMBOL_LINE;
bool INVALID_OPERATION_CODE=false;
string INVALID_OPERATION_CODE_LINE;
bool DUPLICATE_SYMBOL=false;
string DUPLICATE_SYMBOL_LINE;
bool ILLEGAL_EXTDEF=false;
string ILLEGAL_EXTDEF_LINE;
bool DISPLACEMENT_OUT_OF_BOUNDS=false;
string DISPLACEMENT_OUT_OF_BOUNDS_STRING;
bool LITTAB_VALUES_NOT_DEFINED=false;
//Table mapping symbols to their addresses and error flags. Predefined symbols are registers.
//Secondary map for section name.
unordered_map<string, unordered_map<string,string>> SYMTAB = {
{"A",{{"PREDEFINED","0"}}},
{"X",{{"PREDEFINED","1"}}},
{"L",{{"PREDEFINED","2"}}},
{"B",{{"PREDEFINED","3"}}},
{"S",{{"PREDEFINED","4"}}},
{"T",{{"PREDEFINED","5"}}},
{"F",{{"PREDEFINED","6"}}},
{"PC",{{"PREDEFINED","8"}}},
{"SW",{{"PREDEFINED","9"}}}
};
//Literal Table
static unordered_map<string,string> LITTAB;
//EXTREF hashmap
static unordered_set<string> EXTREF_SYMBOLS;
//Modification Records
static vector<string> MODIF_RECORDS;
//Program name to length maps
static unordered_map<string,int> progNames;
string progName="";
//Base value - by default 0
int base_register_value;
//Remove spaces from beginning and end of string
string stripString(string s){
//Removing spaces from end
while(s.length()>0 && s[s.length()-1]==' ')s.pop_back();
if(s.length()==0)return "";
//Removing spaces from beginning
int i=0;
for(;i<s.length();i++){
if(s[i]!=' ')break;
}
return s.substr(i);
}
//Retrieve file name without extension so output file name can be created
string getFileWithoutExtension(string s){
for(int i=(int)s.length()-1;i>=0;i--){
if(s[i]=='.')return s.substr(0,i);
}
return s;
}
//Extract opcode, label and oeprand from a line
/*
!!!IMPORTANT
Every line should have between 21 and 80 columns (inclusive).
First 10 columns for LABEL, next 10 columns for OPCODE and (at max) the next 60 for OPERAND
*/
bool disectLine(string &line, string &label, string &opcode, string &operand, bool &extendedFlag){
if(stripString(line).length()==0){
return false;
}
//Checking if valid line and not comment
if((line.length()>80 || line.length()<21) && stripString(line)[0]!='.'){
return false;
} else if(stripString(line)[0]=='.'){
//Comments are still printed in intermediate.txt
return true;
}
//Lines are padded to make them 80 characters long.
while(line.length()<80)line+=' ';
//Using rules mentioned above. Also, removing leading and trailing spaces.
label=stripString(line.substr(0,10));
opcode=stripString(line.substr(10,10));
operand=stripString(line.substr(20));
//Handle extended format
if(opcode[0]=='+'){
extendedFlag=true;
opcode=opcode.substr(1);
} else {
extendedFlag=false;
}
return true;
}
//Convert decimal number to hexadecimal and return string
string dec_to_hex(int x){
if(x==0)return "0";
string hex;
while(x>0){
int dig=x%16;
if(dig<10){
hex+='0'+dig;
} else {
dig-=10;
hex+='A'+dig;
}
x/=16;
}
reverse(hex.begin(),hex.end());
return hex;
}
//Convert hexadecimal to decimal
int hex_to_dec(string hex){
int dec=0;
for(int i=0;i<hex.length();i++){
int dig=0;
if(hex[i]>='A' && hex[i]<='F')dig=hex[i]-'A'+10;
else dig=hex[i]-'0';
dec=dec*16+dig;
}
return dec;
}
//Used to get length of instruction/reserved space in RESW, RESB and BYTE
string int_to_string(int x){
if(x==0)return "0";
string s;
while(x>0){
s+=x%10+'0';
x/=10;
}
reverse(s.begin(),s.end());
return s;
}
//Check if string only contains digits
bool isInteger(string s){
for(int i=0;i<s.length();i++){
if(!isdigit(s[i]))
return false;
}
return true;
}
//Break a byte into its hexadecimal parts. Used for converting C'<string>' into hexadecimal characters
string getHexBreakup(string s){
string final;
for(int i=0;i<s.length();i++){
//Break into two hexadecimal codes
int x=(s[i]>>4);
int y=s[i]-(x<<4);
final+=dec_to_hex(x);
final+=dec_to_hex(y);
}
return final;
}
//Used to generate line if either label, opcode or operand are changed. Used in hexadecimal string padding.
string createLine(string label, string opcode, string operand){
while(label.length()<10)label+=' ';
while(opcode.length()<10)opcode+=' ';
while(operand.length()<60)operand+=' ';
return label+opcode+operand;
}
//Check out error/warning flags
void errorHandling(){
//Errors:
cout<<"\nTERMINATED WITH THE FOLLOWING ERRORS:\n";
if(UNDEFINED_SYMBOL){
cout<<"UNDEFINED_SYMBOL at lines: "<<UNDEFINED_SYMBOL_LINE<<'\n';
}
if(INVALID_OPERATION_CODE){
cout<<"INVALID_OPERATION_CODE at lines: "<<'\n'<<INVALID_OPERATION_CODE_LINE;
}
if(SYNTAX_ERROR){
cout<<"SYNTAX_ERROR in the following lines:\n"<<SYNTAX_ERROR_LINES<<"Notes for SYNTAX:\n1) All lines should be between 21 and 80 columns (inclusive) in length.\n2) 10 columns for LABEL, 10 columns for OPCODE and 60 columns for OPERAND.\n3) BYTE operand should be inside X\'\' or C\'\'\n";
}
if(DUPLICATE_SYMBOL){
cout<<"DUPLICATE_SYMBOL: "<<DUPLICATE_SYMBOL_LINE<<'\n';
}
if(ILLEGAL_EXTDEF){
cout<<"ILLEGAL_EXTDEF: "<<ILLEGAL_EXTDEF_LINE<<'\n';
}
if(DISPLACEMENT_OUT_OF_BOUNDS){
cout<<"DISPLACEMENT_OUT_OF_BOUNDS: "<<DISPLACEMENT_OUT_OF_BOUNDS_STRING<<'\n';
}
if(LITTAB_VALUES_NOT_DEFINED){
cout<<"LITTAB_VALUES_NOT_DEFINED: "<<"PLEASE USE LTORG BEFORE CSECT\n";
}
}
void makeModificationRecord(int recordStartAddress, int len, char sign, string variable){
string modifRecord = "M";
string modifRecordStartAddress=dec_to_hex(recordStartAddress);
while(modifRecordStartAddress.length()<6)modifRecordStartAddress='0'+modifRecordStartAddress;
string modifRecordLen=dec_to_hex(len);
while(modifRecordLen.length()<2)modifRecordLen='0'+modifRecordLen;
modifRecord+=modifRecordStartAddress+modifRecordLen;
if(sign=='+' || sign=='-')modifRecord+=sign+variable;
MODIF_RECORDS.push_back(modifRecord);
}
string writeTextRecord(int cur_start_addr, int LOCCTR, string textRecord){
string cur_start_addr_string = dec_to_hex(cur_start_addr);
while(cur_start_addr_string.length()<6)cur_start_addr_string='0'+cur_start_addr_string;
string record_len=dec_to_hex(LOCCTR-cur_start_addr);
while(record_len.length()<2)record_len='0'+record_len;
return "T"+cur_start_addr_string+record_len+textRecord;
}
//Evaluate expressions for EQU, WORD and BASE. In the case of WORD, EXTREF symbols can also be used. Not so in the case of BASE and EQU
int evaluateExpression(string s,bool sameProgramFlag,int recordStartAddress, int len){
//First converting to postfix - NO BRACKETS NEEDED THOUGH
//Precedence of operations
map<char,int> precedence = {
{'*',1},{'/',1},{'+',0},{'-',0}
};
vector<string> postFix;
stack<char> stac; //Classic stack algorithm
string temp;
for(int i=0;i<s.length();i++){
if(s[i]!='*' && s[i]!='/' && s[i]!='+' && s[i]!='-'){
temp+=s[i];
} else {
postFix.push_back(temp);
temp="";
while(!stac.empty() && precedence[s[i]]<precedence[stac.top()]){
char c=stac.top();
stac.pop();
temp+=c;
postFix.push_back(temp);
temp="";
}
stac.push(s[i]);
}
}
if(!temp.empty()){
postFix.push_back(temp);
temp="";
}
while(!stac.empty()){
char c=stac.top();
temp+=c;
stac.pop();
postFix.push_back(temp);
temp="";
}
//Evaluate postfix
stack<string> stac2;
for(int i=0;i<postFix.size();i++){
if(postFix[i]!="+" && postFix[i]!="-" && postFix[i]!="*" && postFix[i]!="/"){
temp=postFix[i];
stac2.push(temp);
} else {
//Top two elements
string t2=stac2.top();
stac2.pop();
string t1=stac2.top();
stac2.pop();
int x=0, y=0;
bool xFound=false, yFound=false; //To check if they are in the same program or should be externally fetched.
//If it is a declared symbol
if(SYMTAB.find(t1)!=SYMTAB.end()){
if(SYMTAB[t1].find(progName)!=SYMTAB[t1].end()){
//Same program
xFound=true;
x=hex_to_dec(SYMTAB[t1][progName]);
} else if(SYMTAB[t1].find("PREDEFINED")!=SYMTAB[t1].end()){
xFound=true; //Register values
x=hex_to_dec(SYMTAB[t1]["PREDEFINED"]);
} else if(EXTREF_SYMBOLS.find(t1)==EXTREF_SYMBOLS.end()){
UNDEFINED_SYMBOL=true; //Not even in EXTERNAL REFERENCE symbols. Current program doesn't have access.
return -1;
}
} else if(isInteger(t1)){
//If it is an integer.
xFound=true;
x=stoi(t1);
} else {
UNDEFINED_SYMBOL=true; //Nowhere has it been declared. It isn't even an integer.
return -1;
}
//If it is a declared symbol
if(SYMTAB.find(t2)!=SYMTAB.end()){
if(SYMTAB[t2].find(progName)!=SYMTAB[t2].end()){
//Same program
yFound=true;
y=hex_to_dec(SYMTAB[t2][progName]);
} else if(SYMTAB[t2].find("PREDEFINED")!=SYMTAB[t2].end()){
yFound=true; //Register Values
y=hex_to_dec(SYMTAB[t2]["PREDEFINED"]);
} else if(EXTREF_SYMBOLS.find(t2)==EXTREF_SYMBOLS.end()){
UNDEFINED_SYMBOL=true; //Not even in EXTERNAL REFERENCE symbols. Current program doesn't have access.
return -1;
}
} else if(isInteger(t2)){
//If it is an integer
yFound=true;
y=stoi(t2);
} else {
UNDEFINED_SYMBOL=true; //Nowhere has it been declared. It isn't even an integer.
return -1;
}
//For EQU and BASE, the labels should be declared in the same section. Therefore a flag is passed to indicate this.
if(sameProgramFlag && (!xFound || !yFound)){
UNDEFINED_SYMBOL=true;
return -1;
}
switch(postFix[i][0]){
case '+':{
stac2.push(int_to_string(x+y));
//Make modification records if the symbol is not found
if(!xFound){
makeModificationRecord(recordStartAddress,len,'+',t1);
}
if(!yFound){
makeModificationRecord(recordStartAddress,len,'+',t2);
}
break;
}
case '-':{
stac2.push(int_to_string(x-y));
//Make modification records if the symbol is not found
if(!xFound){
makeModificationRecord(recordStartAddress,len,'+',t1);
}
if(!yFound){
makeModificationRecord(recordStartAddress,len,'-',t2);
}
break;
}
case '*':{
stac2.push(int_to_string(x*y));
//EXTREF not allowed here
if(!xFound || !yFound){
SYNTAX_ERROR=true;
return -1;
}
break;
}
case '/':{
stac2.push(int_to_string(x/y));
//EXTREF not allowed here
if(!xFound || !yFound){
SYNTAX_ERROR=true;
return -1;
}
break;
}
}
}
}
if(isInteger(stac2.top())){
return stoi(stac2.top());
} else {
//If the value is a symbol
if(SYMTAB.find(stac2.top())==SYMTAB.end()){
UNDEFINED_SYMBOL=true; //If not in SYMTAB, UNDEFINED SYMBOL
return -1;
} else if(SYMTAB[stac2.top()].find(progName)!=SYMTAB[stac2.top()].end()){
//If in SYMTAB and in current section
return hex_to_dec(SYMTAB[stac2.top()][progName]);
} else if(EXTREF_SYMBOLS.find(stac2.top())!=EXTREF_SYMBOLS.end()){
//If in EXTREF SYMBOLS
if(sameProgramFlag){
//FOR BASE and EQU, this isn't allowed
UNDEFINED_SYMBOL=true;
return -1;
}
makeModificationRecord(recordStartAddress,len,'+',stac2.top());
return 0;
} else {
//If outside scope, undefined
UNDEFINED_SYMBOL=true;
return -1;
}
}
}
int main(int argc, char ** argv){
//Maintaining current address, start address and calculating program length
int LOCCTR=0,start_address=0,programLength=0;
string label="", opcode="", operand="";
bool extendedFlag=false;
//File input and output streams.
fstream fin, fout;
if(argc<2){
cout<<"Please specify SIC assembly language file (as a .txt file) as argument!!!\n";
return 0;
}
//Open file passed as argument.
fin.open(argv[1], ios::in);
//Intermediate file
fout.open("intermediate.txt", ios::out);
string line;
//PASS 1
while(getline(fin,line,'\n')){
//If line is empty, skip it.
if(stripString(line).length()==0){
continue;
} else if(stripString(line)[0]=='.'){
//Comments are still printed
fout<<" "<<line<<'\n';
continue;
}
//Extract opcode, operand and label from line.
if(!disectLine(line,label,opcode,operand,extendedFlag)){
if(stripString(line).length()!=0){
//Invalid input format
SYNTAX_ERROR=true;
SYNTAX_ERROR_LINES+=line+'\n';
errorHandling();
return 0;
}
continue;
}
//Handle End statement. Address still printed in intermediate file.
if(opcode=="END"){
string hexLOCCTR=dec_to_hex(LOCCTR);
while(hexLOCCTR.length()<6){
hexLOCCTR='0'+hexLOCCTR;
}
fout<<hexLOCCTR<<" 0 "<<line<<'\n';
break;
}
//Get program name and start_address (by default zero) from first line if it is START.
if(opcode=="START"){
progName=label;
start_address=hex_to_dec(operand);
while(operand.length()<6)operand='0'+operand;
LOCCTR=start_address;
//Intermediate file lines start with instruction address and intruction length/reserved space. 6 and 7 characters reserved respectively.
fout<<operand<<" 0 "<<line<<'\n';
continue;
}
//New section starts at CSECT. New program name. New start address.
if(opcode=="CSECT"){
//Map old program name to its length
progNames[progName]=LOCCTR-start_address;
progName=label; //New program name
start_address=0; //new start address is 0
LOCCTR=0;
fout<<'\n'; //One line gap
EXTREF_SYMBOLS.clear();
for(auto u:LITTAB){
//If any literal from the previous program isn't assigned a value, it's an error
if(u.second=="00000000"){
LITTAB_VALUES_NOT_DEFINED=true;
errorHandling();
return 0;
}
}
}
if(opcode=="EXTREF"){
//Identify individual symbols
vector<string> definitions;
string temporary="";
for(int i=0;i<operand.length();i++){
if(operand[i]!=','){
temporary+=operand[i];
} else {
definitions.push_back(stripString(temporary));
temporary="";
}
}
if(temporary.length()){
definitions.push_back(stripString(temporary));
temporary="";
}
for(int i=0;i<definitions.size();i++){
EXTREF_SYMBOLS.insert(definitions[i]);
}
}
//Get hex value of LOCCTR
string hexLOCCTR=dec_to_hex(LOCCTR);
//Fix length of string
while(hexLOCCTR.length()<6){
hexLOCCTR='0'+hexLOCCTR;
}
//If line is not comment
if(stripString(line)[0]!='.'){
//If there is a label, add that label to SYMTAB. If duplicate (in the same section), set error flag.
if(label!=""){
//If either not in symtable at all or irrelevant to current section.
if(SYMTAB.find(label)==SYMTAB.end() || (SYMTAB[label].find(progName)==SYMTAB[label].end() \
&& SYMTAB[label].find("PREDEFINED")==SYMTAB[label].end() && EXTREF_SYMBOLS.find(label)==EXTREF_SYMBOLS.end())){
if(opcode=="EQU"){
//If LOCCTR value is to be used
if(operand=="*"){
SYMTAB[label][progName]=dec_to_hex(LOCCTR);
} else {
//Please check input parameters of evaluate expression.
//This means that only symbols within the section are relevant.
int expressionValue=evaluateExpression(operand,true,0,0);
if(expressionValue==-1){
if(SYNTAX_ERROR){
SYNTAX_ERROR_LINES+=line+'\n';
}
if(UNDEFINED_SYMBOL){
UNDEFINED_SYMBOL_LINE+=line+'\n';
}
errorHandling();
return 0;
}
SYMTAB[label][progName]=dec_to_hex(expressionValue);
}
} else {
//Just store the address otherwise
SYMTAB[label][progName]=dec_to_hex(LOCCTR);
}
} else {
DUPLICATE_SYMBOL=true;
DUPLICATE_SYMBOL_LINE+=label+" ";
errorHandling();
return 0;
}
}
if(OPTAB.find(opcode)!=OPTAB.end()){
//Writing address and length of different types of instructions into intermediate file
if(opcode=="COMPR" || opcode=="CLEAR" || opcode=="TIXR"){
//Format 2 instructions
LOCCTR+=2;
fout<<hexLOCCTR<<" 2 ";
} else {
if(extendedFlag){
//Format 4
LOCCTR+=4;
fout<<hexLOCCTR<<" 4 ";
} else {
//Format 3
LOCCTR+=3;
fout<<hexLOCCTR<<" 3 ";
}
}
//Literal
if(operand[0]=='='){
if(LITTAB.find(operand)==LITTAB.end()){
LITTAB[operand]="00000000"; //Default value 00000000
}
}
} else {
if(opcode=="WORD"){
LOCCTR+=3; //One word=3 bytes
fout<<hexLOCCTR<<" 3 "; //Same reason as above
}
else if(opcode=="RESW"){
LOCCTR+=3*stoi(operand); //One word=3 bytes
string lengthOfOperand = int_to_string(3*stoi(operand));
while(lengthOfOperand.length()<7)lengthOfOperand+=' '; //Padding to make length 7 bytes.
fout<<hexLOCCTR<<' '<<lengthOfOperand; //Printing in intermediate file.
}
else if(opcode=="RESB"){
LOCCTR+=stoi(operand);
string lengthOfOperand = int_to_string(stoi(operand));
while(lengthOfOperand.length()<7)lengthOfOperand+=' ';
fout<<hexLOCCTR<<' '<<lengthOfOperand; //Printing number of reserved bytes in intermediate file
}
else if(opcode=="BYTE"){
if(operand.length()<3){ //Minimum characters=3 (C'' or X'')
//Set syntax error flag
SYNTAX_ERROR=true;
SYNTAX_ERROR_LINES+=line+'\n';
errorHandling();
return 0;
} else if(operand[0]=='X' && operand[1]=='\'' && operand[operand.length()-1]=='\''){
int intermediate=operand.length()-3;
if(intermediate%2){
//If odd length apply padding
cout<<"HEXADECIMAL BYTE OPERAND HAS ODD LENGTH. PADDING WITH 0.\n";
operand.pop_back();
operand+="0\'";
intermediate++;
line=createLine(label,opcode,operand);
}
intermediate = intermediate/2;
LOCCTR+=intermediate;
string lengthOfOperand = int_to_string(intermediate);
while(lengthOfOperand.length()<7)lengthOfOperand+=' ';
fout<<hexLOCCTR<<' '<<lengthOfOperand;
} else if(operand[0]=='C' && operand[1]=='\'' && operand[operand.length()-1]=='\''){
LOCCTR+=operand.length()-3; //Length of string in bytes
string lengthOfOperand = int_to_string(operand.length()-3);
while(lengthOfOperand.length()<7)lengthOfOperand+=' ';
fout<<hexLOCCTR<<' '<<lengthOfOperand;
} else { //First character should either be C or X.
SYNTAX_ERROR=true;
SYNTAX_ERROR_LINES+=line+'\n';
errorHandling();
return 0;
}
} else if(opcode=="LTORG"){
fout<<hexLOCCTR<<" 0 "<<line<<'\n'; //Write LTORG command into intermediate file
for(auto u:LITTAB){ //Assign addresses to all Literal values in LITTAB
if(u.second!="00000000"){
continue;
}
operand=u.first;
operand=operand.substr(1);
if(operand.length()<3){ //Minimum characters=3 (C'' or X'')
//Set syntax error flag
SYNTAX_ERROR=true;
SYNTAX_ERROR_LINES+=line+'\n';
errorHandling();
return 0;
} else if(operand[0]=='X' && operand[1]=='\'' && operand[operand.length()-1]=='\''){
int intermediate=operand.length()-3;
if(intermediate%2){
//If odd length, apply padding
cout<<"HEXADECIMAL BYTE OPERAND HAS ODD LENGTH. PADDING WITH 0.\n";
operand.pop_back();
operand+="0\'";
intermediate++;
}
intermediate = intermediate/2;
//Save into LITTAB
LITTAB[u.first]=dec_to_hex(LOCCTR);
line=createLine("*",u.first,"");
string hex=dec_to_hex(LOCCTR);
while(hex.length()<6)hex='0'+hex;
string len=int_to_string(intermediate);
while(len.length()<7)len+=' ';
fout<<hex<<' '<<len<<line<<'\n'; //Write into intermediate
LOCCTR+=intermediate;
} else if(operand[0]=='C' && operand[1]=='\'' && operand[operand.length()-1]=='\''){
//Save into LITTAB
LITTAB[u.first]=dec_to_hex(LOCCTR);
line=createLine("*",u.first,"");
string hex=dec_to_hex(LOCCTR);
while(hex.length()<6)hex='0'+hex;
string len=int_to_string(operand.length()-3);
while(len.length()<7)len+=' ';
fout<<hex<<' '<<len<<line<<'\n'; //Write into intermediate
LOCCTR+=operand.length()-3;
} else { //First character should either be C or X.
SYNTAX_ERROR=true;
SYNTAX_ERROR_LINES+=line+'\n';
errorHandling();
return 0;
}
}
continue;
} else if(opcode=="EQU" || opcode=="EXTREF" || opcode=="EXTDEF" || opcode=="BASE" || opcode=="CSECT"){
fout<<hexLOCCTR<<" 0 ";
} else { //Unknown opcode. Not comment either.
INVALID_OPERATION_CODE=true;
INVALID_OPERATION_CODE_LINE+=line+'\n';
errorHandling();
return 0;
}
}
} else line=stripString(line); //If comment, remove leading and trailing spaces.
fout<<line<<'\n';
}
//Literal pool at the end of the program.
for(auto u:LITTAB){
if(u.second!="00000000"){
continue;
}
operand=u.first;
operand=operand.substr(1);
if(operand.length()<3){ //Minimum characters=3 (C'' or X'')
//Set syntax error flag
SYNTAX_ERROR=true;
SYNTAX_ERROR_LINES+=line+'\n';
errorHandling();
return 0;
} else if(operand[0]=='X' && operand[1]=='\'' && operand[operand.length()-1]=='\''){
int intermediate=operand.length()-3;
if(intermediate%2){
//If odd, apply padding
cout<<"HEXADECIMAL BYTE OPERAND HAS ODD LENGTH. PADDING WITH 0.\n";
operand.pop_back();
operand+="0\'";
intermediate++;
}
intermediate = intermediate/2;
//Write to LITTAB
LITTAB[u.first]=dec_to_hex(LOCCTR);
line=createLine("*",u.first,"");
string hex=dec_to_hex(LOCCTR);
while(hex.length()<6)hex='0'+hex;
string len=int_to_string(intermediate);
while(len.length()<7)len+=' ';
fout<<hex<<' '<<len<<line<<'\n';
LOCCTR+=intermediate;
} else if(operand[0]=='C' && operand[1]=='\'' && operand[operand.length()-1]=='\''){
//Write to LITTAB
LITTAB[u.first]=dec_to_hex(LOCCTR);
line=createLine("*",u.first,"");
string hex=dec_to_hex(LOCCTR);
while(hex.length()<6)hex='0'+hex;
string len=int_to_string(operand.length()-3);
while(len.length()<7)len+=' ';
fout<<hex<<' '<<len<<line<<'\n';
LOCCTR+=operand.length()-3;
} else { //First character should either be C or X.
SYNTAX_ERROR=true;
SYNTAX_ERROR_LINES+=line+'\n';
errorHandling();
return 0;
}
}
//Map section name with section length
progNames[progName]=LOCCTR-start_address;
//Close streams. For safety.
fin.close();
fout.close();
//PASS 2
//Read from intermediate file and write to object file.
fin.open("intermediate.txt", ios::in);
fout.open("object.txt", ios::out);
bool CSECT_flag=false; //Flag used to write end symbol
int cur_start_addr=0; //Current start address
string textRecord; //To store current text record
while(getline(fin,line,'\n')){
//If empty of comment, continue.
if(stripString(line)=="" || stripString(line)[0]=='.'){
continue;
}
string addr=line.substr(0,6); //Address of instruction
string len=line.substr(7,6); //Length of instruction
line=line.substr(14); //The instruction line
disectLine(line,label,opcode,operand,extendedFlag); //Obtain label, opcode and operand from line
//Get first section name and starting address
if(opcode=="START"){
progName=label;
//Create Header record;
string newProgName=progName;
while(newProgName.length()<6)newProgName+=' '; //Fix length
start_address=hex_to_dec(operand);
cur_start_addr=start_address;
LOCCTR=start_address; //Start counting at start address
EXTREF_SYMBOLS.clear();
string startAddressString=operand;
while(startAddressString.length()<6)startAddressString='0'+startAddressString; //Fix length
int progLength=progNames[progName]; //Length of first section
string progLengthString=dec_to_hex(progLength);
while(progLengthString.length()<6)progLengthString='0'+progLengthString; //Fix length
fout<<'H'<<newProgName<<startAddressString<<progLengthString<<'\n'; //Write header record
}
//New section
if(opcode=="CSECT"){
//Write current text record. Wrap up previous section
if(textRecord.length()){
//Write current record into object code
fout<<writeTextRecord(cur_start_addr,LOCCTR,textRecord)<<'\n';
textRecord=""; //Ready for next instruction
}
//Write down modification records
for(auto u:MODIF_RECORDS){
fout<<u<<'\n';
}
MODIF_RECORDS.clear(); //Empty the list for future records
//If previous section was first section.
if(!CSECT_flag){
//Write end record
string startAddressString=int_to_string(start_address);
while(startAddressString.length()<6)startAddressString='0'+startAddressString;
fout<<'E'<<startAddressString<<'\n';
} else {
fout<<"E\n";
}
fout<<'\n'; //One blank line
CSECT_flag=true;
start_address=0; //Start at address 0
LOCCTR=start_address;
cur_start_addr=start_address;
EXTREF_SYMBOLS.clear(); //New EXTREF SYMBOLS need to be filled
progName=label;
string newProgName=progName;
while(newProgName.length()<6)newProgName+=' '; //Fix length
int progLength=progNames[progName];
string progLengthString=dec_to_hex(progLength);
while(progLengthString.length()<6)progLengthString='0'+progLengthString; //Fix length
fout<<'H'<<newProgName<<"000000"<<progLengthString<<'\n'; //Start address is 000000
}
if(opcode=="EXTDEF"){
//Identify individual symbols
vector<string> definitions;
string temporary="";
for(int i=0;i<operand.length();i++){
if(operand[i]!=','){
temporary+=operand[i];
} else {
definitions.push_back(stripString(temporary));
temporary="";
}
}
if(temporary.length()){
definitions.push_back(stripString(temporary));
temporary="";
}
//Write define record
fout<<'D';
for(int i=0;i<definitions.size();i++){
if(SYMTAB.find(definitions[i])==SYMTAB.end() || SYMTAB[definitions[i]].find(progName)==SYMTAB[definitions[i]].end()){
//The symbol must be defined in this program. ERROR
ILLEGAL_EXTDEF=true;
ILLEGAL_EXTDEF_LINE+=line+'\n';
errorHandling();
return 0;
} else {
//Write into define record
string temp=definitions[i];
while(temp.length()<6)temp+=' ';
string addrString = SYMTAB[definitions[i]][progName];
while(addrString.length()<6)addrString='0'+addrString;
fout<<temp<<addrString;
}
}
fout<<'\n';
continue;
} else if(opcode=="EXTREF"){
//Identify individual symbols
vector<string> definitions;
string temporary="";
for(int i=0;i<operand.length();i++){
if(operand[i]!=','){
temporary+=operand[i];
} else {
definitions.push_back(stripString(temporary));
temporary="";
}
}
if(temporary.length()){
definitions.push_back(stripString(temporary));
temporary="";
}
//Write Refer record
fout<<'R';
for(int i=0;i<definitions.size();i++){
temporary=definitions[i];
EXTREF_SYMBOLS.insert(temporary);
while(temporary.length()<6)temporary+=' ';
fout<<temporary;
}
fout<<'\n';
continue;
} else if(opcode=="BASE"){
//Helps the assembler compute base relative displacement
int expressionValue=evaluateExpression(operand,true,0,0);
if(expressionValue==-1){
//Refer to evaluateExpression() for meaning
if(SYNTAX_ERROR){
SYNTAX_ERROR_LINES+=line+'\n';
errorHandling();
return 0;
}
if(UNDEFINED_SYMBOL){
UNDEFINED_SYMBOL_LINE+=line+'\n';
errorHandling();
return 0;
}
}
base_register_value=expressionValue;
} else if(OPTAB.find(opcode)!=OPTAB.end()){
if(opcode=="COMPR" || opcode=="CLEAR" || opcode=="TIXR"){
//Format 2
string instruction=OPTAB[opcode];
vector<string> registers;
string temporary;
for(int i=0;i<operand.length();i++){
if(operand[i]!=','){
temporary+=operand[i];
} else {
registers.push_back(temporary);
temporary="";
}
}
if(temporary.length()){
registers.push_back(temporary);
}
if(opcode=="COMPR" && (registers.size()!=2 || SYMTAB.find(registers[0])==SYMTAB.end() \
|| SYMTAB.find(registers[1])==SYMTAB.end() || SYMTAB[registers[0]].find("PREDEFINED")==SYMTAB[registers[0]].end() \
|| SYMTAB[registers[1]].find("PREDEFINED")==SYMTAB[registers[1]].end())){
//COMPR has two operands and both must be predefined registers
SYNTAX_ERROR=true;
SYNTAX_ERROR_LINES+=line;
errorHandling();
return 0;
} else if(opcode!="COMPR" && (registers.size()!=1 || SYMTAB.find(registers[0])==SYMTAB.end() \
|| SYMTAB[registers[0]].find("PREDEFINED")==SYMTAB[registers[0]].end())){
//For the other two, one register operand
SYNTAX_ERROR=true;
SYNTAX_ERROR_LINES+=line;
errorHandling();
return 0;
}
if(opcode=="COMPR"){
//Add the register identification values to the instruction
instruction+=SYMTAB[registers[0]]["PREDEFINED"];
instruction+=SYMTAB[registers[1]]["PREDEFINED"];
} else {
//Add the register identification values to the instruction
instruction+=SYMTAB[registers[0]]["PREDEFINED"];
instruction+='0';
}
if(LOCCTR+2-cur_start_addr>30){
//Write text record
fout<<writeTextRecord(cur_start_addr,LOCCTR,textRecord)<<'\n';
textRecord=instruction;
cur_start_addr=LOCCTR;
} else {
textRecord+=instruction;
}
LOCCTR+=2; //2 BYTE instruction
} else {
if(extendedFlag){
string instruction=OPTAB[opcode];
//To help with flags;
int integer_instruction=hex_to_dec(instruction)*(1<<24);
integer_instruction+=(1<<20); //Extended
if(operand[0]=='#'){
//Immediate i=1, n=0
integer_instruction+=(1<<24);