Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <string>
- using namespace std;
- /* POTÊNCIAS DE DOIS CALCULADAS:
- * 31 = 2**6 - 1;
- * 63 = 2**7 - 1;
- * 67108863 = 2**26 - 1;
- */
- static map<short, string>
- opcodes = { {8, "addi"}, {12, "andi"}, {4, "beq"}, {5, "bne"}, {35, "lw"}, {13, "ori"}, {10, "slti"}, {43, "sw"}, },
- functs = { {32, "add"}, {36, "and"}, {39, "nor"}, {37, "or"}, {42, "slt"}, {0, "sll"}, {2, "srl"}, {34, "sub"}, },
- 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"}, };
- static string getOpcode(short opcode) {
- if (opcodes.find(opcode) != opcodes.end())
- return opcodes.find(opcode)->second;
- throw 1;
- }
- static string getFunct(short funct) {
- if (functs.find(funct) != functs.end())
- return functs.find(funct)->second;
- throw 1;
- }
- static string getReg(short reg) {
- if (regs.find(reg) != regs.end())
- return regs.find(reg)->second;
- throw 1;
- }
- static string decode(unsigned int hex) {
- string mips;
- short opcode = (hex >> 26) & 63;
- if (opcode == 0)
- 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));
- else if (opcode == 2)
- mips.append("j ").append(to_string(hex & 67108863));
- else
- mips.append(getOpcode(opcode)).append(" ").append(getReg((hex >> 21) & 31)).append(", ").append(to_string(hex & 65535)).append("(").append(getReg((hex >> 16) & 31)).append(")");
- return mips;
- }
- int main() {
- while (true) {
- try {
- string hex;
- cout << "Informe a instrução hexadecimal: ";
- cin >> hex;
- cout << decode(strtol(hex.c_str(), NULL, 16)) << "\n";
- } catch (int err) {
- if (err == 1)
- cout << "A instrução não é válida!" << "\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement