-
Notifications
You must be signed in to change notification settings - Fork 9
/
decode386am.c
71 lines (62 loc) · 1.64 KB
/
decode386am.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
#include "minx86dec/types.h"
#include "minx86dec/state.h"
#include "minx86dec/opcodes.h"
#include "minx86dec/core386am.h"
#include "minx86dec/opcodes_str.h"
#ifdef IIT_FPU
# include "minx86dec/iit387.h"
#endif
#include <string.h>
#include <stdio.h>
uint8_t buffer[16384];
static void minx86dec_init_state(struct minx86dec_state *st) {
memset(st,0,sizeof(*st));
}
static void minx86dec_set_buffer(struct minx86dec_state *st,uint8_t *buf,int sz) {
st->fence = buf + sz;
st->prefetch_fence = st->fence - 16;
st->read_ip = buf;
}
int main(int argc,char **argv) {
struct minx86dec_state st;
minx86_read_ptr_t iptr;
char arg_c[101];
FILE *fp;
int sz=0;
int c;
if ((fp = fopen(argv[1],"rb")) == NULL) {
fprintf(stderr,"Cannot open %s\n",argv[1]);
return 1;
}
sz = fread(buffer,1,sizeof(buffer),fp);
fclose(fp);
if (sz < 1) {
fprintf(stderr,"File too small\n");
return 1;
}
minx86dec_init_state(&st);
if (argc > 2 && (!strcmp(argv[2],"/32") || !strcmp(argv[2],"-32"))) st.data32 = st.addr32 = 1;
minx86dec_set_buffer(&st,buffer,sz);
while (st.read_ip < st.fence) {
struct minx86dec_instruction i;
st.ip_value = (uint32_t)(st.read_ip - buffer);
minx86dec_decode386am(&st,&i);
#ifdef IIT_FPU
if (i.opcode == MXOP_UD && i.fpu_code != 0)
minx86dec_auxdecode387iit(&st,&i);
#endif
printf("0x%04X ",(unsigned int)(i.start - buffer));
for (c=0,iptr=i.start;iptr != i.end;c++)
printf("%02X ",*iptr++);
for (;c < 8;c++)
printf(" ");
printf("%-8s ",opcode_string[i.opcode]);
for (c=0;c < i.argc;) {
minx86dec_regprint(&i.argv[c],arg_c);
printf("%s",arg_c);
if (++c < i.argc) printf(",");
}
printf("\n");
}
return 0;
}