Exemple #1
0
        public override void Write(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            var s  = FormatValue(value);
            var pt = value.DataType as PrimitiveType;

            if (pt != null)
            {
                if (pt.Domain == Domain.Pointer)
                {
                    writer.WriteAddress(s, Address.FromConstant(value));
                }
                else
                {
                    writer.Write(s);
                }
            }
            else if (value.DataType is Pointer)
            {
                writer.WriteAddress(s, Address.FromConstant(value));
            }
            else
            {
                writer.Write(s);
            }
        }
Exemple #2
0
        public override void Write(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            string fmt;

            switch (Mode)
            {
            case AddressMode.RegDef: fmt = "@{0}"; break;

            case AddressMode.AutoIncr: fmt = "({0})+"; break;

            case AddressMode.AutoIncrDef: fmt = "@({0})+"; break;

            case AddressMode.AutoDecr: fmt = "-({0})"; break;

            case AddressMode.AutoDecrDef: fmt = "@-({0})"; break;

            case AddressMode.Indexed:
                if (Register == Registers.pc &&
                    (options & MachineInstructionWriterOptions.ResolvePcRelativeAddress) != 0)
                {
                    var ea = writer.Address + 4 + EffectiveAddress;
                    writer.WriteAddress("@#" + ea.ToString(), ea);
                    return;
                }
                fmt = "{1:X4}({0})";
                break;

            case AddressMode.IndexedDef:
                if (Register == Registers.pc &&
                    (options & MachineInstructionWriterOptions.ResolvePcRelativeAddress) != 0)
                {
                    writer.WriteString("@(");
                    var ea = writer.Address + 4 + EffectiveAddress;
                    writer.WriteAddress(ea.ToString(), ea);
                    writer.WriteString(")");
                    return;
                }
                fmt = "@{1:X4}({0})";
                break;

            //case AddressMode.Immediate : fmt = "#{1:X4}"; break;
            case AddressMode.Absolute:
                writer.WriteAddress(
                    string.Format("@#{0:X4}", EffectiveAddress),
                    Address.Ptr16(EffectiveAddress));
                return;

            default: throw new NotImplementedException(string.Format("Unknown mode {0}.", Mode));
            }
            writer.WriteString(string.Format(fmt, Register, EffectiveAddress));
        }
Exemple #3
0
 public override void Render(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
 {
     writer.WriteOpcode(this.Opcode.ToString());
     writer.Tab();
     bool sep = false; 
     foreach (var op in Operands)
     {
         if (sep)
             writer.Write(',');
         sep = true;
         if (op is ImmediateOperand)
         {
             writer.Write('#');
             op.Write(writer, options);
         }
         else if (op is MemoryOperand && ((MemoryOperand)op).Base == Registers.pc)
         {
             var addr = this.Address + (this.Length + ((MemoryOperand)op).Offset.ToInt32());
             if ((options & MachineInstructionWriterOptions.ResolvePcRelativeAddress) != 0)
             {
                 writer.WriteAddress(addr.ToString(), addr);
                 writer.AddAnnotation(op.ToString());
             }
             else
             {
                 op.Write(writer, options);
                 writer.AddAnnotation(addr.ToString());
             }
         }
         else
         {
             op.Write(writer, options);
         }
     }
 }
Exemple #4
0
        protected override void RenderOperand(MachineOperand op, MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            switch (op)
            {
            case ImmediateOperand _:
                writer.WriteChar('#');
                op.Write(writer, options);
                return;

            case MemoryOperand mop when mop.Base == Registers.pc:
                var addr = this.Address + this.Length;
                if (mop.Offset != null)
                {
                    addr += mop.Offset.ToInt32();
                }
                if ((options & MachineInstructionWriterOptions.ResolvePcRelativeAddress) != 0)
                {
                    writer.WriteAddress(addr.ToString(), addr);
                    writer.AddAnnotation(op.ToString());
                }
                else
                {
                    op.Write(writer, options);
                    writer.AddAnnotation(addr.ToString());
                }
                return;

            default:
                op.Write(writer, options);
                return;
            }
        }
Exemple #5
0
 public override void Write(bool fExplicit, MachineInstructionWriter writer)
 {
     if (Base != null)
     {
         if (Offset != null)
         {
             int    offset = Offset.ToInt32();
             string fmt;
             if (offset > 0)
             {
                 fmt = "({0}+{1:X2})";
             }
             else if (offset < 0)
             {
                 offset = -offset;
                 fmt    = "({0}-{1:X2})";
             }
             else
             {
                 fmt = "({0})";
             }
             writer.Write(string.Format(fmt, Base, offset));
         }
         else
         {
             writer.Write(string.Format("({0})", Base));
         }
     }
     else
     {
         writer.Write("(");
         writer.WriteAddress(string.Format("{0:X4}", Offset.ToUInt16()), Address.Ptr16(Offset.ToUInt16()));
         writer.Write(")");
     }
 }
Exemple #6
0
        private void Write(MachineOperand op, MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            var memOp = op as MemoryOperand;

            if (memOp != null)
            {
                if (memOp.Base == Registers.rip)
                {
                    var addr = this.Address + this.Length + memOp.Offset.ToInt32();
                    if ((options & MachineInstructionWriterOptions.ResolvePcRelativeAddress) != 0)
                    {
                        writer.Write("[");
                        writer.WriteAddress(addr.ToString(), addr);
                        writer.Write("]");
                        writer.AddAnnotation(op.ToString());
                    }
                    else
                    {
                        op.Write(writer, options);
                        writer.AddAnnotation(addr.ToString());
                    }
                    return;
                }
            }
            op.Write(writer, options);
        }
        private void WriteOp(MachineOperand op, MachineInstructionWriter writer)
        {
            var rop = op as RegisterOperand;

            if (rop != null)
            {
                writer.WriteString(rop.Register.Name);
                return;
            }
            var immop = op as ImmediateOperand;

            if (immop != null)
            {
                writer.WriteString(immop.Value.ToString());
                return;
            }
            var addrop = op as AddressOperand;

            if (addrop != null)
            {
                //$TODO: 32-bit?
                writer.WriteAddress(string.Format("{0:X16}", addrop.Address.ToLinear()), addrop.Address);
                return;
            }
            throw new NotImplementedException();
        }
Exemple #8
0
 public override void Write(bool fExplicit, MachineInstructionWriter writer)
 {
     if (Base != null)
     {
         if (Offset != null)
         {
             int offset = Offset.ToInt32();
             string fmt;
             if (offset > 0)
             {
                 fmt = "({0}+{1:X2})";
             } 
             else if (offset < 0)
             {
                 offset = -offset;
                 fmt = "({0}-{1:X2})";
             }
             else 
             {
                 fmt = "({0})";
             }
             writer.Write(string.Format(fmt, Base, offset));
         }
         else
         {
             writer.Write(string.Format("({0})", Base));
         }
     }
     else
     {
         writer.Write("(");
         writer.WriteAddress(string.Format("{0:X4}", Offset.ToUInt16()), Address.Ptr16(Offset.ToUInt16()));
         writer.Write(")");
     }
 }
Exemple #9
0
        public override void Write(bool fExplicit, MachineInstructionWriter writer)
        {
            var s  = FormatValue(value);
            var pt = value.DataType as PrimitiveType;

            if (pt != null && pt.Domain == Domain.Pointer)
            {
                writer.WriteAddress(s, Address.FromConstant(value));    //$TODO: add WriteAddress(string, Constant) to MachineINstructionWriter
            }
            else if (value.DataType is Pointer)
            {
                writer.WriteAddress(s, Address.FromConstant(value));
            }
            else
            {
                writer.Write(s);
            }
        }
Exemple #10
0
 public override void Write(bool fExplicit, MachineInstructionWriter writer)
 {
     string s;
     if (base.Width.Size == 2)
         s = string.Format("{0:X4}", Address.ToLinear());
     else
         s = Address.ToString();
     writer.WriteAddress(s, Address);
 }
Exemple #11
0
        public void Write(ArmInstructionOperand op, MachineInstructionWriter writer)
        {
            switch (op.Type)
            {
            case ArmInstructionOperandType.Immediate:
                if (instruction.Id == Opcode.B ||
                    instruction.Id == Opcode.BL ||
                    instruction.Id == Opcode.BLX)
                {
                    writer.Write("$");
                    writer.WriteAddress(
                        string.Format("{0:X8}", op.ImmediateValue.Value),
                        Address.Ptr32((uint)op.ImmediateValue.Value));
                    break;
                }
                writer.Write("#");
                WriteImmediateValue(op.ImmediateValue.Value, writer);
                break;

            case ArmInstructionOperandType.CImmediate:
                writer.Write("c{0}", op.ImmediateValue);
                break;

            case ArmInstructionOperandType.PImmediate:
                writer.Write("p{0}", op.ImmediateValue);
                break;

            case ArmInstructionOperandType.Register:
                if (op.IsSubtracted)
                {
                    writer.Write('-');
                }
                writer.Write(A32Registers.RegisterByCapstoneID[op.RegisterValue.Value].Name);
                WriteShift(op, writer);
                break;

            case ArmInstructionOperandType.SysRegister:
                writer.Write(A32Registers.SysRegisterByCapstoneID[op.SysRegisterValue.Value].Name);
                break;

            case ArmInstructionOperandType.Memory:
                WriteMemoryOperand(op, writer);
                break;

            case ArmInstructionOperandType.SetEnd:
                writer.Write(op.SetEndValue.ToString().ToLowerInvariant());
                break;

            default:
                throw new NotImplementedException(string.Format(
                                                      "Can't disasseble {0} {1}. Unknown operand type: {2}",
                                                      instruction.Mnemonic,
                                                      instruction.Operand,
                                                      op.Type));
            }
        }
Exemple #12
0
 public override void Write(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
 {
     if (Base == null)
     {
         writer.WriteFormat("&{0:X4}", (ushort)this.Offset);
         return;
     }
     if (Offset > 0)
     {
         if (Base == Registers.pc && (options & MachineInstructionWriterOptions.ResolvePcRelativeAddress) != 0)
         {
             var addr = writer.Address + 4 + Offset;
             writer.WriteAddress(addr.ToString(), addr);
         }
         else
         {
             writer.WriteFormat("{0:X4}({1})", Offset, Base.Name);
         }
     }
     else if (Offset < 0)
     {
         if (Base == Registers.pc && (options & MachineInstructionWriterOptions.ResolvePcRelativeAddress) != 0)
         {
             var addr = writer.Address + 4 + Offset;
             writer.WriteAddress(addr.ToString(), addr);
         }
         else
         {
             writer.WriteFormat("-{0:X4}({1})", -Offset, Base.Name);
         }
     }
     else
     {
         writer.WriteChar('@');
         writer.WriteString(Base.Name);
         if (PostIncrement)
         {
             writer.WriteChar('+');
         }
     }
 }
Exemple #13
0
        public override void Write(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            string s;

            if (base.Width.Size == 2)
            {
                s = string.Format("{0:X4}", Address.ToLinear());
            }
            else
            {
                s = Address.ToString();
            }
            writer.WriteAddress(s, Address);
        }
Exemple #14
0
        public override void Write(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            var s = FormatValue(Value);

            if (Value.DataType is PrimitiveType pt)
            {
                if (pt.Domain == Domain.Pointer)
                {
                    writer.WriteAddress(s, Address.FromConstant(Value));
                }
                else
                {
                    writer.WriteString(s);
                }
            }
            else if (Value.DataType is Pointer)
            {
                writer.WriteAddress(s, Address.FromConstant(Value));
            }
            else
            {
                writer.WriteString(s);
            }
        }
Exemple #15
0
        private void RenderOperand(MachineOperand op, MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            switch (op)
            {
            case ImmediateOperand imm:
                writer.WriteString(ImmediateOperand.FormatUnsignedValue(imm.Value, ">{1}"));
                break;

            case AddressOperand addr:
                writer.WriteAddress("@" + addr.Address, addr.Address);
                break;

            default:
                op.Write(writer, options);
                break;
            }
        }
Exemple #16
0
 protected override void RenderOperand(MachineOperand op, MachineInstructionWriter writer, MachineInstructionWriterOptions options)
 {
     if (op is MemoryOperand memOp && memOp.Base == Registers.rip)
     {
         var addr = this.Address + this.Length + memOp.Offset.ToInt32();
         if ((options & MachineInstructionWriterOptions.ResolvePcRelativeAddress) != 0)
         {
             writer.WriteString("[");
             writer.WriteAddress(addr.ToString(), addr);
             writer.WriteString("]");
             writer.AddAnnotation(op.ToString());
         }
         else
         {
             op.Write(writer, options);
             writer.AddAnnotation(addr.ToString());
         }
     }
Exemple #17
0
 public override void Write(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
 {
     if (Address == null)
     {
         writer.WriteChar('*');
         writer.WriteString(Register.Name);
     }
     else
     {
         writer.WriteAddress("@" + Address, Address);
         if (Register != null)
         {
             writer.WriteChar('(');
             writer.WriteString(Register.Name);
             writer.WriteChar(')');
         }
     }
 }
Exemple #18
0
 public override void Write(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
 {
     writer.Write('(');
     if (Base != null)
     {
         if (Increment < 0)
         {
             writer.Write("{0}:-", -Increment);
         }
         writer.Write(Base.Name);
         if (Index != null)
         {
             writer.Write('+');
             writer.Write(Index.Name);
         }
         else if (Offset != null)
         {
             int off = Offset.ToInt32();
             int absOff;
             if (off < 0)
             {
                 writer.Write('-');
                 absOff = -off;
             }
             else
             {
                 writer.Write('+');
                 absOff = off;
             }
             writer.Write("0x");
             writer.Write(OffsetFormat(off), absOff);
         }
         if (Increment > 0)
         {
             writer.Write("+:{0}", Increment);
         }
     }
     else
     {
         var addr = Address.Ptr16(Offset.ToUInt16());
         writer.WriteAddress(addr.ToString(), addr);
     }
     writer.Write(')');
 }
Exemple #19
0
 protected override void RenderOperand(MachineOperand op, MachineInstructionWriter writer, MachineInstructionWriterOptions options)
 {
     if (op is MemoryOperand memOp && memOp.Base == Registers.pc)
     {
         var uAddr = Address.ToUInt32() + memOp.Offset.ToInt32();
         var addr  = Address.Ptr32((uint)uAddr);
         if ((options & MachineInstructionWriterOptions.ResolvePcRelativeAddress) != 0)
         {
             writer.WriteAddress(addr.ToString(), addr);
             writer.AddAnnotation(op.ToString());
         }
         else
         {
             op.Write(writer, options);
             writer.AddAnnotation(addr.ToString());
         }
         return;
     }
     op.Write(writer, options);
 }
Exemple #20
0
        private void Render(MachineOperand op, MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            switch (op)
            {
            case ImmediateOperand immOp:
                writer.WriteChar('#');
                immOp.Write(writer, options);
                return;

            case MemoryOperand memOp:
                if (memOp.mode == AddressingMode.PcRelativeDisplacement)
                {
                    uint uAddr = this.Address.ToUInt32();
                    if (memOp.Width.Size == 4)
                    {
                        uAddr &= ~3u;
                    }
                    uAddr += (uint)(memOp.disp + 4);
                    var addr = Core.Address.Ptr32(uAddr);
                    if ((options & MachineInstructionWriterOptions.ResolvePcRelativeAddress) != 0)
                    {
                        writer.WriteChar('(');
                        writer.WriteAddress(addr.ToString(), addr);
                        writer.WriteChar(')');
                        writer.AddAnnotation(op.ToString());
                    }
                    else
                    {
                        op.Write(writer, options);
                        writer.AddAnnotation(addr.ToString());
                    }
                    return;
                }
                goto default;

            default:
                op.Write(writer, options);
                break;
            }
        }
Exemple #21
0
        protected override void RenderOperand(MachineOperand op, MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            switch (op)
            {
            case RegisterOperand rop:
                writer.WriteString(rop.Register.Name);
                return;

            case ImmediateOperand immop:
                immop.Write(writer, options);
                return;

            case AddressOperand addrop:
                //$TODO: 32-bit?
                writer.WriteAddress(string.Format("{0:X16}", addrop.Address.ToLinear()), addrop.Address);
                return;

            case MemoryOperand memop:
                memop.Write(writer, options);
                return;
            }
            throw new NotImplementedException($"Risc-V operand type {op.GetType().Name} not implemented yet.");
        }
        private void Render(MachineOperand op, MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            var immOp = op as ImmediateOperand;

            if (immOp != null)
            {
                writer.Write('#');
                immOp.Write(writer, options);
                return;
            }
            var memOp = op as MemoryOperand;

            if (memOp != null && memOp.mode == AddressingMode.PcRelativeDisplacement)
            {
                uint uAddr = this.Address.ToUInt32();
                if (memOp.Width.Size == 4)
                {
                    uAddr &= ~3u;
                }
                uAddr += (uint)(memOp.disp + 4);
                var addr = Core.Address.Ptr32(uAddr);
                if ((options & MachineInstructionWriterOptions.ResolvePcRelativeAddress) != 0)
                {
                    writer.Write('(');
                    writer.WriteAddress(addr.ToString(), addr);
                    writer.Write(')');
                    writer.AddAnnotation(op.ToString());
                }
                else
                {
                    op.Write(writer, options);
                    writer.AddAnnotation(addr.ToString());
                }
                return;
            }
            op.Write(writer, options);
        }
Exemple #23
0
        private void RenderOperand(MachineOperand op, MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            switch (op)
            {
            case RegisterOperand reg:
                WriteRegister(reg.Register, writer);
                break;

            case ImmediateOperand imm:
                if (imm.Width.Domain == Domain.Real)
                {
                    writer.WriteFormat($"#{imm.Value}");
                }
                else
                {
                    int v = imm.Value.ToInt32();
                    if (0 <= v && v <= 9)
                    {
                        writer.WriteFormat($"#{imm.Value.ToInt32()}");
                    }
                    else
                    {
                        writer.WriteFormat($"#&{imm.Value.ToUInt32():X}");
                    }
                }
                break;

            case AddressOperand addrOp:
                ulong linAddr = addrOp.Address.ToLinear();
                writer.WriteAddress($"#&{linAddr:X}", addrOp.Address);
                break;

            default:
                op.Write(writer, options);
                break;
            }
        }
Exemple #24
0
        public override void Write(bool fExplicit, MachineInstructionWriter writer)
        {
            if (fExplicit)
            {
                string s;
                if (Width == PrimitiveType.Byte)
                {
                    s = "byte ptr ";
                }
                else if (Width == PrimitiveType.Word16)
                {
                    s = "word ptr ";
                }
                else if (Width.Size == 4)
                {
                    s = "dword ptr ";
                }
                else if (Width == PrimitiveType.Word64)
                {
                    s = "qword ptr ";
                }
                else if (Width == PrimitiveType.Real32)
                {
                    s = "float ptr ";
                }
                else if (Width == PrimitiveType.Real64)
                {
                    s = "double ptr ";
                }
                else if (Width == PrimitiveType.Real80 || Width == PrimitiveType.Bcd80)
                {
                    s = "tword ptr ";
                }
                else if (Width == PrimitiveType.Word128)
                {
                    s = "xmmword ptr ";
                }
                else if (Width == PrimitiveType.Word256)
                {
                    s = "ymmword ptr ";
                }
                else
                {
                    throw new ArgumentOutOfRangeException();
                }
                writer.Write(s);
            }

            if (SegOverride != RegisterStorage.None)
            {
                writer.Write(SegOverride.ToString());
                writer.Write(":");
            }
            writer.Write("[");
            if (Base != RegisterStorage.None)
            {
                writer.Write(Base.ToString());
            }
            else
            {
                var s = FormatUnsignedValue(Offset);
                writer.WriteAddress(s, Address.FromConstant(Offset));
            }

            if (Index != RegisterStorage.None)
            {
                writer.Write("+");
                writer.Write(Index.ToString());
                if (Scale > 1)
                {
                    writer.Write("*");
                    writer.Write(Scale);
                }
            }
            if (Base != RegisterStorage.None && Offset != null && Offset.IsValid)
            {
                if (Offset.DataType == PrimitiveType.Byte || Offset.DataType == PrimitiveType.SByte)
                {
                    writer.Write(FormatSignedValue(Offset));
                }
                else
                {
                    writer.Write("+");
                    writer.Write(FormatUnsignedValue(Offset));
                }
            }
            writer.Write("]");
        }
Exemple #25
0
 public override void Write(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
 {
     writer.WriteString("far");
     writer.WriteString(" ");
     writer.WriteAddress(Address.ToString(), Address);
 }
Exemple #26
0
        public override void Write(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            if ((options & MachineInstructionWriterOptions.ExplicitOperandSize) != 0)
            {
                string s;
                if (Width == PrimitiveType.Byte)
                {
                    s = "byte ptr ";
                }
                else if (Width == PrimitiveType.Word16)
                {
                    s = "word ptr ";
                }
                else if (Width.Size == 4)
                {
                    s = "dword ptr ";
                }
                else if (Width == PrimitiveType.Word64)
                {
                    s = "qword ptr ";
                }
                else if (Width == PrimitiveType.Real32)
                {
                    s = "float ptr ";
                }
                else if (Width == PrimitiveType.Real64)
                {
                    s = "double ptr ";
                }
                else if (Width == PrimitiveType.Real80 || Width == PrimitiveType.Bcd80)
                {
                    s = "tword ptr ";
                }
                else if (Width == PrimitiveType.Word128)
                {
                    s = "xmmword ptr ";
                }
                else if (Width == PrimitiveType.Word256)
                {
                    s = "ymmword ptr ";
                }
                else
                {
                    throw new ArgumentOutOfRangeException();
                }
                writer.Write(s);
            }

            if (SegOverride != RegisterStorage.None)
            {
                writer.Write(SegOverride.ToString());
                writer.Write(":");
            }
            writer.Write("[");
            if (Base != RegisterStorage.None)
            {
                writer.Write(Base.ToString());
            }
            else
            {
                var s = FormatUnsignedValue(Offset);
                writer.WriteAddress(s, Address.FromConstant(Offset));
            }

            if (Index != RegisterStorage.None)
            {
                writer.Write("+");
                writer.Write(Index.ToString());
                if (Scale > 1)
                {
                    writer.Write("*");
                    writer.Write(Scale);
                }
            }
            if (Base != RegisterStorage.None && Offset != null && Offset.IsValid)
            {
                if (Offset.DataType == PrimitiveType.Byte || Offset.DataType == PrimitiveType.SByte)
                {
                    writer.Write(FormatSignedValue(Offset));
                }
                else
                {
                    var off = Offset.ToInt32();
                    if (off == Int32.MinValue)
                    {
                        writer.Write("-80000000");
                    }
                    else
                    {
                        var absOff = Math.Abs(off);
                        if (Offset.DataType.Size > 2 && off < 0 && absOff < 0x10000)
                        {
                            // Special case for negative 32-bit offsets whose
                            // absolute value < 0x10000 (GitHub issue #252)
                            writer.Write("-");
                            writer.Write("{0:X8}", absOff);
                        }
                        else
                        {
                            writer.Write("+");
                            writer.Write(FormatUnsignedValue(Offset));
                        }
                    }
                }
            }
            writer.Write("]");
        }
Exemple #27
0
        public void Write(ArmInstructionOperand op, MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            switch (op.Type)
            {
            case ArmInstructionOperandType.Immediate:
                if (instruction.Id == Opcode.B ||
                    instruction.Id == Opcode.BL ||
                    instruction.Id == Opcode.BLX)
                {
                    writer.Write("$");
                    writer.WriteAddress(
                        string.Format("{0:X8}", op.ImmediateValue.Value),
                        Address.Ptr32((uint)op.ImmediateValue.Value));
                    break;
                }
                writer.Write("#");
                WriteImmediateValue(op.ImmediateValue.Value, writer);
                break;

            case ArmInstructionOperandType.CImmediate:
                writer.Write("c{0}", op.ImmediateValue);
                break;

            case ArmInstructionOperandType.PImmediate:
                writer.Write("p{0}", op.ImmediateValue);
                break;

            case ArmInstructionOperandType.Register:
                if (op.IsSubtracted)
                {
                    writer.Write('-');
                }
                writer.Write(A32Registers.RegisterByCapstoneID[op.RegisterValue.Value].Name);
                WriteShift(op, writer);
                break;

            case ArmInstructionOperandType.SysRegister:
                writer.Write(A32Registers.SysRegisterByCapstoneID[op.SysRegisterValue.Value].Name);
                break;

            case ArmInstructionOperandType.Memory:
                if (op.MemoryValue.BaseRegister == ArmRegister.PC)
                {
                    var uAddr = (uint)((int)this.Address.ToUInt32() + op.MemoryValue.Displacement) + 8u;
                    var addr  = Address.Ptr32(uAddr);
                    if (op.MemoryValue.IndexRegister == ArmRegister.Invalid &&
                        (options & MachineInstructionWriterOptions.ResolvePcRelativeAddress) != 0)
                    {
                        writer.Write('[');
                        writer.WriteAddress(addr.ToString(), addr);
                        writer.Write(']');
                        var sr = new StringRenderer();
                        WriteMemoryOperand(op, sr);
                        writer.AddAnnotation(sr.ToString());
                    }
                    else
                    {
                        WriteMemoryOperand(op, writer);
                        writer.AddAnnotation(addr.ToString());
                    }
                    return;
                }
                WriteMemoryOperand(op, writer);
                break;

            case ArmInstructionOperandType.SetEnd:
                writer.Write(op.SetEndValue.ToString().ToLowerInvariant());
                break;

            case ArmInstructionOperandType.FloatingPoint:
                var f = op.FloatingPointValue.Value.ToString("g", CultureInfo.InvariantCulture);
                if (f.IndexOfAny(nosuffixRequired) < 0)
                {
                    f += ".0";
                }
                writer.Write("#{0}", f);
                break;

            default:
                throw new NotImplementedException(string.Format(
                                                      "Can't disassemble {0} {1}. Unknown operand type: {2}",
                                                      instruction.Mnemonic,
                                                      instruction.Operand,
                                                      op.Type));
            }
        }
Exemple #28
0
		public override void Write(bool fExplicit, MachineInstructionWriter writer)
		{
			if (fExplicit)
			{
				string s;
				if (Width == PrimitiveType.Byte)
					s = "byte ptr ";
				else if (Width == PrimitiveType.Word16)
					s = "word ptr ";
				else if (Width.Size == 4)
					s = "dword ptr ";
				else if (Width == PrimitiveType.Word64)
					s = "qword ptr ";
				else if (Width == PrimitiveType.Real32)
					s = "float ptr ";
				else if (Width == PrimitiveType.Real64)
					s = "double ptr ";
				else if (Width == PrimitiveType.Real80 || Width == PrimitiveType.Bcd80)
					s = "tword ptr ";
                else if (Width == PrimitiveType.Word128)
                    s = "xmmword ptr ";
                else if (Width == PrimitiveType.Word256)
                    s = "ymmword ptr ";
                else
					throw new ArgumentOutOfRangeException();
				writer.Write(s);
			}

            if (SegOverride != RegisterStorage.None)
			{
				writer.Write(SegOverride.ToString());
				writer.Write(":");
			}
			writer.Write("[");
			if (Base != RegisterStorage.None)
			{
				writer.Write(Base.ToString());
			}
			else
			{
                var s = FormatUnsignedValue(Offset);
				writer.WriteAddress(s, Address.FromConstant(Offset));
			}

			if (Index != RegisterStorage.None)
			{
				writer.Write("+");
				writer.Write(Index.ToString());
				if (Scale > 1)
				{
					writer.Write("*");
					writer.Write(Scale);
				}
			}
			if (Base != RegisterStorage.None && Offset != null && Offset.IsValid)
			{
				if (Offset.DataType == PrimitiveType.Byte || Offset.DataType == PrimitiveType.SByte)
				{
					writer.Write(FormatSignedValue(Offset));
				}
				else
				{	
					writer.Write("+");
					writer.Write(FormatUnsignedValue(Offset));
				}
			}
			writer.Write("]");
		}
Exemple #29
0
 public override void Write(bool fExplicit, MachineInstructionWriter writer)
 {
     writer.WriteAddress(Address.ToString(), Address);
 }
Exemple #30
0
 public override void Write(bool fExplicit, MachineInstructionWriter writer)
 {
     var s = FormatValue(value);
     var pt = value.DataType as PrimitiveType;
     if (pt != null && pt.Domain == Domain.Pointer)
         writer.WriteAddress(s, Address.FromConstant(value));    //$TODO: add WriteAddress(string, Constant) to MachineINstructionWriter
     else if (value.DataType is Pointer)
         writer.WriteAddress(s, Address.FromConstant(value));
     else
         writer.Write(s);
 }
Exemple #31
0
 public override void Write(bool fExplicit, MachineInstructionWriter writer)
 {
     writer.Write("far");
     writer.Write(" ");
     writer.WriteAddress(Address.ToString(), Address);
 }
Exemple #32
0
        protected override void RenderOperand(MachineOperand op, MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            switch (op)
            {
            case RegisterOperand reg:
                writer.WriteString(reg.Register.Name);
                return;

            case ImmediateOperand imm:
                var sbImm = new StringBuilder("#");
                var sImm  = imm.Value.ToString();
                writer.WriteString(sImm);
                return;

            case MemoryOperand mem:
                if (Prefix != null)
                {
                    writer.WriteFormat("{0}:", Prefix.Name);
                }
                writer.WriteChar('[');
                if (mem.Base != null)
                {
                    writer.WriteString(mem.Base.Name);
                    if (mem.Index != null)
                    {
                        writer.WriteFormat("+{0}", mem.Index.Name);
                    }
                    else if (mem.Offset != 0)
                    {
                        if (mem.Offset >= 0xA0)
                        {
                            writer.WriteFormat("+0{0:X2}h", mem.Offset);
                        }
                        else
                        {
                            writer.WriteFormat("+{0:X2}h", mem.Offset);
                        }
                    }
                }
                else
                {
                    if (mem.Offset >= 0xA000)
                    {
                        writer.WriteFormat("0{0:X4}h", mem.Offset);
                    }
                    else
                    {
                        writer.WriteFormat("{0:X4}h", mem.Offset);
                    }
                    if (mem.Index != null)
                    {
                        writer.WriteFormat("+{0}", mem.Index.Name);
                    }
                }
                writer.WriteChar(']');
                return;

            case BitOperand bit:
                this.RenderOperand(bit.Operand, writer, options);
                writer.WriteFormat(".{0}", bit.BitPosition);
                return;

            case AddressOperand aop:
                writer.WriteAddress(aop.Address.ToString(), aop.Address);
                return;

            case RegisterBankOperand rbop:
                writer.WriteFormat("rb{0}", rbop.Bank);
                return;

            case FlagGroupOperand grf:
                writer.WriteString(grf.FlagGroup.Name);
                return;
            }
            throw new NotImplementedException($"Rl78Instruction - RenderOperand {op.GetType().Name}.");
        }
Exemple #33
0
 public override void Write(bool fExplicit, MachineInstructionWriter writer)
 {
     writer.Write("$");
     writer.WriteAddress(string.Format("{0,8:X}", Address), Address);
 }
Exemple #34
0
 public override void Write(bool fExplicit, MachineInstructionWriter writer)
 {
     writer.Write("$");
     writer.WriteAddress(string.Format("{0,8:X}", Address), Address);
 }
Exemple #35
0
		public override void Write(bool fExplicit, MachineInstructionWriter writer)
		{
			if (fExplicit)
			{
				string s;
				if (Width == PrimitiveType.Byte)
					s = "byte ptr ";
				else if (Width == PrimitiveType.Word16)
					s = "word ptr ";
				else if (Width.Size == 4)
					s = "dword ptr ";
				else if (Width == PrimitiveType.Word64)
					s = "qword ptr ";
				else if (Width == PrimitiveType.Real32)
					s = "float ptr ";
				else if (Width == PrimitiveType.Real64)
					s = "double ptr ";
				else if (Width == PrimitiveType.Real80 || Width == PrimitiveType.Bcd80)
					s = "tword ptr ";
                else if (Width == PrimitiveType.Word128)
                    s = "xmmword ptr ";
                else if (Width == PrimitiveType.Word256)
                    s = "ymmword ptr ";
                else
					throw new ArgumentOutOfRangeException();
				writer.Write(s);
			}

            if (SegOverride != RegisterStorage.None)
			{
				writer.Write(SegOverride.ToString());
				writer.Write(":");
			}
			writer.Write("[");
			if (Base != RegisterStorage.None)
			{
				writer.Write(Base.ToString());
			}
			else
			{
                var s = FormatUnsignedValue(Offset);
				writer.WriteAddress(s, Address.FromConstant(Offset));
			}

			if (Index != RegisterStorage.None)
			{
				writer.Write("+");
				writer.Write(Index.ToString());
				if (Scale > 1)
				{
					writer.Write("*");
					writer.Write(Scale);
				}
			}
			if (Base != RegisterStorage.None && Offset != null && Offset.IsValid)
			{
				if (Offset.DataType == PrimitiveType.Byte || Offset.DataType == PrimitiveType.SByte)
				{
					writer.Write(FormatSignedValue(Offset));
				}
				else
				{
                    var off = Offset.ToInt32();
                    var absOff = Math.Abs(off);
                    if (Offset.DataType.Size > 2 && off < 0 && absOff < 0x10000)
                    {
                        // Special case for negative 32-bit offsets whose 
                        // absolute value < 0x10000 (GitHub issue #252)
                        writer.Write("-");
                        writer.Write("{0:X8}", absOff);
                    }
                    else
                    {
                        writer.Write("+");
                        writer.Write(FormatUnsignedValue(Offset));
                    }
				}
			}
			writer.Write("]");
		}
Exemple #36
0
 public override void Write(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
 {
     writer.WriteAddress(string.Format("${0:X8}", Address.Offset), Address);
 }
Exemple #37
0
 public void Write(ArmInstructionOperand op, MachineInstructionWriter writer)
 {
     switch (op.Type)
     {
     case ArmInstructionOperandType.Immediate:
         if (instruction.Id == Opcode.B ||
             instruction.Id == Opcode.BL ||
             instruction.Id == Opcode.BLX)
         {
             writer.Write("$");
             writer.WriteAddress(
                 string.Format("{0:X8}", op.ImmediateValue.Value),
                 Address.Ptr32((uint)op.ImmediateValue.Value));
             break;
         }
         writer.Write("#");
         WriteImmediateValue(op.ImmediateValue.Value, writer);
         break;
     case ArmInstructionOperandType.CImmediate:
         writer.Write("c{0}", op.ImmediateValue);
         break;
     case ArmInstructionOperandType.PImmediate:
         writer.Write("p{0}", op.ImmediateValue);
         break;
     case ArmInstructionOperandType.Register:
         if (op.IsSubtracted)
             writer.Write('-');
         writer.Write(A32Registers.RegisterByCapstoneID[op.RegisterValue.Value].Name);
         WriteShift(op, writer);
         break;
     case ArmInstructionOperandType.Memory:
         WriteMemoryOperand(op, writer);
         break;
     case ArmInstructionOperandType.SetEnd:
         writer.Write(op.SetEndValue.ToString().ToLowerInvariant());
         break;
     default:
         throw new NotImplementedException(string.Format(
             "Can't disasseble {0} {1}. Unknown operand type: {2}",
             instruction.Mnemonic,
             instruction.Operand,
             op.Type));
     }
 }
Exemple #38
0
 private void WriteOp(MachineOperand op, MachineInstructionWriter writer)
 {
     var rop = op as RegisterOperand;
     if (rop != null)
     {
         writer.Write(rop.Register.Name);
         return;
     }
     var immop = op as ImmediateOperand;
     if (immop != null)
     {
         writer.Write(immop.Value.ToString());
         return;
     }
     var addrop = op as AddressOperand;
     if (addrop != null)
     {
         //$TODO: 32-bit?
         writer.WriteAddress(string.Format("{0:X16}", addrop.Address.ToLinear()), addrop.Address);
         return;
     }
     throw new NotImplementedException();
 }