Skip to content

Commit 2907894

Browse files
committed
Add log.txt for errors
1 parent 9fcc7ca commit 2907894

File tree

2 files changed

+85
-32
lines changed

2 files changed

+85
-32
lines changed

assembler/PassOne.cpp

+25-11
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,17 @@ std::string createIntermediate(std::string assemblyFile) {
4040
string intermediateFile = getIntermediateFileName(assemblyFile);
4141
ifstream fin(assemblyFile.c_str());
4242
ofstream fout(intermediateFile.c_str());
43+
ofstream log("log.txt");
44+
log << "Error at:\n";
4345

4446
vector<string> line;
4547
string opcode, operand, label;
4648
startAddr = 0;
4749
int locctr = 0;
50+
int lineNum = 5;
4851
string currBlock = "DEFAULT";
4952
int blockIndex = 0;
53+
bool errorFlag = false;
5054
insertBlock(currBlock, Block(blockIndex, intToHexStr(startAddr), intToHexStr(locctr)));
5155

5256
if (!fin.is_open()) {
@@ -77,8 +81,10 @@ std::string createIntermediate(std::string assemblyFile) {
7781
label = line[0];
7882
opcode = line[1];
7983
if (OPTAB.safeFind(opcode) == OPTAB.end()) {
80-
cerr << "Undefined Line\n";
81-
printLine(cerr, currLinelocctr, line);
84+
log << "Line " << lineNum << ", Unknown Line: ";
85+
printLine(log, currLinelocctr, line);
86+
errorFlag = true;
87+
cout << "\n";
8288
}
8389
}
8490
} else if (line.size() == 1) {
@@ -93,7 +99,6 @@ std::string createIntermediate(std::string assemblyFile) {
9399
programName = assemblyFile;
94100
}
95101
insertBlock(currBlock, Block(blockIndex, intToHexStr(startAddr), intToHexStr(locctr)));
96-
continue;
97102
} else if (opcode == "END") {
98103
printLine(fout, currLinelocctr, line);
99104
// update current block length
@@ -102,7 +107,6 @@ std::string createIntermediate(std::string assemblyFile) {
102107
break;
103108
} else if (opcode == "BASE" || opcode == "NOBASE") {
104109
printLine(fout, currLinelocctr, line);
105-
continue;
106110
} else if (opcode == "USE") {
107111
printLine(fout, currLinelocctr, line);
108112
string nextBlock = "DEFAULT";
@@ -122,14 +126,15 @@ std::string createIntermediate(std::string assemblyFile) {
122126
// update locctr
123127
locctr = hexStrToInt(it_next->second.blockLength);
124128
currBlock = nextBlock;
125-
continue;
126129
} else {
127130
if (!label.empty()) {
128131
if (SYMTAB.find(label) != SYMTAB.end()) {
129-
cerr << "Duplicate Symbol!\n";
130-
printLine(cerr, currLinelocctr, line);
132+
log << "Line " << lineNum << ", Duplicate Symbol!\n";
133+
printLine(log, currLinelocctr, line);
134+
cout << "\n";
135+
errorFlag = true;
131136
} else {
132-
SYMTAB.insert(pair<string, pss>(label, pss(intToHexStr(locctr), currBlock)));
137+
SYMTAB.insert(pair<string, pss >(label, pss(intToHexStr(locctr), currBlock)));
133138
}
134139
}
135140
auto it = OPTAB.safeFind(opcode);
@@ -160,18 +165,27 @@ std::string createIntermediate(std::string assemblyFile) {
160165
locctr += substr.size();
161166
}
162167
} else {
163-
cerr << "Invalid opcode!\n";
164-
printLine(cerr, currLinelocctr, line);
168+
log << "Line " << lineNum << ", Invalid opcode!\n";
169+
printLine(log, currLinelocctr, line);
170+
cout << "\n";
171+
errorFlag = true;
165172
}
166173
printLine(fout, currLinelocctr, line);
167-
continue;
168174
}
175+
lineNum += 5;
169176
}
170177
string retVal = fout.is_open() ? intermediateFile : "Not created";
171178
fout.close();
172179
fin.close();
180+
log.close();
173181
Block lastBlock = updateBlockAddr();
174182
programLength = hexStrToInt(lastBlock.blockAddr) + hexStrToInt(lastBlock.blockLength);
183+
if (errorFlag) {
184+
cerr << "Assembly not successful, check log.txt for more info\n";
185+
}
186+
else{
187+
cout << "Intermediate File successfully generated\n";
188+
}
175189
return retVal;
176190
}
177191

assembler/PassTwo.cpp

+60-21
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
using namespace std;
1414

15+
ofstream log("log.txt", ios::ate | ios::app);
16+
int lineNum = 0;
17+
bool errorFlag = false;
18+
1519
/* ========== Methods to determine which relative addressing mode is to be used ========== */
1620
bool checkPCRel(int operandVal);
1721

@@ -75,6 +79,15 @@ printListLine(ostream &lout, int lineNum, int locctr, string label, string opcod
7579
lout << setw(10) << left << objectcode << "\n";
7680
}
7781

82+
void printLogLine(vector<string> &line) {
83+
errorFlag = true;
84+
std::string outLine = "";
85+
for (auto const &s : line) {
86+
outLine += s + " ";
87+
}
88+
log << outLine << "\n";
89+
}
90+
7891
string createObjectFile(std::string intermediateFile) {
7992
ifstream fin(intermediateFile.c_str());
8093
if (!fin.is_open()) {
@@ -86,6 +99,9 @@ string createObjectFile(std::string intermediateFile) {
8699

87100
ofstream fout(objectFile.c_str());
88101
ofstream lout(listingFile.c_str());
102+
if (!log.is_open()) {
103+
log.open("log.txt", ios::ate | ios::app);
104+
}
89105

90106
prepareListingFile(lout);
91107

@@ -95,13 +111,14 @@ string createObjectFile(std::string intermediateFile) {
95111
}
96112
vector<string> line;
97113
string opcode, operand, label, locctr;
98-
int lineNum = 5;
114+
lineNum = 5;
99115
string baseLocHex;
100116
string textRecord = "T";
101117
string objectCode = "";
102118
string modificationRecord = "";
103119
bool shouldAddMRecord = startAddr == 0;
104120
bool canUseBase = false;
121+
errorFlag = false;
105122
string currBlock = "DEFAULT";
106123
while (readLine(fin, line)) {
107124
// Skip the line if it's empty or contains on the location counter's value
@@ -149,12 +166,13 @@ string createObjectFile(std::string intermediateFile) {
149166
if (instrInfo.first == "1") {
150167
instructionHex = instrInfo.second;
151168
} else if (instrInfo.first == "2") {
152-
cerr << "No operand specified for " << opcode << " instruction\n";
169+
log << "Line: " << lineNum << ", No operand specified for " << opcode << " instruction\n";
153170
} else if (safe(opcode) == "RSUB") {
154171
int xbpe = opcode[0] == '+' ? 1 : 0;
155172
instructionHex = assInstr_34(instrInfo.second, 3, xbpe, 0);
156173
} else {
157-
cerr << "No operand specified for ";
174+
log << "Line: " << lineNum << ", No operand specified for " << opcode << " instruction\n";
175+
printLogLine(line);
158176
}
159177
break;
160178
}
@@ -169,7 +187,9 @@ string createObjectFile(std::string intermediateFile) {
169187
sym_it = it_sym;
170188
ni = 1;
171189
} else {
172-
cerr << "Can't use Immediate addr with format " << instrInfo.first << "\n";
190+
log << "Line: " << lineNum << "Can't use Immediate addr with format " << instrInfo.first
191+
<< "\n";
192+
printLogLine(line);
173193
}
174194
} else {
175195
instructionHex = handleImmediateIntegerOperand(instrInfo.second, operand, opcode[0]);
@@ -187,10 +207,13 @@ string createObjectFile(std::string intermediateFile) {
187207
sym_it = it_sym;
188208
ni = 2;
189209
} else {
190-
cerr << "Can't use indirect addr with format " << instrInfo.first << "\n";
210+
log << "Line: " << lineNum << ", Can't use indirect addr with format "
211+
<< instrInfo.first << "\n";
212+
printLogLine(line);
191213
}
192214
} else {
193-
cerr << "Symbol: " << it_sym->first << " not found in symtab";
215+
log << "Line: " << lineNum << ", Symbol: " << it_sym->first << " not found in symtab";
216+
printLogLine(line);
194217
}
195218
break;
196219
}
@@ -207,19 +230,23 @@ string createObjectFile(std::string intermediateFile) {
207230
x = 8;
208231
break;
209232
} else if (instrInfo.first != "2") {
210-
cerr << "Can't use indexed addr with format " << instrInfo.first << "\n";
233+
log << "Line: " << lineNum << ", Can't use indexed addr with format " << instrInfo.first
234+
<< "\n";
235+
printLogLine(line);
211236
break;
212237
}
213238
} else {
214-
cerr << "Symbol: " << it_sym->first << " not found in symtab";
239+
log << "Line: " << lineNum << ", Symbol: " << it_sym->first << " not found in symtab";
240+
printLogLine(line);
215241
break;
216242
}
217243
// Break not added to allow the passing of format 2 instructions into the next case.
218244
}
219245
// Two register instructions
220246
case 4: {
221247
if (instrInfo.first != "2") {
222-
cerr << "Unknown symbol: " << operand;
248+
log << "Line: " << lineNum << ", Unknown symbol: " << operand;
249+
printLogLine(line);
223250
} else {
224251
string r1, r2;
225252
unsigned long pos = operand.find(',');
@@ -246,7 +273,8 @@ string createObjectFile(std::string intermediateFile) {
246273
ni = 3;
247274
}
248275
} else {
249-
cerr << "Symbol: " << it_sym->first << " not found in symtab";
276+
log << "Line: " << lineNum << ", Symbol: " << it_sym->first << " not found in symtab";
277+
printLogLine(line);
250278
}
251279
break;
252280
}
@@ -266,7 +294,8 @@ string createObjectFile(std::string intermediateFile) {
266294
} else if (canUseBase && checkBaseRel(baseRelOp)) {
267295
instructionHex = assInstr_34(instrInfo.second, ni, x + 4, baseRelOp);
268296
} else {
269-
cerr << "Can't use PC rel or Base rel addr to assemble this instr\n";
297+
log << "Line: " << lineNum << ", Can't use PC rel or Base rel addr to assemble this instr\n";
298+
printLogLine(line);
270299
}
271300
}
272301
} else if (opcode == "BYTE" | opcode == "WORD") {
@@ -284,7 +313,8 @@ string createObjectFile(std::string intermediateFile) {
284313
instructionHex = intToHexStr(dec_val);
285314
}
286315
catch (const invalid_argument &ia) {
287-
cerr << "wrong WORD value " << operand << "\n";
316+
log << "Line: " << lineNum << ", wrong WORD value " << operand << "\n";
317+
printLogLine(line);
288318
}
289319
}
290320
}
@@ -304,6 +334,12 @@ string createObjectFile(std::string intermediateFile) {
304334
string retVal = fout.is_open() ? objectFile : "Not created";
305335
fin.close();
306336
fout.close();
337+
log.close();
338+
if (errorFlag) {
339+
cerr << "Assembly not successful, check log.txt for more info\n";
340+
} else {
341+
cout << "Object program File successfully generated!\n";
342+
}
307343
return retVal;
308344
}
309345

@@ -353,9 +389,6 @@ void initVariables(vector<string> &line, string &label, string &opcode, string &
353389
if (OPTAB.safeFind(opcode) == OPTAB.end()) {
354390
label = line[1];
355391
opcode = line[2];
356-
if (OPTAB.safeFind(operand) == OPTAB.end()) {
357-
cerr << "Unknown Line\n";
358-
}
359392
}
360393
} else if (line.size() == 2) {
361394
opcode = line[1];
@@ -369,30 +402,35 @@ string handleImmediateIntegerOperand(string opcodeHex, const string &operand, ch
369402
operand_dec = static_cast<unsigned int>(stoi(_operand));
370403
}
371404
catch (const invalid_argument &ia) {
372-
cerr << "Unspecified immediate operand: " << _operand << "\n";
405+
log << "Line: " << lineNum << ", Unspecified immediate operand: " << _operand << "\n";
406+
errorFlag = true;
373407
return "";
374408
}
375409
if (opcode_first_char == '+') {
376410
if (operand_dec > 1048575) {
377-
cerr << "operand too large to fit in format 4: " << operand;
411+
log << "Line: " << lineNum << ", operand too large to fit in format 4: " << operand;
412+
errorFlag = true;
378413
} else {
379414
return assInstr_34(opcodeHex, 1, 1, operand_dec);
380415
}
381416
} else if (operand_dec < 4096) {
382417
return assInstr_34(opcodeHex, 1, 0, operand_dec);
383418
} else {
384-
cerr << "operand too large to fit in format 3: " << operand;
419+
log << "Line: " << lineNum << ", operand too large to fit in format 3: " << operand;
420+
errorFlag = true;
385421
}
386422
}
387423

388424
template<typename T>
389425
bool isRegisterValid(T it1, const string &operand) {
390426
if (it1 == SYMTAB.end()) {
391-
cerr << "Invalid register symbol: " << operand << "\n";
427+
log << "Line: " << lineNum << ", Invalid register symbol: " << operand << "\n";
428+
errorFlag = true;
392429
return false;
393430
}
394431
if (stoi(it1->second.first) < 0 || stoi(it1->second.first) > 9 || stoi(it1->second.first) == 7) {
395-
cerr << "Expected a register, found: " << it1->first << "\n";
432+
log << "Line: " << lineNum << ", Expected a register, found: " << it1->first << "\n";
433+
errorFlag = true;
396434
return false;
397435
}
398436
return true;
@@ -406,7 +444,8 @@ string assInstr_2(string opcodeHex, string r1, string r2) {
406444
b = stoi(r2);
407445
}
408446
catch (const invalid_argument &ia) {
409-
cerr << "Not correct register types: " << r1 << " " << r2 << "\n";
447+
log << "Line: " << lineNum << ", Not correct register types: " << r1 << " " << r2 << "\n";
448+
errorFlag = true;
410449
}
411450
return intToHexStr((opcode_dec << 8) + a + b, 4);
412451
}

0 commit comments

Comments
 (0)