0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-05-20 04:11:17 +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 // Read the immediate value in little-endian format
var imm = Decoder.ReadUInt32(); var imm32 = 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);
// Set the operands // Set the operands
instruction.Operands = $"{destOperand}, {immStr}"; instruction.Operands = $"{destOperand}, 0x{imm32:X8}";
return true; return true;
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -26,24 +26,4 @@ public class AddEaxImmHandlerTests
Assert.Equal("add", instruction.Mnemonic); Assert.Equal("add", instruction.Mnemonic);
Assert.Equal("eax, 0x12345678", instruction.Operands); 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);
}
} }