Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <iterator>
  4. #include <string>
  5. #include <fstream>
  6. #include <regex>
  7. #include <map>
  8. #include <cstring>
  9. #include <sstream>
  10. #include <bitset>
  11.  
  12. using namespace std;
  13. typedef unsigned char byte;
  14.  
  15. typedef struct symb {
  16. int id;
  17. string name;
  18. int offset;
  19. string value;
  20. string sect_name;
  21. string type;
  22. string pc_relative;
  23. } symbol;
  24.  
  25. typedef struct sect {
  26. string name;
  27. string size;
  28. vector<symbol> symbols;
  29. } section;
  30. //sp //pc //psw
  31. const vector<string> register_masks = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1111"};
  32. const vector<string> register_array = {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "psw"};
  33. const vector<string> adressing_masks = {"000", "001", "010", "011", "100", "101"};
  34. const vector<string> zero_operand_masks = {"00001", "11000", "11001"};
  35. const vector<string> zero_operand_instr = {"halt", "ret", "iret"};
  36. const vector<string> one_operand_masks = {"00011", "01010", "10001", "10010", "10011", "10100", "10101", "10110",
  37. "10111"};
  38. const vector<string> one_operand_instr = {"int", "not", "push", "pop", "jmp", "jeq", "jne", "jgt", "call"};
  39. const vector<string> two_operand_masks = {"00010", "00100", "00101", "00111", "01000", "01001", "01011", "01100",
  40. "01101", "01110", "01111", "10000"};
  41. const vector<string> two_operand_instr = {"xchg", "mov", "add", "sub", "mul", "div", "cmp", "and", "or", "xor", "test",
  42. "shl", "shr"};
  43. const vector<string> operand_types = {".global", ".extern"};
  44. const vector<string> section_types = {".text", ".data", ".bss"};
  45. const vector<string> other_options = {".word", ".byte", ".char", ".long"};
  46. int howManyOp = 0;
  47. string tempInstr = "";
  48. static int id = 0;
  49. vector<symbol> symbolTable, tempSymbTable;
  50. vector<section> sectionTable;
  51. string tempString, tempOp1, tempOp2, tempElem, tempSymbType = "local", tempSection = "UND";
  52. string codeFirst, codeSecond, tempLineCode = "";
  53. string byteORword = "1";
  54.  
  55. string getRegisterMask(string reg) {
  56. if (reg == "psw") return register_masks[8];
  57. if (reg == "sp") return register_masks[6];
  58. if (reg == "pc") return register_masks[7];
  59. int i;
  60. for (i = 0; i < 9; i++) {
  61. if (register_array[i] == reg) return register_masks[i];
  62. }
  63. return "greska";
  64. }
  65.  
  66. string getSection(string sect) {
  67. int i;
  68. for (i = 0; i < 3; i++) {
  69. if (section_types[i] == sect) return section_types[i];
  70. }
  71. return "greska";
  72. }
  73.  
  74. string getInstructionMask(string instr) {
  75. int i;
  76. for(i = 0; i < 3; i++) {
  77. if (zero_operand_instr[i] == instr) {
  78. howManyOp = 0;
  79. return zero_operand_masks[i];
  80. }
  81. }
  82. for (i = 0; i < 9; i++) {
  83. if (one_operand_instr[i] == instr) {
  84. howManyOp = 1;
  85. return one_operand_masks[i];
  86. }
  87.  
  88. }
  89. for(i = 0; i < 12; i++) {
  90. if (two_operand_instr[i] == instr) {
  91. howManyOp = 2;
  92. return two_operand_masks[i];
  93. }
  94. }
  95. return "greska";
  96. }
  97.  
  98. int getInstructionBytes(string instr) {
  99. // if (instr == ".char") return 1;
  100. if (instr == ".byte") return 1;
  101. if (instr == ".word") return 2;
  102. // if (instr == ".long") return 4;
  103. return -1;
  104. }
  105.  
  106. int isNumber(string line) {
  107. int i;
  108. for (i = 0; i < line.length(); i++) {
  109. if (line[i] < '0' || line[i] > '9') return -1;
  110. }
  111. cout << "jeste br" ;
  112. return 0;
  113. }
  114.  
  115. int isHexNumber(string line) {
  116. if(line[0] == '0' && line[1] == 'x') return 0;
  117. return -1;
  118. }
  119.  
  120. string convertToBin(string value, int sendBits) {
  121. cout << value;
  122. int intOrHex = isNumber(value) != -1 ? 10 : 16;
  123. stringstream ss;
  124. unsigned n;
  125. cout<<"convert" << endl;
  126. ss << hex << stoi(value, nullptr, intOrHex);
  127. ss >> n;
  128. cout << "usli u convert to bin" << endl;
  129. int numBitset = sendBits;
  130. // (sendBits != 0) ? sendBits : (value.length() == 6 ? 16 : 8) ;
  131. if(sendBits == 8 || intOrHex == 10) {
  132. cout << "usli u number 8" << endl;
  133. bitset<8> b(n);
  134. cout << b.to_string() << endl;
  135. return b.to_string();
  136. }
  137. else {
  138. bitset<16> b1(n);
  139. string changeBytes = b1.to_string();
  140. changeBytes = changeBytes.substr(8) + changeBytes.substr(0,8);
  141. return changeBytes;
  142. }
  143. // outputs "00000000000000000000000000001010"
  144. // cout << b.to_string() << endl;
  145.  
  146. }
  147.  
  148. string addSymbol(string symb, int twoBytes) {
  149. int i;
  150. string tempValue = "UND";
  151. for (i = 0; i < symbolTable.size(); i++) {
  152. if (symbolTable[i].name == symb) {
  153. cout << "simbol vrednost" << symbolTable[i].name;
  154. tempValue = symbolTable[i].value;
  155. break;
  156. }
  157. }
  158. cout << endl;
  159. cout << tempValue << "TEMP VALUE";
  160. if (tempValue != "UND") {
  161. return convertToBin(tempValue, twoBytes == 1 ? 16 : 8);
  162. } else {
  163. symbol symbNew;
  164. symbNew.id = id++;
  165. symbNew.name = symb;
  166. symbNew.sect_name = "UND";
  167. symbNew.offset = 0; //mesto u sekciji
  168. symbNew.value = "-1";
  169. symbNew.type = tempSymbType;
  170. symbNew.pc_relative = '0';
  171. symbolTable.push_back(symbNew);
  172. return twoBytes == 1 ? "1111111111111111" : "11111111";
  173. }
  174. }
  175.  
  176.  
  177. string findOperandMask(string oper, int numOfOper) {
  178. cout << "usli u find operand" << oper << endl;
  179. string operCut = oper.length() > 1 && (oper[2] == 'l' || oper[2] == 'h') ? oper.substr(1) : oper;
  180. //registarsko direktno
  181. cout << "usli u find operand" << endl;
  182. if(getRegisterMask(operCut) != "greska") {
  183. cout << "reg dir" << endl;
  184. return "001" + getRegisterMask(operCut) + (oper[2] == 'h' ? "1" : "0");
  185. }
  186. else {
  187. cout << "usli u else znaci nije reg dir" << endl;
  188. //neposredno adresiranje
  189. if((numOfOper == 2 && howManyOp ==2) || tempInstr == "push") {
  190. cout << "neposredno" << endl;
  191. if(oper[0] == '&') {
  192. return "00000000" + addSymbol(oper.substr(1), 0);
  193. }
  194. else {
  195. cout <<"else neposredno"<< endl;
  196.  
  197. if(isHexNumber(oper) != -1 || isNumber(oper) != -1){
  198. return "00000000" + convertToBin(oper, isNumber(oper) == -1 ? 8 : 16);
  199. }
  200. }
  201. }
  202. //pc relativno i imamo samo simbol
  203. if(oper[0] == '$') {
  204. return "10100000" + addSymbol(oper.substr(1), 1);
  205. }
  206. //memorijsko apsolutno
  207. if(oper[0] == '*') {
  208. return "10100000" + convertToBin(oper.substr(1), 16);
  209. }
  210. cout << "ispred reg ind";
  211. if(oper.find('[') != -1 && oper.find(']') != -1) {
  212. cout << "Sa [ ] " << endl;
  213. string operMove = oper.substr(oper.find('[') + 1, oper.find(']') - oper.find('[') - 1);
  214. cout << "MOVE "<< operMove << endl;
  215. if(operMove != "0") {
  216. //registarsko indirektno sa 8/16 bitnim pomerajem
  217. if(isHexNumber(operMove) != -1 || isNumber(operMove) != -1){
  218. return (isNumber(operMove) != -1 ? "011" : "100") + convertToBin(operMove, isNumber(operMove) != -1 ? 8 : 16);
  219. }
  220. else {
  221. // za sad sam samo sa obuhvatila 8 bitno reg ind sa simbolom
  222. return "011" + convertToBin(operMove, isNumber(operMove) == -1 ? 8 : 16);
  223. }
  224. }
  225. else {
  226. //posto je 0 pomeraj onda je registarsko indirektno bez pomeraja
  227. return "010" + getRegisterMask(operCut) + oper[2] == "h" ? "1" : "0";
  228. }
  229. }
  230. //memorijsko samo sa simbolom
  231. cout << isHexNumber(oper) << " hexa " << isNumber(oper) << " number \n";
  232. if(isHexNumber(oper) == -1 && isNumber(oper) == -1) {
  233. cout << "memorisko samo sa simbolom" << oper << endl;
  234. return "10100000" + addSymbol(oper, 1);
  235. }
  236.  
  237. }
  238. cout << "OPERAND KOJI JE GRESKA " << oper << endl;
  239. return "greska";
  240. }
  241.  
  242. string findOperand(string operandsString) {
  243. cout << operandsString << " operands" << howManyOp << endl;
  244. if(howManyOp == 2) {
  245. if(operandsString.find(',') == -1) {
  246. cout << "Greska, moraju 2 operanda!!" << endl;
  247. return "greska";
  248. }
  249. cout << operandsString << "usli smo u dodelu";
  250. string op1, op2, op1Mask, op2Mask;
  251. op1 = operandsString.substr(0, operandsString.find(','));
  252. op2 = operandsString.substr(operandsString.find(',') + 1);
  253. cout << "op1 " << op1 << " op2" << op2 << endl;
  254. op1Mask = findOperandMask(op1, 1);
  255. cout << "prva maska" << op1Mask << "op2 " << op2<< "evo" << endl;
  256. op2Mask = findOperandMask(op2, 2);
  257. cout << "druga maska" << op2Mask << endl;
  258. return op1Mask + op2Mask;
  259. }
  260. else {
  261. return findOperandMask(operandsString, 1);
  262. }
  263. }
  264.  
  265. string convertStringToHex(string finalCode) {
  266. int i = 0, j = 0;
  267. string part = "", whole = "";
  268. cout << finalCode.length();
  269.  
  270. for(i = 0; i+4 <= finalCode.length() + 1;) {
  271. cout << "usli u for" << whole << endl;
  272.  
  273. if(j % 2 == 0 && j != 0) whole += " ";
  274.  
  275. part = finalCode.substr(i, 4);
  276. cout << part << "i=" << i <<endl;
  277. int until= i+4 ;
  278. i+=4;
  279. j++;
  280. if(part == "0000") {
  281. whole += "0";
  282. // continue;
  283. }
  284. if(part == "0001") {
  285. whole += "1";
  286. // continue;
  287. }
  288. if(part == "0010") {
  289. whole += "2";
  290. // continue;
  291. }
  292. if(part == "0011") {
  293. whole += "3";
  294. // continue;
  295. }
  296. if(part == "0100") {
  297. whole += "4";
  298. // continue;
  299. }
  300. if(part == "0101") {
  301. whole += "5";
  302. // continue;
  303. }
  304. if(part == "0110") {
  305. whole += "6";
  306. // continue;
  307. }
  308. if(part == "0111") {
  309. whole += "7";
  310. // continue;
  311. }
  312. if(part == "1000") {
  313. whole += "8";
  314. // continue;
  315. }
  316. if(part == "1001") {
  317. whole += "9";
  318. // continue;
  319. }
  320. if(part == "1010") {
  321. whole += "A";
  322. // continue;
  323. }
  324. if(part == "1011") {
  325. whole += "B";
  326. // continue;
  327. }
  328. if(part == "1100") {
  329. whole += "C";
  330. // continue;
  331. }
  332. if(part == "1101") {
  333. whole += "D";
  334. // continue;
  335. }
  336. if(part == "1110") {
  337. whole += "E";
  338. // continue;
  339. }
  340. if(part == "1111") {
  341. whole += "F";
  342. // continue;
  343. }
  344. // switch(part) {
  345. // case "0000":
  346. // whole += "0";
  347. // break;
  348. // case "0001":
  349. // whole += "1 ";
  350. // break;
  351. // case "0010":
  352. // whole += "2";
  353. // break;
  354. // case "0011":
  355. // whole += "3";
  356. // break;
  357. // case "0100":
  358. // whole += "4";
  359. // break;
  360. // case "0101":
  361. // whole += "5";
  362. // break;
  363. // case "0101":
  364. // whole += "6";
  365. // break;
  366. // case "0111":
  367. // whole += "7";
  368. // break;
  369. // case "1000":
  370. // whole += "8";
  371. // break;
  372. // case "1001":
  373. // whole += "9";
  374. // break;
  375. // case "1010":
  376. // whole += "A";
  377. // break;
  378. // case "1011":
  379. // whole += "B";
  380. // break;
  381. // case "1100":
  382. // whole += "C";
  383. // break;
  384. // case "1101":
  385. // whole += "D";
  386. // break;
  387. // case "1110":
  388. // whole += "E";
  389. // break;
  390. // case "1111":
  391. // whole += "F";
  392. // break;
  393. // default:
  394. // whole+= "";
  395. // }
  396. }
  397. return whole;
  398. }
  399.  
  400.  
  401.  
  402. int main() {
  403.  
  404. // string tempString, tempOp1, tempOp2, tempElem, tempSymbType = "local", tempSection = "UND";
  405. // string codeFirst, codeSecond, tempLineCode = "";
  406. // string byteORword = "1";
  407. int i;
  408. symbol symbNew;
  409.  
  410. section sectNew;
  411.  
  412. int allOffset = 0xff00, sectionOffset = 0;
  413.  
  414. // ifstream inFile("C:\Users\Antoanea\Desktop\test.txt");
  415. // ifstream inFile("/home/marko95/JetBrains/Projects/ClionProjects/SSAsembler/test1.txt");
  416. ifstream inFile("C:/Users/Antoanea/Desktop/c++/test.txt");
  417. string line;
  418. if (!inFile.is_open()) {
  419. cout << "file not exists";
  420. exit(1);
  421. }
  422.  
  423. while (getline(inFile, line)) {
  424. if (line == ".end") {
  425. sectNew.name = tempSection;
  426. sectNew.size = sectionOffset;
  427. sectNew.symbols = tempSymbTable;
  428. sectionTable.push_back(sectNew);
  429. cout << "kraj fajla";
  430. break;
  431. }
  432. int isComment = line.find('@') != -1 ? line.find('@') : line.find(';');
  433. if (isComment != -1) {
  434. if (isComment == 0) continue;
  435. line = line.substr(0, isComment);
  436. }
  437. while (!line.empty()) {
  438. cout << '\n' << line << " THIS IS LINE \n";
  439. tempString = line.find(' ') != -1 ? line.substr(0, line.find(' ')) : line; //trenutna rec
  440. cout << tempString << '\n';
  441. // if(tempString == "") continue;
  442. line = line.substr(line.find(' ') + 1);
  443.  
  444. if (tempString == ".global" || tempString == ".extern") {
  445. if (tempSection != "UND") {
  446. printf("Greska ne moze gobal posle sekcije");
  447. return 1;
  448. }
  449. tempSymbType = "global";
  450. while (!line.empty()) {
  451. tempElem = line.substr(0, line.find(','));
  452. cout << tempElem;
  453. line = line.substr(line.find(',') + 1);
  454. symbNew.id = id++;
  455. symbNew.name = tempElem;
  456. symbNew.sect_name = tempSection;
  457. symbNew.offset = 0; //mesto u sekciji
  458. symbNew.value = "-1";
  459. symbNew.type = tempSymbType;
  460. symbNew.pc_relative = '0';
  461. symbolTable.push_back(symbNew);
  462. cout << tempElem;
  463. if (tempElem == line) line = "";
  464. }
  465.  
  466. } else {
  467. cout << tempString << " nije global"<< "\n";
  468. if (getSection(tempString) != "greska" || tempString == ".section") {
  469. cout << "sekcija";
  470. //ako vec postoji neka sekcija ubacujemo je u niz sekcija
  471. if (tempSection != "UND") {
  472. sectNew.name = tempSection;
  473. sectNew.size = sectionOffset;
  474. sectNew.symbols = tempSymbTable;
  475. sectionTable.push_back(sectNew);
  476. }
  477. //ako je sekcija sa bilo kojim imenom moramo da dohvatimo ime sekcije
  478. if (tempString == ".section") {
  479. line = line.substr(line.find(' ') + 1);
  480. tempString = "." + line;
  481. }
  482. //dodajemo novu sekciju u tabelu simbola
  483. tempSection = tempString.substr(1);
  484. symbNew.id = id++;
  485. symbNew.name = tempSection;
  486. symbNew.sect_name = tempSection;
  487. symbNew.offset = 0; //mesto u sekciji
  488. symbNew.value = allOffset;
  489. symbNew.type = tempSymbType;
  490. symbNew.pc_relative = '0';
  491. symbolTable.push_back(symbNew);
  492. line = "";
  493. tempSymbTable = {};
  494. sectionOffset = 0;
  495. } else {
  496. cout << "Nije sekcija";
  497. cout << line << "tempS" << tempString << endl;
  498. // cout << "Nije sekcija \n" << line << " aa";
  499. if (tempSection == "UND") {
  500. // printf("%s \n", line);
  501. printf("Ne mogu instrukcije i labele pre prve sekcije");
  502. return 2;
  503. }
  504. if (tempString.find(':') != -1) {
  505. tempElem = tempString.substr(0, tempString.find(':'));
  506. cout << tempString << tempElem << " Nasli smo labelu\n";
  507. cout << "\n LINE" << line << "tempString" << tempString << "\n";
  508. tempString = line.find(' ') != -1 ? line.substr(0, line.find(' ')) : line;
  509. line = line.find(' ') != -1 ? line.substr(line.find(' ') + 1) : line;
  510. cout << "OVO ME ZANIMA" << tempString << " "<< line << endl;
  511. symbNew.id = id++;
  512. symbNew.name = tempElem;
  513. symbNew.sect_name = tempSection;
  514. symbNew.offset = sectionOffset; //mesto u sekciji
  515. symbNew.value = allOffset; //ukupno mesto u fajlu
  516. symbNew.type = tempSymbType;
  517. symbNew.pc_relative = '0';
  518. symbolTable.push_back(symbNew);
  519. tempSymbTable.push_back(symbNew);
  520. cout << tempElem;
  521. if (tempString == line) line = "";
  522.  
  523. }
  524.  
  525. // cout << "temp string starts " << tempString << " this is tempString" << endl;
  526. // cout << "prosao get" << endl;
  527. if (getInstructionMask(tempString) != "greska") {
  528. cout << "USLI SMO U DEO ZA INSTRUKCIJU" << getInstructionMask(tempString) << "\n";
  529. tempInstr = tempString;
  530. tempLineCode = getInstructionMask(tempString) + byteORword + "00";
  531. if (tempString == line) line = "";
  532. cout << "LINIJA ZA INSTRUKCIJU "<< tempLineCode << endl;
  533. } else {
  534. cout << "nije INSTRUKCIJa\n";
  535. if (tempInstr.empty()) {
  536. cout << "\n SKIP WORD BYTE ALIGN prvi put" << endl;
  537. if (getInstructionBytes(tempString) != -1 || tempString == ".align" ||
  538. tempString == ".skip") {
  539. // cout << "\n SKIP WORD BYTE ALIGN" << endl;
  540. //ako je skip ili align dodajemo toliko bajtova na izlazni kod
  541. if (tempString == ".align" || tempString == ".skip") {
  542. for (i = 0; i < stoi(line); i++) {
  543. tempLineCode += "00";
  544. }
  545. } else {
  546. cout << "word/byte" << endl;
  547. int isNum = isNumber(line);
  548. int isHex = isHexNumber(line);
  549. if (isNum == -1 && isHex == -1) {
  550. string tempValue = "UND";
  551. cout << "nekako nije br";
  552. for (i = 0; i < symbolTable.size(); i++) {
  553. if (symbolTable[i].name == line) {
  554. tempValue = symbolTable[i].value;
  555. break;
  556. }
  557. }
  558. if (tempValue != "UND") {
  559. int howManyBits = tempString == ".word" ? 16 : 8;
  560. tempLineCode += convertToBin(tempValue, howManyBits);
  561. cout << tempLineCode;
  562. } else {
  563. symbNew.id = id++;
  564. symbNew.name = line;
  565. symbNew.sect_name = "UND";
  566. symbNew.offset = 0; //mesto u sekciji
  567. symbNew.value = "-1";
  568. symbNew.type = tempSymbType;
  569. symbNew.pc_relative = '0';
  570. symbolTable.push_back(symbNew);
  571. cout << tempElem;
  572. if (tempElem == line) line = "";
  573. }
  574. } else {
  575. int howManyBits = tempString == ".word" ? 16 : 8;
  576. tempLineCode += convertToBin(line, howManyBits);
  577. cout << tempLineCode;
  578. }
  579. }
  580. line = "";
  581. } else {
  582. printf("Greska ne moze nista osim instrukcije posle labele");
  583. return 3;
  584. }
  585. }
  586. else {
  587. cout << "usli u else za operand";
  588. if(howManyOp == 0 && !tempString.empty()) {
  589. cout << "Ne moze da postoji operand!" << endl;
  590. return 4;
  591. }
  592. cout << "pre find operand" << tempString << endl;
  593. tempLineCode += findOperand(tempString);
  594. line = "";
  595. cout << "posle find operand";
  596. }
  597. }
  598. }
  599. }
  600.  
  601. }
  602. allOffset += tempLineCode.length() / 8;
  603. sectionOffset += tempLineCode.length() / 8;
  604. // cout << "\n" << convertStringToHex(tempLineCode) << " a pravi string" << tempLineCode << endl;
  605. codeSecond += tempLineCode + " ";
  606. codeFirst += convertStringToHex(tempLineCode);
  607. tempSymbType = "local";
  608. tempLineCode = "";
  609. tempInstr = "";
  610. }
  611. for(i = 0; i < symbolTable.size(); i++) {
  612. cout << "\n simbol" << symbolTable[i].name << '\n';
  613. }
  614. for(i = 0; i < sectionTable.size(); i++) {
  615. cout << "\n sekcija" << sectionTable[i].name << '\n';
  616. }
  617.  
  618. cout <<"ISPIS" << codeFirst << "\n all offset is " << allOffset;
  619. cout << codeSecond << "\n all offset is " << allOffset;
  620. return 0;
  621. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement