Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.04 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <list>
  4. #include <iterator>
  5. #include <string>
  6. #include <fstream>
  7. #include <regex>
  8. #include <map>
  9. #include <cstring>
  10.  
  11. using namespace std;
  12. typedef unsigned char byte;
  13.  
  14. typedef struct symb {
  15. int id;
  16. string name;
  17. int offset;
  18. int value;
  19. string sect_name;
  20. string type;
  21. string pc_relative;
  22. } symbol;
  23.  
  24. typedef struct sect {
  25. string name;
  26. string size;
  27. vector<symbol> symbols;
  28. } section;
  29. //sp //pc //psw
  30. const vector<string> register_masks = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1111"};
  31. const vector<string> register_array = {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "psw"};
  32. const vector<string> adressing_masks = {"000", "001", "010", "011", "100", "101"};
  33. const vector<string> zero_operand_masks = {"00001", "11000", "11001"};
  34. const vector<string> zero_operand_instr = {"halt", "ret", "iret"};
  35. const vector<string> one_operand_masks = {"00011", "01010", "10001", "10010", "10011", "10100", "10101", "10110",
  36. "10111"};
  37. const vector<string> one_operand_instr = {"int", "not", "push", "pop", "jmp", "jeq", "jne", "jgt", "call"};
  38. const vector<string> two_operand_masks = {"00010", "00100", "00101", "00111", "01000", "01001", "01011", "01100",
  39. "01101", "01110", "01111", "10000"};
  40. const vector<string> two_operand_instr = {"xchg", "mov", "add", "sub", "mul", "div", "cmp", "and", "or", "xor", "test",
  41. "shl", "shr"};
  42. const vector<string> operand_types = {".global", ".extern"};
  43. const vector<string> section_types = {".text", ".data", ".bss"};
  44. const vector<string> other_options = {".word", ".byte", ".char", ".long"};
  45. int howManyOp = 0;
  46. static int id = 0;
  47.  
  48. string getRegisterMask(string reg) {
  49. if (reg == "psw") return register_masks[8];
  50. if (reg == "sp") return register_masks[6];
  51. if (reg == "pc") return register_masks[7];
  52. int i;
  53. for (i = 0; i < 9; i++) {
  54. if (register_array[i] == reg) return register_masks[i];
  55. }
  56. return "greska";
  57. }
  58.  
  59. string getSection(string sect) {
  60. int i;
  61. for (i = 0; i < 3; i++) {
  62. if (section_types[i] == sect) return section_types[i];
  63. }
  64. return "greska";
  65. }
  66.  
  67. string getInstructionMask(string instr) {
  68. int i;
  69. cout << instr;
  70. for (i = 0; i < 9; i++) {
  71. if (zero_operand_instr[i] == instr) {
  72. howManyOp = 0;
  73. return zero_operand_masks[i];
  74. }
  75. if (one_operand_instr[i] == instr) {
  76. howManyOp = 1;
  77. return one_operand_masks[i];
  78. }
  79. if (two_operand_instr[i] == instr) {
  80. howManyOp = 2;
  81. cout << instr << "||oper]";
  82. return two_operand_masks[i];
  83. }
  84. }
  85. return "greska";
  86. }
  87.  
  88. int getInstructionBytes(string instr) {
  89. if (instr == ".char") return 1;
  90. if (instr == ".byte") return 1;
  91. if (instr == ".word") return 2;
  92. if (instr == ".long") return 4;
  93. return -1;
  94. }
  95.  
  96. int isNumber(string line) {
  97. int i;
  98. for (i = 0; i < line.length(); i++) {
  99. if (line[i] < '0' || line[i] > '9') return -1;
  100. }
  101. return 0;
  102. }
  103.  
  104.  
  105. int main() {
  106. vector<symbol> symbolTable, tempSymbTable;
  107. vector<section> sectionTable;
  108. string tempString, tempInstr, tempOp1, tempOp2, tempElem, tempSymbType = "local", tempSection = "UND";
  109. string codeFirst, codeSecond, tempLineCode = "";
  110. string byteORword = "1";
  111. int i;
  112. symbol symbNew;
  113.  
  114. section sectNew;
  115.  
  116. int allOffset = 0, sectionOffset = 0;
  117.  
  118. // ifstream inFile("C:\Users\Antoanea\Desktop\test.txt");
  119. // ifstream inFile("/home/marko95/JetBrains/Projects/ClionProjects/SSAsembler/test1.txt");
  120. ifstream inFile("text.txt");
  121. string line;
  122. if (!inFile.is_open()) {
  123. cout << "file not exists";
  124. exit(1);
  125. }
  126.  
  127. while (getline(inFile, line)) {
  128. if (line == ".end") {
  129. cout << "kraj fajla";
  130. return 0;
  131. }
  132. int isComment = line.find('@') != -1 ? line.find('@') : line.find(';');
  133. if (isComment != -1) {
  134. if (isComment == 0) continue;
  135. line = line.substr(0, isComment);
  136. }
  137. while (!line.empty()) {
  138. cout << line << " THIS IS LINE \n";
  139. tempString = line.find(' ') != -1 ? line.substr(0, line.find(' ')) : line; //trenutna rec
  140. cout << tempString << '\n';
  141. // if(tempString == "") continue;
  142. line = line.substr(line.find(' ') + 1);
  143.  
  144. if (tempString == ".global" || tempString == ".extern") {
  145. if (tempSection != "UND") {
  146. printf("Greska ne moze gobal posle sekcije");
  147. return 1;
  148. }
  149. tempSymbType = "global";
  150. while (!line.empty()) {
  151. tempElem = line.substr(0, line.find(','));
  152. cout << tempElem;
  153. line = line.substr(line.find(',') + 1);
  154. symbNew.id = id++;
  155. symbNew.name = tempElem;
  156. symbNew.sect_name = tempSection;
  157. symbNew.offset = 0; //mesto u sekciji
  158. symbNew.value = -1;
  159. symbNew.type = tempSymbType;
  160. symbNew.pc_relative = '0';
  161. symbolTable.push_back(symbNew);
  162. cout << tempElem;
  163. if (tempElem == line) line = "";
  164. }
  165.  
  166. } else {
  167. cout << tempString << "\n";
  168. if (getSection(tempString) != "greska" || tempString == ".section") {
  169. cout << "sekcija";
  170. //ako vec postoji neka sekcija ubacujemo je u niz sekcija
  171. if (tempSection != "UND") {
  172. sectNew.name = tempSection;
  173. sectNew.size = sectionOffset;
  174. sectNew.symbols = tempSymbTable;
  175. sectionTable.push_back(sectNew);
  176. }
  177. //ako je sekcija sa bilo kojim imenom moramo da dohvatimo ime sekcije
  178. if (tempString == ".section") {
  179. line = line.substr(line.find(' ') + 1);
  180. tempString = "." + line;
  181. }
  182. //dodajemo novu sekciju u tabelu simbola
  183. tempSection = tempString.substr(1);
  184. symbNew.id = id++;
  185. symbNew.name = tempSection;
  186. symbNew.sect_name = tempSection;
  187. symbNew.offset = 0; //mesto u sekciji
  188. symbNew.value = allOffset;
  189. symbNew.type = tempSymbType;
  190. symbNew.pc_relative = '0';
  191. symbolTable.push_back(symbNew);
  192. line = "";
  193. tempSymbTable = {};
  194. sectionOffset = 0;
  195. } else {
  196. if (tempSection == "UND") {
  197. // printf("%s \n", line);
  198. printf("Ne mogu instrukcije i labele pre prve sekcije");
  199. return 2;
  200. }
  201. if (tempString.find(':') != -1) {
  202. tempElem = tempString.substr(0, tempString.find(':'));
  203. cout << tempString << tempElem << " Nasli smo labelu\n";
  204. cout << "\n LINE" << line << "tempString" << tempString << "\n";
  205. tempString = line;
  206. symbNew.id = id++;
  207. symbNew.name = tempElem;
  208. symbNew.sect_name = tempSection;
  209. symbNew.offset = sectionOffset; //mesto u sekciji
  210. symbNew.value = allOffset; //ukupno mesto u fajlu
  211. symbNew.type = tempSymbType;
  212. symbNew.pc_relative = '0';
  213. symbolTable.push_back(symbNew);
  214. tempSymbTable.push_back(symbNew);
  215. cout << tempElem;
  216. if (tempString == line) break;
  217.  
  218. }
  219. cout << "temp zadnji " << tempString[tempString.length() - 1] << " temp zadnji";
  220. if ((tempString[tempString.length() - 1] == 'b' || tempString[tempString.length() - 1] == 'w') &&
  221. tempInstr.empty()) {
  222. byteORword = tempString[tempString.length() - 1] == 'b' ? "0" : "1";
  223. tempString = tempString.substr(0, tempString.length() - 1);
  224. }
  225. if (getInstructionMask(tempString) != "greska") {
  226. cout << "USLI SMO U DEO ZA INSTRUKCIJU" << getInstructionMask(tempString) << "\n";
  227. tempInstr = tempString;
  228. tempLineCode = getInstructionMask(tempString) + byteORword + "00";
  229. } else {
  230. if (tempInstr.empty()) {
  231. if (getInstructionBytes(tempString) != -1 || tempString == ".align" ||
  232. tempString == ".skip") {
  233. //ako je skip ili align dodajemo toliko bajtova na izlazni kod
  234. if (tempString == ".align" || tempString == ".skip") {
  235. for (i = 0; i < stoi(line); i++) {
  236. tempLineCode += "00";
  237. }
  238. } else {
  239. int isNum = isNumber(line);
  240. int tempValue = -1;
  241. if (isNum == -1) {
  242. for (i = 0; i < symbolTable.size(); i++) {
  243. if (symbolTable[i].name == line) {
  244. tempValue = symbolTable[i].value;
  245. break;
  246. }
  247. }
  248. if (tempValue != -1) {
  249. // tempLineCode += convertIntToString(tempValue);
  250. } else {
  251. symbNew.id = id++;
  252. symbNew.name = line;
  253. symbNew.sect_name = "UND";
  254. symbNew.offset = 0; //mesto u sekciji
  255. symbNew.value = -1;
  256. symbNew.type = tempSymbType;
  257. symbNew.pc_relative = '0';
  258. symbolTable.push_back(symbNew);
  259. cout << tempElem;
  260. if (tempElem == line) line = "";
  261. }
  262. } else {
  263.  
  264. }
  265. }
  266. line = "";
  267. } else {
  268. printf("Greska ne moze nista osim instrukcije posle labele");
  269. return 3;
  270. }
  271. } else {
  272.  
  273. }
  274. // if(!tempInstr.empty()) {
  275.  
  276. // }
  277. // else {
  278. // printf("Greska ne moze operand pre instrukcije");
  279. // return 3;
  280. // }
  281. }
  282. }
  283. }
  284.  
  285. }
  286. codeFirst += tempLineCode;
  287. tempSymbType = "local";
  288. tempLineCode = "";
  289. tempInstr = "";
  290. allOffset += 1;
  291. sectionOffset += 1;
  292. }
  293. return 0;
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement