decode.c (11166B)
1 #include "stdio.h" 2 #include "assert.h" 3 #include "stdlib.h" 4 #include <stddef.h> 5 6 typedef struct { 7 unsigned char opcode; 8 unsigned char threeDisambiguationBits; 9 unsigned char opcodeUnset; 10 unsigned char d; 11 unsigned char w; 12 unsigned char s; 13 unsigned char mod; 14 unsigned char reg; 15 unsigned char rm; 16 unsigned char dl; 17 unsigned char dlUnset; 18 unsigned char dh; 19 unsigned char dhUnset; 20 unsigned char immediateToRegister; 21 } Instruction; 22 23 char* regMapping(unsigned char reg, unsigned char w) { 24 char* res = (char*)malloc(2*sizeof(char)); 25 if(w == 0) { 26 switch (reg % 4) { 27 case 0: 28 res[0] = 'a'; 29 break; 30 case 1: 31 res[0] = 'c'; 32 break; 33 case 2: 34 res[0] = 'd'; 35 break; 36 case 3: 37 res[0] = 'b'; 38 break; 39 } 40 if(reg < 4) { 41 res[1] = 'l'; 42 } else { 43 res[1] = 'h'; 44 } 45 } else { 46 switch (reg) { 47 case 0: 48 res[0] = 'a'; 49 break; 50 case 1: 51 res[0] = 'c'; 52 break; 53 case 2: 54 res[0] = 'd'; 55 break; 56 case 3: 57 res[0] = 'b'; 58 break; 59 case 4: 60 res[0] = 's'; 61 break; 62 case 5: 63 res[0] = 'b'; 64 break; 65 case 6: 66 res[0] = 's'; 67 break; 68 case 7: 69 res[0] = 'd'; 70 break; 71 } 72 if(reg < 4) { 73 res[1] = 'x'; 74 } 75 else if(reg < 6) { 76 res[1] = 'p'; 77 } else { 78 res[1] = 'i'; 79 } 80 } 81 82 return res; 83 } 84 85 86 void handleEAC(Instruction inst) { 87 if(inst.d == 1) { 88 char* enc2 = regMapping(inst.reg,inst.w); 89 printf("%c%c, ", enc2[0], enc2[1]); 90 free(enc2); 91 } 92 93 if(inst.rm == 0b0) { 94 printf("[bx + si"); 95 } else if(inst.rm == 0b1) { 96 printf("[bx + di"); 97 } else if(inst.rm == 0b10) { 98 printf("[bp + si"); 99 } else if(inst.rm == 0b11) { 100 printf("[bp + di"); 101 } else if(inst.rm == 0b100) { 102 printf("[si"); 103 } else if(inst.rm == 0b101) { 104 printf("[di"); 105 } else if(inst.rm == 0b110) { 106 printf("[bp"); 107 108 } else if(inst.rm == 0b111) { 109 printf("[bx"); 110 } 111 112 if(inst.mod == 0) { 113 printf("]"); 114 } 115 116 if(inst.mod == 1) { 117 if(inst.dl == 0) { 118 printf("]"); 119 } else { 120 printf(" + %hhd]", inst.dl); 121 } 122 } 123 124 if(inst.mod == 2) { 125 printf(" + %hd]", ((inst.dh) << 8) | inst.dl); 126 } 127 128 if(inst.d == 0) { 129 char* enc2 = regMapping(inst.reg,inst.w); 130 printf(", %c%c\n", enc2[0], enc2[1]); 131 free(enc2); 132 } else { 133 printf("\n"); 134 } 135 136 } 137 138 void printInstructionMnemonic(Instruction inst) { 139 if(inst.opcode == 34 || inst.opcode == 11) { 140 printf("mov "); 141 } else { 142 printf("add "); 143 } 144 } 145 146 // return 1 to inform caller to give us more bytes 147 // return 0 if done 148 unsigned char printInstruction(Instruction inst) { 149 150 if(inst.opcode == 34 || inst.opcode == 0) { 151 // register mode 152 if(inst.mod == 0b11) { 153 if(inst.d == 1) { 154 printInstructionMnemonic(inst); 155 char* enc1 = regMapping(inst.reg,inst.w); 156 printf("%c%c,", enc1[0], enc1[1]); 157 free(enc1); 158 159 char* enc2 = regMapping(inst.rm,inst.w); 160 printf("%c%c\n,", enc2[0], enc2[1]); 161 free(enc2); 162 163 } else { 164 printInstructionMnemonic(inst); 165 char* enc1 = regMapping(inst.rm,inst.w); 166 printf("%c%c,", enc1[0], enc1[1]); 167 free(enc1); 168 char* enc2 = regMapping(inst.reg,inst.w); 169 printf("%c%c\n", enc2[0], enc2[1]); 170 free(enc2); 171 } 172 } else if(inst.mod == 0b01){ 173 // one of the bytes 174 if(inst.dlUnset) { 175 return 1; 176 } 177 printInstructionMnemonic(inst); 178 handleEAC(inst); 179 } else if(inst.mod == 0b10){ 180 // both bytes 181 if(inst.dlUnset || inst.dhUnset) { 182 return 1; 183 } 184 185 printInstructionMnemonic(inst); 186 187 handleEAC(inst); 188 189 } else if(inst.mod == 0b00){ 190 printInstructionMnemonic(inst); 191 handleEAC(inst); 192 // memory mode, no displacement 193 return 0; 194 } 195 196 // mask then check matches 1011 197 } else if(inst.opcode == 0b1011 || inst.opcode == 32){ 198 if(inst.dlUnset) { 199 return 1; 200 } 201 if(inst.w == 1 && inst.dhUnset) { 202 return 1; 203 } 204 205 printInstructionMnemonic(inst); 206 207 char* enc1 = regMapping(inst.reg,inst.w); 208 printf("%c%c,", enc1[0], enc1[1]); 209 if(inst.dhUnset == 0) { 210 printf("%hd\n", ((inst.dh) << 8) | inst.dl); 211 } else { 212 printf("%hhd\n", (int)inst.dl); 213 } 214 215 free(enc1); 216 217 } else { 218 printf("%u\n", (inst.opcode)); 219 220 } 221 return 0; 222 } 223 224 Instruction defaultInstruction() { 225 Instruction inst = (Instruction){ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0}; 226 return inst; 227 } 228 229 void printBinary(int num) { 230 for (int i = sizeof(char) * 8 - 1; i >= 0; i--) { 231 printf("%d", (num >> i) & 1); 232 } 233 printf("\n"); 234 } 235 236 237 char isJump(unsigned char firstByte) { 238 switch (firstByte) { 239 case (116): 240 return 1; 241 case (117): 242 return 1; 243 case (124): 244 return 1; 245 case (126): 246 return 1; 247 case (114): 248 return 1; 249 case (118): 250 return 1; 251 case (122): 252 return 1; 253 case (112): 254 return 1; 255 case (113): 256 return 1; 257 case (120): 258 return 1; 259 case (125): 260 return 1; 261 case (127): 262 return 1; 263 case (119): 264 return 1; 265 case (115): 266 return 1; 267 case (123): 268 return 1; 269 case (121): 270 return 1; 271 case (226): 272 return 1; 273 case (225): 274 return 1; 275 case (224): 276 return 1; 277 case (227): 278 return 1; 279 } 280 return 0; 281 } 282 283 void handleJump(unsigned char firstByte, unsigned char secondByte) { 284 switch (firstByte) { 285 case (116): 286 printf("je %hhd\n", secondByte); 287 return; 288 case (117): 289 printf("jnz %hhd\n", secondByte); 290 return; 291 case (124): 292 printf("jl %hhd\n", secondByte); 293 return; 294 case (126): 295 printf("jle %hhd\n", secondByte); 296 return; 297 case (114): 298 printf("jb %hhd\n", secondByte); 299 return; 300 case (118): 301 printf("jbe %hhd\n", secondByte); 302 return; 303 case (122): 304 printf("jp %hhd\n", secondByte); 305 return; 306 case (112): 307 printf("jo %hhd\n", secondByte); 308 return; 309 case (120): 310 printf("js %hhd\n", secondByte); 311 return; 312 case (125): 313 printf("jnl %hhd\n", secondByte); 314 return; 315 case (127): 316 printf("jg %hhd\n", secondByte); 317 return; 318 case (119): 319 printf("ja %hhd\n", secondByte); 320 return; 321 case (115): 322 printf("jnb %hhd\n", secondByte); 323 return; 324 case (123): 325 printf("jnp %hhd\n", secondByte); 326 return; 327 case (113): 328 printf("jno %hhd\n", secondByte); 329 return; 330 case (121): 331 printf("jns %hhd\n", secondByte); 332 return; 333 case (226): 334 printf("loop %hhd\n", secondByte); 335 return; 336 case (225): 337 printf("loopz %hhd\n", secondByte); 338 return; 339 case (224): 340 printf("loopnz %hhd\n", secondByte); 341 return; 342 case (227): 343 printf("jcxz %hhd\n", secondByte); 344 return; 345 } 346 assert(0 == firstByte); 347 } 348 349 int main(int argc, char** argv) { 350 351 // first 6 := opcode 352 // 7th bit := D / 0 = REG is source, 1 = REG is destination (REG is first) 353 // 8th bit := W / 0 = byte, 1 = word 354 // 9/10 bit := mod := memory mode / register mode 355 // 00 - mem mode, no displace 356 // 01 - mem mode, displace 8 bit -- these have to due with longer instructions it seems 357 // 10 - mem mode, displace 16 bit -- these have to due with longer instructions it seems 358 // 11 - register mode 359 // 11/12 bit := reg == register operand 360 // 12/13 bit := r/m == register or memory operand 361 362 if(argc != 2) { 363 return -1; 364 } 365 366 char* fname = argv[1]; 367 FILE* fp = fopen(fname, "rb"); 368 if(fp == NULL) { 369 return -1; 370 } 371 372 // TODO: Dynamic sizing 373 374 size_t len = 0; 375 376 Instruction inst = defaultInstruction(); 377 unsigned char fl = 0; 378 379 unsigned char buffer[65536]; 380 size_t n = fread(buffer, 1, sizeof(buffer), fp); 381 char jumping = 0; 382 char jumpPrior; 383 384 for(size_t i = 0; i < n; ++i) { 385 // printBinary(buffer[i]); 386 387 if(jumping) { 388 handleJump(jumpPrior, buffer[i]); 389 jumping = 0; 390 inst = defaultInstruction(); 391 continue; 392 } 393 if(fl == 0) { 394 if(inst.opcodeUnset) { 395 unsigned char cur = buffer[i]; 396 if((cur & 0b11110000) == 0b10110000) { 397 inst.opcode = 0b1011; 398 inst.w = (buffer[i] & 0b00001000) >> 3; 399 inst.reg = (buffer[i] & 0b00000111); 400 inst.immediateToRegister = 1; 401 } else { 402 inst.opcode = (buffer[i] & 0b11111100) >> 2; 403 404 if(isJump(buffer[i])) { 405 jumpPrior = buffer[i]; 406 jumping = 1; 407 continue; 408 } 409 410 411 if(inst.opcode == 32) { 412 inst.s = (buffer[i] & 0b00000010) >> 1; 413 printf("Sign: %u\n", inst.s); 414 } else { 415 inst.d = (buffer[i] & 0b00000010) >> 1; 416 } 417 inst.w = buffer[i] & 0b00000001; 418 } 419 inst.opcodeUnset = 0; 420 } else { 421 if(inst.immediateToRegister == 1) { 422 if(inst.dlUnset == 1) { 423 inst.dl = buffer[i]; 424 inst.dlUnset = 0; 425 } else { 426 inst.dh = buffer[i]; 427 inst.dhUnset = 0; 428 } 429 } else { 430 inst.mod = (buffer[i] & 0b11000000) >> 6; 431 inst.reg = (buffer[i] & 0b00111000) >> 3; 432 inst.rm = buffer[i] & 0b00000111; 433 } 434 435 unsigned char res = printInstruction(inst); 436 437 if(res == 0) { 438 inst = defaultInstruction(); 439 } else { 440 if(inst.immediateToRegister == 0) { 441 fl = 1; 442 } 443 } 444 } 445 } else { 446 if(inst.dlUnset) { 447 inst.dlUnset = 0; 448 inst.dl = buffer[i]; 449 unsigned char res = printInstruction(inst); 450 if(res == 0) { 451 fl = 0; 452 inst = defaultInstruction(); 453 } 454 } else { 455 inst.dhUnset = 0; 456 inst.dh = buffer[i]; 457 unsigned char res = printInstruction(inst); 458 fl = 0; 459 inst = defaultInstruction(); 460 } 461 } 462 } 463 464 fclose(fp); 465 }