Exemple #1
0
        internal static void Emulate(VirtualMachine vm, uint count)
        {
            var processor = vm.Processor;
            var memory = vm.PhysicalMemory;

            unsafe
            {
                uint* eip = (uint*)processor.PIP;

                for (uint i = 0; i < count; i++)
                {
                    uint startEIP = *eip;
                    processor.StartEIP = startEIP;

                    byte* ip = processor.CachedInstruction;
                    memory.FetchInstruction(processor.segmentBases[(int)SegmentIndex.CS] + startEIP, ip);

                    uint sizeModeIndex = processor.SizeModeIndex;

                    byte byte1 = ip[0];
                    var inst = OneBytePtrs[sizeModeIndex][byte1];
                    if (inst != null)
                    {
                        *eip = startEIP + 1;
                        processor.CachedIP = ip + 1;
                        inst(vm);
                        continue;
                    }

                    var instSet = TwoBytePtrs[sizeModeIndex][byte1];
                    if (instSet != null)
                    {
                        inst = instSet[ip[1]];
                        if (inst != null)
                        {
                            *eip = startEIP + 2;
                            processor.CachedIP = ip + 2;
                            inst(vm);
                            continue;
                        }
                    }

                    instSet = RmPtrs[sizeModeIndex][byte1];
                    if (instSet != null)
                    {
                        inst = instSet[Intrinsics.ExtractBits(ip[1], 3, 3, 0x38)];
                        if (inst == null)
                            ThrowGetOpcodeException(ip);

                        *eip = startEIP + 1;
                        processor.CachedIP = ip + 1;
                        inst(vm);
                        continue;
                    }

                    var instSetSet = TwoByteRmPtrs[sizeModeIndex][byte1];
                    if (instSetSet != null)
                    {
                        instSet = instSetSet[ip[1]];
                        if (instSet != null)
                        {
                            inst = instSet[Intrinsics.ExtractBits(ip[2], 3, 3, 0x38)];
                            if (inst != null)
                            {
                                *eip = startEIP + 2;
                                processor.CachedIP = ip + 2;
                                inst(vm);
                                continue;
                            }
                        }
                    }

                    ThrowGetOpcodeException(ip);
                }
            }
        }