• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Capstone Disassembler Engine */
2 /* By Sebastian Macke <sebastian@macke.de>, 2018 */
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 
7 #include <capstone/platform.h>
8 #include <capstone/capstone.h>
9 
10 struct platform {
11 	cs_arch arch;
12 	cs_mode mode;
13 	unsigned char *code;
14 	size_t size;
15 	const char *comment;
16 };
17 
18 static csh handle;
19 
print_string_hex(const char * comment,unsigned char * str,size_t len)20 static void print_string_hex(const char *comment, unsigned char *str, size_t len)
21 {
22 	unsigned char *c;
23 
24 	printf("%s", comment);
25 	for (c = str; c < str + len; c++) {
26 		printf(" 0x%02x", *c & 0xff);
27 	}
28 
29 	printf("\n");
30 }
31 
get_am_name(mos65xx_address_mode mode)32 static const char *get_am_name(mos65xx_address_mode mode)
33 {
34 	switch(mode) {
35 		default:
36 		case MOS65XX_AM_NONE:
37 			return "No address mode";
38 		case MOS65XX_AM_IMP:
39 			return "implied addressing (no addressing mode)";
40 		case MOS65XX_AM_ACC:
41 			return "accumulator addressing";
42 		case MOS65XX_AM_ABS:
43 			return "absolute addressing";
44 		case MOS65XX_AM_ZP:
45 			return "zeropage addressing";
46 		case MOS65XX_AM_IMM:
47 			return "8 Bit immediate value";
48 		case MOS65XX_AM_ABSX:
49 			return "indexed absolute addressing by the X index register";
50 		case MOS65XX_AM_ABSY:
51 			return "indexed absolute addressing by the Y index register";
52 		case MOS65XX_AM_INDX:
53 			return "indexed indirect addressing by the X index register";
54 		case MOS65XX_AM_INDY:
55 			return "indirect indexed addressing by the Y index register";
56 		case MOS65XX_AM_ZPX:
57 			return "indexed zeropage addressing by the X index register";
58 		case MOS65XX_AM_ZPY:
59 			return "indexed zeropage addressing by the Y index register";
60 		case MOS65XX_AM_REL:
61 			return "relative addressing used by branches";
62 		case MOS65XX_AM_IND:
63 			return "absolute indirect addressing";
64 	}
65 }
66 
67 
print_insn_detail(cs_insn * ins)68 static void print_insn_detail(cs_insn *ins)
69 {
70 	cs_mos65xx *mos65xx;
71 	int i;
72 
73 	// detail can be NULL on "data" instruction if SKIPDATA option is turned ON
74 	if (ins->detail == NULL)
75 		return;
76 
77 	mos65xx = &(ins->detail->mos65xx);
78 
79 	// printf("insn_detail\n");
80 	printf("\taddress mode: %s\n", get_am_name(mos65xx->am));
81 	printf("\tmodifies flags: %s\n", mos65xx->modifies_flags ? "true": "false");
82 
83 	if (mos65xx->op_count)
84 		printf("\top_count: %u\n", mos65xx->op_count);
85 
86 	for (i = 0; i < mos65xx->op_count; i++) {
87 		cs_mos65xx_op *op = &(mos65xx->operands[i]);
88 		switch((int)op->type) {
89 			default:
90 				break;
91 			case MOS65XX_OP_REG:
92 				printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
93 				break;
94 			case MOS65XX_OP_IMM:
95 				printf("\t\toperands[%u].type: IMM = 0x%x\n", i, op->imm);
96 				break;
97 			case MOS65XX_OP_MEM:
98 				printf("\t\toperands[%u].type: MEM = 0x%x\n", i, op->mem);
99 				break;
100 		}
101 	}
102 }
103 
test()104 static void test()
105 {
106 #define MOS65XX_CODE "\x0d\x34\x12\x00\x81\x87\x6c\x01\x00\x85\xFF\x10\x00\x19\x42\x42\x00\x49\x42"
107 
108 	struct platform platforms[] = {
109 		{
110 			CS_ARCH_MOS65XX,
111 			0,
112 			(unsigned char *)MOS65XX_CODE,
113 			sizeof(MOS65XX_CODE) - 1,
114 			"MOS65XX"
115 		},
116 	};
117 
118 	uint64_t address = 0x1000;
119 	cs_insn *insn;
120 	int i;
121 	size_t count;
122 
123 	for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
124 		cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
125 		if (err) {
126 			printf("Failed on cs_open() with error returned: %u\n", err);
127 			abort();
128 		}
129 
130 		cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
131 
132 		count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
133 
134 		if (count) {
135 			size_t j;
136 
137 			printf("****************\n");
138 			printf("Platform: %s\n", platforms[i].comment);
139 			print_string_hex("Code:", platforms[i].code, platforms[i].size);
140 			printf("Disasm:\n");
141 
142 			for (j = 0; j < count; j++) {
143 				printf("0x%" PRIx64 ":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
144 				print_insn_detail(&insn[j]);
145 				puts("");
146 			}
147 			printf("0x%" PRIx64 ":\n", insn[j-1].address + insn[j-1].size);
148 
149 			// free memory allocated by cs_disasm()
150 			cs_free(insn, count);
151 		} else {
152 			printf("****************\n");
153 			printf("Platform: %s\n", platforms[i].comment);
154 			print_string_hex("Code:", platforms[i].code, platforms[i].size);
155 			printf("ERROR: Failed to disasm given code!\n");
156 			abort();
157 		}
158 
159 		printf("\n");
160 
161 		cs_close(&handle);
162 	}
163 }
164 
main()165 int main()
166 {
167 	test();
168 	return 0;
169 }
170