Advertisement
Guest User

SYNASM Compiler

a guest
Apr 5th, 2015
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. #include <sstream>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. string hex_char_to_bin(string c);
  10. std::string hex_to_bin(const std::string in);
  11.  
  12. vector<string> BINARY_LOOKUP_TABLE =
  13. {
  14.     "00000", "00001", "00010",
  15.     "00011", "00100", "00101",
  16.     "00110", "00111", "01000",
  17.     "01001", "01010", "01011",
  18.     "01100", "01101", "01110",
  19.     "01111", "10000", "10001",
  20.     "10010", "10011", "10100",
  21.     "10101", "10110", "10111",
  22.     "11000", "11001", "11010",
  23.     "11011", "11100", "11101",
  24.     "11110", "11111"
  25. };
  26.  
  27. vector<string> INSTRUCTION_LOOKUP_TABLE =
  28. {
  29.     "NOP", "DB", "SR",
  30.     "LD", "MOV", "ADD",
  31.     "SUB", "SUBC", "INC",
  32.     "DEC", "RSH", "LSH",
  33.     "NOT", "NAND", "XOR",
  34.     "OR", "AND", "DCO",
  35.     "DCR", "DCM", "DCP",
  36.     "LDP", "SP", "JNE",
  37.     "JE", "JMP", "ICP",
  38.     "DIR", "DIM", "DIP",
  39.     "IIP", "RST"
  40. };
  41.  
  42. int main()
  43. {
  44.     string in, str, buf;
  45.     vector<string> asm_stack;
  46.     vector<string> bin_stack;
  47.     vector<string> tokens;
  48.     bool flag = false;
  49.    
  50.     cout << "Enter assembly file to compile: ";
  51.     cin >> in;
  52.  
  53.     ifstream fin(in);
  54.     if (!fin)
  55.     {
  56.         cout << "\nError opening file, check if the file name and directory are correct.\n\n";
  57.         cin >> in;
  58.         return 1;
  59.     }
  60.  
  61.     // Split lines into tokens and remove comments
  62.     while (getline(fin, str))
  63.     {
  64.         int pos = str.find(";");
  65.         if (pos != -1)
  66.             str.erase(str.begin() + pos, str.end());
  67.        
  68.         stringstream ss(str);
  69.         while (ss >> buf)
  70.             tokens.push_back(buf);
  71.  
  72.         asm_stack.push_back(str);
  73.  
  74.         str = "";
  75.     }
  76.  
  77.     // Remove commas from the second argument
  78.     for (int i = 0; i < tokens.size(); i++)
  79.     {
  80.         int pos = tokens[i].find(",");
  81.  
  82.         if (pos != -1)
  83.             tokens[i].erase(pos);
  84.     }
  85.  
  86.     // Compile file to binary code
  87.     for (int i = 0; i < tokens.size(); i++)
  88.     {
  89.         flag = false;
  90.  
  91.         // Instructions
  92.         for (int j = 0; j < INSTRUCTION_LOOKUP_TABLE.size(); j++)
  93.         {
  94.             if (tokens[i] == INSTRUCTION_LOOKUP_TABLE[j])
  95.             {
  96.                 bin_stack.push_back(BINARY_LOOKUP_TABLE[j]);
  97.                 flag = true;
  98.                 break;
  99.             }
  100.         }
  101.         if (flag) continue;
  102.  
  103.         // Arguments
  104.         if (tokens[i] == "NUL")
  105.             bin_stack.push_back("000000000000");
  106.         else if (tokens[i] == "EAX")
  107.             bin_stack.push_back("000000000001");
  108.         else if (tokens[i] == "EBX")
  109.             bin_stack.push_back("000000000010");
  110.         else bin_stack.push_back(hex_to_bin(tokens[i]));
  111.  
  112.     }
  113.  
  114.     in.erase(in.end() - 4, in.end());
  115.     in += ".sc";
  116.     ofstream fout;
  117.     fout.open(in);
  118.     for (int i = 0; i < bin_stack.size(); i += 3)
  119.     {
  120.         fout << bin_stack[i] << bin_stack[i + 1] << bin_stack[i + 2] << "\n";
  121.     }
  122.     fout.close();
  123.  
  124.     cout << "\n\nFile compiled successfully!";
  125.     cin >> in;
  126.     return 0;
  127. }
  128.  
  129. string hex_char_to_bin(string c)
  130. {
  131.     if (c == "0") return "0000";
  132.     if (c == "1") return "0001";
  133.     if (c == "2") return "0010";
  134.     if (c == "3") return "0011";
  135.     if (c == "4") return "0100";
  136.     if (c == "5") return "0101";
  137.     if (c == "6") return "0110";
  138.     if (c == "7") return "0111";
  139.     if (c == "8") return "1000";
  140.     if (c == "9") return "1001";
  141.     if (c == "A") return "1010";
  142.     if (c == "B") return "1011";
  143.     if (c == "C") return "1100";
  144.     if (c == "D") return "1101";
  145.     if (c == "E") return "1110";
  146.     if (c == "F") return "1111";
  147.  
  148.     return "";
  149. }
  150.  
  151. std::string hex_to_bin(std::string in)
  152. {
  153.     string bin, temp;
  154.     string hex = in;
  155.  
  156.     hex.erase(0, 2);
  157.  
  158.     for (int i = 0; i != hex.length(); ++i)
  159.     {
  160.         temp = hex[i];
  161.         bin += hex_char_to_bin(temp);
  162.     }
  163.  
  164.     if (bin.length() == 4)
  165.         bin = "00000000" + bin;
  166.     else if (bin.length() == 8)
  167.         bin = "0000" + bin;
  168.  
  169.     return bin;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement