-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsimulator.cpp
1220 lines (1179 loc) · 31.5 KB
/
simulator.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
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
#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
//structure for storing a label and its address
struct LabelTable
{
string label;
int32_t address;
};
//structure for storing a memory element's label and value
struct MemoryElement
{
string label;
int32_t value;
};
//class for the MIPS Simulator
class MIPSSimulator
{
private:
string Registers[32]; //array to store names of registers
int32_t RegisterValues[32]; //array to store values of registers
string InstructionSet[17]; //array to store names of instructions
int32_t Mode; //to store the Mode of execution
vector<string> InputProgram; //to store the input program
int32_t NumberOfInstructions; //to store the number of lines in the program
string current_instruction; //to store the current instruction being worked with
int32_t ProgramCounter; //to store the line number being worked with
int32_t MaxLength; //to store the maximum length of the input program
int32_t halt_value; //flag to check if program halted
int32_t r[3]; //to store register names, values, etc. for the instruction
vector<struct LabelTable> TableOfLabels; //to store all the labels and addresses
vector<struct MemoryElement> Memory; //to store all the memory elements
int32_t Stack[100]; //stack array
void add();
void addi();
void sub();
void mul();
void andf();
void andi();
void orf();
void ori();
void nor();
void slt();
void slti();
void lw();
void sw();
void beq();
void bne();
void j();
void halt();
void preprocess();
void ReadInstruction(int32_t line);
int32_t ParseInstruction();
void ReportError();
void ExecuteInstruction(int32_t instruction);
void OnlySpaces(int32_t lower, int32_t upper, string str);
void RemoveSpaces(string &str);
void assertNumber(string str);
void findRegister(int32_t number);
string findLabel();
void assertRemoveComma();
void checkStackBounds(int32_t index);
void assertLabelAllowed(string str);
public:
MIPSSimulator(int32_t mode, string fileName);
void Execute();
void displayState();
};
int32_t sorttable(LabelTable a, LabelTable b);
int32_t sortmemory(MemoryElement a, MemoryElement b);
//function to run the simulator
void MIPSSimulator::Execute()
{
getchar(); //to remove effect of pressing enter key while starting
preprocess(); //populate list of memory elements and labels
while(ProgramCounter<NumberOfInstructions && halt_value==0) //traverse instructions till end or till halt
{
ReadInstruction(ProgramCounter); //read instruction
RemoveSpaces(current_instruction);
if(current_instruction=="") //ignore blank instructions
{
ProgramCounter++;
continue;
}
int32_t instruction=ParseInstruction(); //get operationID
ExecuteInstruction(instruction);
if(instruction<13 || instruction>15) //if not jump, update ProgramCounter here
{
ProgramCounter++;
}
if(Mode==0 && halt_value==0) //if step by step mode, display state and wait
{
displayState();
getchar();
}
}
displayState(); //display state at end
if(halt_value==0) //if program ended without halt
{
cout<<"Error: Program ended without halt"<<endl;
exit(1);
}
cout<<endl<<"Execution completed successfully"<<endl<<endl;
}
//constructor to initialize values for the simulator and pass the mode and the input file path
MIPSSimulator::MIPSSimulator(int32_t mode, string fileName)
{
MaxLength=10000; //maximum allowed length of input file
NumberOfInstructions=0;
ProgramCounter=0;
halt_value=0;
Memory.clear();
TableOfLabels.clear();
string tempRegisters[]={"zero","at","v0","v1","a0","a1","a2","a3","t0","t1","t2","t3","t4","t5","t6",
"t7","s0","s1","s2","s3","s4","s5","s6","s7","t8","t9","k0","k1","gp","sp","s8","ra"}; //names of registers
for(int32_t i=0;i<32;i++)
{
Registers[i]=tempRegisters[i];
}
for(int32_t i=0;i<32;i++)
{
RegisterValues[i]=0; //initialize registers to 0
}
string tempInstructionSet[]={"add","sub","mul","and","or","nor","slt","addi","andi","ori","slti","lw",
"sw","beq","bne","j","halt"}; //names of instructions allowed
for(int32_t i=0;i<17;i++)
{
InstructionSet[i]=tempInstructionSet[i];
}
for(int32_t i=0;i<100;i++)
{
Stack[i]=0; //initialize stack elements to 0
}
RegisterValues[29]=40396; //stack pointer at bottom element
RegisterValues[28]=100000000;
Mode=mode; //set mode
ifstream InputFile;
InputFile.open(fileName.c_str(),ios::in); //open file
if(!InputFile) //if open failed
{
cout<<"Error: File does not exist or could not be opened"<<endl;
exit(1);
}
string tempString;
while(getline(InputFile,tempString)) //read line by line
{
NumberOfInstructions++;
if(NumberOfInstructions>MaxLength) ///check number of instructions with maximum allowed
{
cout<<"Error: Number of lines in input too large, maximum allowed is "<<MaxLength<<" line"<<endl;
exit(1);
}
InputProgram.push_back(tempString); //store in InputProgram
}
InputFile.close();
}
//function to store label names and addresses and memory names and values from the whole program
void MIPSSimulator::preprocess()
{
int32_t i=0,j=0;
int32_t current_section=-1; //current_section=0 - data section, current_section=1 - text section
int32_t index; //to hold index of ".data"
int32_t commentindex;
int32_t flag=0; //whether "..data" found
string tempString="";
int isLabel=0;
int doneFlag=0;
int32_t dataStart=0; //line number for start of data section
int32_t textStart=0;
for(i=0;i<NumberOfInstructions;i++)
{
ReadInstruction(i);
if(current_instruction=="")
{
continue;
}
index=current_instruction.find(".data"); //search for beginning of data section
if(index==-1) //not found
{
continue;
}
else if(flag==0)
{
flag=1; //set as found
OnlySpaces(0,index,current_instruction); //assert that nothing is before
OnlySpaces(index+5,current_instruction.size(),current_instruction); //assert that nothing is after
current_section=0; //set section
dataStart=i; //set location where section starts
}
else if(flag==1) //for multiple findings of ".data"
{
cout<<"Error: Multiple instances of .data"<<endl;
ReportError();
}
}
int32_t LabelIndex; //to store index of ":"
if(current_section==0) //in data section
{
for(i=dataStart+1;i<NumberOfInstructions;i++)
{
ReadInstruction(i);
RemoveSpaces(current_instruction);
if(current_instruction=="")
{
continue;
}
LabelIndex=current_instruction.find(":");
if(LabelIndex==-1) //if ":" not found
{
if(current_instruction.find(".text")==-1) //if text section has not started
{
cout<<"Error: Unexpected symbol in data section"<<endl;
ReportError();
}
else
{
break;
}
}
if(LabelIndex==0) //if found at first place
{
cout<<"Error: Label name expected"<<endl;
ReportError();
}
j=LabelIndex-1; //ignore spaces before ":" till label
while(j>=0 && current_instruction[j]==' ' || current_instruction[j]=='\t')
{
j--;
}
tempString=""; //to store label name
int32_t doneFlag=0; //label name complete
for(;j>=0;j--)
{
if(current_instruction[j]!=' ' && current_instruction[j]!='\t' && doneFlag==0) //till label name characters are being found
{
tempString=current_instruction[j]+tempString;
}
else if(current_instruction[j]!=' ' && current_instruction[j]!='\t' && doneFlag==1) //if something found after name ends
{
cout<<"Error: Unexpected text before label name"<<endl;
ReportError();
}
else //name ended
{
doneFlag=1;
}
}
assertLabelAllowed(tempString); //check validity of name
MemoryElement tempMemory;
tempMemory.label=tempString; //create memory and store memory element
int32_t wordIndex=current_instruction.find(".word"); //search for ".word" in the same way
if(wordIndex==-1)
{
cout<<"Error: .word not found"<<endl;
ReportError();
}
OnlySpaces(LabelIndex+1,wordIndex,current_instruction);
int32_t foundValue=0;
int32_t doneFinding=0;
tempString="";
for(j=wordIndex+5;j<current_instruction.size();j++)
{
if(foundValue==1 && (current_instruction[j]==' ' || current_instruction[j]=='\t') && doneFinding==0)
{
doneFinding=1;
}
else if(foundValue==1 && current_instruction[j]!=' ' && current_instruction[j]!='\t' && doneFinding==1)
{
cout<<"Error: Unexpected text after value"<<endl;
ReportError();
}
else if(foundValue==0 && current_instruction[j]!=' ' && current_instruction[j]!='\t')
{
foundValue=1;
tempString=tempString+current_instruction[j];
}
else if(foundValue==1 && current_instruction[j]!=' ' && current_instruction[j]!='\t')
{
tempString=tempString+current_instruction[j];
}
}
assertNumber(tempString); //check that number found is a valid integer
tempMemory.value=stoi(tempString); //change type and store
Memory.push_back(tempMemory); //add to list
}
}
sort(Memory.begin(),Memory.end(),sortmemory); //sort for easy comparison
for(i=0;Memory.size()>0 && i<Memory.size()-1;i++) //check for duplicates
{
if(Memory[i].label==Memory[i+1].label)
{
cout<<"Error: One or more labels are repeated"<<endl;
exit(1);
}
}
int32_t textFlag=0;
int32_t textIndex=0;
for(i=ProgramCounter;i<NumberOfInstructions;i++)
{
ReadInstruction(i);
if(current_instruction=="")
{
continue;
}
textIndex=current_instruction.find(".text"); //find text section similar as above
if(textIndex==-1)
{
continue;
}
else if(textFlag==0)
{
textFlag=1;
OnlySpaces(0,textIndex,current_instruction);
OnlySpaces(textIndex+5,current_instruction.size(),current_instruction);
current_section=1;
textStart=i;
}
else if(textFlag==1)
{
cout<<"Error: Multiple instances of .text"<<endl;
ReportError();
}
}
if(current_section!=1) //if text section not found
{
cout<<"Error: Text section does not exist or found unknown string"<<endl;
exit(1);
}
int32_t MainIndex=0; //location of main label
int32_t foundMain=0; //whether main label found
LabelIndex=0;
for(i=textStart+1;i<NumberOfInstructions;i++)
{
ReadInstruction(i);
if(current_instruction=="")
{
continue;
}
LabelIndex=current_instruction.find(":"); //find labels similar as above
if(LabelIndex==0)
{
cout<<"Error: Label name expected"<<endl;
ReportError();
}
if(LabelIndex==-1)
{
continue;
}
j=LabelIndex-1;
while(j>=0 && current_instruction[j]==' ' || current_instruction[j]=='\t')
{
j--;
}
tempString="";
isLabel=0;
doneFlag=0;
for(;j>=0;j--)
{
if(current_instruction[j]!=' ' && current_instruction[j]!='\t' && doneFlag==0)
{
isLabel=1;
tempString=current_instruction[j]+tempString;
}
else if(current_instruction[j]!=' ' && current_instruction[j]!='\t' && doneFlag==1)
{
cout<<"Error: Unexpected text before label name"<<endl;
ReportError();
}
else if(isLabel==0)
{
cout<<"Error: Label name expected"<<endl;
ReportError();
}
else
{
doneFlag=1;
}
}
assertLabelAllowed(tempString);
OnlySpaces(LabelIndex+1,current_instruction.size(),current_instruction); //check that nothing is after label
if(tempString=="main") //for main, set variables as needed
{
foundMain=1;
MainIndex=ProgramCounter+1;
}
else
{
LabelTable tempLabel;
tempLabel.address=ProgramCounter;
tempLabel.label=tempString;
TableOfLabels.push_back(tempLabel); //store labels
}
}
sort(TableOfLabels.begin(),TableOfLabels.end(),sorttable); //sort labels
for(i=0;TableOfLabels.size()>0 && i<(TableOfLabels.size()-1);i++) //check for duplicates
{
if(TableOfLabels[i].label==TableOfLabels[i+1].label)
{
cout<<"Error: One or more labels are repeated"<<endl;
exit(1);
}
}
if(foundMain==0) //if main label not found
{
cout<<"Error: Could not find main"<<endl;
exit(1);
}
ProgramCounter=MainIndex; //set ProgramCounter
cout<<"Initialized and ready to execute. Current state is as follows:"<<endl;
displayState();
cout<<endl<<"Starting execution"<<endl<<endl;
}
//function to display line number and instruction at which error occurred and exit the program
void MIPSSimulator::ReportError()
{
cout<<"Error found in line: "<<(ProgramCounter+1)<<": "<<InputProgram[ProgramCounter]<<endl;
displayState();
exit(1);
}
//function to read an instruction, crop out comments and set the ProgramCounter value
void MIPSSimulator::ReadInstruction(int32_t line)
{
current_instruction=InputProgram[line]; //set current_instruction
if(current_instruction.find("#")!=-1) //remove comments
{
current_instruction=current_instruction.substr(0,current_instruction.find("#"));
}
ProgramCounter=line; //set ProgramCounter
}
//function to find out which instruction is to be executed and and populate values in r[]
int32_t MIPSSimulator::ParseInstruction()
{
int32_t i=0,j=0;
RemoveSpaces(current_instruction); //remove spaces
if(current_instruction.find(":")!=-1) //if label encountered
{
return -2;
}
if(current_instruction.size()<4) //no valid instruction is this small
{
cout<<"Error: Unknown operation"<<endl;
ReportError();
}
for(j=0;j<4;j++) //find length of operation
{
if(current_instruction[j]==' ' || current_instruction[j]=='\t')
{
break;
}
}
string operation=current_instruction.substr(0,j); //cut the operation out
if(current_instruction.size()>0 && j<current_instruction.size()-1)
{
current_instruction=current_instruction.substr(j+1);
}
int32_t foundOp=0; //whether valid operation or not
int32_t OperationID=-1; //set operation index from array
for(i=0;i<17;i++) //check operation with allowed operations
{
if(operation==InstructionSet[i])
{
OperationID=i;
break;
}
}
if(OperationID==-1) //if not valid
{
cout<<"Error: Unknown operation"<<endl;
ReportError();
}
if(OperationID<7) //for R-format instructions
{
for(int32_t count=0;count<3;count++) //find three registers separated by commas and put them in r[]
{
RemoveSpaces(current_instruction);
findRegister(count);
RemoveSpaces(current_instruction);
if(count==2)
{
break;
}
assertRemoveComma();
}
if(current_instruction!="") //if something more found
{
cout<<"Error: Extra arguments provided"<<endl;
ReportError();
}
}
else if(OperationID<11) //for I-format instructions
{
for(int32_t count=0;count<2;count++) //find two registers separated by commas
{
RemoveSpaces(current_instruction);
findRegister(count);
RemoveSpaces(current_instruction);
assertRemoveComma();
}
RemoveSpaces(current_instruction);
string tempString=findLabel(); //find third argument, a number
assertNumber(tempString); //check validity
r[2]=stoi(tempString); //convert and store
}
else if(OperationID<13) //for lw, sw
{
string tempString="";
int32_t offset;
RemoveSpaces(current_instruction);
findRegister(0); //find source/destination register
RemoveSpaces(current_instruction);
assertRemoveComma(); //find comma, ignoring extra spaces
RemoveSpaces(current_instruction);
if((current_instruction[0]>47 && current_instruction[0]<58) || current_instruction[0]=='-') //if offset type
{
j=0;
while(j<current_instruction.size() && current_instruction[j]!=' ' && current_instruction[j]!='\t' && current_instruction[j]!='(')
{
tempString=tempString+current_instruction[j]; //find offset
j++;
}
if(j==current_instruction.size()) //if instruction ends there
{
cout<<"Error: '(' expected"<<endl;
ReportError();
}
assertNumber(tempString); //check validity of offset
offset=stoi(tempString); //convert and store
current_instruction=current_instruction.substr(j);
RemoveSpaces(current_instruction);
if(current_instruction=="" || current_instruction[0]!='(' || current_instruction.size()<2)
{
cout<<"Error: '(' expected"<<endl;
ReportError();
}
current_instruction=current_instruction.substr(1);
RemoveSpaces(current_instruction);
findRegister(1); //find register containing address
RemoveSpaces(current_instruction);
if(current_instruction=="" || current_instruction[0]!=')')
{
cout<<"Error: ')' expected"<<endl;
ReportError();
}
current_instruction=current_instruction.substr(1);
OnlySpaces(0,current_instruction.size(),current_instruction);
r[2]=offset;
if(r[2]==-1) //-1 reserved for non offset type, anyway an invalid offset, others checked later
{
cout<<"Error: Invalid offset"<<endl;
ReportError();
}
}
else //if label type
{
tempString=findLabel(); //find label
int32_t foundLocation=0;
for(j=0;j<Memory.size();j++)
{
if(tempString==Memory[j].label) //check for label from memory
{
foundLocation=1; //label found
if(OperationID==11)
{
r[1]=Memory[j].value; //for lw, send value
}
else
{
r[1]=j; ///for sw, send index in memory
}
break;
}
}
if(foundLocation==0) //if label not found
{
cout<<"Error: Invalid label"<<endl;
ReportError();
}
r[2]=-1; //to indicate that it is not offset type
}
}
else if(OperationID<15) //for beq, bne
{
for(int32_t count=0;count<2;count++) //find two registers separated by commas
{
RemoveSpaces(current_instruction);
findRegister(count);
RemoveSpaces(current_instruction);
assertRemoveComma();
}
RemoveSpaces(current_instruction);
string tempString=findLabel(); //find label
int32_t foundAddress=0;
for(j=0;j<TableOfLabels.size();j++) //search for label
{
if(tempString==TableOfLabels[j].label)
{
foundAddress=1; //if label found
r[2]=TableOfLabels[j].address; //set r[2]
break;
}
}
if(foundAddress==0) //if label not found
{
cout<<"Error: Invalid label"<<endl;
ReportError();
}
}
else if(OperationID==15) //for j
{
RemoveSpaces(current_instruction);
int32_t foundAddress=0;
string tempString=findLabel(); //find jump label
for(j=0;j<TableOfLabels.size();j++) //search for label
{
if(tempString==TableOfLabels[j].label)
{
foundAddress=1; //if label found
r[0]=TableOfLabels[j].address; //set r[0]
}
}
if(foundAddress==0) //if label not found
{
cout<<"Error: Invalid label"<<endl;
ReportError();
}
}
else if(OperationID==16) //for halt
{
RemoveSpaces(current_instruction);
}
return OperationID;
}
//function to check that between lower and upper-1 indices, str has only spaces, else report an error
void MIPSSimulator::OnlySpaces(int32_t lower, int32_t upper, string str)
{
for(int32_t i=lower;i<upper;i++)
{
if(str[i]!=' ' && str[i]!='\t') //check that only ' ' and '\t' characters exist
{
cout<<"Error: Unexpected character"<<endl;
ReportError();
}
}
}
//function to call the appropriate operation function based on the operation to execute
void MIPSSimulator::ExecuteInstruction(int32_t instruction)
{
switch(instruction) //call appropriate function based on the value of instruction
{
case 0:
add();
break;
case 1:
sub();
break;
case 2:
mul();
break;
case 3:
andf();
break;
case 4:
orf();
break;
case 5:
nor();
break;
case 6:
slt();
break;
case 7:
addi();
break;
case 8:
andi();
break;
case 9:
ori();
break;
case 10:
slti();
break;
case 11:
lw();
break;
case 12:
sw();
break;
case 13:
beq();
break;
case 14:
bne();
break;
case 15:
j();
break;
case 16:
halt();
break;
case -2: //if instruction containing label, ignore
break;
default:
cout<<"Error: Invalid instruction received"<<endl;
ReportError();
}
}
//function to remove spaces starting from first elements till they exist continuously in str
void MIPSSimulator::RemoveSpaces(string &str)
{
int32_t j=0;
while(j<str.size() && (str[j]==' ' || str[j]=='\t')) //till only ' ' or '\t' found
{
j++;
}
str=str.substr(j); //remove all of those
}
//function to execute add
void MIPSSimulator::add()
{
if(r[0]==29) //check that value of stack pointer is within bounds
{
checkStackBounds(RegisterValues[r[1]]+RegisterValues[r[2]]);
}
if(r[0]!=0 && r[0]!=1 && r[1]!=1 && r[2]!=1) //cannot modify $zero or use $at
{
RegisterValues[r[0]]=RegisterValues[r[1]]+RegisterValues[r[2]]; //execute
}
else
{
cout<<"Error: Invalid usage of registers"<<endl;
ReportError();
}
}
//function to execute addi
void MIPSSimulator::addi()
{
if(r[0]==29)
{
checkStackBounds(RegisterValues[r[1]]+r[2]);
}
if(r[0]!=0 && r[0]!=1 && r[1]!=1)
{
RegisterValues[r[0]]=RegisterValues[r[1]]+r[2];
}
else
{
cout<<"Error: Invalid usage of registers"<<endl;
ReportError();
}
}
//function to execute sub
void MIPSSimulator::sub()
{
if(r[0]==29)
{
checkStackBounds(RegisterValues[r[1]]-RegisterValues[r[2]]);
}
if(r[0]!=0 && r[0]!=1 && r[1]!=1 && r[2]!=1)
{
RegisterValues[r[0]]=RegisterValues[r[1]]-RegisterValues[r[2]];
}
else
{
cout<<"Error: Invalid usage of registers"<<endl;
ReportError();
}
}
//function to execute mul
void MIPSSimulator::mul()//last 32 bits?
{
if(r[0]==29)
{
checkStackBounds(RegisterValues[r[1]]*RegisterValues[r[2]]);
}
if(r[0]!=0 && r[0]!=1 && r[1]!=1 && r[2]!=1)
{
RegisterValues[r[0]]=RegisterValues[r[1]]*RegisterValues[r[2]];
}
else
{
cout<<"Error: Invalid usage of registers"<<endl;
ReportError();
}
}
//function to execute andf
void MIPSSimulator::andf()
{
if(r[0]==29)
{
checkStackBounds(RegisterValues[r[1]]&RegisterValues[r[2]]);
}
if(r[0]!=0 && r[0]!=1 && r[1]!=1 && r[2]!=1)
{
RegisterValues[r[0]]=RegisterValues[r[1]]&RegisterValues[r[2]];
}
else
{
cout<<"Error: Invalid usage of registers"<<endl;
ReportError();
}
}
//function to execute andi
void MIPSSimulator::andi()
{
if(r[0]==29)
{
checkStackBounds(RegisterValues[r[1]]&r[2]);
}
if(r[0]!=0 && r[0]!=1 && r[1]!=1)
{
RegisterValues[r[0]]=RegisterValues[r[1]]&r[2];
}
else
{
cout<<"Error: Invalid usage of registers"<<endl;
ReportError();
}
}
//function to execute orf
void MIPSSimulator::orf()
{
if(r[0]==29)
{
checkStackBounds(RegisterValues[r[1]]|RegisterValues[r[2]]);
}
if(r[0]!=0 && r[0]!=1 && r[1]!=1 && r[2]!=1)
{
RegisterValues[r[0]]=RegisterValues[r[1]]|RegisterValues[r[2]];
}
else
{
cout<<"Error: Invalid usage of registers"<<endl;
ReportError();
}
}
//function to execute ori
void MIPSSimulator::ori()
{
if(r[0]==29)
{
checkStackBounds(RegisterValues[r[1]]|r[2]);
}
if(r[0]!=0 && r[0]!=1 && r[1]!=1)
{
RegisterValues[r[0]]=RegisterValues[r[1]]|r[2];
}
else
{
cout<<"Error: Invalid usage of registers"<<endl;
ReportError();
}
}
//function to execute nor
void MIPSSimulator::nor()
{
if(r[0]==29)
{
checkStackBounds(~(RegisterValues[r[1]]|RegisterValues[r[2]]));
}
if(r[0]!=0 && r[0]!=1 && r[1]!=1 && r[2]!=1)
{
RegisterValues[r[0]]=~(RegisterValues[r[1]]|RegisterValues[r[2]]);
}
else
{
cout<<"Error: Invalid usage of registers"<<endl;
ReportError();
}
}
//function to execute slt
void MIPSSimulator::slt()
{
if(r[0]!=0 && r[0]!=1 && r[1]!=1 && r[2]!=1)
{
RegisterValues[r[0]]=RegisterValues[r[1]]<RegisterValues[r[2]];
}
else
{
cout<<"Error: Invalid usage of registers"<<endl;
ReportError();
}
}
//function to execute slti
void MIPSSimulator::slti()
{
if(r[0]!=0 && r[0]!=1 && r[1]!=1)
{
RegisterValues[r[0]]=RegisterValues[r[1]]<r[2];
}
else
{
cout<<"Error: Invalid usage of registers"<<endl;
ReportError();
}
}
//function to execute lw
void MIPSSimulator::lw()
{
if(r[0]==29)
{
checkStackBounds(r[1]);
}
if(r[0]!=0 && r[0]!=1 && r[2]==-1) //if label type
{
RegisterValues[r[0]]=r[1];
}
else if(r[0]!=0 && r[0]!=1) //if offset type
{
checkStackBounds(RegisterValues[r[1]]+r[2]); //check validity of offset
RegisterValues[r[0]]=Stack[(RegisterValues[r[1]]+r[2]-40000)/4];
}
else
{
cout<<"Error: Invalid usage of registers"<<endl;
ReportError();
}
}
//function to execute sw
void MIPSSimulator::sw()
{
if(r[0]!=1 && r[2]==-1) //if label type
{
Memory[r[1]].value=RegisterValues[r[0]];
}
else if(r[0]!=1) //if offset type
{
checkStackBounds(RegisterValues[r[1]]+r[2]); //check validity of offset
Stack[(RegisterValues[r[1]]+r[2]-40000)/4]=RegisterValues[r[0]];
}
else
{
cout<<"Error: Invalid usage of registers"<<endl;
ReportError();
}
}
//function to execute beq
void MIPSSimulator::beq()
{
if(r[0]!=1 && r[1]!=1)
{
if(RegisterValues[r[0]]==RegisterValues[r[1]])
{
ProgramCounter=r[2]; //if branch taken, update ProgramCounter with new address
}
else
{
ProgramCounter++; //else increment as usual
}
}
else
{
cout<<"Error: Invalid usage of registers"<<endl;
ReportError();
}
}
//function to execute bne
void MIPSSimulator::bne()
{
if(r[0]!=1 && r[1]!=1)
{
if(RegisterValues[r[0]]!=RegisterValues[r[1]])
{
ProgramCounter=r[2];
}
else
{