blob: 13d42f79a3628cda0717b082e47433429f9984a9 [file] [log] [blame]
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001/* Capstone Disassembler Engine */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */
3
4#include <stdio.h>
5#include <inttypes.h>
6
7#include <capstone.h>
8
9struct platform {
10 cs_arch arch;
11 cs_mode mode;
12 unsigned char *code;
13 size_t size;
14 char *comment;
15};
16
17static csh handle;
18
19static void print_string_hex(char *comment, unsigned char *str, int len)
20{
21 unsigned char *c;
22
23 printf("%s", comment);
24 for (c = str; c < str + len; c++) {
25 printf("0x%02x ", *c & 0xff);
26 }
27
28 printf("\n");
29}
30
31static void print_insn_detail(cs_insn *ins)
32{
33 cs_sysz *sysz = &(ins->detail->sysz);
34
35 if (sysz->op_count)
36 printf("\top_count: %u\n", sysz->op_count);
37
38 int i;
39 for (i = 0; i < sysz->op_count; i++) {
40 cs_sysz_op *op = &(sysz->operands[i]);
41 switch((int)op->type) {
42 default:
43 break;
44 case SYSZ_OP_REG:
45 printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
46 break;
Nguyen Anh Quynhda1e8332014-03-23 11:12:07 +080047 case SYSZ_OP_ACREG:
48 printf("\t\toperands[%u].type: ACREG = %u\n", i, op->reg);
49 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080050 case SYSZ_OP_IMM:
Nguyen Anh Quynhcf2f9e12014-03-23 09:20:47 +080051 printf("\t\toperands[%u].type: IMM = 0x%"PRIx64"\n", i, op->imm);
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080052 break;
53 case SYSZ_OP_MEM:
54 printf("\t\toperands[%u].type: MEM\n", i);
55 if (op->mem.base != X86_REG_INVALID)
56 printf("\t\t\toperands[%u].mem.base: REG = %s\n",
57 i, cs_reg_name(handle, op->mem.base));
58 if (op->mem.index != X86_REG_INVALID)
59 printf("\t\t\toperands[%u].mem.index: REG = %s\n",
60 i, cs_reg_name(handle, op->mem.index));
Nguyen Anh Quynhe4fae872014-03-24 17:26:57 +080061 if (op->mem.length != 0)
62 printf("\t\t\toperands[%u].mem.length: 0x%"PRIx64"\n", i, op->mem.length);
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080063 if (op->mem.disp != 0)
64 printf("\t\t\toperands[%u].mem.disp: 0x%"PRIx64"\n", i, op->mem.disp);
65
66 break;
67 }
68 }
69
70 if (sysz->cc != 0)
71 printf("\tCode condition: %u\n", sysz->cc);
72
73 printf("\n");
74}
75
76static void test()
77{
Nguyen Anh Quynhda1e8332014-03-23 11:12:07 +080078#define 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"
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080079
80 struct platform platforms[] = {
81 {
82 .arch = CS_ARCH_SYSZ,
83 .mode = CS_MODE_BIG_ENDIAN,
84 .code = (unsigned char*)SYSZ_CODE,
85 .size = sizeof(SYSZ_CODE) - 1,
86 .comment = "SystemZ",
87 },
88 };
89
90 uint64_t address = 0x1000;
91 cs_insn *insn;
92 int i;
93
94 for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
95 cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
96 if (err) {
97 printf("Failed on cs_open() with error returned: %u\n", err);
98 continue;
99 }
100
101 cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
102
103 size_t count = cs_disasm_ex(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
104 if (count) {
105 printf("****************\n");
106 printf("Platform: %s\n", platforms[i].comment);
107 print_string_hex("Code:", platforms[i].code, platforms[i].size);
108 printf("Disasm:\n");
109
110 size_t j;
111 for (j = 0; j < count; j++) {
112 printf("0x%"PRIx64":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
113 print_insn_detail(&insn[j]);
114 }
115 printf("0x%"PRIx64":\n", insn[j-1].address + insn[j-1].size);
116
117 // free memory allocated by cs_disasm_ex()
118 cs_free(insn, count);
119 } else {
120 printf("****************\n");
121 printf("Platform: %s\n", platforms[i].comment);
122 print_string_hex("Code:", platforms[i].code, platforms[i].size);
123 printf("ERROR: Failed to disasm given code!\n");
124 }
125
126 printf("\n");
127
128 cs_close(&handle);
129 }
130}
131
132int main()
133{
134 test();
135
136 return 0;
137}