Example #1
0
        /// <summary>
        /// Translates to ATT syntax .
        /// </summary>
        /// <param name="u">TODO u.</param>
        private void Ud_translate_att(Instruction u)
        {
            Int32   size = 0;
            Boolean star = false;

            // check if P_OSO prefix is used
            if (BitOps.P_OSO(u.ItabEntry.Prefix) == 0 && u.PfxOpr != 0)
            {
                switch (u.DisMode)
                {
                case ArchitectureMode.x86_16:
                    Content.AppendFormat("o32 ");
                    break;

                case ArchitectureMode.x86_32:
                case ArchitectureMode.x86_64:
                    Content.AppendFormat("o16 ");
                    break;
                }
            }

            // check if P_ASO prefix was used
            if (BitOps.P_ASO(u.ItabEntry.Prefix) == 0 && u.PfxAdr != 0)
            {
                switch (u.DisMode)
                {
                case ArchitectureMode.x86_16:
                    Content.AppendFormat("a32 ");
                    break;

                case ArchitectureMode.x86_32:
                    Content.AppendFormat("a16 ");
                    break;

                case ArchitectureMode.x86_64:
                    Content.AppendFormat("a32 ");
                    break;
                }
            }

            if (u.PfxLock != 0)
            {
                Content.AppendFormat("lock ");
            }

            if (u.PfxRep != 0)
            {
                Content.AppendFormat("rep ");
            }
            else if (u.PfxRepe != 0)
            {
                Content.AppendFormat("repe ");
            }
            else if (u.PfxRepne != 0)
            {
                Content.AppendFormat("repne ");
            }

            // special instructions
            switch (u.Mnemonic)
            {
            case UdMnemonicCode.UD_Iretf:
                Content.AppendFormat("lret ");
                size = -1;
                break;

            case UdMnemonicCode.UD_Idb:
                Content.AppendFormat(".byte 0x{0:x2}", u.Operands[0].LvalByte);
                return;

            case UdMnemonicCode.UD_Ijmp:
            case UdMnemonicCode.UD_Icall:
                if (u.BrFar != 0)
                {
                    Content.AppendFormat("l");
                    size = -1;
                }

                if (u.Operands[0].Type == UdType.UD_OP_REG)
                {
                    star = true;
                }

                this.Content.AppendFormat("{0}", Udis86.UdLookupMnemonic(u.Mnemonic));
                break;

            case UdMnemonicCode.UD_Ibound:
            case UdMnemonicCode.UD_Ienter:
                if (u.Operands.Length > 0 && u.Operands[0].Type != UdType.UD_NONE)
                {
                    this.Gen_operand(u, u.Operands[0]);
                }

                if (u.Operands.Length > 1 && u.Operands[1].Type != UdType.UD_NONE)
                {
                    this.Content.AppendFormat(",");
                    this.Gen_operand(u, u.Operands[1]);
                }

                return;

            default:
                this.Content.AppendFormat("{0}", Udis86.UdLookupMnemonic(u.Mnemonic));
                break;
            }

            if (size != -1 && u.Operands.Length > 0 && u.Operands.Any(o => o.Type == UdType.UD_OP_MEM))
            {
                size = u.Operands[0].Size;
            }

            if (size == 8)
            {
                Content.AppendFormat("b");
            }
            else if (size == 16)
            {
                Content.AppendFormat("w");
            }
            else if (size == 32)
            {
                Content.AppendFormat("l");
            }
            else if (size == 64)
            {
                Content.AppendFormat("q");
            }
            else if (size == 80)
            {
                Content.AppendFormat("t");
            }

            if (star)
            {
                Content.AppendFormat(" *");
            }
            else
            {
                Content.AppendFormat(" ");
            }

            if (u.Operands.Length > 3 && u.Operands[3].Type != UdType.UD_NONE)
            {
                this.Gen_operand(u, u.Operands[3]);
                Content.AppendFormat(", ");
            }

            if (u.Operands.Length > 2 && u.Operands[2].Type != UdType.UD_NONE)
            {
                this.Gen_operand(u, u.Operands[2]);
                Content.AppendFormat(", ");
            }

            if (u.Operands.Length > 1 && u.Operands[1].Type != UdType.UD_NONE)
            {
                this.Gen_operand(u, u.Operands[1]);
                this.Content.AppendFormat(", ");
            }

            if (u.Operands.Length > 0 && u.Operands[0].Type != UdType.UD_NONE)
            {
                this.Gen_operand(u, u.Operands[0]);
            }
        }
Example #2
0
        internal Instruction(ref Udis86.ud u, bool keepBinary)
        {
            this.Offset = u.insn_offset;
            this.PC = u.pc;
            this.Mnemonic = u.mnemonic;

            // Add operands
            List<Operand> operands = new List<Operand>(4);
            if (u.operand[0].type != Udis86.ud_type.UD_NONE)
            {
                operands.Add(new Operand(u.operand[0]));
                if (u.operand[1].type != Udis86.ud_type.UD_NONE)
                {
                    operands.Add(new Operand(u.operand[1]));
                    if (u.operand[2].type != Udis86.ud_type.UD_NONE)
                    {
                        operands.Add(new Operand(u.operand[2]));
                        if (u.operand[3].type != Udis86.ud_type.UD_NONE) operands.Add(new Operand(u.operand[3]));
                    }
                }
            }
            this.Operands = operands.ToArray();

            this.Length = u.inp_ctr;

            // Copy the instruction bytes
            if (keepBinary)
            {
				this.Bytes = AssemblyCode.CopyToBytes(u.inp_buf, u.inp_buf_index - this.Length, this.Length);
            }

            if (u.error > 0)
            {
                this.Error = true;
                this.ErrorMessage = u.errorMessage;
            }
            else if (this.Mnemonic == Udis86.ud_mnemonic_code.UD_Iinvalid)
            {
                this.Error = true;
                this.ErrorMessage = "Invalid instruction";
            }

            this.itab_entry = u.itab_entry;
            this.dis_mode = (ArchitectureMode) u.dis_mode;
            this.pfx_rex        = u.pfx_rex;
            this.pfx_seg        = u.pfx_seg;
            this.pfx_opr        = u.pfx_opr;
            this.pfx_adr        = u.pfx_adr;
            this.pfx_lock       = u.pfx_lock;
            this.pfx_str        = u.pfx_str;
            this.pfx_rep        = u.pfx_rep;
            this.pfx_repe       = u.pfx_repe;
            this.pfx_repne      = u.pfx_repne;
            this.opr_mode       = u.opr_mode;
            this.adr_mode       = u.adr_mode;
            this.br_far         = u.br_far;
            this.br_near        = u.br_near;
            this.have_modrm     = u.have_modrm;
            this.modrm          = u.modrm;
            this.primary_opcode = u.primary_opcode;
        }
Example #3
0
        /// <summary>
        /// Translates to intel syntax.
        /// </summary>
        /// <param name="insn">TODO insn.</param>
        private void UdTranslateIntel(Instruction insn)
        {
            /* check if P_OSO prefix is used */
            if (BitOps.P_OSO(insn.ItabEntry.Prefix) == 0 && insn.PfxOpr > 0)
            {
                switch (insn.DisMode)
                {
                case ArchitectureMode.x86_16:
                    Content.AppendFormat("o32 ");
                    break;

                case ArchitectureMode.x86_32:
                case ArchitectureMode.x86_64:
                    Content.AppendFormat("o16 ");
                    break;
                }
            }

            // check if P_ASO prefix was used
            if (BitOps.P_ASO(insn.ItabEntry.Prefix) == 0 && insn.PfxAdr > 0)
            {
                switch (insn.DisMode)
                {
                case ArchitectureMode.x86_16:
                    this.Content.AppendFormat("a32 ");
                    break;

                case ArchitectureMode.x86_32:
                    this.Content.AppendFormat("a16 ");
                    break;

                case ArchitectureMode.x86_64:
                    this.Content.AppendFormat("a32 ");
                    break;
                }
            }

            if (insn.PfxSeg > 0 &&
                insn.Operands[0].Type != UdType.UD_OP_MEM &&
                insn.Operands[1].Type != UdType.UD_OP_MEM)
            {
                this.Content.AppendFormat("{0} ", this.RegisterForType((UdType)insn.PfxSeg));
            }

            if (insn.PfxLock > 0)
            {
                this.Content.AppendFormat("lock ");
            }

            if (insn.PfxRep > 0)
            {
                this.Content.AppendFormat("rep ");
            }
            else if (insn.PfxRepe > 0)
            {
                this.Content.AppendFormat("repe ");
            }
            else if (insn.PfxRepne > 0)
            {
                this.Content.AppendFormat("repne ");
            }

            // Print the instruction mnemonic
            this.Content.AppendFormat("{0}", SharpDisasm.Udis86.Udis86.UdLookupMnemonic(insn.Mnemonic));

            if (insn.Operands.Length > 0 && insn.Operands[0].Type != UdType.UD_NONE)
            {
                Int32 cast = 0;
                Content.AppendFormat(" ");
                if (insn.Operands[0].Type == UdType.UD_OP_MEM)
                {
                    if ((insn.Operands.Length > 1 &&
                         (insn.Operands[1].Type == UdType.UD_OP_IMM ||
                          insn.Operands[1].Type == UdType.UD_OP_CONST)) ||
                        insn.Operands.Length < 2 || ////insn.Operands[1].Type == ud_type.UD_NONE) ||
                        (insn.Operands.Length > 1 &&
                         insn.Operands[0].Size != insn.Operands[1].Size &&
                         insn.Operands[1].Type != UdType.UD_OP_REG))
                    {
                        cast = 1;
                    }
                    else if (insn.Operands[1].Type == UdType.UD_OP_REG &&
                             insn.Operands[1].Base == UdType.UD_R_CL)
                    {
                        switch (insn.Mnemonic)
                        {
                        case UdMnemonicCode.UD_Ircl:
                        case UdMnemonicCode.UD_Irol:
                        case UdMnemonicCode.UD_Iror:
                        case UdMnemonicCode.UD_Ircr:
                        case UdMnemonicCode.UD_Ishl:
                        case UdMnemonicCode.UD_Ishr:
                        case UdMnemonicCode.UD_Isar:
                            cast = 1;
                            break;

                        default:
                            break;
                        }
                    }
                }

                this.Gen_operand(insn, insn.Operands[0], cast);
            }

            if (insn.Operands.Length > 1 && insn.Operands[1].Type != UdType.UD_NONE)
            {
                Int32 cast = 0;
                Content.AppendFormat(", ");
                if (insn.Operands[1].Type == UdType.UD_OP_MEM &&
                    insn.Operands[0].Size != insn.Operands[1].Size &&
                    !Udis86.UdOprIsSeg(insn.Operands[0].UdOperand))
                {
                    cast = 1;
                }

                this.Gen_operand(insn, insn.Operands[1], cast);
            }

            if (insn.Operands.Length > 2 && insn.Operands[1].Type != UdType.UD_NONE)
            {
                Int32 cast = 0;
                Content.AppendFormat(", ");
                if (insn.Operands[2].Type == UdType.UD_OP_MEM && insn.Operands[2].Size != insn.Operands[1].Size)
                {
                    cast = 1;
                }

                this.Gen_operand(insn, insn.Operands[2], cast);
            }

            if (insn.Operands.Length > 3 && insn.Operands[3].Type != UdType.UD_NONE)
            {
                Content.AppendFormat(", ");
                this.Gen_operand(insn, insn.Operands[3], 0);
            }
        }