Here is a minimal example of using XED from the file examples/xed-min.c.
There is a makefile in the examples directory. Here's how to compile it from a kit:
where path-to-xed2-kit is where you have your include, examples and lib directories from an installed XED2 kit.
Here is a more detailed example (examples/xed-ex1.cpp) that walks the operands much like the printing routines do for the xed_decoded_inst_t .
extern "C" {
}
#include "xed-examples-ostreams.H"
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cassert>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;
int main(int argc, char** argv);
printf("REAL REP ");
}
printf("F3 PREFIX\n");
}
printf("F2 PREFIX\n");
}
printf("67 PREFIX\n");
}
printf("66-OSZ PREFIX\n");
}
printf("ANY 66 PREFIX\n");
}
printf("RING0 only\n");
}
}
printf("WRITE-MASKING\n");
if (np)
printf("Number of legacy prefixes: %d \n", np);
if (vl_bits)
printf("Vector length: %d \n", vl_bits);
}
cout << "ATTRIBUTES: ";
for(i=0;i<nattributes;i++) {
}
cout << endl;
}
printf("READS ZF\n");
}
}
}
unsigned int i, nflags;
cout << "FLAGS:" << endl;
cout << " reads-rflags ";
}
cout << " may-write-rflags ";
}
cout << " must-write-rflags ";
}
}
for( i=0;i<nflags ;i++) {
char buf[500];
cout << buf << " ";
}
cout << endl;
char buf[500];
cout << " read: " << setw(30) << buf
<< " mask=0x" << hex
cout << " written: " << setw(30) << buf
<< " mask=0x" << hex
cout << " undefined: " << setw(30) << buf
<< " mask=0x" << hex <<
}
}
cout << "Memory Operands" << endl;
for( i=0;i<memops ; i++) {
cout << " " << i << " ";
cout << " read ";
r_or_w = true;
}
cout << "written ";
r_or_w = true;
}
if (!r_or_w) {
cout << " agen ";
}
}
<< setw(3)
}
<< "/" << setw(3)
}
}
if (disp_bits) {
cout << "DISPLACEMENT_BYTES= " << disp_bits << " ";
cout << "0x" << hex << setfill('0')
<< setw(16) << disp << setfill(' ') << dec
<< " base10=" << disp;
}
cout << " ASZ" << i << "="
cout << endl;
}
cout << " MemopBytes = "
}
unsigned int i, noperands;
cout << "Operands" << endl;
cout << "# TYPE DETAILS VIS RW OC2 BITS BYTES NELEM ELEMSZ ELEMTYPE"
<< endl;
cout << "# ==== ======= === == === ==== ===== ===== ====== ========"
<< endl;
for( i=0; i < noperands ; i++) {
ostringstream os;
switch(op_name) {
os << "(see below)";
break;
if (disp_bits) {
os << "BRANCH_DISPLACEMENT_BYTES= " << disp_bits << " ";
xed_int32_t disp =
os << hex << setfill('0') << setw(8) <<
disp << setfill(' ') << dec;
}
}
break;
os << hex << "0x" << setfill('0');
os << setw(swidth) << x;
}
else {
os << setw(swidth) << x;
}
os << setfill(' ') << dec << '(' << bits << "b)";
break;
}
os << hex << "0x" << setfill('0') << setw(2)
<< (int)x << setfill(' ') << dec;
break;
}
{
break;
}
default:
os << "need to add support for printing operand: "
assert(0);
}
cout << setw(21) << os.str();
cout << " " << setw(10)
<< " " << setw(3)
<< " " << setw(9)
cout << " " << setw(3) << bits;
cout << " " << setw(4) << ((bits +7) >> 3);
cout << " " << setw(3)
cout << " " << setw(10)
cout << endl;
}
}
int main(int argc, char** argv) {
int i, bytes = 0;
int first_argv;
#if defined(XED_MPX)
unsigned int mpx_mode=0;
#endif
first_argv = 1;
for(i=1;i< argc;i++) {
if (strcmp(argv[i], "-64") == 0) {
assert(already_set_mode == 0);
already_set_mode = 1;
first_argv++;
}
#if defined(XED_MPX)
else if (strcmp(argv[i], "-mpx") == 0) {
mpx_mode = 1;
first_argv++;
}
#endif
else if (strcmp(argv[i], "-16") == 0) {
assert(already_set_mode == 0);
already_set_mode = 1;
first_argv++;
}
else if (strcmp(argv[i], "-s16") == 0) {
already_set_mode = 1;
first_argv++;
}
else if (strcmp(argv[i], "-chip") == 0) {
assert(i+1 < argc);
first_argv+=2;
}
}
assert(first_argv < argc);
#if defined(XED_MPX)
#endif
for( i=first_argv ;i < argc; i++) {
unsigned int x, len, p;
len = (unsigned int) strlen(argv[i]);
if ((len & 1) == 1) {
cout << "Must supply even number of nibbles per substring" << endl;
exit(1);
}
for(p=0;p<len;p+=2) {
char t[3];
t[0] = argv[i][p];
t[1] = argv[i][p+1];
t[2] = 0;
istringstream s(t);
s >> hex >> x;
}
}
if (bytes == 0) {
cout << "Must supply some hex bytes" << endl;
exit(1);
}
cout << "Attempting to decode: " << hex << setfill('0') ;
for(i=0;i<bytes;i++)
cout << setw(2) <<
static_cast<xed_uint_t>(itext[i]) <<
" ";
cout << endl << setfill(' ') << dec;
&xedd,
bytes);
switch(xed_error) {
break;
cout << "Not enough bytes provided" << endl;
exit(1);
cout << "The instruction was not valid for the specified chip." << endl;
exit(1);
cout << "Could not decode given input." << endl;
exit(1);
default:
cout << "Unhandled error code "
exit(1);
}
cout << "iclass "
cout << "category "
<< "\t";
cout << "ISA-extension "
<< "\t";
cout << "ISA-set "
<< endl;
cout << "instruction-length "
cout << "operand-width "
cout << "effective-operand-width "
<< endl;
cout << "effective-address-width "
<< endl;
cout << "stack-address-width "
<< endl;
cout << "iform-enum-name "
<< endl;
cout << "iform-enum-name-dispatch (zero based) "
cout << "iclass-max-iform-dispatch "
<< endl;
print_operands(&xedd);
print_memops(&xedd);
print_flags(&xedd);
print_reads_zf_flag(&xedd);
print_attributes(&xedd);
print_misc(&xedd);
return 0;
}
% ./xed-ex1 0 0
iclass ADD category INT_ALU ISA-extension BASE
instruction-length 2
effective-operand-width 8b
effective-address-width 32b
Operands
0 MEM0 EXPLICIT / RW
1 REG AL EXPLICIT / R
2 REG EFLAGS SUPPRESSED / W
Memory Operands
0 read SEG= DS BASE= EAX/REG32
MemopLength = 1
FLAGS:
must-write-rflags of-mod sf-mod zf-mod af-mod pf-mod cf-mod
read:
written: of sf zf af pf cf
===============================================================================
% ./xed-ex1 f2 0f 58 9c 24 e0 00 00 00
iclass ADDSD category SSE ISA-extension SSE2
instruction-length 9
effective-operand-width 32b
effective-address-width 32b
Operands
0 REG XMM3 EXPLICIT / RW
1 MEM0 EXPLICIT / R
Memory Operands
0 read SEG= SS BASE= ESP/REG32 DISPLACEMENT= DISP32 0x000000e0
MemopLength = 8
===============================================================================
./xed-ex1 f3 90
iclass PAUSE category INT_ALU ISA-extension BASE
instruction-length 2
effective-operand-width 32b
effective-address-width 32b
Operands
Memory Operands
MemopLength = 0
===============================================================================