SHARE
TWEET

Untitled

a guest Apr 23rd, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. #include<map>
  3. #include<string>
  4. #include<fstream>
  5. #include<regex>
  6. #include<vector>
  7.  
  8. #include"assemblerdata.h"
  9.  
  10. using namespace std;
  11.  
  12. map<string, struct_opcode> opTable;
  13. map<unsigned int, listing_line> listing_table;
  14. map<string, unsigned int> symbol_table;
  15.  
  16. unsigned int starting_address;
  17. unsigned int program_length;
  18.  
  19. unsigned int current_line_number;
  20. unsigned int LOCCTR;
  21. listing_line current_line;
  22.  
  23. void loadOpTable(string path) {
  24.     string line;
  25.     ifstream infile;
  26.     infile.open(path);
  27.     while (getline(infile, line)) {
  28.         regex r("(\\w+)\\s+(\\w+)\\s+(\\w+)");
  29.         smatch m;
  30.         regex_search(line, m, r);
  31.         opTable[m[1]].format = stoi(m[2]);
  32.         opTable[m[1]].opcode = stoi(m[3], 0, 16);
  33.         //TODO delete this line
  34.         cout << m[1] << "      " << opTable[m[1]].format<< "      " << opTable[m[1]].opcode << endl ;
  35.     }
  36.     infile.close();
  37. }
  38.  
  39. void build_listing_table(string path) {
  40.     string line;
  41.     ifstream infile;
  42.     infile.open(path);
  43.     int i = 0;
  44.     while (getline(infile, line)) {
  45.         regex rComment("(\\.)(.*)");
  46.         smatch m;
  47.         regex_search(line, m, rComment);
  48.         if (m.size() > 0 && m.position(0) == 0) {
  49.             listing_table[i].isAllComment = true;
  50.             listing_table[i].comment = m[2];
  51.             i++;
  52.             continue;
  53.         } else {
  54.             regex rInstruction("(\\w?)(\\s*)(\\w?)(\\s*)(\\S?)(\\s*)(\\S?)");
  55.             regex_search(line, m ,rInstruction);
  56.             if(m.size() > 0) {
  57.                 listing_table[i].isAllComment = false;
  58.                 listing_table[i].label = m[1];
  59.                 listing_table[i].mnemonic = m[3];
  60.                 listing_table[i].operand = m[5];
  61.                 listing_table[i].comment = m[7];
  62.             } else {
  63.                 listing_table[i].error.insert(listing_table[i].error.begin(), "Invalid Instruction");
  64.             }
  65.         }
  66.         i++;
  67.     }
  68.     infile.close();
  69. }
  70.  
  71. void pass1_Algorithm(string codePath) {
  72.     //TODO load listing_table
  73.     loadOpTable("optable.txt");
  74.     current_line_number = 1;
  75.  
  76.     //skip the comments
  77.     while (listing_table[current_line_number].isAllComment) {
  78.         current_line_number++;
  79.     }
  80.  
  81.     current_line = listing_table[current_line_number];
  82.     //check start addressing
  83.     if (iequals(current_line.mnemonic, "START")) {
  84.         starting_address = stoi(current_line.operand, 0, 16);
  85.         listing_table[current_line_number].address = starting_address;
  86.         current_line = listing_table[++current_line_number];
  87.     } else {
  88.         starting_address = 0;
  89.     }
  90.     LOCCTR = starting_address;
  91.  
  92.     //reading code loop
  93.     while (!iequals(current_line.mnemonic, "END")) {
  94.         //process the line if not a comment
  95.         if (!current_line.isAllComment) {
  96.             //assign address to the line
  97.             listing_table[current_line_number].address = LOCCTR;
  98.  
  99.             //process the label field
  100.             if (!current_line.label.empty()) {
  101.                 if (symbol_table.find(current_line.label)
  102.                     != symbol_table.end()) {
  103.                     listing_table[current_line_number].error.push_back("symbol '"
  104.                                                                        + current_line.label + "' is already defined");
  105.                 } else {
  106.                     symbol_table[current_line.label] = LOCCTR;
  107.                 }
  108.             }
  109.  
  110.             //process the mnemonic
  111.             if (opTable.find(current_line.mnemonic) != opTable.end()) {
  112.                 //not directive
  113.                 if (opTable[current_line.mnemonic].format == 3 && current_line.isFormat4) {
  114.                     LOCCTR += 4;
  115.                 } else if (opTable[current_line.mnemonic].format == 2 && current_line.isFormat4) {
  116.                     listing_table[current_line_number].error.push_back(
  117.                             "Can't use format 4 with mnemonic " + current_line.mnemonic);
  118.                 } else {
  119.                     LOCCTR += opTable[current_line.mnemonic].format;
  120.                 }
  121.             } else if (false /*TODO handle all the directives*/) {
  122.  
  123.             } else {
  124.                 listing_table[current_line_number].error.push_back("Invalid operation code");
  125.             }
  126.         }//end line process
  127.         current_line = listing_table[++current_line_number];
  128.     }
  129.     program_length = LOCCTR - starting_address;
  130.  
  131. }
  132.  
  133. int main() {
  134. //    build_listing_table("inputFile.txt");
  135.     loadOpTable("optable.txt");
  136.     return 0;
  137. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top