1(* Capstone Disassembly Engine 2* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 *) 3 4open Printf 5open Capstone 6open Mips 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 _MIPS_CODE = "\x0C\x10\x00\x97\x00\x00\x00\x00\x24\x02\x00\x0c\x8f\xa2\x00\x00\x34\x21\x34\x56";; 18let _MIPS_CODE2 = "\x56\x34\x21\x34\xc2\x17\x01\x00";; 19 20let all_tests = [ 21 (CS_ARCH_MIPS, [CS_MODE_MIPS32; CS_MODE_BIG_ENDIAN], _MIPS_CODE, "MIPS-32 (Big-endian)"); 22 (CS_ARCH_MIPS, [CS_MODE_MIPS64; CS_MODE_LITTLE_ENDIAN], _MIPS_CODE2, "MIPS-64-EL (Little-endian)"); 23];; 24 25let print_op handle i op = 26 ( match op.value with 27 | MIPS_OP_INVALID _ -> (); (* this would never happens *) 28 | MIPS_OP_REG reg -> printf "\t\top[%d]: REG = %s\n" i (cs_reg_name handle reg); 29 | MIPS_OP_IMM imm -> printf "\t\top[%d]: IMM = 0x%x\n" i imm; 30 | MIPS_OP_MEM mem -> ( printf "\t\top[%d]: MEM\n" i; 31 if mem.base != 0 then 32 printf "\t\t\toperands[%u].mem.base: REG = %s\n" i (cs_reg_name handle mem.base); 33 if mem.disp != 0 then 34 printf "\t\t\toperands[%u].mem.disp: 0x%x\n" i mem.disp; 35 ); 36 ); 37 ();; 38 39 40let print_detail handle insn = 41 match insn.arch with 42 | CS_INFO_MIPS mips -> ( 43 (* print all operands info (type & value) *) 44 if (Array.length mips.operands) > 0 then ( 45 printf "\top_count: %d\n" (Array.length mips.operands); 46 Array.iteri (print_op handle) mips.operands; 47 ); 48 printf "\n"; 49 ); 50 | _ -> (); 51 ;; 52 53 54let print_insn handle insn = 55 printf "0x%x\t%s\t%s\n" insn.address insn.mnemonic insn.op_str; 56 print_detail handle insn 57 58 59let print_arch x = 60 let (arch, mode, code, comment) = x in 61 let handle = cs_open arch mode in 62 let err = cs_option handle CS_OPT_DETAIL _CS_OPT_ON in 63 match err with 64 | _ -> (); 65 let insns = cs_disasm handle code 0x1000L 0L in 66 printf "*************\n"; 67 printf "Platform: %s\n" comment; 68 List.iter (print_insn handle) insns; 69 match cs_close handle with 70 | 0 -> (); 71 | _ -> printf "Failed to close handle"; 72 ;; 73 74 75List.iter print_arch all_tests;; 76