• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Second-Best Disassembler Engine */
2 /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 
7 #include <capstone.h>
8 
9 void print_string_hex(char *comment, unsigned char *str, size_t len);
10 
print_insn_detail_x86(csh ud,cs_mode mode,cs_insn * ins)11 void print_insn_detail_x86(csh ud, cs_mode mode, cs_insn *ins)
12 {
13 	int count, i;
14 	cs_x86 *x86;
15 
16 	// detail can be NULL on "data" instruction if SKIPDATA option is turned ON
17 	if (ins->detail == NULL)
18 		return;
19 
20 	x86 = &(ins->detail->x86);
21 
22 	print_string_hex("\tPrefix:", x86->prefix, 4);
23 
24 	print_string_hex("\tOpcode:", x86->opcode, 4);
25 
26 	printf("\trex: 0x%x\n", x86->rex);
27 
28 	printf("\taddr_size: %u\n", x86->addr_size);
29 	printf("\tmodrm: 0x%x\n", x86->modrm);
30 	printf("\tdisp: 0x%x\n", x86->disp);
31 
32 	// SIB is not available in 16-bit mode
33 	if ((mode & CS_MODE_16) == 0) {
34 		printf("\tsib: 0x%x\n", x86->sib);
35 		if (x86->sib_base != X86_REG_INVALID)
36 			printf("\t\tsib_base: %s\n", cs_reg_name(ud, x86->sib_base));
37 		if (x86->sib_index != X86_REG_INVALID)
38 			printf("\t\tsib_index: %s\n", cs_reg_name(ud, x86->sib_index));
39 		if (x86->sib_scale != 0)
40 			printf("\t\tsib_scale: %d\n", x86->sib_scale);
41 	}
42 
43 	// SSE code condition
44 	if (x86->sse_cc != X86_SSE_CC_INVALID) {
45 		printf("\tsse_cc: %u\n", x86->sse_cc);
46 	}
47 
48 	// AVX code condition
49 	if (x86->avx_cc != X86_AVX_CC_INVALID) {
50 		printf("\tavx_cc: %u\n", x86->avx_cc);
51 	}
52 
53 	// AVX Suppress All Exception
54 	if (x86->avx_sae) {
55 		printf("\tavx_sae: %u\n", x86->avx_sae);
56 	}
57 
58 	// AVX Rounding Mode
59 	if (x86->avx_rm != X86_AVX_RM_INVALID) {
60 		printf("\tavx_rm: %u\n", x86->avx_rm);
61 	}
62 
63 	count = cs_op_count(ud, ins, X86_OP_IMM);
64 	if (count > 0) {
65 		printf("\timm_count: %u\n", count);
66 		for (i = 1; i < count + 1; i++) {
67 			int index = cs_op_index(ud, ins, X86_OP_IMM, i);
68 			printf("\t\timms[%u]: 0x%" PRIx64 "\n", i, x86->operands[index].imm);
69 		}
70 	}
71 
72 	if (x86->op_count)
73 		printf("\top_count: %u\n", x86->op_count);
74 	for (i = 0; i < x86->op_count; i++) {
75 		cs_x86_op *op = &(x86->operands[i]);
76 
77 		switch((int)op->type) {
78 			case X86_OP_REG:
79 				printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(ud, op->reg));
80 				break;
81 			case X86_OP_IMM:
82 				printf("\t\toperands[%u].type: IMM = 0x%" PRIx64 "\n", i, op->imm);
83 				break;
84 			case X86_OP_MEM:
85 				printf("\t\toperands[%u].type: MEM\n", i);
86 				if (op->mem.segment != X86_REG_INVALID)
87 					printf("\t\t\toperands[%u].mem.segment: REG = %s\n", i, cs_reg_name(ud, op->mem.segment));
88 				if (op->mem.base != X86_REG_INVALID)
89 					printf("\t\t\toperands[%u].mem.base: REG = %s\n", i, cs_reg_name(ud, op->mem.base));
90 				if (op->mem.index != X86_REG_INVALID)
91 					printf("\t\t\toperands[%u].mem.index: REG = %s\n", i, cs_reg_name(ud, op->mem.index));
92 				if (op->mem.scale != 1)
93 					printf("\t\t\toperands[%u].mem.scale: %u\n", i, op->mem.scale);
94 				if (op->mem.disp != 0)
95 					printf("\t\t\toperands[%u].mem.disp: 0x%" PRIx64 "\n", i, op->mem.disp);
96 				break;
97 			default:
98 				break;
99 		}
100 
101 		// AVX broadcast type
102 		if (op->avx_bcast != X86_AVX_BCAST_INVALID)
103 			printf("\t\toperands[%u].avx_bcast: %u\n", i, op->avx_bcast);
104 
105 		// AVX zero opmask {z}
106 		if (op->avx_zero_opmask != false)
107 			printf("\t\toperands[%u].avx_zero_opmask: TRUE\n", i);
108 
109 		printf("\t\toperands[%u].size: %u\n", i, op->size);
110 	}
111 }
112