1(* Capstone Disassembly Engine
2* By Guillaume Jeanne <guillaume.jeanne@ensimag.fr>, 2014> *)
3
4open Printf
5open Capstone
6open Systemz
7
8
9let print_string_hex comment str =
10	printf "%s" comment;
11	for i = 0 to (Array.length str - 1) do
12		printf "0x%02x " str.(i)
13	done;
14	printf "\n"
15
16
17let _SYSZ_CODE = "\xed\x00\x00\x00\x00\x1a\x5a\x0f\x1f\xff\xc2\x09\x80\x00\x00\x00\x07\xf7\xeb\x2a\xff\xff\x7f\x57\xe3\x01\xff\xff\x7f\x57\xeb\x00\xf0\x00\x00\x24\xb2\x4f\x00\x78";;
18
19
20
21let all_tests = [
22	(CS_ARCH_SYSZ, [CS_MODE_LITTLE_ENDIAN], _SYSZ_CODE, "SystemZ");
23];;
24
25let print_op handle i op =
26	( match op.value with
27	| SYSZ_OP_INVALID _ -> ();	(* this would never happens *)
28	| SYSZ_OP_REG reg -> printf "\t\top[%d]: REG = %s\n" i (cs_reg_name handle reg);
29	| SYSZ_OP_ACREG reg -> printf "\t\top[%d]: ACREG = %u\n" i reg;
30	| SYSZ_OP_IMM imm -> printf "\t\top[%d]: IMM = 0x%x\n" i imm;
31	| SYSZ_OP_MEM mem -> ( printf "\t\top[%d]: MEM\n" i;
32		if mem.base != 0 then
33			printf "\t\t\toperands[%u].mem.base: REG = %s\n" i (cs_reg_name handle mem.base);
34		if mem.index != 0 then
35			printf "\t\t\toperands[%u].mem.index: 0x%x\n" i mem.index;
36		if mem.length != 0L then
37			printf "\t\t\toperands[%u].mem.length: 0x%Lx\n" i mem.length;
38		if mem.disp != 0L then
39			printf "\t\t\toperands[%u].mem.disp: 0x%Lx\n" i mem.disp;
40		);
41	);
42	();;
43
44
45let print_detail handle insn =
46	match insn.arch with
47	| CS_INFO_SYSZ sysz -> (
48			(* print all operands info (type & value) *)
49			if (Array.length sysz.operands) > 0 then (
50				printf "\top_count: %d\n" (Array.length sysz.operands);
51				Array.iteri (print_op handle) sysz.operands;
52			);
53			printf "\n";
54		);
55	| _ -> ();
56	;;
57
58
59let print_insn handle insn =
60	printf "0x%x\t%s\t%s\n" insn.address insn.mnemonic insn.op_str;
61	print_detail handle insn
62
63
64let print_arch x =
65	let (arch, mode, code, comment) = x in
66		let handle = cs_open arch mode in
67		let err = cs_option handle CS_OPT_DETAIL _CS_OPT_ON in
68		match err with
69		| _ -> ();
70		let insns = cs_disasm handle code 0x1000L 0L in
71			printf "*************\n";
72			printf "Platform: %s\n" comment;
73			List.iter (print_insn handle) insns;
74		match cs_close handle with
75		| 0 -> ();
76		| _ -> printf "Failed to close handle";
77		;;
78
79
80List.iter print_arch all_tests;;
81