Diff for /np2/i386c/ia32/disasm.c between versions 1.6 and 1.12

version 1.6, 2004/02/20 16:09:04 version 1.12, 2012/01/08 11:32:16
Line 1 Line 1
 /*      $Id$    */  
   
 /*  /*
  * Copyright (c) 2004 NONAKA Kimihiro   * Copyright (c) 2004 NONAKA Kimihiro
  * All rights reserved.   * All rights reserved.
Line 12 Line 10
  * 2. Redistributions in binary form must reproduce the above copyright   * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the   *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.   *    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   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Line 332  static const char *opcode2_g9[8] = { Line 328  static const char *opcode2_g9[8] = {
         NULL, "cmpxchg8b", NULL, NULL, NULL, NULL, NULL, NULL          NULL, "cmpxchg8b", NULL, NULL, NULL, NULL, NULL, NULL
 };  };
   
   #if 0
 static const char *sep[2] = { " ", ", " };  static const char *sep[2] = { " ", ", " };
   #endif
   
 /*  
  * context  
  */  
 typedef struct {  
         UINT32 val;  
   
         UINT32 eip;  
         BOOL op32;  
         BOOL as32;  
   
         UINT32 baseaddr;  
         UINT8 opcode[3];  
         UINT8 modrm;  
         UINT8 sib;  
   
         BOOL useseg;  
         int seg;  
   
         UINT8 opbyte[32];  
         int nopbytes;  
   
         char str[256];  
         size_t remain;  
   
         char *next;  
         char *prefix;  
         char *op;  
         char *arg[3];  
         int narg;  
   
         char pad;  
 } disasm_context_t;  
   
   
 /*  /*
Line 417  disasm_codefetch_1(disasm_context_t *ctx Line 381  disasm_codefetch_1(disasm_context_t *ctx
         return 0;          return 0;
 }  }
   
   #if 0
 static int  static int
 disasm_codefetch_2(disasm_context_t *ctx)  disasm_codefetch_2(disasm_context_t *ctx)
 {  {
Line 666  ea(disasm_context_t *ctx) Line 631  ea(disasm_context_t *ctx)
   
         return 0;          return 0;
 }  }
   #endif
   
 /*  /*
  * get opcode   * get opcode
  */   */
 static int  static int
 op(disasm_context_t *ctx)  get_opcode(disasm_context_t *ctx)
 {  {
         const char *opcode;          const char *opcode;
         UINT8 op[3];          UINT8 op[3];
Line 686  op(disasm_context_t *ctx) Line 652  op(disasm_context_t *ctx)
                         return rv;                          return rv;
   
                 op[0] = (UINT8)(ctx->val & 0xff);                  op[0] = (UINT8)(ctx->val & 0xff);
                 if (insttable_info[op[0]] & INST_PREFIX) {                  if (!(insttable_info[op[0]] & INST_PREFIX))
                         if (ctx->prefix == 0)                          break;
                                 ctx->prefix = ctx->next;  
                   if (ctx->prefix == 0)
                         switch (op[0]) {                          ctx->prefix = ctx->next;
                         case 0x26:      /* ES: */  
                         case 0x2e:      /* CS: */                  switch (op[0]) {
                         case 0x36:      /* SS: */                  case 0x26:      /* ES: */
                         case 0x3e:      /* DS: */                  case 0x2e:      /* CS: */
                                 ctx->useseg = TRUE;                  case 0x36:      /* SS: */
                                 ctx->seg = (op[0] >> 3) & 3;                  case 0x3e:      /* DS: */
                                 break;                          ctx->useseg = TRUE;
                           ctx->seg = (op[0] >> 3) & 3;
                         case 0x64:      /* FS: */                          break;
                         case 0x65:      /* GS: */  
                                 ctx->useseg = TRUE;                  case 0x64:      /* FS: */
                                 ctx->seg = (op[0] - 0x64) + 4;                  case 0x65:      /* GS: */
                                 break;                          ctx->useseg = TRUE;
                           ctx->seg = (op[0] - 0x64) + 4;
                         case 0x66:      /* OPSize: */                          break;
                                 ctx->op32 = !CPU_INST_OP32;  
                                 break;                  case 0x66:      /* OPSize: */
                           ctx->op32 = !CPU_STATSAVE.cpu_inst_default.op_32;
                         case 0x67:      /* AddrSize: */                          break;
                                 ctx->as32 = !CPU_INST_AS32;  
                                 break;                  case 0x67:      /* AddrSize: */
                         }                          ctx->as32 = !CPU_STATSAVE.cpu_inst_default.as_32;
                         continue;                          break;
                 }                  }
                 break;  
         }          }
         if (prefix == MAX_PREFIX)          if (prefix == MAX_PREFIX)
                 return 1;                  return 1;
Line 813  op(disasm_context_t *ctx) Line 778  op(disasm_context_t *ctx)
  * interface   * interface
  */   */
 int  int
 disasm(UINT32 *eip, char *buf, size_t size)  disasm(UINT32 *eip, disasm_context_t *ctx)
 {  {
         disasm_context_t ctx;  
         char tmp[32];  
         int rv;          int rv;
         int i;  
   
         memset(&ctx, 0, sizeof(ctx));          memset(ctx, 0, sizeof(disasm_context_t));
         ctx.remain = sizeof(ctx.str) - 1;          ctx->remain = sizeof(ctx->str) - 1;
         ctx.next = ctx.str;          ctx->next = ctx->str;
         ctx.prefix = 0;          ctx->prefix = 0;
         ctx.op = 0;          ctx->op = 0;
         ctx.arg[0] = 0;          ctx->arg[0] = 0;
         ctx.arg[1] = 0;          ctx->arg[1] = 0;
         ctx.arg[2] = 0;          ctx->arg[2] = 0;
   
         ctx.eip = *eip;          ctx->eip = *eip;
         ctx.op32 = CPU_INST_OP32;          ctx->op32 = CPU_STATSAVE.cpu_inst_default.op_32;
         ctx.as32 = CPU_INST_AS32;          ctx->as32 = CPU_STATSAVE.cpu_inst_default.as_32;
         ctx.seg = -1;          ctx->seg = -1;
   
         ctx.baseaddr = ctx.eip;          ctx->baseaddr = ctx->eip;
         ctx.pad = ' ';          ctx->pad = ' ';
   
         rv = op(&ctx);          rv = get_opcode(ctx);
         if (rv) {          if (rv) {
                 memset(&ctx, 0, sizeof(ctx));                  memset(ctx, 0, sizeof(disasm_context_t));
                 return rv;                  return rv;
         }          }
           *eip = ctx->eip;
   
         *eip = ctx.eip;          return 0;
   }
   
         memset(buf, 0, size);  char *
         for (i = 0; i < ctx.nopbytes; i++) {  cpu_disasm2str(UINT32 eip)
                 snprintf(tmp, sizeof(tmp), "%02x ", ctx.opbyte[i]);  {
                 milstr_ncat(buf, tmp, size);          static char output[2048];
         }          disasm_context_t d;
           UINT32 eip2 = eip;
           int rv;
   
         milstr_ncpy(tmp, "   ", sizeof(tmp));          output[0] = '\0';
         for (; i < 8; i++) {          rv = disasm(&eip2, &d);
                 milstr_ncat(buf, tmp, size);          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));
                           }
                   }
         }          }
         milstr_ncat(buf, ctx.str, size);          return output;
         return 0;  
 }  }

Removed from v.1.6  
changed lines
  Added in v.1.12


RetroPC.NET-CVS <cvs@retropc.net>