using X86Disassembler.X86; using X86Disassembler.X86.Operands; namespace X86DisassemblerTests.InstructionTests; /// /// Tests for exchange instruction handlers /// public class XchgInstructionTests { /// /// Tests the XchgEaxRegHandler for decoding NOP instruction (XCHG EAX, EAX) /// [Fact] public void XchgEaxRegHandler_DecodesNop_Correctly() { // Arrange // NOP (90) - No operation (XCHG EAX, EAX) byte[] codeBuffer = new byte[] { 0x90 }; var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length); // Act var instruction = decoder.DecodeInstruction(); // Assert Assert.NotNull(instruction); Assert.Equal(InstructionType.Nop, instruction.Type); // Check that we have no operands for NOP Assert.Empty(instruction.StructuredOperands); } /// /// Tests the XchgEaxRegHandler for decoding XCHG EAX, ECX instruction /// [Fact] public void XchgEaxRegHandler_DecodesXchgEaxEcx_Correctly() { // Arrange // XCHG EAX, ECX (91) - Exchange EAX and ECX byte[] codeBuffer = new byte[] { 0x91 }; var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length); // Act var instruction = decoder.DecodeInstruction(); // Assert Assert.NotNull(instruction); Assert.Equal(InstructionType.Xchg, instruction.Type); // Check that we have two operands Assert.Equal(2, instruction.StructuredOperands.Count); // Check the first operand (EAX) var eaxOperand = instruction.StructuredOperands[0]; Assert.IsType(eaxOperand); var registerOperand1 = (RegisterOperand)eaxOperand; Assert.Equal(RegisterIndex.A, registerOperand1.Register); Assert.Equal(32, registerOperand1.Size); // Validate that it's a 32-bit register (EAX) // Check the second operand (ECX) var ecxOperand = instruction.StructuredOperands[1]; Assert.IsType(ecxOperand); var registerOperand2 = (RegisterOperand)ecxOperand; Assert.Equal(RegisterIndex.C, registerOperand2.Register); Assert.Equal(32, registerOperand2.Size); // Validate that it's a 32-bit register (ECX) } /// /// Tests the XchgEaxRegHandler for decoding XCHG EAX, EDX instruction /// [Fact] public void XchgEaxRegHandler_DecodesXchgEaxEdx_Correctly() { // Arrange // XCHG EAX, EDX (92) - Exchange EAX and EDX byte[] codeBuffer = new byte[] { 0x92 }; var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length); // Act var instruction = decoder.DecodeInstruction(); // Assert Assert.NotNull(instruction); Assert.Equal(InstructionType.Xchg, instruction.Type); // Check that we have two operands Assert.Equal(2, instruction.StructuredOperands.Count); // Check the first operand (EAX) var eaxOperand = instruction.StructuredOperands[0]; Assert.IsType(eaxOperand); var registerOperand1 = (RegisterOperand)eaxOperand; Assert.Equal(RegisterIndex.A, registerOperand1.Register); Assert.Equal(32, registerOperand1.Size); // Validate that it's a 32-bit register (EAX) // Check the second operand (EDX) var edxOperand = instruction.StructuredOperands[1]; Assert.IsType(edxOperand); var registerOperand2 = (RegisterOperand)edxOperand; Assert.Equal(RegisterIndex.D, registerOperand2.Register); Assert.Equal(32, registerOperand2.Size); // Validate that it's a 32-bit register (EDX) } /// /// Tests the XchgEaxRegHandler for decoding XCHG EAX, EBX instruction /// [Fact] public void XchgEaxRegHandler_DecodesXchgEaxEbx_Correctly() { // Arrange // XCHG EAX, EBX (93) - Exchange EAX and EBX byte[] codeBuffer = new byte[] { 0x93 }; var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length); // Act var instruction = decoder.DecodeInstruction(); // Assert Assert.NotNull(instruction); Assert.Equal(InstructionType.Xchg, instruction.Type); // Check that we have two operands Assert.Equal(2, instruction.StructuredOperands.Count); // Check the first operand (EAX) var eaxOperand = instruction.StructuredOperands[0]; Assert.IsType(eaxOperand); var registerOperand1 = (RegisterOperand)eaxOperand; Assert.Equal(RegisterIndex.A, registerOperand1.Register); Assert.Equal(32, registerOperand1.Size); // Validate that it's a 32-bit register (EAX) // Check the second operand (EBX) var ebxOperand = instruction.StructuredOperands[1]; Assert.IsType(ebxOperand); var registerOperand2 = (RegisterOperand)ebxOperand; Assert.Equal(RegisterIndex.B, registerOperand2.Register); Assert.Equal(32, registerOperand2.Size); // Validate that it's a 32-bit register (EBX) } /// /// Tests the XchgEaxRegHandler for decoding XCHG EAX, ESP instruction /// [Fact] public void XchgEaxRegHandler_DecodesXchgEaxEsp_Correctly() { // Arrange // XCHG EAX, ESP (94) - Exchange EAX and ESP byte[] codeBuffer = new byte[] { 0x94 }; var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length); // Act var instruction = decoder.DecodeInstruction(); // Assert Assert.NotNull(instruction); Assert.Equal(InstructionType.Xchg, instruction.Type); // Check that we have two operands Assert.Equal(2, instruction.StructuredOperands.Count); // Check the first operand (EAX) var eaxOperand = instruction.StructuredOperands[0]; Assert.IsType(eaxOperand); var registerOperand1 = (RegisterOperand)eaxOperand; Assert.Equal(RegisterIndex.A, registerOperand1.Register); Assert.Equal(32, registerOperand1.Size); // Validate that it's a 32-bit register (EAX) // Check the second operand (ESP) var espOperand = instruction.StructuredOperands[1]; Assert.IsType(espOperand); var registerOperand2 = (RegisterOperand)espOperand; Assert.Equal(RegisterIndex.Sp, registerOperand2.Register); Assert.Equal(32, registerOperand2.Size); // Validate that it's a 32-bit register (ESP) } /// /// Tests the XchgEaxRegHandler for decoding XCHG EAX, EBP instruction /// [Fact] public void XchgEaxRegHandler_DecodesXchgEaxEbp_Correctly() { // Arrange // XCHG EAX, EBP (95) - Exchange EAX and EBP byte[] codeBuffer = new byte[] { 0x95 }; var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length); // Act var instruction = decoder.DecodeInstruction(); // Assert Assert.NotNull(instruction); Assert.Equal(InstructionType.Xchg, instruction.Type); // Check that we have two operands Assert.Equal(2, instruction.StructuredOperands.Count); // Check the first operand (EAX) var eaxOperand = instruction.StructuredOperands[0]; Assert.IsType(eaxOperand); var registerOperand1 = (RegisterOperand)eaxOperand; Assert.Equal(RegisterIndex.A, registerOperand1.Register); Assert.Equal(32, registerOperand1.Size); // Validate that it's a 32-bit register (EAX) // Check the second operand (EBP) var ebpOperand = instruction.StructuredOperands[1]; Assert.IsType(ebpOperand); var registerOperand2 = (RegisterOperand)ebpOperand; Assert.Equal(RegisterIndex.Bp, registerOperand2.Register); Assert.Equal(32, registerOperand2.Size); // Validate that it's a 32-bit register (EBP) } /// /// Tests the XchgEaxRegHandler for decoding XCHG EAX, ESI instruction /// [Fact] public void XchgEaxRegHandler_DecodesXchgEaxEsi_Correctly() { // Arrange // XCHG EAX, ESI (96) - Exchange EAX and ESI byte[] codeBuffer = new byte[] { 0x96 }; var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length); // Act var instruction = decoder.DecodeInstruction(); // Assert Assert.NotNull(instruction); Assert.Equal(InstructionType.Xchg, instruction.Type); // Check that we have two operands Assert.Equal(2, instruction.StructuredOperands.Count); // Check the first operand (EAX) var eaxOperand = instruction.StructuredOperands[0]; Assert.IsType(eaxOperand); var registerOperand1 = (RegisterOperand)eaxOperand; Assert.Equal(RegisterIndex.A, registerOperand1.Register); Assert.Equal(32, registerOperand1.Size); // Validate that it's a 32-bit register (EAX) // Check the second operand (ESI) var esiOperand = instruction.StructuredOperands[1]; Assert.IsType(esiOperand); var registerOperand2 = (RegisterOperand)esiOperand; Assert.Equal(RegisterIndex.Si, registerOperand2.Register); Assert.Equal(32, registerOperand2.Size); // Validate that it's a 32-bit register (ESI) } /// /// Tests the XchgEaxRegHandler for decoding XCHG EAX, EDI instruction /// [Fact] public void XchgEaxRegHandler_DecodesXchgEaxEdi_Correctly() { // Arrange // XCHG EAX, EDI (97) - Exchange EAX and EDI byte[] codeBuffer = new byte[] { 0x97 }; var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length); // Act var instruction = decoder.DecodeInstruction(); // Assert Assert.NotNull(instruction); Assert.Equal(InstructionType.Xchg, instruction.Type); // Check that we have two operands Assert.Equal(2, instruction.StructuredOperands.Count); // Check the first operand (EAX) var eaxOperand = instruction.StructuredOperands[0]; Assert.IsType(eaxOperand); var registerOperand1 = (RegisterOperand)eaxOperand; Assert.Equal(RegisterIndex.A, registerOperand1.Register); Assert.Equal(32, registerOperand1.Size); // Validate that it's a 32-bit register (EAX) // Check the second operand (EDI) var ediOperand = instruction.StructuredOperands[1]; Assert.IsType(ediOperand); var registerOperand2 = (RegisterOperand)ediOperand; Assert.Equal(RegisterIndex.Di, registerOperand2.Register); Assert.Equal(32, registerOperand2.Size); // Validate that it's a 32-bit register (EDI) } /// /// Tests a sequence with NOP instructions /// [Fact] public void XchgEaxRegHandler_DecodesNopSequence_Correctly() { // Arrange // Multiple NOPs followed by XCHG EAX, ECX byte[] codeBuffer = new byte[] { 0x90, 0x90, 0x90, 0x91 }; var disassembler = new Disassembler(codeBuffer, 0); // Act var instructions = disassembler.Disassemble(); // Assert Assert.Equal(4, instructions.Count); // First three instructions should be NOPs for (int i = 0; i < 3; i++) { Assert.Equal(InstructionType.Nop, instructions[i].Type); Assert.Empty(instructions[i].StructuredOperands); } // Last instruction should be XCHG EAX, ECX Assert.Equal(InstructionType.Xchg, instructions[3].Type); // Check that we have two operands Assert.Equal(2, instructions[3].StructuredOperands.Count); // Check the first operand (EAX) var eaxOperand = instructions[3].StructuredOperands[0]; Assert.IsType(eaxOperand); var registerOperand1 = (RegisterOperand)eaxOperand; Assert.Equal(RegisterIndex.A, registerOperand1.Register); Assert.Equal(32, registerOperand1.Size); // Validate that it's a 32-bit register (EAX) // Check the second operand (ECX) var ecxOperand = instructions[3].StructuredOperands[1]; Assert.IsType(ecxOperand); var registerOperand2 = (RegisterOperand)ecxOperand; Assert.Equal(RegisterIndex.C, registerOperand2.Register); Assert.Equal(32, registerOperand2.Size); // Validate that it's a 32-bit register (ECX) } }