Пример #1
0
        public void Test_AsmSourceTools_parseMemOperand()
        {
            // see intel manual : 3.7.5 Specifying an Offset

            // 32-bit mode:
            // possible bases: EAX, EBX, ECX, EDX, ESP, EBP, ESI, EDI
            // possible index: EAX, EBX, ECX, EDX,      EBP, ESI, EDI
            // scale: 1, 2, 4, 8
            // displacement none, 8-bit, 16-bit, 32-bit

            // 64-bit mode:
            // possible bases: RAX, RBX, RCX, RDX, RSP, RBP, RSI, RDI
            // possible index: RAX, RBX, RCX, RDX, RSP, RBP, RSI, RDI
            // scale: 1, 2, 4, 8
            // displacement none, 8-bit, 16-bit, 32-bit

            Random rnd = new Random((int)DateTime.Now.Ticks);

            Rn[] bases32 = new Rn[] { Rn.EAX, Rn.EBX, Rn.ECX, Rn.EDX, Rn.ESP, Rn.EBP, Rn.ESI, Rn.EDI };
            Rn[] index32 = new Rn[] { Rn.EAX, Rn.EBX, Rn.ECX, Rn.EDX, Rn.EBP, Rn.ESI, Rn.EDI };

            Rn[] bases64 = new Rn[] { Rn.RAX, Rn.RBX, Rn.RCX, Rn.RDX, Rn.RSP, Rn.RBP, Rn.RSI, Rn.RDI };
            Rn[] index74 = new Rn[] { Rn.RAX, Rn.RBX, Rn.RCX, Rn.RDX, Rn.RSP, Rn.RBP, Rn.RSI, Rn.RDI };

            int[] scales = new int[] { 1, 2, 4, 8 };

            for (int i = 0; i < bases32.Length; ++i)
            {
                Rn b = bases32[i];
                {
                    string str = "[" + b + "]";
                    (bool valid, Rn baseReg, Rn indexReg, int scale, long displacement, int nBits, string errorMessage) = AsmSourceTools.Parse_Mem_Operand(str);
                    Assert.AreEqual(true, valid, str);
                    Assert.AreEqual(b, baseReg, "base: " + str);
                    Assert.AreEqual(Rn.NOREG, indexReg, "index: " + str);
                    Assert.AreEqual(0, scale, "scale: " + str);
                    Assert.AreEqual(0, displacement, "displacement: " + str);
                }

                for (int j = 0; j < index32.Length; ++j)
                {
                    Rn idx = index32[j];
                    {
                        string str = "[" + b + "+" + idx + "]";
                        (bool valid, Rn baseReg, Rn indexReg, int scale, long displacement, int nBits, string errorMessage) = AsmSourceTools.Parse_Mem_Operand(str);
                        Assert.AreEqual(true, valid, str);
                        Assert.AreEqual(b, baseReg, "base: " + str);
                        Assert.AreEqual(idx, indexReg, "index: " + str);
                        Assert.AreEqual(1, scale, "scale: " + str);
                        Assert.AreEqual(0, displacement, "displacement: " + str);
                    }
                    {
                        string str = "[" + idx + "+" + b + "]";
                        (bool valid, Rn baseReg, Rn indexReg, int scale, long displacement, int nBits, string errorMessage) = AsmSourceTools.Parse_Mem_Operand(str);
                        Assert.AreEqual(true, valid, str);
                        // idx and base can be interchanged
                        // Assert.AreEqual(b, t.Item2, "base: " + str);
                        // Assert.AreEqual(idx, t.Item3, "index: " + str);
                        Assert.AreEqual(1, scale, "scale: " + str);
                        Assert.AreEqual(0, displacement, "displacement: " + str);
                    }

                    for (int k = 0; k < scales.Length; ++k)
                    {
                        int s = scales[k];

                        // Offset = Base + (Index * Scale) + Displacement
                        {
                            string str = "[" + b + "+" + idx + " * " + s + "]";
                            (bool valid, Rn baseReg, Rn indexReg, int scale, long displacement, int nBits, string errorMessage) = AsmSourceTools.Parse_Mem_Operand(str);
                            Assert.AreEqual(true, valid, str);
                            Assert.AreEqual(b, baseReg, "base: " + str);
                            Assert.AreEqual(idx, indexReg, "index: " + str);
                            Assert.AreEqual(s, scale, "scale: " + str);
                            Assert.AreEqual(0, displacement, "displacement: " + str);
                        }
                        {
                            string str = "[" + b + "+" + s + " * " + idx + "]";
                            (bool valid, Rn baseReg, Rn indexReg, int scale, long displacement, int nBits, string errorMessage) = AsmSourceTools.Parse_Mem_Operand(str);
                            Assert.AreEqual(true, valid, str);
                            Assert.AreEqual(b, baseReg, "base: " + str);
                            Assert.AreEqual(idx, indexReg, "index: " + str);
                            Assert.AreEqual(s, scale, "scale: " + str);
                            Assert.AreEqual(0, displacement, "displacement: " + str);
                        }
                        {
                            string str = "[" + s + " * " + idx + "+" + b + "]";
                            (bool valid, Rn baseReg, Rn indexReg, int scale, long displacement, int nBits, string errorMessage) = AsmSourceTools.Parse_Mem_Operand(str);
                            Assert.AreEqual(true, valid, str);
                            Assert.AreEqual(b, baseReg, "base: " + str);
                            Assert.AreEqual(idx, indexReg, "index: " + str);
                            Assert.AreEqual(s, scale, "scale: " + str);
                            Assert.AreEqual(0, displacement, "displacement: " + str);
                        }
                        {
                            string str = "[" + idx + " * " + s + "+" + b + "]";
                            (bool valid, Rn baseReg, Rn indexReg, int scale, long displacement, int nBits, string errorMessage) = AsmSourceTools.Parse_Mem_Operand(str);
                            Assert.AreEqual(true, valid, str);
                            Assert.AreEqual(b, baseReg, "base: " + str);
                            Assert.AreEqual(idx, indexReg, "index: " + str);
                            Assert.AreEqual(s, scale, "scale: " + str);
                            Assert.AreEqual(0, displacement, "displacement: " + str);
                        }

                        for (int m = 0; m < 10; ++m)
                        {
                            long disp = RandLong(32, rnd);
                            {
                                {
                                    string str = "[" + b + "+" + idx + " * " + s + "+" + disp + "]";
                                    (bool valid, Rn baseReg, Rn indexReg, int scale, long displacement, int nBits, string errorMessage) = AsmSourceTools.Parse_Mem_Operand(str);
                                    Assert.AreEqual(true, valid, str);
                                    Assert.AreEqual(b, baseReg, "base: " + str);
                                    Assert.AreEqual(idx, indexReg, "index: " + str);
                                    Assert.AreEqual(s, scale, "scale: " + str);
                                    Assert.AreEqual(disp, displacement, "displacement: " + str);
                                }
                                {
                                    string str = "[" + b + "+" + s + " * " + idx + "+" + disp + "]";
                                    (bool valid, Rn baseReg, Rn indexReg, int scale, long displacement, int nBits, string errorMessage) = AsmSourceTools.Parse_Mem_Operand(str);
                                    Assert.AreEqual(true, valid, str);
                                    Assert.AreEqual(b, baseReg, "base: " + str);
                                    Assert.AreEqual(idx, indexReg, "index: " + str);
                                    Assert.AreEqual(s, scale, "scale: " + str);
                                    Assert.AreEqual(disp, displacement, "displacement: " + str);
                                }
                                {
                                    string str = "[" + s + " * " + idx + "+" + b + "+" + disp + "]";
                                    (bool valid, Rn baseReg, Rn indexReg, int scale, long displacement, int nBits, string errorMessage) = AsmSourceTools.Parse_Mem_Operand(str);
                                    Assert.AreEqual(true, valid, str);
                                    Assert.AreEqual(b, baseReg, "base: " + str);
                                    Assert.AreEqual(idx, indexReg, "index: " + str);
                                    Assert.AreEqual(s, scale, "scale: " + str);
                                    Assert.AreEqual(disp, displacement, "displacement: " + str);
                                }
                                {
                                    string str = "[" + idx + " * " + s + "+" + b + "+" + disp + "]";
                                    (bool valid, Rn baseReg, Rn indexReg, int scale, long displacement, int nBits, string errorMessage) = AsmSourceTools.Parse_Mem_Operand(str);
                                    Assert.AreEqual(true, valid, str);
                                    Assert.AreEqual(b, baseReg, "base: " + str);
                                    Assert.AreEqual(idx, indexReg, "index: " + str);
                                    Assert.AreEqual(s, scale, "scale: " + str);
                                    Assert.AreEqual(disp, displacement, "displacement: " + str);
                                }
                            }
                            {
                                {
                                    string str = "[" + disp + "+" + b + "+" + idx + " * " + s + "]";
                                    (bool valid, Rn baseReg, Rn indexReg, int scale, long displacement, int nBits, string errorMessage) = AsmSourceTools.Parse_Mem_Operand(str);
                                    Assert.AreEqual(true, valid, str);
                                    Assert.AreEqual(b, baseReg, "base: " + str);
                                    Assert.AreEqual(idx, indexReg, "index: " + str);
                                    Assert.AreEqual(s, scale, "scale: " + str);
                                    Assert.AreEqual(disp, displacement, "displacement: " + str);
                                }
                                {
                                    string str = "[" + b + "+" + disp + "+" + idx + " * " + s + "]";
                                    (bool valid, Rn baseReg, Rn indexReg, int scale, long displacement, int nBits, string errorMessage) = AsmSourceTools.Parse_Mem_Operand(str);
                                    Assert.AreEqual(true, valid, str);
                                    Assert.AreEqual(b, baseReg, "base: " + str);
                                    Assert.AreEqual(idx, indexReg, "index: " + str);
                                    Assert.AreEqual(s, scale, "scale: " + str);
                                    Assert.AreEqual(disp, displacement, "displacement: " + str);
                                }
                                {
                                    string str = "[" + b + "+" + disp + "+" + s + " * " + idx + "]";
                                    (bool valid, Rn baseReg, Rn indexReg, int scale, long displacement, int nBits, string errorMessage) = AsmSourceTools.Parse_Mem_Operand(str);
                                    Assert.AreEqual(true, valid, str);
                                    Assert.AreEqual(b, baseReg, "base: " + str);
                                    Assert.AreEqual(idx, indexReg, "index: " + str);
                                    Assert.AreEqual(s, scale, "scale: " + str);
                                    Assert.AreEqual(disp, displacement, "displacement: " + str);
                                }
                                {
                                    string str = "[" + s + " * " + idx + "+" + disp + "+" + b + "]";
                                    (bool valid, Rn baseReg, Rn indexReg, int scale, long displacement, int nBits, string errorMessage) = AsmSourceTools.Parse_Mem_Operand(str);
                                    Assert.AreEqual(true, valid, str);
                                    Assert.AreEqual(b, baseReg, "base: " + str);
                                    Assert.AreEqual(idx, indexReg, "index: " + str);
                                    Assert.AreEqual(s, scale, "scale: " + str);
                                    Assert.AreEqual(disp, displacement, "displacement: " + str);
                                }
                                {
                                    string str = "[" + idx + " * " + s + "+" + disp + "+" + b + "]";
                                    (bool valid, Rn baseReg, Rn indexReg, int scale, long displacement, int nBits, string errorMessage) = AsmSourceTools.Parse_Mem_Operand(str);
                                    Assert.AreEqual(true, valid, str);
                                    Assert.AreEqual(b, baseReg, "base: " + str);
                                    Assert.AreEqual(idx, indexReg, "index: " + str);
                                    Assert.AreEqual(s, scale, "scale: " + str);
                                    Assert.AreEqual(disp, displacement, "displacement: " + str);
                                }
                            }
                        }
                    }
                }
            }
        }