-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20_12_Zaid_Shaikh.cpp
More file actions
245 lines (213 loc) · 5.54 KB
/
Copy path20_12_Zaid_Shaikh.cpp
File metadata and controls
245 lines (213 loc) · 5.54 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
#include <iostream>
#include <string> // for npos and string declarations
#include <fstream> // for file handling
#include <cstring> // for strcpy
// all functions are declared using standard namespace
using namespace std;
// Declaring input and output file classes
ifstream inputFile;
ofstream outputFile;
// Declaring all Functions
void initialize(char*, char*, char*, int, int, int);
void GD(char, int);
void PD(char, int);
void Halt(int&);
void LR(char*, int, char*);
void SR(char*, int, char*);
void CR(char*, int, char*, int);
void BT(int, int&, int&);
void mos(char*, int, int, int);
void loadprogram(char*, char*, char*, int, int, int);
void start_execution(char*, char*, char*, int, int, int);
void execute_user_program(char*, char*, char*, int, int, int);
// Initialize Function
void initialize(char M[100][4], char *IR, char *R, int &IC, int &SI, int &C)
{
for (int i = 0; i < 100; i++)
for (int j = 0; j < 4; j++)
M[i][j] = '\0';
for (int i = 0; i < 4; i++)
IR[i] = R[i] = '\0';
IC = SI = C = 0;
}
// GetData From File Function
void GD(char M[100][4], int mem)
{
char line[40];
string line2;
getline(inputFile, line2);
strcpy(line, line2.c_str());
char c = line[0];
int index = 0;
int line_byte = 0;
while (c != '\0')
{
M[mem][line_byte] = c;
index++;
c = line[index];
if (line_byte == 3)
mem += 1;
line_byte = index % 4;
}
//cout << line << endl << endl; // For printing the data in the Line array
//cout << line2 << endl << endl; // For printing the data in the string we read from file
}
// PutData into the File
void PD(char M[100][4], int mem)
{
char ch, *buffer;
buffer = (char *)malloc(sizeof(char) * 4);
int index = 0;
for (int i = 0; i < 10; i++)
{
for (int k = 0; k < 4; k++)
buffer[k] = M[mem][k];
for (int j = 0; j < 4; j++)
outputFile.put(buffer[j]);
mem += 1;
}
outputFile.put('\n');
}
// Halt the program Function
void Halt(int &end_program)
{
outputFile.put('\n');
outputFile.put('\n');
end_program = 1;
}
void LR(char M[100][4], int mem, char *R)
{
for (int k = 0; k < 4; k++)
R[k] = M[mem][k];
}
void SR(char M[100][4], int mem, char *R)
{
for (int k = 0; k < 4; k++)
M[mem][k] = R[k];
}
void CR(char M[100][4], int mem, char *R, int &C)
{
char operand2[4];
int result=1;
for (int k = 0; k < 4; k++)
{
operand2[k] = M[mem][k];
if(operand2[k]!=R[k])
{result=0;break;}
}
C = result;
}
void BT(int mem, int &IC, int &C)
{
if (C)
IC = mem;
C = 0;
}
void mos(char M[100][4], int mem, int &SI, int &end_program)
{
switch (SI)
{
case 1: GD(M, mem);
break;
case 2: PD(M, mem);
break;
case 3: Halt(end_program);
break;
default:
cout << "Mos Code is not working !!!" << endl;
}
SI = 0;
}
void execute_user_program(char M[100][4], char *IR, char *R, int &IC, int &SI, int &C)
{
IC = 0, C = 0, SI = 0;
int end_program = 0;
int mem,a0,a1;
while (!end_program)
{
for (int k = 0; k < 4; k++)
IR[k] = M[IC][k];
a1 = IR[2] - '0';
a0 = IR[3] - '0';
mem = a1 * 10 + a0;
IC++;
if (IR[0] == 'G')
{
SI = 1;
mos(M, mem, SI, end_program);
}
else if (IR[0] == 'P')
{
SI = 2;
mos(M, mem, SI, end_program);
}
else if (IR[0] == 'H')
{
SI = 3;
mos(M, mem, SI, end_program);
}
else if (IR[0] == 'L')
LR(M, mem, R);
else if (IR[0] == 'S')
SR(M, mem, R);
else if (IR[0] == 'C')
CR(M, mem, R, C);
else if (IR[0] == 'B')
BT(mem, IC, C);
}
}
void start_execution(char M[100][4], char *IR, char *R, int &IC, int &SI, int &C)
{
IC = 0;
execute_user_program(M, IR, R, IC, SI, C);
}
void loadprogram(char M[100][4], char *IR, char *R, int &IC, int &SI, int &C)
{
int instruction_count;
int program_cards;
int m;
string line;
while (!inputFile.eof())
{
getline(inputFile, line);
if (!(line.find("$AMJ") == string::npos))
{
m = 0;
instruction_count = stoi(line.substr(8, 4));
if (instruction_count % 10 == 0)
program_cards = instruction_count / 10;
else
program_cards = (instruction_count / 10) + 1;
for (int i = 0; i < program_cards; i++)
{
getline(inputFile, line);
// cout << program_cards << endl;
for (int j = 0; j < line.length(); j += 4)
{
for (int n = 0; n < 4; n++)
{
M[m][n] = line[j + n];
}
m++;
}
}
cout << "\n";
}
else if (!(line.find("$DTA") == string::npos))
start_execution(M, IR, R, IC, SI, C);
else if (!(line.find("$END") == string::npos))
initialize(M, IR, R, IC, SI, C);
}
}
int main()
{
char M[100][4], IR[4], R[4];
int C = 0, IC = 0, SI = 0;
inputFile.open("input_phase1.txt");
outputFile.open("output_phase1.txt");
initialize(M, IR, R, IC, SI, C);
loadprogram(M, IR, R, IC, SI, C);
inputFile.close();
outputFile.close();
return 0;
}