public void Write(X86Instruction instruction)
{
var opcode = instruction.OpCode;
WriteOpCode(opcode);
var mnemonicIndex = Array.IndexOf(opcode.Mnemonics, instruction.Mnemonic);
if (mnemonicIndex == -1)
throw new ArgumentException("Instruction's mnemonic is not supported by its opcode.");
if (opcode.HasRegisterToken)
{
var token = (byte)(ComputeRegisterTokenPart(opcode.OperandTypes1[mnemonicIndex],
opcode.OperandSizes1[mnemonicIndex], instruction.Operand1) |
ComputeRegisterTokenPart(opcode.OperandTypes2[mnemonicIndex],
opcode.OperandSizes2[mnemonicIndex], instruction.Operand2));
if (opcode.HasOpCodeModifier)
{
token |= (byte)(mnemonicIndex << 3);
}
_writer.WriteByte(token);
}
if (instruction.Operand1 != null)
{
WriteOperand(opcode.OperandTypes1[mnemonicIndex],
opcode.OperandSizes1[mnemonicIndex], instruction.Operand1);
if (instruction.Operand2 != null)
{
WriteOperand(opcode.OperandTypes2[mnemonicIndex],
opcode.OperandSizes2[mnemonicIndex], instruction.Operand2);
}
}
}