• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1(* Capstone Disassembly Engine
2* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 *)
3
4open Printf
5open Capstone
6open X86
7open X86_const
8
9
10let print_string_hex comment str =
11	printf "%s" comment;
12	for i = 0 to (Array.length str - 1) do
13		printf "0x%02x " str.(i)
14	done;
15	printf "\n"
16
17
18let _X86_CODE16 = "\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00";;
19let _X86_CODE32 = "\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00";;
20let _X86_CODE64 = "\x55\x48\x8b\x05\xb8\x13\x00\x00";;
21
22
23let all_tests = [
24	(CS_ARCH_X86, [CS_MODE_16], _X86_CODE16, "X86 16bit (Intel syntax)", 0L);
25	(CS_ARCH_X86, [CS_MODE_32], _X86_CODE32, "X86 32bit (ATT syntax)", _CS_OPT_SYNTAX_ATT);
26	(CS_ARCH_X86, [CS_MODE_32], _X86_CODE32, "X86 32 (Intel syntax)", 0L);
27	(CS_ARCH_X86, [CS_MODE_64], _X86_CODE64, "X86 64 (Intel syntax)", 0L);
28];;
29
30let print_op handle i op =
31	( match op.value with
32	| X86_OP_INVALID _ -> ();	(* this would never happens *)
33	| X86_OP_REG reg -> printf "\t\top[%d]: REG = %s  [sz=%d]\n" i (cs_reg_name handle reg) op.size;
34	| X86_OP_IMM imm -> printf "\t\top[%d]: IMM = 0x%x  [sz=%d]\n" i imm op.size;
35	| X86_OP_MEM mem -> ( printf "\t\top[%d]: MEM  [sz=%d]\n" i op.size;
36		if mem.base != 0 then
37			printf "\t\t\toperands[%u].mem.base: REG = %s\n" i (cs_reg_name handle  mem.base);
38		if mem.index != 0 then
39			printf "\t\t\toperands[%u].mem.index: REG = %s\n" i (cs_reg_name handle mem.index);
40		if mem.scale != 1 then
41			printf "\t\t\toperands[%u].mem.scale: %d\n" i mem.scale;
42		if mem.disp != 0 then
43			printf "\t\t\toperands[%u].mem.disp: 0x%x\n" i mem.disp;
44		);
45	);
46	();;
47
48
49let print_detail handle mode insn =
50	match insn.arch with
51	| CS_INFO_X86 x86 -> (
52			print_string_hex "\tPrefix: " x86.prefix;
53
54			(* print instruction's opcode *)
55			print_string_hex "\tOpcode: " x86.opcode;
56
57			(* print operand's size, address size, displacement size & immediate size *)
58			printf "\taddr_size: %u\n" x86.addr_size;
59
60			(* print modRM byte *)
61			printf "\tmodrm: 0x%x\n" x86.modrm;
62
63			(* print displacement value *)
64			if x86.disp != 0 then
65			printf "\tdisp: 0x%x\n" x86.disp;
66
67			(* SIB is invalid in 16-bit mode *)
68			if not (List.mem CS_MODE_16 mode) then (
69				(* print SIB byte *)
70				printf "\tsib: 0x%x\n" x86.sib;
71
72				(* print sib index/scale/base (if applicable) *)
73				if x86.sib_index != _X86_REG_INVALID then
74				printf "\tsib_index: %s, sib_scale: %u, sib_base: %s\n"
75				(cs_reg_name handle x86.sib_index)
76				x86.sib_scale
77				(cs_reg_name handle x86.sib_base);
78			);
79
80			(* print all operands info (type & value) *)
81			if (Array.length x86.operands) > 0 then (
82				printf "\top_count: %d\n" (Array.length x86.operands);
83				Array.iteri (print_op handle) x86.operands;
84			);
85			printf "\n";
86	);
87	| _ -> ();
88	;;
89
90
91let print_insn handle mode insn =
92	printf "0x%x\t%s\t%s\n" insn.address insn.mnemonic insn.op_str;
93	print_detail handle mode insn
94
95
96let print_arch x =
97	let (arch, mode, code, comment, syntax) = x in
98	let handle = cs_open arch mode in (
99		if syntax != 0L then (
100			let err = cs_option handle CS_OPT_SYNTAX syntax in
101			match err with
102			| _ -> ();
103		);
104		let err = cs_option handle CS_OPT_DETAIL _CS_OPT_ON in
105		match err with
106		| _ -> ();
107		let insns = cs_disasm handle code 0x1000L 0L in (
108			printf "*************\n";
109			printf "Platform: %s\n" comment;
110			List.iter (print_insn handle mode) insns;
111		);
112		match cs_close handle with
113		| 0 -> ();
114		| _ -> printf "Failed to close handle";
115	);;
116
117List.iter print_arch all_tests;;
118