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
|
#include "stdafx.h"
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <fstream>
using namespace std;
int checkpseudo(string text);
void checkMachine(ofstream& outputST , ofstream& demoLT , int& LC , int& L , string text0 , string text1 , string text2);
void checkLabel(ofstream& outputST, int& LC , int& L , string text0 , string text1 , string text2);
void checkDS(ofstream& outputST , int& LC , int& L , string text0 , string text1 , string text2);
void checkDC(ofstream& outputST , int& LC , int& L , string text0 , string text1 , string text2);
void checkLTORG(ofstream& outputLT , ofstream& demoLT , int& LC);
void checkEND(ofstream& outputLT , ofstream& demoLT , int& LC);
int _tmain(int argc, _TCHAR* argv[])
{
int L = 0 , LC = 0 , i = 0;
string field[3];
char read[100];
char *pch;
ifstream readfile;
ofstream output2 , outputST , demoLT , outputLT;
readfile.open("input1.txt");
output2.open("input2.txt");
outputST.open("ST.txt");
demoLT.open("LT.DAT");
outputLT.open("LT.txt");
if(!readfile)
{
cerr << "Can not open file \"input1.txt\" " << endl;
exit(1);
}
if(!output2)
{
cerr << "Can not open file \"input2.txt\" " << endl;
exit(2);
}
if(!outputST)
{
cerr << "Can not open file \"ST.txt\" " << endl;
exit(102);
}
while(!readfile.eof())
{
//cin >> read;
readfile.getline(read,100);
//====================== Field Filter ================
pch = strtok(read," ");
i = 0;
while (pch != NULL)
{
//cout << pch << endl;
field[i] = pch;
pch = strtok(NULL, " ");
i++;
}
//====================================================
if(i == 2)
{
field[2] = field[1];
field[1] = field[0];
field[0] = "";
}
if(i == 1)
{
field[2] = "";
field[1] = field[0];
field[0] = "";
}
cout << left << setw(10) << field[0] << setw(10) << field[1] << setw(10) << field[2] << endl;
switch(checkpseudo(field[1]))
{
case 1 : checkDS(outputST , LC , L , field[0] , field[1] , field[2]); break;
case 2 : checkDC(outputST , LC , L , field[0] , field[1] , field[2]); break;
case 3 : checkLabel(outputST, LC , L , field[0] , field[1] , field[2]); break;
case 4 : break;
case 5 : break;
case 6 : checkEND(outputLT , demoLT , LC);break;
case 7 : checkLTORG(outputLT , demoLT , LC); break;
case 8 : checkLabel(outputST, LC , L , field[0] , field[1] , field[2]); break;
case 0 : checkMachine(outputST , demoLT , LC , L , field[0] , field[1] , field[2]); break;
}
output2 << left << setw(10) << field[0] << setw(10) << field[1] << setw(10) << field[2] << endl;
field[0] = "";
field[1] = "";
field[2] = "";
}
return 0;
}
int checkpseudo(string text)
{
if(text == "DS")
{return 1;}
else if(text == "DC")
{return 2;}
else if(text == "EQU")
{return 3;}
else if(text == "USING")
{return 4;}
else if(text == "DROP")
{return 5;}
else if(text == "END")
{return 6;}
else if(text == "LTORG")
{return 7;}
else if(text == "START")
{return 8;}
else
{return 0;}
}
void checkLabel(ofstream& outputST , int& LC , int& L , string text0 , string text1 , string text2)
{
int tempLC = LC;
char relocation = 'R';
if(text0 != "")
{
if(text1 == "EQU" || text1 == "START")
{
L = 1;
if(text1 == "EQU" && text2 != "*")
{
LC = atol(text2.c_str()); // Strint to int
relocation = 'A';
}
else
{
relocation = 'R';
}
}
else
{
L = 4;
}
outputST << left << setw(10) << text0 << setw(10) << LC << setw(10) << L << setw(10) << relocation << endl;
LC = tempLC;
}
}
void checkDS(ofstream& outputST , int& LC , int& L , string text0 , string text1 , string text2)
{
int tempLC = LC;
char relocation = 'R';
string lines = text2;
while(LC % 4 != 0)
{
LC ++;
}
L = 4;
string textOperand (text2 , 0 , text2.length()-1);
if(text0 != "")
{
outputST << left << setw(10) << text0 << setw(10) << LC << setw(10) << L << setw(10) << relocation << endl;
}
tempLC = atol(textOperand.c_str());
tempLC = tempLC * 4;
LC = LC + tempLC;
}
void checkDC(ofstream& outputST , int& LC , int& L , string text0 , string text1 , string text2)
{
int tempLC = LC;
int Count = 0 ;
char relocation = 'R';
while(LC % 4 != 0)
{
LC ++;
}
L = 4;
string textOperand = text2;
string delimiters = "F',";
unsigned int end;
unsigned int start = textOperand.find_first_not_of (delimiters);
while (start != string::npos)
{
end = textOperand.find_first_of (delimiters, start + 1);
string str= textOperand.substr (start , end - start);
start = textOperand.find_first_not_of (delimiters, end + 1);
Count++;
}
if(text0 != "")
{
outputST << left << setw(10) << text0 << setw(10) << LC << setw(10) << L << setw(10) << relocation << endl;
}
tempLC = Count * 4;
LC = tempLC;
}
void checkMachine(ofstream& outputST , ofstream& demoLT , int& LC , int& L , string text0 , string text1 , string text2)
{
char relocation = 'R';
string Check;
string s2(text1 , text1.length()-1 , 1);
if(s2 == "R")
L = 2;
else
L = 4;
int locationCut = text2.rfind("=");
if(locationCut != string::npos)
{
Check = text2.substr(locationCut + 1);
demoLT << left << setw(10) << Check << setw(10) << L << setw(10) << relocation << endl;
}
if(text0 != "")
{
outputST << left << setw(10) << text0 << setw(10) << LC << setw(10) << L << setw(10) << relocation << endl;
}
LC = LC + L;
}
void checkLTORG(ofstream& outputLT , ofstream& demoLT , int& LC)
{
ifstream readLT;
readLT.open("LT.DAT");
string field[4];
while(LC % 8 != 0)
{
LC ++;
}
while(!readLT.eof())
{
readLT >> field[0] >> field[1] >> field[2];
outputLT << left << setw(10) << field[0] << setw(10) << LC << setw(10) << field[1] << setw(10) << field[2] << endl;
int L = 4;
LC = LC + L;
}
}
void checkEND(ofstream& outputLT , ofstream& demoLT , int& LC)
{
ifstream readLT;
readLT.open("LT.DAT");
string field[4];
while(!readLT.eof())
{
readLT >> field[0] >> field[1] >> field[2];
outputLT << left << setw(10) << field[0] << setw(10) << LC << setw(10) << field[1] << setw(10) << field[2] << endl;
int L = 4;
LC = LC + L;
}
}
|