sim.cpp (4949B)
1 #include <filesystem> 2 #include <fstream> 3 #include <string> 4 #include <vector> 5 #include <unistd.h> 6 #include <iostream> 7 8 9 enum Mnemonic { 10 MOV, 11 UNKNOWN 12 }; 13 14 void printMnemonic(Mnemonic mn) { 15 switch(mn) { 16 case (MOV): 17 std::cout << "mov"; 18 break; 19 case (UNKNOWN): 20 std::cout << "unknown"; 21 break; 22 } 23 } 24 25 26 int isHalfReg(std::string reg) { 27 if(reg[1] == 'l' || reg[1] == 'h') { 28 return 1; 29 } 30 return 0; 31 } 32 33 Mnemonic getMnemonic(std::string line) { 34 if(line.substr(0,3) == "mov") { 35 return MOV; 36 } 37 return UNKNOWN; 38 } 39 40 std::string readFileToString(std::string filePath) { 41 auto in = std::ifstream(filePath); 42 std::ostringstream sstr; 43 sstr << in.rdbuf(); 44 return sstr.str(); 45 } 46 47 48 // assumes entire instruction is input 49 std::string parseDestination(std::string st) { 50 std::string source = st.substr(st.find(' ')+1); 51 return source.substr(source.find(' ')+1); 52 } 53 54 55 // assumes entire instruction is input 56 std::string parseSource(std::string st) { 57 std::string source = st.substr(st.find(' ')+1); 58 return source.substr(0,source.find(',')); 59 } 60 61 void printRegisters(std::vector<std::pair<std::string,uint16_t>> registers) { 62 for(auto& reg : registers) { 63 printf("\t%s: 0x%.2X\n", reg.first.c_str(), reg.second); 64 } 65 } 66 67 68 // return -1 for not register or byte offset into register vector 69 int8_t getReg(std::string location) { 70 if (location == "ax") { 71 return 0; 72 } 73 if (location == "bx") { 74 return 2; 75 } 76 if (location == "cx") { 77 return 4; 78 } 79 if (location == "dx") { 80 return 6; 81 } 82 if (location == "sp") { 83 return 8; 84 } 85 if (location == "bp") { 86 return 10; 87 } 88 if (location == "si") { 89 return 12; 90 } 91 if (location == "di") { 92 return 14; 93 } 94 95 return -1; 96 } 97 98 uint16_t valueAt(std::vector<std::pair<std::string,uint16_t>> registers, int idx) { 99 return registers[idx/2].second; 100 } 101 102 int main(int argc, char** argv) { 103 104 std::vector<std::pair<std::string,uint16_t>> registers{}; 105 registers.push_back({"ax",0}); 106 registers.push_back({"bx",0}); 107 registers.push_back({"cx",0}); 108 registers.push_back({"dx",0}); 109 registers.push_back({"sp",0}); 110 registers.push_back({"bp",0}); 111 registers.push_back({"si",0}); 112 registers.push_back({"di",0}); 113 114 std::string ftext = readFileToString(argv[1]); 115 std::vector<std::string> lines {}; 116 117 std::string acc = ""; 118 119 for(size_t i = 0; i < ftext.size(); ++i) { 120 if(ftext[i] == '\n') { 121 lines.push_back(acc); 122 acc = ""; 123 } 124 else { 125 acc += ftext[i]; 126 } 127 } 128 if(acc != "") { 129 lines.push_back(acc); 130 } 131 132 std::cout << "INSTRUCTIONS:" << std::endl; 133 134 for(auto& line : lines) { 135 Mnemonic mne = getMnemonic(line); 136 printMnemonic(mne); 137 std::cout << " "; 138 139 std::string source = parseSource(line); 140 std::string dest = parseDestination(line); 141 std::cout << source << ", " << dest; 142 143 if(mne == MOV) { 144 auto reg = getReg(source); 145 146 if(reg != -1) { 147 try { 148 if(isHalfReg(source)) { 149 auto before = registers[reg/2].second; 150 auto current = registers[reg/2].second; 151 if(reg%2 == 1) { 152 // lower (right) 153 current -= current & 0b0000000011111111; 154 current += std::stoi(dest); 155 } else { 156 // upper (left) 157 current -= current & 0b1111111100000000; 158 current += std::stoi(dest) << 8; 159 } 160 registers[reg/2].second = current; 161 printf("\t; 0x%.2X->0x%.2X", before , current); 162 } else { 163 auto before = registers[reg/2].second; 164 auto current = std::stoi(dest); 165 registers[reg/2].second = current; 166 printf("\t; 0x%.2X->0x%.2X", before , current); 167 } 168 } catch (...) { 169 auto num = getReg(dest); 170 if(num == -1) { 171 return -1; // not implemented yet 172 } 173 auto destVal = valueAt(registers,num); 174 175 auto before = registers[reg/2].second; 176 auto current = destVal; 177 registers[reg/2].second = current; 178 printf("\t; 0x%.2X->0x%.2X", before , current); 179 180 } 181 } 182 } 183 std::cout << std::endl; 184 185 } 186 187 std::cout << std::endl; 188 std::cout << "FINAL REGISTERS:" << std::endl; 189 printRegisters(registers); 190 191 }