From 6f0715b0ca1caeb3cbf3ea04d12083fe9e862719 Mon Sep 17 00:00:00 2001 From: doyle Date: Sun, 16 Apr 2023 21:36:10 +1000 Subject: [PATCH] perfaware/part1: Support listing 0048 --- part1/build.bat | 4 ++-- part1/sim8086.c | 18 ++++++++++++++++++ part1/sim8086.h | 26 ++++++++++++++------------ project.rdbg | Bin 2565 -> 2484 bytes 4 files changed, 34 insertions(+), 14 deletions(-) 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 97d0b68fd40009d4921bddf8e94686e620523425..3cf1ea31891b4f705cd703ce59d87215f60312d4 100644 GIT binary patch delta 215 zcmZn_*&@85mdXAaD+2>F0|Nsm5T|A4q{bH{mShM3IZ35yX{kl=sd*{!nRzLx6+#HH z;*!Lo5{T&JU}jTBp2-Sq?wc<#tFla<%I4y33RJ19n_7{Yte~r#lb^1enO9s=RGM6p znV+XyP*S9jlUZDnnU@}KU|?bqpIH!Jl$s6{Ni72Eg8*(I#Xi}PLw>R(2QMQtkfklk c0~BLK0vtd-NJfv5XY+dwF~-RYIVBh^0GyXL1^@s6 delta 320 zcmdlY+$yr6mZ`ptm4Shmfq{Vqh*J_v68V9&MyZ9SMjeQ!psir236j>x%quZ8i!adx ziLyi0>L?`fK;*i&BeAb4v6wb8}NuG80QuL1G|h#pmV67iXsD#V2K!Oy0v}%E&!Ak=cE-C9^6E zD;H1=`(#B9`N