Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Release 1.0
[palacios.git] / misc / decoder_test / XED2 / examples / xed-ex2.cpp
1 /*BEGIN_LEGAL 
2 Intel Open Source License 
3
4 Copyright (c) 2002-2007 Intel Corporation 
5 All rights reserved. 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are
8 met:
9
10 Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.  Redistributions
12 in binary form must reproduce the above copyright notice, this list of
13 conditions and the following disclaimer in the documentation and/or
14 other materials provided with the distribution.  Neither the name of
15 the Intel Corporation nor the names of its contributors may be used to
16 endorse or promote products derived from this software without
17 specific prior written permission.
18  
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
23 ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 END_LEGAL */
31 /// @file xed-ex2.cpp
32 /// @author Mark Charney   <mark.charney@intel.com>
33
34 // decoder example. C++ version of xed-ex4.c
35
36 extern "C" {
37 #include "xed-interface.h"
38 }
39 #include <iostream>
40 #include <iomanip>
41 #include <sstream>
42 #include <cassert>
43 using namespace std;
44
45 int main(int argc, char** argv);
46
47 int main(int argc, char** argv) {
48     xed_bool_t long_mode = false;
49     xed_state_t dstate;
50     int first_argv;
51     int bytes = 0;
52     unsigned char itext[XED_MAX_INSTRUCTION_BYTES];
53     int i;
54     unsigned int u;
55     xed_decoded_inst_t xedd;
56 #define BUFLEN  1000
57     char buffer[BUFLEN];
58
59     xed_tables_init();
60     xed_state_zero(&dstate);
61     xed_set_verbosity( 99 );
62
63     if (argc > 2 && strcmp(argv[1], "-64") == 0) 
64         long_mode = true;
65
66     if (long_mode)  {
67         first_argv = 2;
68         dstate.mmode=XED_MACHINE_MODE_LONG_64;
69     }
70     else {
71         first_argv=1;
72         xed_state_init(&dstate,
73                        XED_MACHINE_MODE_LEGACY_32, 
74                        XED_ADDRESS_WIDTH_32b, 
75                        XED_ADDRESS_WIDTH_32b);
76     }
77     xed_decoded_inst_zero_set_mode(&xedd, &dstate);
78     for( i=first_argv ;i < argc; i++)  {
79         unsigned int x;
80         istringstream s(argv[i]);
81         s >> hex >> x;
82
83         assert(bytes < XED_MAX_INSTRUCTION_BYTES);
84         itext[bytes++] = STATIC_CAST(xed_uint8_t,x);
85     }
86     if (bytes == 0) {
87         cout << "Must supply some hex bytes" << endl;
88         exit(1);
89     }
90
91     cout << "PARSING BYTES: ";
92     for( i=0;i<bytes; i++) 
93         cout << hex << setw(2) << setfill('0')
94              << STATIC_CAST(unsigned int,itext[i]) << " ";
95     cout << endl << setfill(' ');
96
97     xed_error_enum_t xed_error = xed_decode(&xedd, 
98                                             REINTERPRET_CAST(xed_uint8_t*,itext),
99                                             bytes);
100     switch(xed_error)
101     {
102       case XED_ERROR_NONE:
103         break;
104       case XED_ERROR_BUFFER_TOO_SHORT:
105         cout << "Not enough bytes provided" << endl;
106         exit(1);
107       case XED_ERROR_GENERAL_ERROR:
108         cout << "Could not decode given input." << endl;
109         exit(1);
110       default:
111         cout << "Unhandled error code " << xed_error_enum_t2str(xed_error) << endl;
112         exit(1);
113     }
114
115     xed_decoded_inst_dump(&xedd,buffer,BUFLEN);
116     cout << buffer << endl;
117
118     xed_bool_t ok;
119     for(u=  XED_SYNTAX_XED; u < XED_SYNTAX_LAST; u++) {
120         xed_syntax_enum_t syntax = static_cast<xed_syntax_enum_t>(u);
121         ok = xed_format(syntax, &xedd, buffer, BUFLEN, 0);
122         if (ok)
123             cout << xed_syntax_enum_t2str(syntax) << " syntax: "  << buffer << endl;
124         else
125             cout << "Error disassembling " << xed_syntax_enum_t2str(syntax) << " syntax" << endl;
126     }
127     return 0;
128 }