--- np2/i386c/ia32/disasm.c 2004/03/05 14:17:35 1.7 +++ np2/i386c/ia32/disasm.c 2012/01/08 11:32:16 1.12 @@ -1,5 +1,3 @@ -/* $Id: disasm.c,v 1.7 2004/03/05 14:17:35 monaka Exp $ */ - /* * Copyright (c) 2004 NONAKA Kimihiro * All rights reserved. @@ -12,8 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES @@ -332,7 +328,9 @@ static const char *opcode2_g9[8] = { NULL, "cmpxchg8b", NULL, NULL, NULL, NULL, NULL, NULL }; +#if 0 static const char *sep[2] = { " ", ", " }; +#endif /* @@ -383,6 +381,7 @@ disasm_codefetch_1(disasm_context_t *ctx return 0; } +#if 0 static int disasm_codefetch_2(disasm_context_t *ctx) { @@ -632,12 +631,13 @@ ea(disasm_context_t *ctx) return 0; } +#endif /* * get opcode */ static int -op(disasm_context_t *ctx) +get_opcode(disasm_context_t *ctx) { const char *opcode; UINT8 op[3]; @@ -652,36 +652,35 @@ op(disasm_context_t *ctx) return rv; op[0] = (UINT8)(ctx->val & 0xff); - if (insttable_info[op[0]] & INST_PREFIX) { - if (ctx->prefix == 0) - ctx->prefix = ctx->next; - - switch (op[0]) { - case 0x26: /* ES: */ - case 0x2e: /* CS: */ - case 0x36: /* SS: */ - case 0x3e: /* DS: */ - ctx->useseg = TRUE; - ctx->seg = (op[0] >> 3) & 3; - break; - - case 0x64: /* FS: */ - case 0x65: /* GS: */ - ctx->useseg = TRUE; - ctx->seg = (op[0] - 0x64) + 4; - break; - - case 0x66: /* OPSize: */ - ctx->op32 = !CPU_INST_OP32; - break; - - case 0x67: /* AddrSize: */ - ctx->as32 = !CPU_INST_AS32; - break; - } - continue; + if (!(insttable_info[op[0]] & INST_PREFIX)) + break; + + if (ctx->prefix == 0) + ctx->prefix = ctx->next; + + switch (op[0]) { + case 0x26: /* ES: */ + case 0x2e: /* CS: */ + case 0x36: /* SS: */ + case 0x3e: /* DS: */ + ctx->useseg = TRUE; + ctx->seg = (op[0] >> 3) & 3; + break; + + case 0x64: /* FS: */ + case 0x65: /* GS: */ + ctx->useseg = TRUE; + ctx->seg = (op[0] - 0x64) + 4; + break; + + case 0x66: /* OPSize: */ + ctx->op32 = !CPU_STATSAVE.cpu_inst_default.op_32; + break; + + case 0x67: /* AddrSize: */ + ctx->as32 = !CPU_STATSAVE.cpu_inst_default.as_32; + break; } - break; } if (prefix == MAX_PREFIX) return 1; @@ -793,14 +792,14 @@ disasm(UINT32 *eip, disasm_context_t *ct ctx->arg[2] = 0; ctx->eip = *eip; - ctx->op32 = CPU_INST_OP32; - ctx->as32 = CPU_INST_AS32; + ctx->op32 = CPU_STATSAVE.cpu_inst_default.op_32; + ctx->as32 = CPU_STATSAVE.cpu_inst_default.as_32; ctx->seg = -1; ctx->baseaddr = ctx->eip; ctx->pad = ' '; - rv = op(ctx); + rv = get_opcode(ctx); if (rv) { memset(ctx, 0, sizeof(disasm_context_t)); return rv; @@ -809,3 +808,54 @@ disasm(UINT32 *eip, disasm_context_t *ct return 0; } + +char * +cpu_disasm2str(UINT32 eip) +{ + static char output[2048]; + disasm_context_t d; + UINT32 eip2 = eip; + int rv; + + output[0] = '\0'; + rv = disasm(&eip2, &d); + if (rv == 0) { + char buf[256]; + char tmp[32]; + int len = d.nopbytes > 8 ? 8 : d.nopbytes; + int i; + + buf[0] = '\0'; + for (i = 0; i < len; i++) { + snprintf(tmp, sizeof(tmp), "%02x ", d.opbyte[i]); + milstr_ncat(buf, tmp, sizeof(buf)); + } + for (; i < 8; i++) { + milstr_ncat(buf, " ", sizeof(buf)); + } + snprintf(output, sizeof(output), "%04x:%08x: %s%s", + CPU_CS, eip, buf, d.str); + + if (i < d.nopbytes) { + char t[256]; + buf[0] = '\0'; + for (; i < d.nopbytes; i++) { + snprintf(tmp, sizeof(tmp), "%02x ", + d.opbyte[i]); + milstr_ncat(buf, tmp, sizeof(buf)); + if ((i % 8) == 7) { + snprintf(t, sizeof(t), + "\n : %s", buf); + milstr_ncat(output, t, sizeof(output)); + buf[0] = '\0'; + } + } + if ((i % 8) != 0) { + snprintf(t, sizeof(t), + "\n : %s", buf); + milstr_ncat(output, t, sizeof(output)); + } + } + } + return output; +}