Advertisement
marcizhu

Compilador RISC SISA

Nov 9th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <sstream>
  5. #include <stdio.h>
  6. #include <cctype>
  7.  
  8. // Simple SISA RISC Assembler (SASM)
  9. // NOTA: NO compila las instrucciones LD, LDB, ST y STB (por ahora)
  10.  
  11. const std::string instructions[3][8] =
  12. {
  13.     { "AND", "OR", "XOR", "NOT", "ADD", "SUB", "SHA", "SHL" },
  14.     { "CMPLT", "CMPLE", "-", "CMPEQ", "CMPLTU", "CMPLEU", "-", "-" },
  15.     { "ADDI", "-", "-", "-", "-", "-", "-", "-" }
  16. };
  17.  
  18. std::vector<std::string> splitString(const std::string& str, char delimiter)
  19. {
  20.     std::vector<std::string> split;
  21.     std::stringstream ss(str);
  22.     std::string item;
  23.  
  24.     while (getline(ss, item, delimiter)) split.push_back(item);
  25.  
  26.     return split;
  27. }
  28.  
  29. void findAndReplaceAll(std::string& data, std::string toSearch, std::string replaceStr)
  30. {
  31.     size_t pos = data.find(toSearch);
  32.  
  33.     while( pos != std::string::npos)
  34.     {
  35.         data.replace(pos, toSearch.size(), replaceStr);
  36.         pos = data.find(toSearch, pos + toSearch.size());
  37.     }
  38. }
  39.  
  40. inline int getN(const std::string& reg) { return (reg[1] - '0'); }
  41.  
  42. int processString(const std::string& str)
  43. {
  44.     std::vector<std::string> tokens = splitString(str, ' ');
  45.  
  46.     int instr = 0x0000;
  47.     int foundLine = -1;
  48.  
  49.     for(int i = 0; i < 3; i++)
  50.     {
  51.         for(int j = 0; j < 8; j++)
  52.         {
  53.             if(tokens[0] == instructions[i][j])
  54.             {
  55.                 foundLine = i;
  56.                 instr |= (i << 12);
  57.                 instr |= j;
  58.                 break;
  59.             }
  60.         }
  61.     }
  62.  
  63.     if(foundLine == -1)
  64.     {
  65.         if(tokens[0] == "JALR")
  66.         {
  67.             // JALR   Rd, Ra
  68.             instr |= ((0b0111) << 12);
  69.             instr |= (getN(tokens[2]) << 9);
  70.             instr |= (getN(tokens[1]) << 6);
  71.         }
  72.         else
  73.         {
  74.             std::string list[] = { "LD", "ST", "LDB", "STB", "JALR", "BNZ", "BZ", "MOVHI", "MOVI", "OUT", "IN" };
  75.  
  76.             for(int i = 0; i < 11; i++)
  77.             {
  78.                 if(tokens[0] == list[i])
  79.                 {
  80.                     if(i > 4)
  81.                     {
  82.                         instr |= ((0b1000 + (i - 5) / 2) << 12);
  83.  
  84.                         if(tokens[0] != "OUT")
  85.                         {
  86.                             instr |= (getN(tokens[1]) << 9);
  87.                             instr |= std::stoi(tokens[2]) & 0xFF;
  88.                         }
  89.                         else
  90.                         {
  91.                             instr |= (getN(tokens[2]) << 9);
  92.                             instr |= std::stoi(tokens[1]) & 0xFF;
  93.                         }
  94.  
  95.                         if(i % 2 == 1) instr |= (1 << 8);
  96.                     }
  97.                     else
  98.                     {
  99.                         instr |= ((0b0011 + i) << 12);
  100.                         // LD, ST, LDB, STB
  101.                     }
  102.                 }
  103.             }
  104.         }
  105.     }
  106.     else if(foundLine > 1)
  107.     {
  108.         instr |= (getN(tokens[2]) << 9);
  109.         instr |= (getN(tokens[1]) << 6);
  110.         instr |= std::stoi(tokens[3]) & 0x3F;
  111.     }
  112.     else
  113.     {
  114.         instr |= (getN(tokens[2]) << 9);
  115.         instr |= (getN(tokens[3]) << 6);
  116.         instr |= (getN(tokens[1]) << 3);
  117.     }
  118.  
  119.     return instr;
  120. }
  121.  
  122. void strupr(char* str)
  123. {
  124.     while(*str != '\0')
  125.     {
  126.         *str = toupper(*str);
  127.         ++str;
  128.     }
  129. }
  130.  
  131. int main()
  132. {
  133.     char line[256];
  134.  
  135.     while(std::cin.getline(line, 256, '\n'))
  136.     {
  137.         strupr(line);
  138.         std::string instr(line);
  139.  
  140.         std::replace(instr.begin(), instr.end(), ',', ' ');
  141.         std::replace(instr.begin(), instr.end(), '\t', ' ');
  142.         findAndReplaceAll(instr, "  ", " ");
  143.  
  144.         printf("0x%04X\n", processString(instr));
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement