int x86_format_mnemonic(x86_insn_t *insn, char *buf, int len,
                        enum x86_asm_format format){
        char str[MAX_OP_STRING];

        memset( buf, 0, len );
        STRNCAT( buf, insn->prefix_string, len );
        if ( format == att_syntax ) {
                format_att_mnemonic( insn, str, sizeof str );
                STRNCAT( buf, str, len );
        } else {
                STRNCAT( buf, insn->mnemonic, len );
        }

        return( strlen( buf ) );
}
int x86_format_insn( x86_insn_t *insn, char *buf, int len,
                     enum x86_asm_format format ){
        char str[MAX_OP_STRING];
        x86_op_t *src, *dst;
        int i;

        memset(buf, 0, len);
        if ( format == intel_syntax ) {
                /* INTEL STYLE: mnemonic dest, src, imm */
                STRNCAT( buf, insn->prefix_string, len );
                STRNCAT( buf, insn->mnemonic, len );
                STRNCAT( buf, "\t", len );

                /* dest */
		if ( (dst = x86_operand_1st( insn )) && !(dst->flags & op_implied) ) {
        		x86_format_operand( dst, str, MAX_OP_STRING, format);
                	STRNCAT( buf, str, len );
                }

                /* src */
		if ( (src = x86_operand_2nd( insn )) ) {
                        if ( !(dst->flags & op_implied) ) {
                	        STRNCAT( buf, ", ", len );
                        }
        		x86_format_operand( src, str, MAX_OP_STRING, format);
                	STRNCAT( buf, str, len );
                }

                /* imm */
		if ( x86_operand_3rd( insn )) {
                	STRNCAT( buf, ", ", len );
        		x86_format_operand( x86_operand_3rd( insn ), 
				str, MAX_OP_STRING, format);
                	STRNCAT( buf, str, len );
		}

        } else if ( format == att_syntax ) {
                /* ATT STYLE: mnemonic src, dest, imm */
                STRNCAT( buf, insn->prefix_string, len );
                format_att_mnemonic(insn, str, MAX_OP_STRING);
                STRNCATF( buf, "%s\t", str, len);


		/* not sure which is correct? sometimes GNU as requires
		 * an imm as the first operand, sometimes as the third... */
                /* imm */
		if ( x86_operand_3rd( insn ) ) {
        		x86_format_operand(x86_operand_3rd( insn ), 
				str, MAX_OP_STRING, format);
                	STRNCAT( buf, str, len );
			/* there is always 'dest' operand if there is 'src' */
			STRNCAT( buf, ", ", len );
		}

                if ( (insn->note & insn_note_nonswap ) == 0 ) {
                        /* regular AT&T style swap */
                        src = x86_operand_2nd( insn );
                        dst = x86_operand_1st( insn );
                }
                else {
                        /* special-case instructions */
                        src = x86_operand_1st( insn );
                        dst = x86_operand_2nd( insn );
                }

                /* src */
                if ( src ) {
                        x86_format_operand(src, str, MAX_OP_STRING, format);
                        STRNCAT( buf, str, len );
                        /* there is always 'dest' operand if there is 'src' */
                        if ( dst && !(dst->flags & op_implied) ) {
                                STRNCAT( buf, ", ", len );
                        }
                }

                /* dest */
                if ( dst && !(dst->flags & op_implied) ) {
                        x86_format_operand( dst, str, MAX_OP_STRING, format);
                        STRNCAT( buf, str, len );
                }


        } else if ( format == raw_syntax ) {
                format_raw_insn( insn, buf, len );
        } else if ( format == xml_syntax ) {
                format_xml_insn( insn, buf, len );
        } else { /* default to native */
                /* NATIVE style: RVA\tBYTES\tMNEMONIC\tOPERANDS */
                /* print address */
                STRNCATF( buf, "%08X\t", insn->addr, len );

                /* print bytes */
                for ( i = 0; i < insn->size; i++ ) {
                        STRNCATF( buf, "%02X ", insn->bytes[i], len );
                }

                STRNCAT( buf, "\t", len );

                /* print mnemonic */
                STRNCAT( buf, insn->prefix_string, len );
                STRNCAT( buf, insn->mnemonic, len );
                STRNCAT( buf, "\t", len );

                /* print operands */
                /* dest */
		if ( x86_operand_1st( insn )  ) {
        		x86_format_operand( x86_operand_1st( insn ), 
				str, MAX_OP_STRING, format);
                	STRNCATF( buf, "%s\t", str, len );
		}

                /* src */
		if ( x86_operand_2nd( insn ) ) {
        		x86_format_operand(x86_operand_2nd( insn ), 
				str, MAX_OP_STRING, format);
                	STRNCATF( buf, "%s\t", str, len );
		}

                /* imm */
		if ( x86_operand_3rd( insn )) {
        		x86_format_operand( x86_operand_3rd( insn ), 
				str, MAX_OP_STRING, format);
                	STRNCAT( buf, str, len );
		}
        }

        return( strlen( buf ) );
}
Esempio n. 3
0
int x86_format_insn( x86_insn_t *insn, char *buf, int len,
                     enum x86_asm_format format ) {
    char str[MAX_OP_STRING];
    int i;

    memset(buf, 0, len);
    if ( format == intel_syntax ) {
        /* INTEL STYLE: mnemonic dest, src, imm */
        STRNCAT( buf, insn->prefix_string, len );
        STRNCAT( buf, insn->mnemonic, len );
        STRNCAT( buf, "\t", len );

        /* dest */
        if ( x86_operand_1st( insn )  ) {
            x86_format_operand( x86_operand_1st( insn ),
                                insn, str, MAX_OP_STRING, format);
            STRNCAT( buf, str, len );
        }

        /* src */
        if ( x86_operand_2nd( insn ) ) {
            STRNCAT( buf, ", ", len );
            x86_format_operand(x86_operand_2nd( insn ),
                               insn, str, MAX_OP_STRING, format);
            STRNCAT( buf, str, len );
        }

        /* imm */
        if ( x86_operand_3rd( insn )) {
            STRNCAT( buf, ", ", len );
            x86_format_operand( x86_operand_3rd( insn ),
                                insn, str, MAX_OP_STRING, format);
            STRNCAT( buf, str, len );
        }

    } else if ( format == att_syntax ) {
        /* ATT STYLE: mnemonic src, dest, imm */
        STRNCAT( buf, insn->prefix_string, len );
        format_att_mnemonic(insn, str, MAX_OP_STRING);
        STRNCATF( buf, "%s\t", str, len);

        /* src */
        if ( x86_operand_2nd( insn ) ) {
            x86_format_operand(x86_operand_2nd( insn ),
                               insn, str, MAX_OP_STRING, format);
            STRNCAT( buf, str, len );
            /* there is always 'dest' operand if there is 'src' */
            STRNCAT( buf, ", ", len );
        }

        /* dest */
        if ( x86_operand_1st( insn )  ) {
            x86_format_operand( x86_operand_1st( insn ),
                                insn, str, MAX_OP_STRING, format);
            STRNCAT( buf, str, len );
        }


        /* imm */
        if ( x86_operand_3rd( insn ) ) {
            STRNCAT( buf, ", ", len );
            x86_format_operand(x86_operand_3rd( insn ),
                               insn, str, MAX_OP_STRING, format);
            STRNCAT( buf, str, len );
        }

    } else if ( format == raw_syntax ) {
        format_raw_insn( insn, buf, len );
    } else if ( format == xml_syntax ) {
        format_xml_insn( insn, buf, len );
    } else { /* default to native */
        /* NATIVE style: RVA\tBYTES\tMNEMONIC\tOPERANDS */
        /* print address */
        STRNCATF( buf, "%08lX\t", insn->addr, len );

        /* print bytes */
        for ( i = 0; i < insn->size; i++ ) {
            STRNCATF( buf, "%02X ", insn->bytes[i], len );
        }

        STRNCAT( buf, "\t", len );

        /* print mnemonic */
        STRNCAT( buf, insn->prefix_string, len );
        STRNCAT( buf, insn->mnemonic, len );
        STRNCAT( buf, "\t", len );

        /* print operands */
        /* dest */
        if ( x86_operand_1st( insn )  ) {
            x86_format_operand( x86_operand_1st( insn ),
                                insn, str, MAX_OP_STRING, format);
            STRNCATF( buf, "%s\t", str, len );
        }

        /* src */
        if ( x86_operand_2nd( insn ) ) {
            x86_format_operand(x86_operand_2nd( insn ),
                               insn, str, MAX_OP_STRING, format);
            STRNCATF( buf, "%s\t", str, len );
        }

        /* imm */
        if ( x86_operand_3rd( insn )) {
            x86_format_operand( x86_operand_3rd( insn ),
                                insn, str, MAX_OP_STRING, format);
            STRNCAT( buf, str, len );
        }
    }

    return( strlen( buf ) );
}