-
Notifications
You must be signed in to change notification settings - Fork 9
/
decode586.c
65 lines (56 loc) · 1.52 KB
/
decode586.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
#include "minx86dec/types.h"
#include "minx86dec/state.h"
#include "minx86dec/opcodes.h"
#include "minx86dec/core586.h"
#include "minx86dec/opcodes_str.h"
#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;
minx86dec_init_instruction(&i);
st.ip_value = (uint32_t)(st.read_ip - buffer);
minx86dec_decode586(&st,&i);
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;
}