0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-05-19 03:41:18 +03:00

simplify reading logic

This commit is contained in:
bird_egop 2025-04-13 23:22:30 +03:00
parent 0ea3294c61
commit 00547ed273
13 changed files with 31 additions and 79 deletions

View File

@ -65,18 +65,10 @@ public class AdcImmToRm32Handler : InstructionHandler
}
// Read the immediate value in little-endian format
var imm = Decoder.ReadUInt32();
// Format the immediate value as expected by the tests (0x12345678)
// Note: The bytes are reversed to match the expected format in the tests
string immStr = $"0x{imm:X8}";
// Advance the position past the immediate value
position += 4;
Decoder.SetPosition(position);
var imm32 = Decoder.ReadUInt32();
// Set the operands
instruction.Operands = $"{destOperand}, {immStr}";
instruction.Operands = $"{destOperand}, 0x{imm32:X8}";
return true;
}

View File

@ -50,18 +50,14 @@ public class AddR32Rm32Handler : InstructionHandler
// Get the register name
string regName = ModRMDecoder.GetRegisterName(reg, 32);
// For memory operands, set the operand
if (mod != 3) // Memory operand
if (mod == 3)
{
string operand = ModRMDecoder.DecodeModRM(mod, rm, false);
instruction.Operands = $"{regName}, {operand}";
}
else // Register operand
{
string rmName = ModRMDecoder.GetRegisterName(rm, 32);
instruction.Operands = $"{regName}, {rmName}";
// Register operand
destOperand = ModRMDecoder.GetRegisterName(rm, 32);
}
instruction.Operands = $"{regName}, {destOperand}";
return true;
}
}
}

View File

@ -48,20 +48,16 @@ public class AddRm32R32Handler : InstructionHandler
instruction.Mnemonic = "add";
// Get the register name
string regName = ModRMDecoder.GetRegisterName(reg, 32);;
string regName = ModRMDecoder.GetRegisterName(reg, 32);
// For memory operands, set the operand
if (mod != 3) // Memory operand
if (mod == 3)
{
string operand = ModRMDecoder.DecodeModRM(mod, rm, false);
instruction.Operands = $"{operand}, {regName}";
}
else // Register operand
{
string rmName = ModRMDecoder.GetRegisterName(rm, 32);;
instruction.Operands = $"{rmName}, {regName}";
// Register operand
destOperand = ModRMDecoder.GetRegisterName(rm, 32);
}
instruction.Operands = $"{destOperand}, {regName}";
return true;
}
}
}

View File

@ -53,14 +53,11 @@ public class AndMemRegHandler : InstructionHandler
// For mod == 3, both operands are registers
if (mod == 3)
{
string rmRegName = ModRMDecoder.GetRegisterName(rm, 32);
instruction.Operands = $"{rmRegName}, {regName}";
memOperand = ModRMDecoder.GetRegisterName(rm, 32);
}
else // Memory operand
{
instruction.Operands = $"{memOperand}, {regName}";
}
instruction.Operands = $"{memOperand}, {regName}";
return true;
}
}

View File

@ -53,14 +53,11 @@ public class AndR32Rm32Handler : InstructionHandler
// For mod == 3, both operands are registers
if (mod == 3)
{
string rmRegName = ModRMDecoder.GetRegisterName(rm, 32);
instruction.Operands = $"{regName}, {rmRegName}";
memOperand = ModRMDecoder.GetRegisterName(rm, 32);
}
else // Memory operand
{
instruction.Operands = $"{regName}, {memOperand}";
}
instruction.Operands = $"{regName}, {memOperand}";
return true;
}
}

View File

@ -45,8 +45,7 @@ public class CallRel32Handler : InstructionHandler
}
// Read the relative offset
int offset = BitConverter.ToInt32(CodeBuffer, position);
Decoder.SetPosition(position + 4);
uint offset = Decoder.ReadUInt32();
// Calculate the target address
uint targetAddress = (uint)(position + offset + 4);

View File

@ -45,8 +45,7 @@ public class JmpRel32Handler : InstructionHandler
}
// Read the relative offset
int offset = BitConverter.ToInt32(CodeBuffer, position);
Decoder.SetPosition(position + 4);
uint offset = Decoder.ReadUInt32();
// Calculate the target address
uint targetAddress = (uint)(position + offset + 4);

View File

@ -74,8 +74,7 @@ public class TwoByteConditionalJumpHandler : InstructionHandler
}
// Read the relative offset (32-bit)
int offset = BitConverter.ToInt32(CodeBuffer, position);
Decoder.SetPosition(position + 4);
uint offset = Decoder.ReadUInt32();
// Calculate the target address
uint targetAddress = (uint)(position + offset + 4);

View File

@ -1,4 +1,4 @@
namespace X86Disassembler.X86.Handlers;
namespace X86Disassembler.X86.Handlers.Nop;
/// <summary>
/// Handler for INT3 instruction (0xCC)

View File

@ -64,8 +64,7 @@ public class OrImmToRm32Handler : InstructionHandler
return false;
}
uint imm32 = BitConverter.ToUInt32(CodeBuffer, position);
Decoder.SetPosition(position + 4);
uint imm32 = Decoder.ReadUInt32();
// Set the operands
instruction.Operands = $"{destOperand}, 0x{imm32:X8}";

View File

@ -45,8 +45,7 @@ public class RetImmHandler : InstructionHandler
}
// Read the immediate value
ushort imm16 = BitConverter.ToUInt16(CodeBuffer, position);
Decoder.SetPosition(position + 2);
ushort imm16 = Decoder.ReadUInt16();
// Set the operands
instruction.Operands = $"0x{imm16:X4}";

View File

@ -68,9 +68,8 @@ public class TestImmWithRm32Handler : InstructionHandler
return false;
}
// Read the immediate value using BitConverter
uint imm32 = BitConverter.ToUInt32(CodeBuffer, position);
Decoder.SetPosition(position + 4);
// Read the immediate value
uint imm32 = Decoder.ReadUInt32();
// Set the operands
instruction.Operands = $"{destOperand}, 0x{imm32:X8}";

View File

@ -26,24 +26,4 @@ public class AddEaxImmHandlerTests
Assert.Equal("add", instruction.Mnemonic);
Assert.Equal("eax, 0x12345678", instruction.Operands);
}
/// <summary>
/// Tests the AddEaxImmHandler for handling insufficient bytes
/// </summary>
[Fact]
public void AddEaxImmHandler_HandlesInsufficientBytes_Gracefully()
{
// Arrange
// ADD EAX, ?? (05) - missing immediate value
byte[] codeBuffer = new byte[] { 0x05 };
var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length);
// Act
var instruction = decoder.DecodeInstruction();
// Assert
Assert.NotNull(instruction);
Assert.Equal("add", instruction.Mnemonic);
Assert.Equal("eax, ??", instruction.Operands);
}
}