2 Intel Open Source License
4 Copyright (c) 2002-2007 Intel Corporation
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are
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.
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.
32 /// @author Mark Charney <mark.charney@intel.com>
38 #include "xed-interface.h"
39 #include "xed-examples-util.h"
41 #include "xed-enc-lang.H"
48 int main(int argc, char** argv);
54 cerr << "Usage: " << progname << " [-16|-32|-64] [-a16|-a32] [-s16|-s32] encode-string" << endl;
58 ascii_encode_request_t
59 parse_args(unsigned int argc, char** argv)
64 ascii_encode_request_t r;
65 r.dstate.mmode = XED_MACHINE_MODE_LEGACY_32;
66 r.dstate.addr_width = XED_ADDRESS_WIDTH_32b;
67 r.dstate.stack_addr_width = XED_ADDRESS_WIDTH_32b;
72 if (strcmp(argv[i],"-16")==0)
73 r.dstate.mmode = XED_MACHINE_MODE_LEGACY_16;
74 else if (strcmp(argv[i],"-32")==0)
75 r.dstate.mmode = XED_MACHINE_MODE_LEGACY_32;
76 else if (strcmp("-64", argv[i]) == 0) {
77 r.dstate.mmode = XED_MACHINE_MODE_LONG_64;
78 r.dstate.addr_width = XED_ADDRESS_WIDTH_64b;
80 else if (strcmp("-32", argv[i]) == 0)
81 r.dstate.mmode = XED_MACHINE_MODE_LEGACY_32;
82 else if (strcmp("-16", argv[i]) == 0)
83 r.dstate.mmode = XED_MACHINE_MODE_LEGACY_16;
84 else if (strcmp(argv[i],"-a32")==0)
85 r.dstate.addr_width = XED_ADDRESS_WIDTH_32b;
86 else if (strcmp(argv[i],"-a16")==0)
87 r.dstate.addr_width = XED_ADDRESS_WIDTH_16b;
88 else if (strcmp(argv[i],"-s32")==0)
89 r.dstate.stack_addr_width = XED_ADDRESS_WIDTH_32b;
90 else if (strcmp(argv[i],"-s16")==0)
91 r.dstate.stack_addr_width = XED_ADDRESS_WIDTH_16b;
99 c = c + " " + argv[i];
101 r.command = c.c_str();
105 int main(int argc, char** argv) {
108 ascii_encode_request_t areq = parse_args(argc,argv);
109 xed_encoder_request_t req = parse_encode_request(areq);
111 cout << "Encode request:" << endl;
112 xed_encode_request_print(&req, buf, 5000);
115 unsigned int ilen = XED_MAX_INSTRUCTION_BYTES;
117 xed_uint8_t array[XED_MAX_INSTRUCTION_BYTES];
119 xed_error_enum_t xed_error = xed_encode(&req, array, ilen, &olen);
120 xed_bool_t encode_okay = ( xed_error == XED_ERROR_NONE);
122 cout << "Could not encode" << endl;
125 xed_print_hex_line(buf, array, olen);
126 cout << "Encodable! " << buf << endl;
128 //#define TEST_DISPLACEMENT_MODIFICATION
129 //#define TEST_IMMEDIATE_MODIFICATION
130 #if defined(TEST_DISPLACEMENT_MODIFICATION) || defined(TEST_IMMEDIATE_MODIFICATION)
131 # if defined(TEST_DISPLACEMENT_MODIFICATION)
134 INT64 new_disp = 0x11223344;
135 unsigned int new_disp_length = 4;
136 xed_bool_t update_okay = req.update_displacement(new_disp, new_disp_length, array);
137 # elif defined(TEST_IMMEDIATE_MODIFICATION)
140 INT64 new_immed = 0x11223344;
141 unsigned int new_immed_length = 4;
142 xed_bool_t update_okay = req.update_immediate(new_immed, new_immed_length, array);
146 cout << "Update succeeded" << endl;
148 print_hex_line(os, array, olen);
149 cout << os.str() << endl;
152 cout << "Update failed" << endl;