x86_format_insn(3)

✨ x86_format_insn(3) ✨ β€” the libdisasm girlies' guide to pretty-printing instructions

okay so bestie, we're in libdisasm territory today and honestly? it's giving disassembler glamour πŸ’… this is the page for x86_format_insn, x86_format_mnemonic, x86_format_operand and x86_format_header β€” aka the four functions that take your disassembled instruction and turn it into a cute little string you can actually print. πŸŽ€


🌸 NAME 🌸

x86_format_insn, x86_format_mnemonic, x86_format_operand, x86_format_header β€” generate a string representation of a disassembled instruction.


✨ SYNOPSIS ✨

#include <libdis.h>

int x86_format_operand(x86_op_t *op, char *buf, int len,
     enum x86_asm_format format);

int x86_format_mnemonic(x86_insn_t *insn, char *buf, int len,
     enum x86_asm_format format );

int x86_format_insn(x86_insn_t *insn, char *buf, int len,
     enum x86_asm_format format );

int x86_format_header( char *buf, int len,
     enum x86_asm_format format);

πŸ’– DESCRIPTION πŸ’–

x86_format_insn builds an assembly-language representation of the disassembled instruction, in whatever format you asked for. x86_format_mnemonic and x86_format_operand are the ones x86_format_insn calls to format the instruction mnemonic and the operands respectively β€” but you're a main character, so you can absolutely call them directly yourself. 🫢

Every one of these routines fills the buffer buf of len bytes with an ASCII string representing the instruction, mnemonic, or operand.

x86_format_header fills buffer buf of len bytes with a description of the specified format. yes, a whole function just to describe a format. it's iconic of it honestly. πŸ‘‘

πŸ¦‹ The formats available πŸ¦‹

  • native_syntax β€” Intel syntax with address and hex
  • intel_syntax β€” Intel x86 syntax
  • att_syntax β€” AT&T Syntax
  • raw_syntax β€” Pipe-delimited internal format
  • xml_syntax β€” XML representation

πŸ’œ native_syntax

Native syntax uses dest, src ordering and displays the address, opcode bytes, and instruction in tab-delimited format:

"ADDRESS\tBYTES\tMNEMONIC\tDEST\tSRC\tIMM"

πŸ’œ intel_syntax

Intel syntax uses dest, src ordering and displays the instruction in tab-and-comma delimited format:

"MNEMONIC\tDEST, SRC, IMM"

πŸ’œ att_syntax

AT&T syntax uses src, dest ordering (yes, the ordering flips, keep up bestie 😭) and displays the instruction in tab-and-comma delimited format:

"MNEMONIC\tSRC, DEST, IMM"

πŸ’œ raw_syntax

Raw syntax spills all the details of the instruction in pipe-delimited format:

"ADDRESS|OFFSET|SIZE|BYTES|PREFIX|PREFIX_STRING|

πŸ’œ xml_syntax

XML syntax also spills all the details of the instruction, but make it XML:

GROUP|TYPE|NOTES|MNEMONIC|CPU|ISA|FLAGS_SET|
FLAGS_TESTED|STACK_MOD|STACK_MOD_VAL"
[|OP_TYPE|OP_DATATYPE|OP_ACCESS|OP_FLAGS|OP]*"
"<x86_insn>
       <address rva= offset= size= bytes=/>
       <prefix type= string=/>
       <mnemonic group= type= string= cpu= isa= note= />
       <flags type=set>
          <flag name=>
       </flags>
       <stack_mod val= >
       <flags type=tested>
          <flag name=>
       </flags>
       <operand name=>
          <register name= type= size=/>
          <immediate type= value=/>
          <relative_offset value=/>
          <absolute_address value=>
             <segment value=/>
          </absolute_address>
          <address_expression>
             <segment value=/>
             <base>
                <register name= type= size=/>
             </base>
             <index>
                <register name= type= size=/>
             </index>
             <scale>
                <immediate value=/>
             </scale>
             <displacement>
                <immediate value=/>
                <address value=/>
             </displacement>
          </address_expression>
          <segment_offset>
             <address value=/>
          </segment_offset>
       </operand>
    </x86_insn>"

πŸ”₯ EXAMPLES πŸ”₯

okay this first one will print insn in Intel syntax. no cap, that's literally what the comment says:

void att_print( x86_insn_t *insn ) {
        char line[256];
        x86_format_insn(insn, line, 256, intel_syntax);
        printf( "%s\n", line);
}

and this next routine formats an instruction manually, using AT&T syntax:

void manual_print( x86_insn_t *insn, void *arg ) {
        char buf[128];
        int i;
        printf("%08lX", insn->addr );
        for ( i = 0; i < 10; i++ ) {
                if ( i < insn->size ) {
                        printf(" %02X", insn->bytes[i]);
                } else {
                        printf("   ");
                }
        }
        x86_format_mnemonic( insn, buf, 128, att_syntax );
        printf( "\t%s\t", buf );
        if ( insn->operands[op_src].type != op_unused ) {
                x86_format_operand( &insn->operands[op_src],
                               insn, buf, 128, att_syntax );
                /* if src is present, so is dest */
                printf("%s, ", buf);
        }
        if ( insn->operands[op_dest].type != op_unused ) {
                x86_format_operand( &insn->operands[op_dest],
                               insn, buf, 128, att_syntax );
                printf("%s", buf);
        }
        if ( insn->operands[op_imm].type != op_unused ) {
                x86_format_operand( &insn->operands[op_imm],
                               insn, buf, 128, att_syntax );
                /* if src is present, so is dest */
                printf(", %s", buf);
        }
        printf("\n");
}
bestie, copy these exactly as written β€” that's what the page gives you, character for character. πŸ™ˆ

πŸ‘‘ SEE ALSO πŸ‘‘

libdisasm(7), x86_disasm(3), x86_init(3), x86dis(1)


and that's the whole vibe check, bestie. this page came to you courtesy of mammon_, version 0.21, dated back in the day, and it's filed under x86_format_insn(3). go format some instructions, you absolute icon. πŸ’–βœ¨πŸ¦‹