-
Notifications
You must be signed in to change notification settings - Fork 0
/
Instruction.cpp
508 lines (403 loc) · 13.2 KB
/
Instruction.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
//Name : Akshay Mandania
//Course : CMPS361 - Software Design
//Project : Assembler
//Instructor: Professor Victor Miller
//Current File : Instruction.cpp
/*
* Current File : Instruction.cpp
* Description :
Parses from the file to determine the instruction
Also parses to ensure the proper labels assigned to proper values along with all op code readings
Error repots will be line by line and not all errors at once.
No Error logging. Meaning no error output.
*/
#include "stdafx.h"
#include "Instruction.h"
/**/
/*
Instruction::Instruction() Instruction::Instruction()
NAME
Instruction::Instruction
DESCRIPTION
This constructor sets each variable to an arbitrary value,
in order to compare later on for opcodes and labels.
RETURNS
((void))
*/
/**/
Instruction::Instruction() {
ClearInfo();
}
/**/
/*
Instruction::ClearInfo() Instruction::ClearInfo()
NAME
void Instruction::ClearInfo
DESCRIPTION
Resets the variables to a "blank" value for the current line instruction
at hand. Therefore, not old values are placed in the wrong line instruction.
RETURNS
((none))
*/
/**/
void Instruction::ClearInfo() {
// All strings can be checked using .empty ::
m_label = "";
m_opCode = "";
m_operand = "";
m_instruction = "";
// Numerical value outside the valid opcodes (-1)
m_opCodeNum = -1;
m_operandValue = -1;
// Assume the operand is anything other than a numeric value for now
m_isNumericOperand = false;
// Assume blank or commented first line.
m_type = ST_Comment;
}
/**/
/*
Instruction::ParseInstruction() Instruction::ParseInstruction()
NAME
Instruction::InstructionType Instruction::ParseInstruction
DESCRIPTION
Parses the line, and carry out instructions as needed.
RETURNS
Returns the instruction type of the current line: String passed in
*/
/**/
Instruction::InstructionType Instruction::ParseInstruction(string &a_buff) {
ClearInfo();
// Return as comment if nothing was in the buffer
if (a_buff.empty()) {
return m_type;
}
m_instruction = a_buff;
string segment = m_instruction;
int count = 1;
// Storing relevant information [Label | Opcode | Operand] while parsing out comments.
ParseOutComment(segment);
std::istringstream singleOut(segment);
vector <string> lineSegment;
while (singleOut >> segment) {
//allows possibility of emptiness that must be handled.
if (!segment.empty()) lineSegment.push_back(segment);
}
if (lineSegment.empty()) return m_type;
// max size [Label | Opcode | Operand] so 3
if (lineSegment.size() > 3) {
Errors::RecordError(Errors::CreateError(string("too many arguments on the line.")));
}
bool isAssem = false, isMach = false;
for (unsigned column = 1; column <= lineSegment.size(); column++) {
segment = lineSegment.at(column - 1);
// handles a mismatch
transform(segment.begin(), segment.end(), segment.begin(), toupper);
if (column == 1 && lineSegment.size() == 3) {
m_label = lineSegment.at(column - 1);
isValidLabel(m_label);
continue;
}
// Determining these later orig INIT to FALSE, changes upon later if-elseif :: therefore the column will be incremented for the operand
if (isMach) {
m_operand = lineSegment.at(column - 1);
isValidLabel(m_operand);
if (m_isNumericOperand = (!containNumericString(m_operand))) {
Errors::RecordError(Errors::CreateError("illegal use of numerical operand \"" + m_operand + "\" on Machine Language operation."));
}
continue;
}
else if (isAssem) {
m_operand = lineSegment.at(column - 1);
// Checking for numerical operand
if (!(m_isNumericOperand = isNumeric(m_operand))) {
Errors::RecordError(Errors::CreateError("illegal use of operand \"" + m_operand + "\" on Assembly Language operation"));
}
else m_operandValue = stoi(lineSegment.at(column - 1));
continue;
}
if (isMach = isMachineInstruct(segment)) continue;
else if (isAssem = isAssemInstruct(segment)) continue;
}
// Calls to check valid syntax / semantics of a complete line.
if (isAssem) SyntaxChecking();
else if (isMach) SyntaxMatch();
else {
Errors::RecordError(Errors::CreateError("ERROR: Invalid Line. Unabale to parse instruction."));
m_opCode = m_MISSING_OPCODE;
m_operand = m_STR_UNKNOWN_OP;
}
return m_type;
}
/*Instruction::InstructionType Instruction::ParseInstruction(string &a_buff);*/
/**/
/*
Instruction::isAssemInstruct() Instruction::isAssemInstruct()
NAME
bool Instruction::isAssemInstruct
DESCRIPTION
This function is responsible for determining whether this is an
assembler instruction by comparing it to the strings of assembler list.
RETURNS
Returns TRUE upon matching the section with an assembler instruction
and will assign values to the corresponding member variables:
m_opCode, m_opCodeNum = 0, m_type
FALSE if not found (not an assembler instruction).
*/
/**/
bool Instruction::isAssemInstruct(const string &a_segment) {
auto it = m_AssemList.find(a_segment);
if (it != m_AssemList.end()) {
m_opCode = it->first;
m_opCodeNum = 0;
if (it->second == AT_END) m_type = ST_End;
else m_type = ST_AssemblerInstr;
return true;
}
return false;
}
/*bool Instruction::isAssemInstruct(const string &a_segment);*/
/**/
/*
Instruction::isMachineInstruct() Instruction::isMachineInstruct()
NAME
bool Instruction::isMachineInstruct
DESCRIPTION
This function is responsible for determining whether this is an
machine instruction by comparing it to the strings of machine list
RETURNS
Returns TRUE upon matching the section with a machine instruction,
and will assign values to corresponding member variables:
m_opCode, m_opCodeNum, m_type
Returns FALSE if not found (not a machine instruction).
*/
/**/
bool Instruction::isMachineInstruct(const string &a_segment) {
auto it = m_MachList.find(a_segment);
if (it != m_MachList.end()) {
m_opCode = it->first;
m_opCodeNum = it->second;
m_type = ST_MachineLanguage;
return true;
}
else return false;
}
/*bool Instruction::isMachineInstruct(const string &a_segment);*/
/**/
/*
Instruction::ParseOutComment() Instruction::ParseOutComment()
NAME
void Instruction::ParseOutComment
DESCRIPTION
This function is responsible for checking if there is a comment
in the middle of a "phrase" ie if the user placed a comment next to
an operand or a language operation.
RETURNS
((void))
*/
/**/
void Instruction::ParseOutComment(string &a_section) {
//check if the "single word" is attached to a comment!
auto commentSplit = a_section.find(";");
if (commentSplit != string::npos) {
//there was a comment found, parse the string
a_section = a_section.substr(0, commentSplit);
return;
}
return;
}
/*void Instruction::ParseOutComment(string &a_section);*/
/**/
/*
Instruction::LocationNextInstruction() Instruction::LocationNextInstruction()
NAME
int Instruction::LocationNextInstruction
DESCRIPTION
This function is responsible for determining the upcoming instruction
location, whether it was marked by an instruction, or
just a line incrementation.
RETURNS
Returns the INTEGER value of the next instruction.
*/
/**/
int Instruction::LocationNextInstruction(int a_loc) {
if (m_type == ST_AssemblerInstr && !m_operand.empty()) {
//should look for the location @m_OPCode
if (m_AssemList.at(m_opCode) == AT_ORG) {
//ORG tells us WHERE to go.
if (m_isNumericOperand) return m_operandValue;
}
else if (m_AssemList.at(m_opCode) == AT_DS) {
//adding "space" therefore INCREASE the location by the operand value
if (m_isNumericOperand) return a_loc + m_operandValue;
}
}
return a_loc + 1;
}
/*int Instruction::LocationNextInstruction(int a_loc);*/
/**/
/*
Instruction::isNumeric() Instruction::isNumeric()
NAME
bool Instruction::isNumeric
DESCRIPTION
This function is responsible for determining whether a string
contains all digits.
RETURNS
Returns FALSE if empty string
FALSE if a character other than digit is found
TRUE if all characters within the string are digits
*/
/**/
bool Instruction::isNumeric(const string &a_segment) {
//checks each digit from string.begin() to string.end() by using the isdigit built-in function
return !a_segment.empty() && find_if(a_segment.begin(), a_segment.end(),
[](char check) { return !isdigit(check); }) == a_segment.end();
}
/*bool Instruction::isNumeric(const string &a_segment);*/
/**/
/*
Instruction::containNumericString() Instruction::containNumericString()
NAME
bool Instruction::containNumericString
DESCRIPTION
This function is responsible for determining whether a string
is all digits
RETURNS
Returns FALSE if empty string
FALSE if a character other than digit is found
TRUE if all characters within the string are digits
*/
/**/
bool Instruction::containNumericString(const string &a_segment) {
if (!isalpha(a_segment.at(0))) {
return false;
}
//checks each digit from string.begin() to string.end() by using the isalnum built-in function
return !a_segment.empty() && find_if(a_segment.begin(), a_segment.end(),
[](char check) { return !isalnum(check); }) == a_segment.end();
}
/**/
/*
Instruction::isValidLabel() Instruction::isValidLabel()
NAME
bool Instruction::isValidLabel
DESCRIPTION
This function is responsible for checking the label/operand :
(1) if it begins with a letter.
(2) if the rest of the string is only numbers/digits
(3) if it is between 1 -> 10 characters
Report to error class if any errors.
RETURNS
Returns FALSE if empty string
FALSE if string is > 10 characters
FALSE if string's first character is something other than a letter
FALSE if the string contains special characters
TRUE if the string meets the standards specified
*/
/**/
bool Instruction::isValidLabel(const string &a_segment) {
//isValid will help reporting multiple errors on one label/operand
bool isValid = true;
if (a_segment.empty()) return isValid;
if (!isalpha(a_segment[0])) {
Errors::RecordError(Errors::CreateError(a_segment + " has invalid label/operand syntax, cannot start with character : " + a_segment[0]));
isValid = false;
}
if (a_segment.size() > 10) {
Errors::RecordError(Errors::CreateError(a_segment + " has invalid label/operand size, must be between 1 -> 10 characters."));
isValid = false;
}
for (auto it = a_segment.begin(); it != a_segment.end(); ++it) {
if (!isalnum(*it)) {
Errors::RecordError(Errors::CreateError(a_segment + " has invalid character : \"" + *it +"\""));
isValid = false;
}
}
return isValid;
}
/**/
/*
Instruction::SyntaxChecking() Instruction::SyntaxChecking()
NAME
void Instruction::SyntaxChecking
DESCRIPTION
This function is responsible for determining if:
(1) there are labels on DS/DC
(2) NO labels on ORG
(3) END contains no Label OR Operand
(4) If the operand exists *important
(5) Constant value too large?
RETURNS
Returns a string with the latest error message.
*/
/**/
void Instruction::SyntaxChecking() {
if (!m_opCode.empty()) {
string segment = m_opCode;
transform(segment.begin(), segment.end(), segment.begin(), toupper);
auto it = m_AssemList.find(segment);
if (it != m_AssemList.end()) {
switch (it->second) {
case Instruction::AT_ORG:
if (isLabel()) Errors::RecordError(Errors::CreateError("cannot have a label on the Assembly Instruction \"" + m_opCode + "\""));
break;
case Instruction::AT_DC:
case Instruction::AT_DS:
if (!isLabel()) Errors::RecordError(Errors::CreateError("Assembly Instructions \"" + m_opCode + "\"require a label."));
break;
default:
if (isLabel() || isOperand()) Errors::RecordError(Errors::CreateError("cannot have a label or operand on the END instruction."));
return;
}
if(!isOperand()){
Errors::RecordError(Errors::CreateError("missing operand on assembly instruction \"" + m_opCode + "\" "));
}
if (m_isNumericOperand && isOperand() && m_operandValue > m_MAX_VALUE) {
Errors::RecordError(Errors::CreateError("operand value too large for memory is greater than the max size of " + m_MAX_VALUE));
}
}
else {
Errors::RecordError(Errors::CreateError("FATAL ERROR :: read assembly instruction now missing in program memory."));
Errors::ReportCurrentError();
system("pause");
exit(1);
}
}
}
/**/
/*
Instruction::SyntaxMatch() Instruction::SyntaxMatch()
NAME
void Instruction::SyntaxMatch
DESCRIPTION
This function is responsible for determining if:
(1) there are operands on the instruction
(2) HALT does not take an operand
RETURNS
Returns a string with the latest error message.
*/
/**/
void Instruction::SyntaxMatch() {
if (!m_opCode.empty()) {
string segment = m_opCode;
transform(segment.begin(), segment.end(), segment.begin(), toupper);
auto it = m_MachList.find(segment);
if (it != m_MachList.end()) {
switch (it->second) {
case Instruction::MT_HALT:
if (isOperand()) Errors::RecordError(Errors::CreateError("operand not accepted on HALT machine instructions."));
return;
default:
if (!isOperand()) Errors::RecordError(Errors::CreateError("operand required after \"" + m_opCode +"\" machine instruction."));
break;
}
}
}
// There was no operation code on a line, report a fatal error.
else {
Errors::RecordError(Errors::CreateError("FATAL ERROR :: read machine instruction now missing in program memory."));
Errors::ReportCurrentError();
system("pause");
exit(1);
}
}