Advertisement
ranisalt

Hex to MIPS bitwise

Jun 23rd, 2013
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. /* POTÊNCIAS DE DOIS CALCULADAS:
  8.  * 31 = 2**6 - 1;
  9.  * 63 = 2**7 - 1;
  10.  * 67108863 = 2**26 - 1;
  11.  */
  12.  
  13. static map<short, string>
  14.     opcodes = { {8, "addi"}, {12, "andi"}, {4, "beq"}, {5, "bne"}, {35, "lw"}, {13, "ori"}, {10, "slti"}, {43, "sw"}, },
  15.     functs = { {32, "add"}, {36, "and"}, {39, "nor"}, {37, "or"}, {42, "slt"}, {0, "sll"}, {2, "srl"}, {34, "sub"}, },
  16.     regs = { {0, "$zero"}, {1, "$at"}, {2, "$v1"}, {3, "$v2"}, {4, "$a0"}, {5, "$a1"}, {6, "$a2"}, {7, "$a3"}, {8, "$t0"}, {9, "$t1"}, {10, "$t2"}, {11, "$t3"}, {12, "$t4"}, {13, "$t5"}, {14, "$t6"}, {15, "$t7"}, {16, "$s0"}, {17, "$s1"}, {18, "$s2"}, {19, "$s3"}, {20, "$s4"}, {21, "$s5"}, {22, "$s6"}, {23, "$s7"}, {26, "$k0"}, {27, "$k1"}, {28, "$gp"}, {29, "$sp"}, {30, "$fp"}, {31, "$ra"}, };
  17.  
  18. static string getOpcode(short opcode) {
  19.     if (opcodes.find(opcode) != opcodes.end())
  20.         return opcodes.find(opcode)->second;
  21.     throw 1;
  22. }
  23.  
  24. static string getFunct(short funct) {
  25.     if (functs.find(funct) != functs.end())
  26.         return functs.find(funct)->second;
  27.     throw 1;
  28. }
  29.  
  30. static string getReg(short reg) {
  31.     if (regs.find(reg) != regs.end())
  32.         return regs.find(reg)->second;
  33.     throw 1;
  34. }
  35.  
  36. static string decode(unsigned int hex) {
  37.     string mips;
  38.     short opcode = (hex >> 26) & 63;
  39.     if (opcode == 0)
  40.         mips.append(getFunct(hex & 63)).append(" ").append(getReg((hex >> 21) & 31)).append(", ").append(getReg((hex >> 16) & 31)).append(", ").append(hex & 63 <= 2 ? getReg((hex >> 11) & 31) : getReg((hex >> 6) & 63));
  41.     else if (opcode == 2)
  42.         mips.append("j ").append(to_string(hex & 67108863));
  43.     else
  44.         mips.append(getOpcode(opcode)).append(" ").append(getReg((hex >> 21) & 31)).append(", ").append(to_string(hex & 65535)).append("(").append(getReg((hex >> 16) & 31)).append(")");
  45.     return mips;
  46. }
  47.  
  48. int main() {
  49.     while (true) {
  50.         try {
  51.             string hex;
  52.             cout << "Informe a instrução hexadecimal: ";
  53.             cin >> hex;
  54.             cout << decode(strtol(hex.c_str(), NULL, 16)) << "\n";
  55.         } catch (int err) {
  56.             if (err == 1)
  57.                 cout << "A instrução não é válida!" << "\n";
  58.         }
  59.     }
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement