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-ex4.c
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-ex4.c
32 /// @author Mark Charney   <mark.charney@intel.com>
33
34 // decoder example. This is the "C" version of xed-ex2.cpp (which is C++).
35
36 #include "xed-interface.h"
37 #include "xed-examples-util.h"
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h> //strcmp
41 #include <assert.h> 
42
43 int main(int argc, char** argv);
44
45 int 
46 main(int argc, char** argv)
47 {
48     xed_error_enum_t xed_error;
49
50     xed_bool_t long_mode = 0;
51     xed_state_t dstate;
52     unsigned int first_argv;
53     unsigned int bytes = 0;
54     unsigned char itext[XED_MAX_INSTRUCTION_BYTES];
55     int i;
56     unsigned int u;
57     xed_decoded_inst_t xedd;
58 #define BUFLEN  1000
59     char buffer[BUFLEN];
60     xed_bool_t ok;
61     xed_syntax_enum_t syntax;
62
63     xed_tables_init();
64     xed_state_zero(&dstate);
65     xed_set_verbosity( 99 );
66
67     if (argc > 2 && strcmp(argv[1], "-64") == 0) 
68         long_mode = 1;
69
70     if (long_mode) {
71         first_argv = 2;
72         dstate.mmode=XED_MACHINE_MODE_LONG_64;
73     }
74     else {
75         first_argv=1;
76         xed_state_init(&dstate,
77                        XED_MACHINE_MODE_LEGACY_32, 
78                        XED_ADDRESS_WIDTH_32b, 
79                        XED_ADDRESS_WIDTH_32b);
80     }
81
82     xed_decoded_inst_zero_set_mode(&xedd, &dstate);
83     for( i=first_argv ;i < argc; i++)    {
84         xed_uint8_t x = (xed_uint8_t)(xed_atoi_hex(argv[i]));
85         assert(bytes < XED_MAX_INSTRUCTION_BYTES);
86         itext[bytes++] = x;
87     }
88     if (bytes == 0)    {
89         fprintf(stderr, "Must supply some hex bytes\n");
90         exit(1);
91     }
92
93     printf("PARSING BYTES: ");
94     for( u=0;u<bytes; u++) 
95         printf("%02x ", STATIC_CAST(unsigned int,itext[u]));
96     printf("\n");
97
98     xed_error = xed_decode(&xedd, 
99                            REINTERPRET_CAST(const xed_uint8_t*,itext),
100                            bytes);
101     switch(xed_error)
102     {
103       case XED_ERROR_NONE:
104         break;
105       case XED_ERROR_BUFFER_TOO_SHORT:
106         fprintf(stderr,"Not enough bytes provided\n");
107         exit(1);
108       case XED_ERROR_GENERAL_ERROR:
109         fprintf(stderr,"Could not decode given input.\n");
110         exit(1);
111       default:
112         fprintf(stderr,"Unhandled error code %s\n", xed_error_enum_t2str(xed_error));
113         exit(1);
114     }
115         
116     //memset(buffer,0,BUFLEN);
117     xed_decoded_inst_dump(&xedd,buffer, BUFLEN);
118     printf("%s\n",buffer);
119
120
121     for(syntax=  XED_SYNTAX_XED; syntax < XED_SYNTAX_LAST; syntax++)    {
122         ok = xed_format(syntax, &xedd, buffer, BUFLEN, 0);
123         if (ok)
124             printf("%s syntax: %s\n", xed_syntax_enum_t2str(syntax), buffer);
125         else
126             printf("Error disassembling %s syntax\n", xed_syntax_enum_t2str(syntax));
127     }
128     return 0;
129 }