daily pastebin goal
59%
SHARE
TWEET

Compilador RISC SISA a hexadecimal

marcizhu Nov 9th, 2018 (edited) 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <sstream>
  5. #include <stdio.h>
  6. #include <cctype>
  7.  
  8. const std::string instructions[3][8] =
  9. {
  10.     { "AND", "OR", "XOR", "NOT", "ADD", "SUB", "SHA", "SHL" },
  11.     { "CMPLT", "CMPLE", "-", "CMPEQ", "CMPTLU", "CMPLEU", "-", "-" },
  12.     { "ADDI", "-", "-", "-", "-", "-", "-", "-" }
  13. };
  14.  
  15. std::vector<std::string> splitString(const std::string& str, char delimiter)
  16. {
  17.     std::vector<std::string> split;
  18.     std::stringstream ss(str);
  19.     std::string item;
  20.  
  21.     while (getline(ss, item, delimiter)) split.push_back(item);
  22.  
  23.     return split;
  24. }
  25.  
  26. void findAndReplaceAll(std::string& data, std::string toSearch, std::string replaceStr)
  27. {
  28.     size_t pos = data.find(toSearch);
  29.  
  30.     while( pos != std::string::npos)
  31.     {
  32.         data.replace(pos, toSearch.size(), replaceStr);
  33.         pos = data.find(toSearch, pos + toSearch.size());
  34.     }
  35. }
  36.  
  37. inline int getN(const std::string& reg) { return (reg[1] - '0'); }
  38.  
  39. int processString(const std::string& str)
  40. {
  41.     std::vector<std::string> tokens = splitString(str, ' ');
  42.  
  43.     int instr = 0x0000;
  44.     int foundLine = -1;
  45.  
  46.     for(int i = 0; i < 4; i++)
  47.     {
  48.         for(int j = 0; j < 8; j++)
  49.         {
  50.             if(tokens[0] == instructions[i][j])
  51.             {
  52.                 foundLine = i;
  53.                 instr |= (i << 12);
  54.                 instr |= j;
  55.                 break;
  56.             }
  57.         }
  58.     }
  59.  
  60.     if(foundLine == -1)
  61.     {
  62.         std::string list[] = { "BNZ", "BZ", "MOVHI", "MOVI", "OUT", "IN" };
  63.  
  64.         for(int i = 0; i < 6; i++)
  65.         {
  66.             if(tokens[0] == list[i])
  67.             {
  68.                 instr |= ((0b1000 + i / 2) << 12);
  69.                 instr |= (getN(tokens[1]) << 9);
  70.                 instr |= std::stoi(tokens[2]) & 0xFF;
  71.  
  72.                 if(i % 2 == 0) instr |= (1 << 8);
  73.             }
  74.         }
  75.  
  76.         // else LD, ST, LDB, STB
  77.     }
  78.     else if(foundLine > 1)
  79.     {
  80.         instr |= (getN(tokens[2]) << 9);
  81.         instr |= (getN(tokens[1]) << 6);
  82.         instr |= std::stoi(tokens[3]) & 0x3F;
  83.     }
  84.     else
  85.     {
  86.         instr |= (getN(tokens[2]) << 9);
  87.         instr |= (getN(tokens[3]) << 6);
  88.         instr |= (getN(tokens[1]) << 3);
  89.     }
  90.  
  91.     return instr;
  92. }
  93.  
  94. void strupr(char* str)
  95. {
  96.     while(*str != '\0')
  97.     {
  98.         *str = toupper(*str);
  99.         ++str;
  100.     }
  101. }
  102.  
  103. int main()
  104. {
  105.     char line[256];
  106.  
  107.     while(std::cin.getline(line, 256, '\n'))
  108.     {
  109.         strupr(line);
  110.         std::string instr(line);
  111.  
  112.         std::replace(instr.begin(), instr.end(), ',', ' ');
  113.         std::replace(instr.begin(), instr.end(), '\t', ' ');
  114.         findAndReplaceAll(instr, "  ", " ");
  115.  
  116.         printf("0x%04X\n", processString(instr));
  117.     }
  118. }
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