-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathxed-enc2-3.c
113 lines (96 loc) · 3.07 KB
/
xed-enc2-3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* BEGIN_LEGAL
Copyright (c) 2023 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
END_LEGAL */
#include "xed/xed-interface.h"
#include "xed/xed-enc2-m64-a64.h"
#include <stdio.h>
#include <stdlib.h>
static xed_uint32_t test_push_reg(xed_uint8_t* output_buffer,
xed_reg_enum_t reg)
{
xed_enc2_req_t request;
xed_enc2_req_t_init(&request, output_buffer);
xed_enc_push_r64(&request,reg);
return xed_enc2_encoded_length(&request);
}
static xed_uint32_t test_push_reg_short(xed_uint8_t* output_buffer,
xed_reg_enum_t reg)
{
xed_enc2_req_t request;
xed_enc2_req_t_init(&request, output_buffer);
xed_enc_push_r64_vr1(&request,reg);
return xed_enc2_encoded_length(&request);
}
static void disassemble(xed_decoded_inst_t* xedd)
{
xed_bool_t ok;
xed_print_info_t pi;
#define XBUFLEN 200
char buf[XBUFLEN];
xed_init_print_info(&pi);
pi.p = xedd;
pi.blen = XBUFLEN;
pi.buf = buf;
pi.buf[0]=0; //allow use of strcat
ok = xed_format_generic(&pi);
if (ok)
printf("Disassembly: %s\n", buf);
else
printf("Disassembly: %%ERROR%%\n");
}
static int decode(xed_uint8_t* buf, xed_uint32_t len) {
xed_decoded_inst_t xedd;
xed_error_enum_t err;
xed_state_t dstate;
static int first = 1;
if (first) {
xed_tables_init();
first = 0;
}
xed_state_zero(&dstate);
dstate.mmode=XED_MACHINE_MODE_LONG_64;
xed_decoded_inst_zero_set_mode(&xedd, &dstate);
err = xed_decode(&xedd, buf, len);
if (err == XED_ERROR_NONE) {
disassemble(&xedd);
return 0;
}
printf("ERROR: %s\n", xed_error_enum_t2str(err));
return 1;
}
static void dump(xed_uint8_t* buf, xed_uint32_t len) {
xed_uint_t i;
for(i=0;i<len;i++)
printf("%02x ",buf[i]);
}
int main(int argc, char** argv) {
xed_uint8_t output_buffer[XED_MAX_INSTRUCTION_BYTES];
xed_uint32_t enclen;
int retval=0, r=0,i=0;
xed_reg_enum_t reg=XED_REG_INVALID;
for(i=0;i<2;i++) {
for (reg = XED_REG_RAX; reg<=XED_REG_R15; reg++) {
if (i==0)
enclen = test_push_reg_short(output_buffer,reg);
else
enclen = test_push_reg(output_buffer,reg);
printf("Encoded: ");
dump(output_buffer, enclen);
printf("\n");
r = decode(output_buffer, enclen);
retval += r;
printf("decode returned %d\n\n",r);
}
}
(void)argc; (void)argv;
return retval;
}