diff --git a/part1/build.bat b/part1/build.bat index 3058f43..56b88bd 100644 --- a/part1/build.bat +++ b/part1/build.bat @@ -153,8 +153,8 @@ set build_dir_listing_0048=%build_dir%\%listing_0048% copy /Y %script_dir%\%listing_0048% %build_dir% 1>NUL copy /Y %script_dir%\%listing_0048%.txt %build_dir% 1>NUL -%build_dir%\sim8086.exe --exec %build_dir_listing_0048% > %build_dir_listing_0048%_disassembled.txt -%build_dir%\sim8086.exe %build_dir_listing_0048% > %build_dir_listing_0048%_disassembled.asm +%build_dir%\sim8086.exe --exec --log-instruction-ptr %build_dir_listing_0048% > %build_dir_listing_0048%_disassembled.txt +%build_dir%\sim8086.exe %build_dir_listing_0048% > %build_dir_listing_0048%_disassembled.asm nasm %build_dir_listing_0048%_disassembled.asm diff --git a/part1/sim8086.c b/part1/sim8086.c index 7e4e3d7..c6828af 100644 --- a/part1/sim8086.c +++ b/part1/sim8086.c @@ -332,6 +332,7 @@ S86_Opcode S86_DecodeOpcode(S86_BufferIterator *buffer_it, bool *lock_prefix, S86_MnemonicOp *seg_reg) { + size_t buffer_start_index = buffer_it->index; char op_code_bytes[2] = {0}; size_t op_code_size = 0; op_code_bytes[op_code_size++] = S86_BufferIteratorNextByte(buffer_it); @@ -815,6 +816,8 @@ S86_Opcode S86_DecodeOpcode(S86_BufferIterator *buffer_it, if (op_decode_type != S86_OpDecodeType_SEGMENT) *seg_reg = S86_MnemonicOp_Invalid; + size_t buffer_end_index = buffer_it->index; + result.byte_size = S86_CAST(uint8_t)(buffer_end_index - buffer_start_index); return result; } @@ -1207,6 +1210,8 @@ int main(int argc, char **argv) {.mnemonic_op = S86_MnemonicOp_DS, .mnemonic_op_reg16 = S86_MnemonicOp_DS, .reg = ®ister_file.ds, .byte = S86_RegisterByte_Nil}, }; + // NOTE: Count opcodes, allocate then decode in 1 swoop + // ========================================================================= S86_Opcode *opcode_array = NULL; size_t opcode_size = 0; { @@ -1235,6 +1240,8 @@ int main(int argc, char **argv) } } + // NOTE: Execute the assembly + // ========================================================================= for (size_t opcode_index = 0; opcode_index < opcode_size; opcode_index++) { S86_Opcode *opcode = opcode_array + opcode_index; S86_PrintOpcode(*opcode); @@ -1479,9 +1486,16 @@ int main(int argc, char **argv) S86_PrintFmt(" ; %.*s:0x%x->0x%x ", S86_STR8_FMT(dest_reg16), prev_dest.word, dest.word); *dest_map->reg = dest; } + } break; } + // NOTE: Print Instruction Pointer + if (log_instruction_ptr) + S86_PrintFmt("ip:0x%x->0x%x ", register_file.instruction_ptr, register_file.instruction_ptr + opcode->byte_size); + register_file.instruction_ptr += opcode->byte_size; + + // NOTE: Print Flags if (!S86_RegisterFileFlagsEq(register_file.flags, prev_flags)) { S86_PrintFmt("flags:"); if (prev_flags.carry) @@ -1512,6 +1526,7 @@ int main(int argc, char **argv) S86_PrintFmt("O"); S86_PrintFmt(" "); } + S86_Print(S86_STR8("\n")); } @@ -1540,6 +1555,9 @@ int main(int argc, char **argv) if (register_file.ds.word) S86_PrintLnFmt(" ds: 0x%04x (%u)", register_file.ds, register_file.ds); + if (log_instruction_ptr) + S86_PrintLnFmt(" ip: 0x%04x (%u)", register_file.instruction_ptr, register_file.instruction_ptr); + S86_RegisterFileFlags nil_flags = {0}; if (!S86_RegisterFileFlagsEq(register_file.flags, nil_flags)) { S86_PrintFmt(" flags: "); diff --git a/part1/sim8086.h b/part1/sim8086.h index ae5c6dd..60752f8 100644 --- a/part1/sim8086.h +++ b/part1/sim8086.h @@ -322,6 +322,7 @@ typedef enum S86_WidePrefix { } S86_WidePrefix; typedef struct S86_Opcode { + uint8_t byte_size; ///< Number of bytes used to encode this opcode S86_Mnemonic mnemonic; ///< Mnemonic type S86_EffectiveAddress effective_addr; ///< Src/dest op is an effective address calculation bool effective_addr_loads_mem; ///< Effective address uses '[]' notation to load address memory @@ -360,21 +361,22 @@ typedef struct S86_RegisterFileFlags { typedef struct S86_RegisterFile { S86_RegisterFileFlags flags; + uint16_t instruction_ptr; - S86_Register16 ax; - S86_Register16 bx; - S86_Register16 cx; - S86_Register16 dx; + S86_Register16 ax; + S86_Register16 bx; + S86_Register16 cx; + S86_Register16 dx; - S86_Register16 sp; - S86_Register16 bp; - S86_Register16 si; - S86_Register16 di; + S86_Register16 sp; + S86_Register16 bp; + S86_Register16 si; + S86_Register16 di; - S86_Register16 es; - S86_Register16 cs; - S86_Register16 ss; - S86_Register16 ds; + S86_Register16 es; + S86_Register16 cs; + S86_Register16 ss; + S86_Register16 ds; } S86_RegisterFile; bool S86_RegisterFileFlagsEq (S86_RegisterFileFlags lhs, S86_RegisterFileFlags rhs); diff --git a/project.rdbg b/project.rdbg index 97d0b68..3cf1ea3 100644 Binary files a/project.rdbg and b/project.rdbg differ