Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <vector>
- using namespace std;
- string hex_char_to_bin(string c);
- std::string hex_to_bin(const std::string in);
- vector<string> BINARY_LOOKUP_TABLE =
- {
- "00000", "00001", "00010",
- "00011", "00100", "00101",
- "00110", "00111", "01000",
- "01001", "01010", "01011",
- "01100", "01101", "01110",
- "01111", "10000", "10001",
- "10010", "10011", "10100",
- "10101", "10110", "10111",
- "11000", "11001", "11010",
- "11011", "11100", "11101",
- "11110", "11111"
- };
- vector<string> INSTRUCTION_LOOKUP_TABLE =
- {
- "NOP", "DB", "SR",
- "LD", "MOV", "ADD",
- "SUB", "SUBC", "INC",
- "DEC", "RSH", "LSH",
- "NOT", "NAND", "XOR",
- "OR", "AND", "DCO",
- "DCR", "DCM", "DCP",
- "LDP", "SP", "JNE",
- "JE", "JMP", "ICP",
- "DIR", "DIM", "DIP",
- "IIP", "RST"
- };
- int main()
- {
- string in, str, buf;
- vector<string> asm_stack;
- vector<string> bin_stack;
- vector<string> tokens;
- bool flag = false;
- cout << "Enter assembly file to compile: ";
- cin >> in;
- ifstream fin(in);
- if (!fin)
- {
- cout << "\nError opening file, check if the file name and directory are correct.\n\n";
- cin >> in;
- return 1;
- }
- // Split lines into tokens and remove comments
- while (getline(fin, str))
- {
- int pos = str.find(";");
- if (pos != -1)
- str.erase(str.begin() + pos, str.end());
- stringstream ss(str);
- while (ss >> buf)
- tokens.push_back(buf);
- asm_stack.push_back(str);
- str = "";
- }
- // Remove commas from the second argument
- for (int i = 0; i < tokens.size(); i++)
- {
- int pos = tokens[i].find(",");
- if (pos != -1)
- tokens[i].erase(pos);
- }
- // Compile file to binary code
- for (int i = 0; i < tokens.size(); i++)
- {
- flag = false;
- // Instructions
- for (int j = 0; j < INSTRUCTION_LOOKUP_TABLE.size(); j++)
- {
- if (tokens[i] == INSTRUCTION_LOOKUP_TABLE[j])
- {
- bin_stack.push_back(BINARY_LOOKUP_TABLE[j]);
- flag = true;
- break;
- }
- }
- if (flag) continue;
- // Arguments
- if (tokens[i] == "NUL")
- bin_stack.push_back("000000000000");
- else if (tokens[i] == "EAX")
- bin_stack.push_back("000000000001");
- else if (tokens[i] == "EBX")
- bin_stack.push_back("000000000010");
- else bin_stack.push_back(hex_to_bin(tokens[i]));
- }
- in.erase(in.end() - 4, in.end());
- in += ".sc";
- ofstream fout;
- fout.open(in);
- for (int i = 0; i < bin_stack.size(); i += 3)
- {
- fout << bin_stack[i] << bin_stack[i + 1] << bin_stack[i + 2] << "\n";
- }
- fout.close();
- cout << "\n\nFile compiled successfully!";
- cin >> in;
- return 0;
- }
- string hex_char_to_bin(string c)
- {
- if (c == "0") return "0000";
- if (c == "1") return "0001";
- if (c == "2") return "0010";
- if (c == "3") return "0011";
- if (c == "4") return "0100";
- if (c == "5") return "0101";
- if (c == "6") return "0110";
- if (c == "7") return "0111";
- if (c == "8") return "1000";
- if (c == "9") return "1001";
- if (c == "A") return "1010";
- if (c == "B") return "1011";
- if (c == "C") return "1100";
- if (c == "D") return "1101";
- if (c == "E") return "1110";
- if (c == "F") return "1111";
- return "";
- }
- std::string hex_to_bin(std::string in)
- {
- string bin, temp;
- string hex = in;
- hex.erase(0, 2);
- for (int i = 0; i != hex.length(); ++i)
- {
- temp = hex[i];
- bin += hex_char_to_bin(temp);
- }
- if (bin.length() == 4)
- bin = "00000000" + bin;
- else if (bin.length() == 8)
- bin = "0000" + bin;
- return bin;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement