XED
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Small Examples of using XED

Here is a minimal example of using XED from the file examples/xed-min.c.

/*BEGIN_LEGAL
Intel Open Source License
Copyright (c) 2002-2014 Intel Corporation. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer. Redistributions
in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution. Neither the name of
the Intel Corporation nor the names of its contributors may be used to
endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
END_LEGAL */
#include "xed-interface.h"
#include <stdio.h>
int main(int argc, char** argv);
int main(int argc, char** argv) {
xed_address_width_enum_t stack_addr_width;
xed_bool_t long_mode = 0;
// create the decoded instruction, and fill in the machine mode (dstate)
// make up a simple 2Byte instruction to decode
unsigned int bytes = 0;
unsigned char itext[15] = { 0xf, 0x85, 0x99, 0x00, 0x00, 0x00 };
// initialize the XED tables -- one time.
// The state of the machine -- required for decoding
if (long_mode) {
stack_addr_width = XED_ADDRESS_WIDTH_64b;
}
else {
stack_addr_width = XED_ADDRESS_WIDTH_32b;
}
// This is a test of error handling. I vary the instuction length from
// 0 bytes to 15 bytes. Normally, you should send in 15 bytes of itext
// unless you are near the end of a page and don't want to take a page
// fault or tlb miss. Note, you have to reinitialize the xedd each time
// you try to decode in to it.
// Try different instruction lengths to see when XED recognizes an
// instruction as valid.
for(bytes = 0;bytes<=15;bytes++) {
xed_error_enum_t xed_error;
xed_decoded_inst_set_mode(&xedd, mmode, stack_addr_width);
xed_error = xed_decode(&xedd,
XED_STATIC_CAST(const xed_uint8_t*,itext),
bytes);
printf("%d %s\n",(int)bytes, xed_error_enum_t2str(xed_error));
}
return 0;
(void) argc; (void) argv; //pacify compiler
}

There is a makefile in the examples directory. Here's how to compile it from a kit:

% g++ -Ipath-to-xed2-kit/include -Ipath-to-xed2-kit/examples \
-c path-to-xed2-kit/examples/xed-min.cpp
% g++ -o xed-min xed-min.o path-to-xed2-kit/lib/libxed.a

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 .

/*BEGIN_LEGAL
Intel Open Source License
Copyright (c) 2002-2014 Intel Corporation. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer. Redistributions
in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution. Neither the name of
the Intel Corporation nor the names of its contributors may be used to
endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
END_LEGAL */
extern "C" {
#include "xed-interface.h"
}
#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);
void print_misc(xed_decoded_inst_t* xedd) {
printf("REAL REP ");
}
printf("F3 PREFIX\n");
}
printf("F2 PREFIX\n");
}
printf("67 PREFIX\n");
}
/* this 66 prefix is not part of the opcode */
printf("66-OSZ PREFIX\n");
}
/* this is any 66 prefix including the above */
printf("ANY 66 PREFIX\n");
}
printf("RING0 only\n");
}
const xed_inst_t* xi = xed_decoded_inst_inst(xedd);
printf("EXCEPTION TYPE: %s\n", xed_exception_enum_t2str(e));
}
// does not include instructions that have XED_ATTRIBUTE_MASK_AS_CONTROL.
// does not include vetor instructions that have k0 as a mask register.
printf("WRITE-MASKING\n");
if (np)
printf("Number of legacy prefixes: %d \n", np);
if (vl_bits)
printf("Vector length: %d \n", vl_bits);
}
void print_attributes(xed_decoded_inst_t* xedd) {
/* Walk the attributes. Generally, you'll know the one you want to
* query and just access that one directly. */
const xed_inst_t* xi = xed_decoded_inst_inst(xedd);
unsigned int i, nattributes = xed_attribute_max();
cout << "ATTRIBUTES: ";
for(i=0;i<nattributes;i++) {
if (xed_inst_get_attribute(xi,attr))
cout << xed_attribute_enum_t2str(attr) << " ";
}
cout << endl;
}
void print_reads_zf_flag(xed_decoded_inst_t* xedd) {
/* example of reading one bit from the flags set */
if (read_set->s.zf) {
printf("READS ZF\n");
}
}
}
void print_flags(xed_decoded_inst_t* xedd) {
unsigned int i, nflags;
cout << "FLAGS:" << endl;
cout << " reads-rflags ";
}
//XED provides may-write and must-write information
cout << " may-write-rflags ";
}
cout << " must-write-rflags ";
}
}
for( i=0;i<nflags ;i++) {
const xed_flag_action_t* fa =
char buf[500];
xed_flag_action_print(fa,buf,500);
cout << buf << " ";
}
cout << endl;
// or as as bit-union
const xed_flag_set_t* read_set =
/* written set include undefined flags */
const xed_flag_set_t* written_set =
const xed_flag_set_t* undefined_set =
char buf[500];
xed_flag_set_print(read_set,buf,500);
cout << " read: " << setw(30) << buf
<< " mask=0x" << hex
<< xed_flag_set_mask(read_set) << dec << endl;
xed_flag_set_print(written_set,buf,500);
cout << " written: " << setw(30) << buf
<< " mask=0x" << hex
<< xed_flag_set_mask(written_set) << dec << endl;
xed_flag_set_print(undefined_set,buf,500);
cout << " undefined: " << setw(30) << buf
<< " mask=0x" << hex <<
xed_flag_set_mask(undefined_set) << dec << endl;
}
}
void print_memops(xed_decoded_inst_t* xedd) {
unsigned int i, memops = xed_decoded_inst_number_of_memory_operands(xedd);
cout << "Memory Operands" << endl;
for( i=0;i<memops ; i++) {
xed_bool_t r_or_w = false;
cout << " " << i << " ";
if ( xed_decoded_inst_mem_read(xedd,i)) {
cout << " read ";
r_or_w = true;
}
cout << "written ";
r_or_w = true;
}
if (!r_or_w) {
cout << " agen "; // LEA instructions
}
if (seg != XED_REG_INVALID) {
cout << "SEG= " << xed_reg_enum_t2str(seg) << " ";
}
if (base != XED_REG_INVALID) {
cout << "BASE= " << setw(3) << xed_reg_enum_t2str(base) << "/"
<< setw(3)
}
if (i == 0 && indx != XED_REG_INVALID) {
cout << "INDEX= " << setw(3) << xed_reg_enum_t2str(indx)
<< "/" << setw(3)
if (xed_decoded_inst_get_scale(xedd,i) != 0) {
// only have a scale if the index exists.
cout << "SCALE= " << xed_decoded_inst_get_scale(xedd,i) << " ";
}
}
xed_uint_t disp_bits =
if (disp_bits) {
cout << "DISPLACEMENT_BYTES= " << disp_bits << " ";
xed_int64_t disp = xed_decoded_inst_get_memory_displacement(xedd,i);
cout << "0x" << hex << setfill('0')
<< setw(16) << disp << setfill(' ') << dec
<< " base10=" << disp;
}
cout << " ASZ" << i << "="
cout << endl;
}
cout << " MemopBytes = "
}
void print_operands(xed_decoded_inst_t* xedd) {
unsigned int i, noperands;
cout << "Operands" << endl;
const xed_inst_t* xi = xed_decoded_inst_inst(xedd);
noperands = xed_inst_noperands(xi);
cout << "# TYPE DETAILS VIS RW OC2 BITS BYTES NELEM ELEMSZ ELEMTYPE"
<< endl;
cout << "# ==== ======= === == === ==== ===== ===== ====== ========"
<< endl;
for( i=0; i < noperands ; i++) {
const xed_operand_t* op = xed_inst_operand(xi,i);
cout << i << " " << setw(6) << xed_operand_enum_t2str(op_name) << " ";
ostringstream os;
switch(op_name) {
// we print memops in a different function
os << "(see below)";
break;
case XED_OPERAND_PTR: // pointer (always in conjunction with a IMM0)
case XED_OPERAND_RELBR: { // branch displacements
xed_uint_t disp_bits =
if (disp_bits) {
os << "BRANCH_DISPLACEMENT_BYTES= " << disp_bits << " ";
xed_int32_t disp =
os << hex << setfill('0') << setw(8) <<
disp << setfill(' ') << dec;
}
}
break;
case XED_OPERAND_IMM0: { // immediates
os << hex << "0x" << setfill('0');
xed_uint_t swidth = bits?(bits/4):8;
os << setw(swidth) << x;
}
else {
xed_uint_t swidth = bits?(bits/4):16;
os << setw(swidth) << x;
}
os << setfill(' ') << dec << '(' << bits << "b)";
break;
}
case XED_OPERAND_IMM1: { // 2nd immediate is always 1 byte.
os << hex << "0x" << setfill('0') << setw(2)
<< (int)x << setfill(' ') << dec;
break;
}
{
os << xed_operand_enum_t2str(op_name) << "="
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;
/* rounding, bits might not be a multiple of 8 */
cout << " " << setw(4) << ((bits +7) >> 3);
cout << " " << setw(2) << xed_decoded_inst_operand_elements(xedd,i);
cout << " " << setw(3)
cout << " " << setw(10)
cout << endl;
}
}
int main(int argc, char** argv) {
xed_state_t dstate;
int i, bytes = 0;
unsigned char itext[XED_MAX_INSTRUCTION_BYTES];
int first_argv;
xed_bool_t already_set_mode = 0;
#if defined(XED_MPX)
unsigned int mpx_mode=0;
#endif
xed_state_zero(&dstate);
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);
chip = str2xed_chip_enum_t(argv[i+1]);
printf("Setting chip to %s\n", xed_chip_enum_t2str(chip));
assert(chip != XED_CHIP_INVALID);
first_argv+=2;
}
}
assert(first_argv < argc);
#if defined(XED_MPX)
xed3_operand_set_mpxmode(&xedd, mpx_mode);
#endif
// convert ascii hex to hex bytes
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;
assert(bytes < XED_MAX_INSTRUCTION_BYTES);
itext[bytes++] = XED_STATIC_CAST(xed_uint8_t,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,
XED_REINTERPRET_CAST(const xed_uint8_t*,itext),
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 "
<< xed_error_enum_t2str(xed_error) << endl;
exit(1);
}
cout << "iclass "
cout << "category "
<< "\t";
cout << "ISA-extension "
<< "\t";
cout << "ISA-set "
<< endl;
cout << "instruction-length "
<< xed_decoded_inst_get_length(&xedd) << endl;
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;
// operands
print_operands(&xedd);
// memops
print_memops(&xedd);
// flags
print_flags(&xedd);
print_reads_zf_flag(&xedd);
// attributes
print_attributes(&xedd);
// misc
print_misc(&xedd);
return 0;
}

Here are a few examples of running the program:

% ./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
===============================================================================