diff --git a/decode.c b/decode.c index c8225bf..2111f9b 100644 --- a/decode.c +++ b/decode.c @@ -145,9 +145,9 @@ int main(int argc, char** argv) } else { + bool is_direct_addr = mod == 0 && rm == 0b110; // This is a trick because mod == 1 and mod == 2 will displace one and two bytes // respectively but mod == 3 wraps to 0 since it doesn't displace - bool is_direct_addr = mod == 0 && rm == 0b110; int bytes_to_read = is_direct_addr ? 2 : mod % 3; bytes_read = fread(buf, sizeof(char), bytes_to_read, f); char* eac_name = is_direct_addr ? "" : get_eac_registers(rm); @@ -166,8 +166,8 @@ int main(int argc, char** argv) // Immediate to register/memory else if ((inst & ~0x1) == (char)0b11000110) { - char w = inst & 0b00000001; bytes_read = fread(buf, sizeof(char), 1, f); + char w = inst & 0b00000001; char mod = (buf[0] & 0b11000000) >> 6; char rm = (buf[0] & 0b00000111); int bytes_to_read = 1; @@ -176,28 +176,11 @@ int main(int argc, char** argv) bytes_to_read += mod % 3; bytes_read = fread(buf, sizeof(char), bytes_to_read, f); char *eac_name = get_eac_registers(rm); - char *data_ptr = (char*)buf + (char)bytes_to_read - (w == 0 ? 1 : 2); - i16 data = w == 0 ? data_ptr[0] : (i16)data_ptr[1] << 8 | data_ptr[0]; - char *word = w == 0 ? "byte" : "word"; - char disp_str[16]; - if (mod > 0 && mod < 3) - { - if (mod == 1) - { - sprintf(disp_str, " + %d", buf[1]); - } - else - { - i16 disp = (i16)buf[1] << 8 | buf[0]; - sprintf(disp_str, " + %d", disp); - } - } - else - { - disp_str[0] = '\0'; - } - - printf("mov [%s%s], %s %d ;w %d mod %d rm %d", eac_name, disp_str, word, data, w, mod, rm); + i16 data = get_data(buf + (char)bytes_to_read - (w == 0 ? 1 : 2), w); + char *word_str = w == 0 ? "byte" : "word"; + char disp_str[16] = {'\0'}; + if (mod % 3 > 1) sprintf(disp_str, " + %d", get_data(buf, (mod % 3) - 1)); + printf("mov [%s%s], %s %d ;3", eac_name, disp_str, word_str, data); } // Immediate to register else if ((inst & ~0xF) == (char)0b10110000) @@ -206,7 +189,7 @@ int main(int argc, char** argv) Register reg = registers[(size_t)inst & 0b00000111]; char bytes_to_read = w == 1 ? 2 : 1; bytes_read = fread(buf, sizeof(char), bytes_to_read, f); - printf("mov %s, %hd ; Immediate to register", reg_name(reg, w), get_data(buf, w)); + printf("mov %s, %hd ;4", reg_name(reg, w), get_data(buf, w)); } // Memory to accumulator else if ((inst & ~0x1) == (char)0b10100000)