File: cstool_sh.c

package info (click to toggle)
capstone 5.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 58,188 kB
  • sloc: ansic: 96,086; cpp: 67,489; cs: 29,510; python: 25,829; pascal: 24,412; java: 15,582; ml: 14,473; makefile: 1,275; sh: 479; ruby: 386
file content (87 lines) | stat: -rw-r--r-- 2,162 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//
//  cstool_sh.c
//
//  Yoshinori Sato 2022/09/07
//

#include <stdio.h>
#include <capstone/capstone.h>
#include "cstool.h"

static const char* s_addressing_modes[] = {
	"<invalid mode>",

	"Register Direct",	/// Rn

	"Register Indirect",	/// @Rn
	"Register Indirect with Postincrement",	/// @Rn+
	"Register Indirect with Predecrement",	/// @-Rn
	"Register Indirect with Displacement",	/// @(disp,Rn)

	"Indexed register indirect",		/// @(R0, Rn)
	"GBR indirect with displacement",	/// @(disp,GBR)
	"Indexed GBR indirect",			/// @(R0, GBR)

	"PC-relative with Displacement",	/// @(disp, PC)

	"Immediate value",
};

static void print_read_write_regs(cs_detail* detail, csh handle)
{
	int i;

	for (i = 0; i < detail->regs_read_count; ++i) {
		uint16_t reg_id = detail->regs_read[i];
		const char* reg_name = cs_reg_name(handle, reg_id);
		printf("\treading from reg: %s\n", reg_name);
	}

	for (i = 0; i < detail->regs_write_count; ++i) {
		uint16_t reg_id = detail->regs_write[i];
		const char* reg_name = cs_reg_name(handle, reg_id);
		printf("\twriting to reg:   %s\n", reg_name);
	}
}

void print_insn_detail_sh(csh handle, cs_insn *ins)
{
	cs_detail* detail;
	int i;

	// detail can be NULL on "data" instruction if SKIPDATA option is turned ON
	if (ins->detail == NULL)
		return;

	detail = ins->detail;

	print_read_write_regs(detail, handle);

	printf("\tgroups_count: %u\n", detail->groups_count);

	for (i = 0; i < detail->sh.op_count; i++) {
		cs_sh_op* op = &(detail->sh.operands[i]);

		switch((int)op->type) {
			default:
				break;
			case SH_OP_REG:
				printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
				break;
			case SH_OP_IMM:
				printf("\t\toperands[%u].type: IMM = 0x%x\n", i, (int)op->imm);
				break;
			case SH_OP_MEM:
				printf("\t\toperands[%u].type: MEM\n", i);
				if (op->mem.reg != SH_REG_INVALID)
					printf("\t\t\toperands[%u].mem.reg: REG = %s\n",
							i, cs_reg_name(handle, op->mem.reg));
				if (op->mem.disp != 0)
					printf("\t\t\toperands[%u].mem.disp: 0x%x\n",
							i, op->mem.disp);
				printf("\t\taddress mode: %s\n", s_addressing_modes[op->mem.address]);
				break;
		}
	}
}