Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <list>
- #include <iterator>
- #include <string>
- #include <fstream>
- #include <regex>
- #include <map>
- #include <cstring>
- using namespace std;
- typedef unsigned char byte;
- typedef struct symb {
- int id;
- string name;
- int offset;
- int value;
- string sect_name;
- string type;
- string pc_relative;
- } symbol;
- typedef struct sect {
- string name;
- string size;
- vector<symbol> symbols;
- } section;
- //sp //pc //psw
- const vector<string> register_masks = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1111"};
- const vector<string> register_array = {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "psw"};
- const vector<string> adressing_masks = {"000", "001", "010", "011", "100", "101"};
- const vector<string> zero_operand_masks = {"00001", "11000", "11001"};
- const vector<string> zero_operand_instr = {"halt", "ret", "iret"};
- const vector<string> one_operand_masks = {"00011", "01010", "10001", "10010", "10011", "10100", "10101", "10110",
- "10111"};
- const vector<string> one_operand_instr = {"int", "not", "push", "pop", "jmp", "jeq", "jne", "jgt", "call"};
- const vector<string> two_operand_masks = {"00010", "00100", "00101", "00111", "01000", "01001", "01011", "01100",
- "01101", "01110", "01111", "10000"};
- const vector<string> two_operand_instr = {"xchg", "mov", "add", "sub", "mul", "div", "cmp", "and", "or", "xor", "test",
- "shl", "shr"};
- const vector<string> operand_types = {".global", ".extern"};
- const vector<string> section_types = {".text", ".data", ".bss"};
- const vector<string> other_options = {".word", ".byte", ".char", ".long"};
- int howManyOp = 0;
- static int id = 0;
- string getRegisterMask(string reg) {
- if (reg == "psw") return register_masks[8];
- if (reg == "sp") return register_masks[6];
- if (reg == "pc") return register_masks[7];
- int i;
- for (i = 0; i < 9; i++) {
- if (register_array[i] == reg) return register_masks[i];
- }
- return "greska";
- }
- string getSection(string sect) {
- int i;
- for (i = 0; i < 3; i++) {
- if (section_types[i] == sect) return section_types[i];
- }
- return "greska";
- }
- string getInstructionMask(string instr) {
- int i;
- cout << instr;
- for (i = 0; i < 9; i++) {
- if (zero_operand_instr[i] == instr) {
- howManyOp = 0;
- return zero_operand_masks[i];
- }
- if (one_operand_instr[i] == instr) {
- howManyOp = 1;
- return one_operand_masks[i];
- }
- if (two_operand_instr[i] == instr) {
- howManyOp = 2;
- cout << instr << "||oper]";
- return two_operand_masks[i];
- }
- }
- return "greska";
- }
- int getInstructionBytes(string instr) {
- if (instr == ".char") return 1;
- if (instr == ".byte") return 1;
- if (instr == ".word") return 2;
- if (instr == ".long") return 4;
- return -1;
- }
- int isNumber(string line) {
- int i;
- for (i = 0; i < line.length(); i++) {
- if (line[i] < '0' || line[i] > '9') return -1;
- }
- return 0;
- }
- int main() {
- vector<symbol> symbolTable, tempSymbTable;
- vector<section> sectionTable;
- string tempString, tempInstr, tempOp1, tempOp2, tempElem, tempSymbType = "local", tempSection = "UND";
- string codeFirst, codeSecond, tempLineCode = "";
- string byteORword = "1";
- int i;
- symbol symbNew;
- section sectNew;
- int allOffset = 0, sectionOffset = 0;
- // ifstream inFile("C:\Users\Antoanea\Desktop\test.txt");
- // ifstream inFile("/home/marko95/JetBrains/Projects/ClionProjects/SSAsembler/test1.txt");
- ifstream inFile("text.txt");
- string line;
- if (!inFile.is_open()) {
- cout << "file not exists";
- exit(1);
- }
- while (getline(inFile, line)) {
- if (line == ".end") {
- cout << "kraj fajla";
- return 0;
- }
- int isComment = line.find('@') != -1 ? line.find('@') : line.find(';');
- if (isComment != -1) {
- if (isComment == 0) continue;
- line = line.substr(0, isComment);
- }
- while (!line.empty()) {
- cout << line << " THIS IS LINE \n";
- tempString = line.find(' ') != -1 ? line.substr(0, line.find(' ')) : line; //trenutna rec
- cout << tempString << '\n';
- // if(tempString == "") continue;
- line = line.substr(line.find(' ') + 1);
- if (tempString == ".global" || tempString == ".extern") {
- if (tempSection != "UND") {
- printf("Greska ne moze gobal posle sekcije");
- return 1;
- }
- tempSymbType = "global";
- while (!line.empty()) {
- tempElem = line.substr(0, line.find(','));
- cout << tempElem;
- line = line.substr(line.find(',') + 1);
- symbNew.id = id++;
- symbNew.name = tempElem;
- symbNew.sect_name = tempSection;
- symbNew.offset = 0; //mesto u sekciji
- symbNew.value = -1;
- symbNew.type = tempSymbType;
- symbNew.pc_relative = '0';
- symbolTable.push_back(symbNew);
- cout << tempElem;
- if (tempElem == line) line = "";
- }
- } else {
- cout << tempString << "\n";
- if (getSection(tempString) != "greska" || tempString == ".section") {
- cout << "sekcija";
- //ako vec postoji neka sekcija ubacujemo je u niz sekcija
- if (tempSection != "UND") {
- sectNew.name = tempSection;
- sectNew.size = sectionOffset;
- sectNew.symbols = tempSymbTable;
- sectionTable.push_back(sectNew);
- }
- //ako je sekcija sa bilo kojim imenom moramo da dohvatimo ime sekcije
- if (tempString == ".section") {
- line = line.substr(line.find(' ') + 1);
- tempString = "." + line;
- }
- //dodajemo novu sekciju u tabelu simbola
- tempSection = tempString.substr(1);
- symbNew.id = id++;
- symbNew.name = tempSection;
- symbNew.sect_name = tempSection;
- symbNew.offset = 0; //mesto u sekciji
- symbNew.value = allOffset;
- symbNew.type = tempSymbType;
- symbNew.pc_relative = '0';
- symbolTable.push_back(symbNew);
- line = "";
- tempSymbTable = {};
- sectionOffset = 0;
- } else {
- if (tempSection == "UND") {
- // printf("%s \n", line);
- printf("Ne mogu instrukcije i labele pre prve sekcije");
- return 2;
- }
- if (tempString.find(':') != -1) {
- tempElem = tempString.substr(0, tempString.find(':'));
- cout << tempString << tempElem << " Nasli smo labelu\n";
- cout << "\n LINE" << line << "tempString" << tempString << "\n";
- tempString = line;
- symbNew.id = id++;
- symbNew.name = tempElem;
- symbNew.sect_name = tempSection;
- symbNew.offset = sectionOffset; //mesto u sekciji
- symbNew.value = allOffset; //ukupno mesto u fajlu
- symbNew.type = tempSymbType;
- symbNew.pc_relative = '0';
- symbolTable.push_back(symbNew);
- tempSymbTable.push_back(symbNew);
- cout << tempElem;
- if (tempString == line) break;
- }
- cout << "temp zadnji " << tempString[tempString.length() - 1] << " temp zadnji";
- if ((tempString[tempString.length() - 1] == 'b' || tempString[tempString.length() - 1] == 'w') &&
- tempInstr.empty()) {
- byteORword = tempString[tempString.length() - 1] == 'b' ? "0" : "1";
- tempString = tempString.substr(0, tempString.length() - 1);
- }
- if (getInstructionMask(tempString) != "greska") {
- cout << "USLI SMO U DEO ZA INSTRUKCIJU" << getInstructionMask(tempString) << "\n";
- tempInstr = tempString;
- tempLineCode = getInstructionMask(tempString) + byteORword + "00";
- } else {
- if (tempInstr.empty()) {
- if (getInstructionBytes(tempString) != -1 || tempString == ".align" ||
- tempString == ".skip") {
- //ako je skip ili align dodajemo toliko bajtova na izlazni kod
- if (tempString == ".align" || tempString == ".skip") {
- for (i = 0; i < stoi(line); i++) {
- tempLineCode += "00";
- }
- } else {
- int isNum = isNumber(line);
- int tempValue = -1;
- if (isNum == -1) {
- for (i = 0; i < symbolTable.size(); i++) {
- if (symbolTable[i].name == line) {
- tempValue = symbolTable[i].value;
- break;
- }
- }
- if (tempValue != -1) {
- // tempLineCode += convertIntToString(tempValue);
- } else {
- symbNew.id = id++;
- symbNew.name = line;
- symbNew.sect_name = "UND";
- symbNew.offset = 0; //mesto u sekciji
- symbNew.value = -1;
- symbNew.type = tempSymbType;
- symbNew.pc_relative = '0';
- symbolTable.push_back(symbNew);
- cout << tempElem;
- if (tempElem == line) line = "";
- }
- } else {
- }
- }
- line = "";
- } else {
- printf("Greska ne moze nista osim instrukcije posle labele");
- return 3;
- }
- } else {
- }
- // if(!tempInstr.empty()) {
- // }
- // else {
- // printf("Greska ne moze operand pre instrukcije");
- // return 3;
- // }
- }
- }
- }
- }
- codeFirst += tempLineCode;
- tempSymbType = "local";
- tempLineCode = "";
- tempInstr = "";
- allOffset += 1;
- sectionOffset += 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement