5 import static capstone.Capstone.CS_AC_READ;
6 import static capstone.Capstone.CS_AC_WRITE;
7 import capstone.Capstone.CsRegsAccess;
14 static byte[] hexString2Byte(String s) {
17 byte[]
data =
new byte[
len / 2];
18 for (
int i = 0;
i <
len;
i += 2) {
19 data[
i / 2] = (
byte) ((Character.digit(s.charAt(
i), 16) << 4)
20 + Character.digit(s.charAt(
i+1), 16));
25 static final String X86_CODE64 =
"55488b05b8130000";
26 static final String X86_CODE16 =
"8d4c320801d881c6341200000523010000368b849123010000418d8439896700008d8789670000b4c6";
27 static final String X86_CODE32 =
"8d4c320801d881c6341200000523010000368b849123010000418d8439896700008d8789670000b4c6";
29 public static Capstone
cs;
31 private static String
hex(
int i) {
32 return Integer.toString(
i, 16);
35 private static String
hex(
long i) {
36 return Long.toString(
i, 16);
41 for (
int i=0 ;
i<arr.length;
i++)
42 ret += String.format(
"0x%02x ", arr[
i]);
47 System.out.printf(
"0x%x:\t%s\t%s\n", ins.address, ins.mnemonic, ins.opStr);
51 System.out.printf(
"\tPrefix: %s\n",
array2hex(operands.prefix));
53 System.out.printf(
"\tOpcode: %s\n",
array2hex(operands.opcode));
56 System.out.printf(
"\trex: 0x%x\n", operands.rex);
59 System.out.printf(
"\taddr_size: %d\n", operands.addrSize);
62 System.out.printf(
"\tmodrm: 0x%x\n", operands.modrm);
65 if (operands.encoding.modrmOffset != 0) {
66 System.out.printf(
"\tmodrm offset: 0x%x\n", operands.encoding.modrmOffset);
70 System.out.printf(
"\tdisp: 0x%x\n", operands.disp);
73 if (operands.encoding.dispOffset != 0) {
74 System.out.printf(
"\tdisp offset: 0x%x\n", operands.encoding.dispOffset);
78 if (operands.encoding.dispSize != 0) {
79 System.out.printf(
"\tdisp size: 0x%x\n", operands.encoding.dispSize);
83 if ( (
cs.mode & Capstone.CS_MODE_16) == 0) {
85 System.out.printf(
"\tsib: 0x%x\n", operands.sib);
86 if (operands.sib != 0)
87 System.out.printf(
"\t\tsib_base: %s\n\t\tsib_index: %s\n\t\tsib_scale: %d\n",
88 ins.regName(operands.sibBase), ins.regName(operands.sibIndex), operands.sibScale);
91 if (operands.xopCC != 0)
92 System.out.printf(
"\txop_cc: %u\n", operands.xopCC);
94 if (operands.sseCC != 0)
95 System.out.printf(
"\tsse_cc: %u\n", operands.sseCC);
97 if (operands.avxCC != 0)
98 System.out.printf(
"\tavx_cc: %u\n", operands.avxCC);
101 System.out.printf(
"\tavx_sae: TRUE\n");
103 if (operands.avxRm != 0)
104 System.out.printf(
"\tavx_rm: %u\n", operands.avxRm);
108 System.out.printf(
"\timm_count: %d\n",
count);
109 System.out.printf(
"\timm offset: 0x%x\n", operands.encoding.immOffset);
110 System.out.printf(
"\timm size: 0x%x\n", operands.encoding.immSize);
113 System.out.printf(
"\t\timms[%d]: 0x%x\n",
i+1, (operands.op[
index].value.imm));
117 if (operands.op.length != 0) {
118 System.out.printf(
"\top_count: %d\n", operands.op.length);
119 for (
int c=0;
c<operands.op.length;
c++) {
121 String imm =
hex(
i.value.imm);
123 System.out.printf(
"\t\toperands[%d].type: REG = %s\n",
c, ins.regName(
i.value.reg));
125 System.out.printf(
"\t\toperands[%d].type: IMM = 0x%x\n",
c,
i.value.imm);
127 System.out.printf(
"\t\toperands[%d].type: MEM\n",
c);
128 String segment = ins.regName(
i.value.mem.segment);
129 String
base = ins.regName(
i.value.mem.base);
130 String
index = ins.regName(
i.value.mem.index);
132 System.out.printf(
"\t\t\toperands[%d].mem.segment: REG = %s\n",
c, segment);
134 System.out.printf(
"\t\t\toperands[%d].mem.base: REG = %s\n",
c,
base);
136 System.out.printf(
"\t\t\toperands[%d].mem.index: REG = %s\n",
c,
index);
137 if (
i.value.mem.scale != 1)
138 System.out.printf(
"\t\t\toperands[%d].mem.scale: %d\n",
c,
i.value.mem.scale);
139 if (
i.value.mem.disp != 0)
140 System.out.printf(
"\t\t\toperands[%d].mem.disp: 0x%x\n",
c,
i.value.mem.disp);
145 System.out.printf(
"\t\toperands[%d].avx_bcast: %d\n",
c,
i.avx_bcast);
149 if (
i.avx_zero_opmask) {
150 System.out.printf(
"\t\toperands[%d].avx_zero_opmask: TRUE\n",
c);
153 System.out.printf(
"\t\toperands[%d].size: %d\n",
c,
i.size);
156 System.out.printf(
"\t\toperands[%d].access: READ\n",
c);
159 System.out.printf(
"\t\toperands[%d].access: WRITE\n",
c);
162 System.out.printf(
"\t\toperands[%d].access: READ | WRITE\n",
c);
168 CsRegsAccess regsAccess = ins.regsAccess();
169 if (regsAccess !=
null) {
170 short[] regsRead = regsAccess.regsRead;
171 short[] regsWrite = regsAccess.regsWrite;
173 if (regsRead.length > 0) {
174 System.out.printf(
"\tRegisters read:");
175 for (
int i = 0;
i < regsRead.length;
i++) {
176 System.out.printf(
" %s", ins.regName(regsRead[
i]));
178 System.out.print(
"\n");
181 if (regsWrite.length > 0) {
182 System.out.printf(
"\tRegister modified:");
183 for (
int i = 0;
i < regsWrite.length;
i++) {
184 System.out.printf(
" %s", ins.regName(regsWrite[
i]));
186 System.out.print(
"\n");
192 public static void main(String argv[]) {
196 new TestBasic.
platform(Capstone.CS_ARCH_X86, Capstone.CS_MODE_32, Capstone.CS_OPT_SYNTAX_ATT, hexString2Byte(
X86_CODE32),
"X86 32 (AT&T syntax)"),
203 System.out.println(
new String(
new char[16]).replace(
"\0",
"*"));
204 System.out.println(
"Platform: " +
test.comment);
206 System.out.println(
"Disasm:");
209 cs.setDetail(Capstone.CS_OPT_ON);
210 if (
test.syntax != 0) {
211 cs.setSyntax(
test.syntax);
213 Capstone.CsInsn[] all_ins =
cs.disasm(
test.code, 0x1000);
215 for (
int j = 0; j < all_ins.length; j++) {
217 System.out.println();
220 System.out.printf(
"0x%x:\n\n", all_ins[all_ins.length-1].address + all_ins[all_ins.length-1].size);