Advertisement
ShrekOP

Assg1 C++

Dec 16th, 2022
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.33 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class Tables
  4. {
  5. public:
  6. int LC = 0, LT[2] = {0};
  7. vector<vector<string>> MOT{
  8. {"AREG", "RG", "01"},
  9. {"BREG", "RG", "02"},
  10. {"CREG", "RG", "03"},
  11. {"DREG", "RG", "04"},
  12. {"EQ", "CC", "01"},
  13. {"LT", "CC", "02"},
  14. {"GT", "CC", "03"},
  15. {"LE", "CC", "04"},
  16. {"GE", "CC", "05"},
  17. {"ANY", "CC", "06"},
  18. {"STOP", "IS", "00"},
  19. {"ADD", "IS", "01"},
  20. {"SUB", "IS", "02"},
  21. {"MULT", "IS", "03"},
  22. {"MOVER", "IS", "04"},
  23. {"MOVEM", "IS", "05"},
  24. {"COMP", "IS", "06"},
  25. {"BC", "IS", "07"},
  26. {"DIV", "IS", "08"},
  27. {"READ", "IS", "09"},
  28. {"PRINT", "IS", "10"},
  29. {"LOAD", "IS", "11"},
  30. {"START", "AD", "01"},
  31. {"END", "AD", "02"},
  32. {"ORIGIN", "AD", "03"},
  33. {"EQU", "AD", "04"},
  34. {"LTORG", "AD", "05"},
  35. {"DS", "DL", "01"},
  36. {"DC", "DL", "02"}};
  37. vector<vector<string>> CODE, INTCODE, SYMTAB, LITTAB, POOLTAB;
  38. int getIndex(string str, vector<vector<string>> table) {
  39. for (int i = 0; i < table.size(); i++)
  40. if (str == table[i][0])
  41. return i;
  42. return -1;
  43. }
  44. string getClass(string mnemonic)
  45. {
  46. int i = getIndex(mnemonic, MOT);
  47. if (i != -1)
  48. return MOT[i][1];
  49. return "NULL";
  50. }
  51. string getOpCODE(string mnemonic)
  52. {
  53. int i = getIndex(mnemonic, MOT);
  54. if (i != -1)
  55. return MOT[i][2];
  56. return "NULL";
  57. }
  58. bool isMnemonic(string str)
  59. {
  60. if (getIndex(str, MOT) > -1)
  61. return true;
  62. return false;
  63. }
  64. bool isConstant(string str)
  65. {
  66. try
  67. {
  68. if (stoi(str) >= 0)
  69. return true;
  70. }
  71. catch (exception &e)
  72. {
  73. return false;
  74. }
  75. return false;
  76. }
  77. bool isOperator(string str)
  78. {
  79. if (str.length() == 1)
  80. if (int(str[0]) > 41 && int(str[0]) < 48)
  81. return true;
  82. return false;
  83. }
  84. bool isLiteral(string str)
  85. {
  86. if (str[0] == '=')
  87. return true;
  88. return false;
  89. }
  90. bool isSymbol(string str)
  91. {
  92. if (!isMnemonic(str) && !isConstant(str) && !isOperator(str) && !isLiteral(str)) return true;
  93. return false;
  94. }
  95. void genTAB()
  96. {
  97. for (int i = 0; i < CODE.size(); i++)
  98. {
  99. for (int j = 0; j < CODE[i].size(); j++)
  100. {
  101. if (isSymbol(CODE[i][j]) && getIndex(CODE[i][j], SYMTAB)== -1 && j != 0)
  102. SYMTAB.push_back({CODE[i][j]});
  103. else if (isLiteral(CODE[i][j]))
  104. LITTAB.push_back({CODE[i][j]});
  105. }
  106. }
  107. }
  108. void showTAB(vector<vector<string>> table, string name)
  109. {
  110. cout << "\n---------------------------------" << endl;
  111. cout << " " << name << endl;
  112. cout << "---------------------------------\n"
  113. << endl;
  114. for (int i = 0; i < table.size(); i++)
  115. {
  116. for (int j = 0; j < table[i].size(); j++)
  117. cout << " " << table[i][j] << "\t";
  118. if (table[i].size() != 0)
  119. cout << endl;
  120. }
  121. cout << endl;
  122. }
  123. void genINTCODE()
  124. {
  125. for (int i = 0; i < CODE.size(); i++)
  126. {
  127. vector<string> temp;
  128. for (int j = 0; j < CODE[i].size(); j++)
  129. {
  130. string result = "";
  131. if (isMnemonic(CODE[i][j]) && getClass(CODE[i][j]) != "CC")
  132. {
  133. if (getClass(CODE[i][j]) == "RG")
  134. result = "(" + to_string(stoi(getOpCODE(CODE[i][j]))) + ")";
  135. else
  136. result = "(" + getClass(CODE[i][j]) + ", " + getOpCODE(CODE[i][j]) + ")";
  137. if (CODE[i][j] == "ORIGIN")
  138. {
  139. temp.push_back(result);
  140. break;
  141. }
  142. }
  143. else if (isConstant(CODE[i][j]))
  144. {
  145. if (stoi(CODE[i][j]) < 10)
  146. result = "(C, 0" + to_string(stoi(CODE[i][j])) + ")";
  147. else
  148. result = "(C, " + CODE[i][j] + ")";
  149. }
  150. else if (isLiteral(CODE[i][j]))
  151. {
  152. if (getIndex(CODE[i][j], LITTAB) == -1)
  153. LITTAB.push_back({CODE[i][j]});
  154. if (j != 0)
  155. if (getIndex(CODE[i][j], LITTAB) + 1 < 10)
  156. result = "(L, 0" + to_string(getIndex(CODE[i][j], LITTAB) + 1) + ")";
  157. else
  158. result = "(L, " + to_string(getIndex(CODE[i][j], LITTAB) + 1) + ")";
  159. }
  160. else if (isSymbol(CODE[i][j]))
  161. {
  162. if (getIndex(CODE[i][j], SYMTAB) == -1)
  163. SYMTAB.push_back({CODE[i][j]});
  164. if (j != 0)
  165. if (getIndex(CODE[i][j], SYMTAB) + 1 < 10)
  166. result = "(S, 0" + to_string(getIndex(CODE[i][j], SYMTAB) + 1) + ")";
  167. else
  168. result = "(S, " + to_string(getIndex(CODE[i][j], SYMTAB) + 1) + ")";
  169. }
  170. if (result != "")
  171. temp.push_back(result);
  172. }
  173. if (temp.size() == 2)
  174. {
  175. string tmp = temp[1];
  176. temp.erase(temp.begin() + 1);
  177. temp.push_back(" ");
  178. temp.push_back(tmp);
  179. }
  180. INTCODE.push_back(temp);
  181. }
  182. }
  183. void genAddrTAB()
  184. {
  185. LC = stoi(CODE[0][1]) - 1;
  186. for (int i = 0; i < CODE.size(); i++)
  187. {
  188. if (CODE[i].size() > 0)
  189. {
  190. int k = 0;
  191. for (int j = 0; j < CODE[i].size(); j++)
  192. if (isLiteral(CODE[i][j]))
  193. LT[1]++;
  194. if (isSymbol(CODE[i][0]))
  195. k = 1;
  196. if (CODE[i][k] == "EQU")
  197. {
  198. LC++;
  199. SYMTAB[getIndex(CODE[i][0], SYMTAB)].push_back(to_string(LC));
  200. }
  201. else if (getClass(CODE[i][k]) != "AD")
  202. {
  203. LC++;
  204. if (isSymbol(CODE[i][0]))
  205. {
  206. SYMTAB[getIndex(CODE[i][0], SYMTAB)].push_back(to_string(LC));
  207. }
  208. }
  209. else if (CODE[i][k] == "LTORG" || CODE[i][k] == "END")
  210. {
  211. bool pflag = false;
  212. vector<string> temp;
  213. for (int m = LT[0]; m < LT[1]; m++)
  214. {
  215. LC++;
  216. LITTAB[m].push_back(to_string(LC));
  217. if (!pflag)
  218. {
  219. temp.push_back(to_string(LT[0] + 1));
  220. POOLTAB.push_back(temp);
  221. pflag = true;
  222. }
  223. }
  224. LT[0] = LT[1];
  225. }
  226. else
  227. {
  228. if (CODE[i][k] == "ORIGIN")
  229. {
  230. if (CODE[i].size() == 2)
  231. {
  232. if (isSymbol(CODE[i][k + 1]))
  233. LC = stoi(SYMTAB[getIndex(CODE[i][k + 1], SYMTAB)][1]) - 1;
  234. else
  235. LC++;
  236. }
  237. else
  238. {
  239. switch (CODE[i][k + 2][0])
  240. {
  241. case '+':
  242. LC = (stoi(SYMTAB[getIndex(CODE[i][k + 1],
  243. SYMTAB)][1]) + stoi(CODE[i][k + 3])) - 1;
  244. break;
  245. case '-':
  246. LC = (stoi(SYMTAB[getIndex(CODE[i][k + 1],
  247. SYMTAB)][1]) - stoi(CODE[i][k + 3])) - 1;
  248. break;
  249. case '*':
  250. LC = (stoi(SYMTAB[getIndex(CODE[i][k + 1],
  251. SYMTAB)][1]) * stoi(CODE[i][k + 3])) - 1;
  252. break;
  253. case '/':
  254. LC = (stoi(SYMTAB[getIndex(CODE[i][k + 1],
  255. SYMTAB)][1]) / stoi(CODE[i][k + 3])) - 1;
  256. break;
  257. }
  258. }
  259. }
  260. }
  261. CODE[i].push_back(to_string(LC));
  262. }
  263. }
  264. }
  265. };
  266. class Assembler : public Tables
  267. {
  268. public:
  269. void toVector(string filename)
  270. {
  271. string data, word;
  272. ifstream file(filename);
  273. while (getline(file, data))
  274. {
  275. string line = " ";
  276. for (int i = 0; i < data.length(); i++)
  277. {
  278. if (data[i] == ';')
  279. break;
  280. else if (data[i] == ',')
  281. line += ' ';
  282. else if (data[i] == '+')
  283. line += " + ";
  284. else if (data[i] == '-')
  285. line += " - ";
  286. else if (data[i] == '*')
  287. line += " * ";
  288. else if (data[i] == '/')
  289. line += " / ";
  290. else
  291. line += data[i];
  292. }
  293. istringstream iss(line);
  294. vector<string> temp;
  295. for (string word; iss >> word;)
  296. {
  297. temp.push_back(word);
  298. }
  299. CODE.push_back(temp);
  300. }
  301. file.close();
  302. }
  303. };
  304. int main(int argc, char **argv)
  305. {
  306. Assembler assm;
  307. assm.toVector("input.asm");
  308. assm.genTAB();
  309. assm.genINTCODE();
  310. assm.genAddrTAB();
  311. assm.showTAB(assm.INTCODE, "Intermediate Code (IC)");
  312. assm.showTAB(assm.SYMTAB, "Symbol Table (ST)");
  313. assm.showTAB(assm.LITTAB, "Literal Table (LT)");
  314. assm.showTAB(assm.POOLTAB, "Pool Table (PT)"); return 0;
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement