Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /* -----------------------------------------------------------------------------
  2.  * decode.c
  3.  *
  4.  * Copyright (c) 2005, 2006, Vivek Mohan <vivek@sig9.com>
  5.  * All rights reserved. See LICENSE
  6.  * -----------------------------------------------------------------------------
  7.  */
  8.  
  9. #include <assert.h>
  10. #include <string.h>
  11.  
  12. #include "types.h"
  13. #include "itab.h"
  14. #include "input.h"
  15. #include "decode.h"
  16.  
  17. /* The max number of prefixes to an instruction */
  18. #define MAX_PREFIXES    15
  19.  
  20. static struct ud_itab_entry ie_invalid = { UD_Iinvalid, O_NONE, O_NONE, O_NONE, P_none };
  21. static struct ud_itab_entry ie_pause   = { UD_Ipause,   O_NONE, O_NONE, O_NONE, P_none };
  22. static struct ud_itab_entry ie_nop     = { UD_Inop,     O_NONE, O_NONE, O_NONE, P_none };
  23.  
  24.  
  25. /* Looks up mnemonic code in the mnemonic string table
  26.  * Returns NULL if the mnemonic code is invalid
  27.  */
  28. const char * ud_lookup_mnemonic( enum ud_mnemonic_code c )
  29. {
  30.     if ( c < UD_Id3vil )
  31.         return ud_mnemonics_str[ c ];
  32.     return NULL;
  33. }
  34.  
  35.  
  36. /* Extracts instruction prefixes.
  37.  */
  38. static int get_prefixes( struct ud* u )
  39. {
  40.     unsigned int have_pfx = 1;
  41.     unsigned int i;
  42.     uint8_t curr;
  43.  
  44.     /* if in error state, bail out */
  45.     if ( u->error )
  46.         return -1;
  47.  
  48.     /* keep going as long as there are prefixes available */
  49.     for ( i = 0; have_pfx ; ++i ) {
  50.  
  51.         /* Get next byte. */
  52.         inp_next(u);
  53.         if ( u->error )
  54.             return -1;
  55.         curr = inp_curr( u );
  56.  
  57.         /* rex prefixes in 64bit mode */
  58.         if ( u->dis_mode == 64 && ( curr & 0xF0 ) == 0x40 ) {
  59.             u->pfx_rex = curr;  
  60.         } else {
  61.             switch ( curr )  
  62.             {
  63.             case 0x2E :
  64.                 u->pfx_seg = UD_R_CS;
  65.                 u->pfx_rex = 0;
  66.                 break;
  67.             case 0x36 :    
  68.                 u->pfx_seg = UD_R_SS;
  69.                 u->pfx_rex = 0;
  70.                 break;
  71.             case 0x3E :
  72.                 u->pfx_seg = UD_R_DS;
  73.                 u->pfx_rex = 0;
  74.                 break;
  75.             case 0x26 :
  76.                 u->pfx_seg = UD_R_ES;
  77.                 u->pfx_rex = 0;
  78.                 break;
  79.             case 0x64 :
  80.                 u->pfx_seg = UD_R_FS;
  81.                 u->pfx_rex = 0;
  82.                 break;
  83.             case 0x65 :
  84.                 u->pfx_seg = UD_R_GS;
  85.                 u->pfx_rex = 0;
  86.                 break;
  87.             case 0x67 : /* adress-size override prefix */
  88.                 u->pfx_adr = 0x67;
  89.                 u->pfx_rex = 0;
  90.                 break;
  91.             case 0xF0 :
  92.                 u->pfx_lock = 0xF0;
  93.                 u->pfx_rex  = 0;
  94.                 break;
  95.             case 0x66:
  96.                 /* the 0x66 sse prefix is only effective if no other sse prefix
  97.                  * has already been specified.
  98.                  */
  99.                 if ( !u->pfx_insn ) u->pfx_insn = 0x66;
  100.                 u->pfx_opr = 0x66;          
  101.                 u->pfx_rex = 0;
  102.                 break;
  103.             case 0xF2:
  104.                 u->pfx_insn  = 0xF2;
  105.                 u->pfx_repne = 0xF2;
  106.                 u->pfx_rex   = 0;
  107.                 break;
  108.             case 0xF3:
  109.                 u->pfx_insn = 0xF3;
  110.                 u->pfx_rep  = 0xF3;
  111.                 u->pfx_repe = 0xF3;
  112.                 u->pfx_rex  = 0;
  113.                 break;
  114.             default :
  115.                 /* No more prefixes */
  116.                 have_pfx = 0;
  117.                 break;
  118.             }
  119.         }
  120.  
  121.         /* check if we reached max instruction length */
  122.         if ( i + 1 == MAX_INSN_LENGTH ) {
  123.             u->error = 1;
  124.             break;
  125.         }
  126.     }
  127.  
  128.     /* return status */
  129.     if ( u->error )
  130.         return -1;
  131.  
  132.     /* rewind back one byte in stream, since the above loop
  133.      * stops with a non-prefix byte.
  134.      */
  135.     inp_back(u);
  136.  
  137.     /* speculatively determine the effective operand mode,
  138.      * based on the prefixes and the current disassembly
  139.      * mode. This may be inaccurate, but useful for mode
  140.      * dependent decoding.
  141.      */
  142.     if ( u->dis_mode == 64 ) {
  143.         u->opr_mode = REX_W( u->pfx_rex ) ? 64 : ( ( u->pfx_opr ) ? 16 : 32 ) ;
  144.         u->adr_mode = ( u->pfx_adr ) ? 32 : 64;
  145.     } else if ( u->dis_mode == 32 ) {
  146.         u->opr_mode = ( u->pfx_opr ) ? 16 : 32;
  147.         u->adr_mode = ( u->pfx_adr ) ? 16 : 32;
  148.     } else if ( u->dis_mode == 16 ) {
  149.         u->opr_mode = ( u->pfx_opr ) ? 32 : 16;
  150.         u->adr_mode = ( u->pfx_adr ) ? 32 : 16;
  151.     }
  152.  
  153.     return 0;
  154. }
  155.  
  156.  
  157. /* Searches the instruction tables for the right entry.
  158.  */
  159. static int search_itab( struct ud * u )
  160. {
  161.     struct ud_itab_entry * e = NULL;
  162.     enum ud_itab_index table;
  163.     uint8_t peek;
  164.     uint8_t did_peek = 0;
  165.     uint8_t curr;
  166.     uint8_t index;
  167.  
  168.     /* if in state of error, return */
  169.     if ( u->error )
  170.         return -1;
  171.  
  172.     /* get first byte of opcode. */
  173.     inp_next(u);
  174.     if ( u->error )
  175.         return -1;
  176.     curr = inp_curr(u);
  177.  
  178.     /* resolve xchg, nop, pause crazyness */
  179.     if ( 0x90 == curr ) {
  180.         if ( !( u->dis_mode == 64 && REX_B( u->pfx_rex ) ) ) {
  181.             if ( u->pfx_rep ) {
  182.                 u->pfx_rep = 0;
  183.                 e = & ie_pause;
  184.             } else {
  185.                 e = & ie_nop;
  186.             }
  187.             goto found_entry;
  188.         }
  189.     }
  190.  
  191.     /* get top-level table */
  192.     if ( 0x0F == curr ) {
  193.         table = ITAB__0F;
  194.         curr  = inp_next(u);
  195.         if ( u->error )
  196.             return -1;
  197.  
  198.         /* 2byte opcodes can be modified by 0x66, F3, and F2 prefixes */
  199.         if ( 0x66 == u->pfx_insn ) {
  200.             if ( ud_itab_list[ ITAB__PFX_SSE66__0F ][ curr ].mnemonic != UD_Iinvalid ) {
  201.                 table = ITAB__PFX_SSE66__0F;
  202.                 u->pfx_opr = 0;
  203.             }
  204.         } else if ( 0xF2 == u->pfx_insn ) {
  205.             if ( ud_itab_list[ ITAB__PFX_SSEF2__0F ][ curr ].mnemonic != UD_Iinvalid ) {
  206.                 table = ITAB__PFX_SSEF2__0F;
  207.                 u->pfx_repne = 0;
  208.             }
  209.         } else if ( 0xF3 == u->pfx_insn ) {
  210.             if ( ud_itab_list[ ITAB__PFX_SSEF3__0F ][ curr ].mnemonic != UD_Iinvalid ) {
  211.                 table = ITAB__PFX_SSEF3__0F;
  212.                 u->pfx_repe = 0;
  213.                 u->pfx_rep  = 0;
  214.             }
  215.         }
  216.     /* pick an instruction from the 1byte table */
  217.     } else {
  218.         table = ITAB__1BYTE;
  219.     }
  220.  
  221.     index = curr;
  222.  
  223. search:
  224.  
  225.     e = & ud_itab_list[ table ][ index ];
  226.  
  227.     /* if mnemonic constant is a standard instruction constant
  228.      * our search is over.
  229.      */
  230.    
  231.     if ( e->mnemonic < UD_Id3vil ) {
  232.         if ( e->mnemonic == UD_Iinvalid ) {
  233.             if ( did_peek ) {
  234.                 inp_next( u ); if ( u->error ) return -1;
  235.             }
  236.             goto found_entry;
  237.         }
  238.         goto found_entry;
  239.     }
  240.  
  241.     table = e->prefix;
  242.  
  243.     switch ( e->mnemonic )
  244.     {
  245.     case UD_Igrp_reg:
  246.         peek     = inp_peek( u );
  247.         did_peek = 1;
  248.         index    = MODRM_REG( peek );
  249.         break;
  250.  
  251.     case UD_Igrp_mod:
  252.         peek     = inp_peek( u );
  253.         did_peek = 1;
  254.         index    = MODRM_MOD( peek );
  255.         if ( index == 3 )
  256.            index = ITAB__MOD_INDX__11;
  257.         else
  258.            index = ITAB__MOD_INDX__NOT_11;
  259.         break;
  260.  
  261.     case UD_Igrp_rm:
  262.         curr     = inp_next( u );
  263.         did_peek = 0;
  264.         if ( u->error )
  265.             return -1;
  266.         index    = MODRM_RM( curr );
  267.         break;
  268.  
  269.     case UD_Igrp_x87:
  270.         curr     = inp_next( u );
  271.         did_peek = 0;
  272.         if ( u->error )
  273.             return -1;
  274.         index    = curr - 0xC0;
  275.         break;
  276.  
  277.     case UD_Igrp_osize:
  278.         if ( u->opr_mode == 64 )
  279.             index = ITAB__MODE_INDX__64;
  280.         else if ( u->opr_mode == 32 )
  281.             index = ITAB__MODE_INDX__32;
  282.         else
  283.             index = ITAB__MODE_INDX__16;
  284.         break;
  285.  
  286.     case UD_Igrp_asize:
  287.         if ( u->adr_mode == 64 )
  288.             index = ITAB__MODE_INDX__64;
  289.         else if ( u->adr_mode == 32 )
  290.             index = ITAB__MODE_INDX__32;
  291.         else
  292.             index = ITAB__MODE_INDX__16;
  293.         break;              
  294.  
  295.     case UD_Igrp_mode:
  296.         if ( u->dis_mode == 64 )
  297.             index = ITAB__MODE_INDX__64;
  298.         else if ( u->dis_mode == 32 )
  299.             index = ITAB__MODE_INDX__32;
  300.         else
  301.             index = ITAB__MODE_INDX__16;
  302.         break;
  303.  
  304.     case UD_Igrp_vendor:
  305.         if ( u->vendor == UD_VENDOR_INTEL )
  306.             index = ITAB__VENDOR_INDX__INTEL;
  307.         else if ( u->vendor == UD_VENDOR_AMD )
  308.             index = ITAB__VENDOR_INDX__AMD;
  309.         else
  310.             assert( !"unrecognized vendor id" );
  311.         break;
  312.  
  313.     case UD_Id3vil:
  314.         assert( !"invalid instruction mnemonic constant Id3vil" );
  315.         break;
  316.  
  317.     default:
  318.         assert( !"invalid instruction mnemonic constant" );
  319.         break;
  320.     }
  321.  
  322.     goto search;
  323.  
  324. found_entry:
  325.  
  326.     u->itab_entry = e;
  327.     u->mnemonic = u->itab_entry->mnemonic;
  328.  
  329.     return 0;
  330. }
  331.  
  332.  
  333. static unsigned int resolve_operand_size( const struct ud * u, unsigned int s )
  334. {
  335.     switch ( s )
  336.     {
  337.     case SZ_V:
  338.         return ( u->opr_mode );
  339.     case SZ_Z:  
  340.         return ( u->opr_mode == 16 ) ? 16 : 32;
  341.     case SZ_P:  
  342.         return ( u->opr_mode == 16 ) ? SZ_WP : SZ_DP;
  343.     case SZ_MDQ:
  344.         return ( u->opr_mode == 16 ) ? 32 : u->opr_mode;
  345.     case SZ_RDQ:
  346.         return ( u->dis_mode == 64 ) ? 64 : 32;
  347.     default:
  348.         return s;
  349.     }
  350. }
  351.  
  352.  
  353. static int resolve_mnemonic( struct ud* u )
  354. {
  355.   /* far/near flags */
  356.   u->br_far = 0;
  357.   u->br_near = 0;
  358.   /* readjust operand sizes for call/jmp instrcutions */
  359.   if ( u->mnemonic == UD_Icall || u->mnemonic == UD_Ijmp ) {
  360.     /* WP: 16bit pointer */
  361.     if ( u->operand[ 0 ].size == SZ_WP ) {
  362.         u->operand[ 0 ].size = 16;
  363.         u->br_far = 1;
  364.         u->br_near= 0;
  365.     /* DP: 32bit pointer */
  366.     } else if ( u->operand[ 0 ].size == SZ_DP ) {
  367.         u->operand[ 0 ].size = 32;
  368.         u->br_far = 1;
  369.         u->br_near= 0;
  370.     } else {
  371.         u->br_far = 0;
  372.         u->br_near= 1;
  373.     }
  374.   /* resolve 3dnow weirdness. */
  375.   } else if ( u->mnemonic == UD_I3dnow ) {
  376.     u->mnemonic = ud_itab_list[ ITAB__3DNOW ][ inp_curr( u )  ].mnemonic;
  377.   }
  378.   /* SWAPGS is only valid in 64bits mode */
  379.   if ( u->mnemonic == UD_Iswapgs && u->dis_mode != 64 ) {
  380.     u->error = 1;
  381.     return -1;
  382.   }
  383.  
  384.   return 0;
  385. }
  386.  
  387.  
  388. /* -----------------------------------------------------------------------------
  389.  * decode_a()- Decodes operands of the type seg:offset
  390.  * -----------------------------------------------------------------------------
  391.  */
  392. static void
  393. decode_a(struct ud* u, struct ud_operand *op)
  394. {
  395.   if (u->opr_mode == 16) {  
  396.     /* seg16:off16 */
  397.     op->type = UD_OP_PTR;
  398.     op->size = 32;
  399.     op->lval.ptr.off = inp_uint16(u);
  400.     op->lval.ptr.seg = inp_uint16(u);
  401.   } else {
  402.     /* seg16:off32 */
  403.     op->type = UD_OP_PTR;
  404.     op->size = 48;
  405.     op->lval.ptr.off = inp_uint32(u);
  406.     op->lval.ptr.seg = inp_uint16(u);
  407.   }
  408. }
  409.  
  410. /* -----------------------------------------------------------------------------
  411.  * decode_gpr() - Returns decoded General Purpose Register
  412.  * -----------------------------------------------------------------------------
  413.  */
  414. static enum ud_type
  415. decode_gpr(register struct ud* u, unsigned int s, unsigned char rm)
  416. {
  417.   s = resolve_operand_size(u, s);
  418.        
  419.   switch (s) {
  420.     case 64:
  421.         return UD_R_RAX + rm;
  422.     case SZ_DP:
  423.     case 32:
  424.         return UD_R_EAX + rm;
  425.     case SZ_WP:
  426.     case 16:
  427.         return UD_R_AX  + rm;
  428.     case  8:
  429.         if (u->dis_mode == 64 && u->pfx_rex) {
  430.             if (rm >= 4)
  431.                 return UD_R_SPL + (rm-4);
  432.             return UD_R_AL + rm;
  433.         } else return UD_R_AL + rm;
  434.     default:
  435.         return 0;
  436.   }
  437. }
  438.  
  439. /* -----------------------------------------------------------------------------
  440.  * resolve_gpr64() - 64bit General Purpose Register-Selection.
  441.  * -----------------------------------------------------------------------------
  442.  */
  443. static enum ud_type
  444. resolve_gpr64(struct ud* u, enum ud_operand_code gpr_op)
  445. {
  446.   if (gpr_op >= OP_rAXr8 && gpr_op <= OP_rDIr15)
  447.     gpr_op = (gpr_op - OP_rAXr8) | (REX_B(u->pfx_rex) << 3);          
  448.   else  gpr_op = (gpr_op - OP_rAX);
  449.  
  450.   if (u->opr_mode == 16)
  451.     return gpr_op + UD_R_AX;
  452.   if (u->dis_mode == 32 ||
  453.     (u->opr_mode == 32 && ! (REX_W(u->pfx_rex) || u->default64))) {
  454.     return gpr_op + UD_R_EAX;
  455.   }
  456.  
  457.   return gpr_op + UD_R_RAX;
  458. }
  459.  
  460. /* -----------------------------------------------------------------------------
  461.  * resolve_gpr32 () - 32bit General Purpose Register-Selection.
  462.  * -----------------------------------------------------------------------------
  463.  */
  464. static enum ud_type
  465. resolve_gpr32(struct ud* u, enum ud_operand_code gpr_op)
  466. {
  467.   gpr_op = gpr_op - OP_eAX;
  468.  
  469.   if (u->opr_mode == 16)
  470.     return gpr_op + UD_R_AX;
  471.  
  472.   return gpr_op +  UD_R_EAX;
  473. }
  474.  
  475. /* -----------------------------------------------------------------------------
  476.  * resolve_reg() - Resolves the register type
  477.  * -----------------------------------------------------------------------------
  478.  */
  479. static enum ud_type
  480. resolve_reg(struct ud* u, unsigned int type, unsigned char i)
  481. {
  482.   switch (type) {
  483.     case T_MMX :    return UD_R_MM0  + (i & 7);
  484.     case T_XMM :    return UD_R_XMM0 + i;
  485.     case T_CRG :    return UD_R_CR0  + i;
  486.     case T_DBG :    return UD_R_DR0  + i;
  487.     case T_SEG :    return UD_R_ES   + (i & 7);
  488.     case T_NONE:
  489.     default:    return UD_NONE;
  490.   }
  491. }
  492.  
  493. /* -----------------------------------------------------------------------------
  494.  * decode_imm() - Decodes Immediate values.
  495.  * -----------------------------------------------------------------------------
  496.  */
  497. static void
  498. decode_imm(struct ud* u, unsigned int s, struct ud_operand *op)
  499. {
  500.   op->size = resolve_operand_size(u, s);
  501.   op->type = UD_OP_IMM;
  502.  
  503.   switch (op->size) {
  504.     case  8: op->lval.sbyte = inp_uint8(u);   break;
  505.     case 16: op->lval.uword = inp_uint16(u);  break;
  506.     case 32: op->lval.udword = inp_uint32(u); break;
  507.     case 64: op->lval.uqword = inp_uint64(u); break;
  508.     default: return;
  509.   }
  510. }
  511.  
  512. /* -----------------------------------------------------------------------------
  513.  * decode_modrm() - Decodes ModRM Byte
  514.  * -----------------------------------------------------------------------------
  515.  */
  516. static void
  517. decode_modrm(struct ud* u, struct ud_operand *op, unsigned int s,
  518.          unsigned char rm_type, struct ud_operand *opreg,
  519.          unsigned char reg_size, unsigned char reg_type)
  520. {
  521.   unsigned char mod, rm, reg;
  522.  
  523.   inp_next(u);
  524.  
  525.   /* get mod, r/m and reg fields */
  526.   mod = MODRM_MOD(inp_curr(u));
  527.   rm  = (REX_B(u->pfx_rex) << 3) | MODRM_RM(inp_curr(u));
  528.   reg = (REX_R(u->pfx_rex) << 3) | MODRM_REG(inp_curr(u));
  529.  
  530.   op->size = resolve_operand_size(u, s);
  531.  
  532.   /* if mod is 11b, then the UD_R_m specifies a gpr/mmx/sse/control/debug */
  533.   if (mod == 3) {
  534.     op->type = UD_OP_REG;
  535.     if (rm_type ==  T_GPR)
  536.         op->base = decode_gpr(u, op->size, rm);
  537.     else    op->base = resolve_reg(u, rm_type, (REX_B(u->pfx_rex) << 3) | (rm&7));
  538.   }
  539.   /* else its memory addressing */  
  540.   else {
  541.     op->type = UD_OP_MEM;
  542.  
  543.     /* 64bit addressing */
  544.     if (u->adr_mode == 64) {
  545.  
  546.         op->base = UD_R_RAX + rm;
  547.  
  548.         /* get offset type */
  549.         if (mod == 1)
  550.             op->offset = 8;
  551.         else if (mod == 2)
  552.             op->offset = 32;
  553.         else if (mod == 0 && (rm & 7) == 5) {          
  554.             op->base = UD_R_RIP;
  555.             op->offset = 32;
  556.         } else  op->offset = 0;
  557.  
  558.         /* Scale-Index-Base (SIB) */
  559.         if ((rm & 7) == 4) {
  560.             inp_next(u);
  561.            
  562.             op->scale = (1 << SIB_S(inp_curr(u))) & ~1;
  563.             op->index = UD_R_RAX + (SIB_I(inp_curr(u)) | (REX_X(u->pfx_rex) << 3));
  564.             op->base  = UD_R_RAX + (SIB_B(inp_curr(u)) | (REX_B(u->pfx_rex) << 3));
  565.  
  566.             /* special conditions for base reference */
  567.             if (op->index == UD_R_RSP) {
  568.                 op->index = UD_NONE;
  569.                 op->scale = UD_NONE;
  570.             }
  571.  
  572.             if (op->base == UD_R_RBP || op->base == UD_R_R13) {
  573.                 if (mod == 0)
  574.                     op->base = UD_NONE;
  575.                 if (mod == 1)
  576.                     op->offset = 8;
  577.                 else op->offset = 32;
  578.             }
  579.         }
  580.     }
  581.  
  582.     /* 32-Bit addressing mode */
  583.     else if (u->adr_mode == 32) {
  584.  
  585.         /* get base */
  586.         op->base = UD_R_EAX + rm;
  587.  
  588.         /* get offset type */
  589.         if (mod == 1)
  590.             op->offset = 8;
  591.         else if (mod == 2)
  592.             op->offset = 32;
  593.         else if (mod == 0 && rm == 5) {
  594.             op->base = UD_NONE;
  595.             op->offset = 32;
  596.         } else  op->offset = 0;
  597.  
  598.         /* Scale-Index-Base (SIB) */
  599.         if ((rm & 7) == 4) {
  600.             inp_next(u);
  601.  
  602.             op->scale = (1 << SIB_S(inp_curr(u))) & ~1;
  603.             op->index = UD_R_EAX + (SIB_I(inp_curr(u)) | (REX_X(u->pfx_rex) << 3));
  604.             op->base  = UD_R_EAX + (SIB_B(inp_curr(u)) | (REX_B(u->pfx_rex) << 3));
  605.  
  606.             if (op->index == UD_R_ESP) {
  607.                 op->index = UD_NONE;
  608.                 op->scale = UD_NONE;
  609.             }
  610.  
  611.             /* special condition for base reference */
  612.             if (op->base == UD_R_EBP) {
  613.                 if (mod == 0)
  614.                     op->base = UD_NONE;
  615.                 if (mod == 1)
  616.                     op->offset = 8;
  617.                 else op->offset = 32;
  618.             }
  619.         }
  620.     }
  621.  
  622.     /* 16bit addressing mode */
  623.     else  {
  624.         switch (rm) {
  625.             case 0: op->base = UD_R_BX; op->index = UD_R_SI; break;
  626.             case 1: op->base = UD_R_BX; op->index = UD_R_DI; break;
  627.             case 2: op->base = UD_R_BP; op->index = UD_R_SI; break;
  628.             case 3: op->base = UD_R_BP; op->index = UD_R_DI; break;
  629.             case 4: op->base = UD_R_SI; break;
  630.             case 5: op->base = UD_R_DI; break;
  631.             case 6: op->base = UD_R_BP; break;
  632.             case 7: op->base = UD_R_BX; break;
  633.         }
  634.  
  635.         if (mod == 0 && rm == 6) {
  636.             op->offset= 16;
  637.             op->base = UD_NONE;
  638.         }
  639.         else if (mod == 1)
  640.             op->offset = 8;
  641.         else if (mod == 2)
  642.             op->offset = 16;
  643.     }
  644.   }  
  645.  
  646.   /* extract offset, if any */
  647.   switch(op->offset) {
  648.     case 8 : op->lval.ubyte  = inp_uint8(u);  break;
  649.     case 16: op->lval.uword  = inp_uint16(u);  break;
  650.     case 32: op->lval.udword = inp_uint32(u); break;
  651.     case 64: op->lval.uqword = inp_uint64(u); break;
  652.     default: break;
  653.   }
  654.  
  655.   /* resolve register encoded in reg field */
  656.   if (opreg) {
  657.     opreg->type = UD_OP_REG;
  658.     opreg->size = resolve_operand_size(u, reg_size);
  659.     if (reg_type == T_GPR)
  660.         opreg->base = decode_gpr(u, opreg->size, reg);
  661.     else opreg->base = resolve_reg(u, reg_type, reg);
  662.   }
  663. }
  664.  
  665. /* -----------------------------------------------------------------------------
  666.  * decode_o() - Decodes offset
  667.  * -----------------------------------------------------------------------------
  668.  */
  669. static void
  670. decode_o(struct ud* u, unsigned int s, struct ud_operand *op)
  671. {
  672.   switch (u->adr_mode) {
  673.     case 64:
  674.         op->offset = 64;
  675.         op->lval.uqword = inp_uint64(u);
  676.         break;
  677.     case 32:
  678.         op->offset = 32;
  679.         op->lval.udword = inp_uint32(u);
  680.         break;
  681.     case 16:
  682.         op->offset = 16;
  683.         op->lval.uword  = inp_uint16(u);
  684.         break;
  685.     default:
  686.         return;
  687.   }
  688.   op->type = UD_OP_MEM;
  689.   op->size = resolve_operand_size(u, s);
  690. }
  691.  
  692. /* -----------------------------------------------------------------------------
  693.  * disasm_operands() - Disassembles Operands.
  694.  * -----------------------------------------------------------------------------
  695.  */
  696. static int disasm_operands(register struct ud* u)
  697. {
  698.  
  699.  
  700.   /* mopXt = map entry, operand X, type; */
  701.   enum ud_operand_code mop1t = u->itab_entry->operand1.type;
  702.   enum ud_operand_code mop2t = u->itab_entry->operand2.type;
  703.   enum ud_operand_code mop3t = u->itab_entry->operand3.type;
  704.  
  705.   /* mopXs = map entry, operand X, size */
  706.   unsigned int mop1s = u->itab_entry->operand1.size;
  707.   unsigned int mop2s = u->itab_entry->operand2.size;
  708.   unsigned int mop3s = u->itab_entry->operand3.size;
  709.  
  710.   /* iop = instruction operand */
  711.   register struct ud_operand* iop = u->operand;
  712.    
  713.   switch(mop1t) {
  714.    
  715.     case OP_A :
  716.         decode_a(u, &(iop[0]));
  717.         break;
  718.    
  719.     /* M[b] ... */
  720.     case OP_M :
  721.         if (MODRM_MOD(inp_peek(u)) == 3)
  722.             u->error= 1;
  723.     /* E, G/P/V/I/CL/1/S */
  724.     case OP_E :
  725.         if (mop2t == OP_G) {
  726.             decode_modrm(u, &(iop[0]), mop1s, T_GPR, &(iop[1]), mop2s, T_GPR);
  727.             if (mop3t == OP_I)
  728.                 decode_imm(u, mop3s, &(iop[2]));
  729.             else if (mop3t == OP_CL) {
  730.                 iop[2].type = UD_OP_REG;
  731.                 iop[2].base = UD_R_CL;
  732.                 iop[2].size = 8;
  733.             }
  734.         }
  735.         else if (mop2t == OP_P)
  736.             decode_modrm(u, &(iop[0]), mop1s, T_GPR, &(iop[1]), mop2s, T_MMX);
  737.         else if (mop2t == OP_V)
  738.             decode_modrm(u, &(iop[0]), mop1s, T_GPR, &(iop[1]), mop2s, T_XMM);
  739.         else if (mop2t == OP_S)
  740.             decode_modrm(u, &(iop[0]), mop1s, T_GPR, &(iop[1]), mop2s, T_SEG);
  741.         else {
  742.             decode_modrm(u, &(iop[0]), mop1s, T_GPR, NULL, 0, T_NONE);
  743.             if (mop2t == OP_CL) {
  744.                 iop[1].type = UD_OP_REG;
  745.                 iop[1].base = UD_R_CL;
  746.                 iop[1].size = 8;
  747.             } else if (mop2t == OP_I1) {
  748.                 iop[1].type = UD_OP_CONST;
  749.                 u->operand[1].lval.udword = 1;
  750.             } else if (mop2t == OP_I) {
  751.                 decode_imm(u, mop2s, &(iop[1]));
  752.             }
  753.         }
  754.         break;
  755.  
  756.     /* G, E/PR[,I]/VR */
  757.     case OP_G :
  758.         if (mop2t == OP_M) {
  759.             if (MODRM_MOD(inp_peek(u)) == 3)
  760.                 u->error= 1;
  761.             decode_modrm(u, &(iop[1]), mop2s, T_GPR, &(iop[0]), mop1s, T_GPR);
  762.         } else if (mop2t == OP_E) {
  763.             decode_modrm(u, &(iop[1]), mop2s, T_GPR, &(iop[0]), mop1s, T_GPR);
  764.             if (mop3t == OP_I)
  765.                 decode_imm(u, mop3s, &(iop[2]));
  766.         } else if (mop2t == OP_PR) {
  767.             decode_modrm(u, &(iop[1]), mop2s, T_MMX, &(iop[0]), mop1s, T_GPR);
  768.             if (mop3t == OP_I)
  769.                 decode_imm(u, mop3s, &(iop[2]));
  770.         } else if (mop2t == OP_VR) {
  771.             if (MODRM_MOD(inp_peek(u)) != 3)
  772.                 u->error = 1;
  773.             decode_modrm(u, &(iop[1]), mop2s, T_XMM, &(iop[0]), mop1s, T_GPR);
  774.         } else if (mop2t == OP_W)
  775.             decode_modrm(u, &(iop[1]), mop2s, T_XMM, &(iop[0]), mop1s, T_GPR);
  776.         break;
  777.  
  778.     /* AL..BH, I/O/DX */
  779.     case OP_AL : case OP_CL : case OP_DL : case OP_BL :
  780.     case OP_AH : case OP_CH : case OP_DH : case OP_BH :
  781.  
  782.         iop[0].type = UD_OP_REG;
  783.         iop[0].base = UD_R_AL + (mop1t - OP_AL);
  784.         iop[0].size = 8;
  785.  
  786.         if (mop2t == OP_I)
  787.             decode_imm(u, mop2s, &(iop[1]));
  788.         else if (mop2t == OP_DX) {
  789.             iop[1].type = UD_OP_REG;
  790.             iop[1].base = UD_R_DX;
  791.             iop[1].size = 16;
  792.         }
  793.         else if (mop2t == OP_O)
  794.             decode_o(u, mop2s, &(iop[1]));
  795.         break;
  796.  
  797.     /* rAX[r8]..rDI[r15], I/rAX..rDI/O */
  798.     case OP_rAXr8 : case OP_rCXr9 : case OP_rDXr10 : case OP_rBXr11 :
  799.     case OP_rSPr12: case OP_rBPr13: case OP_rSIr14 : case OP_rDIr15 :
  800.     case OP_rAX : case OP_rCX : case OP_rDX : case OP_rBX :
  801.     case OP_rSP : case OP_rBP : case OP_rSI : case OP_rDI :
  802.  
  803.         iop[0].type = UD_OP_REG;
  804.         iop[0].base = resolve_gpr64(u, mop1t);
  805.  
  806.         if (mop2t == OP_I)
  807.             decode_imm(u, mop2s, &(iop[1]));
  808.         else if (mop2t >= OP_rAX && mop2t <= OP_rDI) {
  809.             iop[1].type = UD_OP_REG;
  810.             iop[1].base = resolve_gpr64(u, mop2t);
  811.         }
  812.         else if (mop2t == OP_O) {
  813.             decode_o(u, mop2s, &(iop[1]));  
  814.             iop[0].size = resolve_operand_size(u, mop2s);
  815.         }
  816.         break;
  817.  
  818.     /* AL[r8b]..BH[r15b], I */
  819.     case OP_ALr8b : case OP_CLr9b : case OP_DLr10b : case OP_BLr11b :
  820.     case OP_AHr12b: case OP_CHr13b: case OP_DHr14b : case OP_BHr15b :
  821.     {
  822.         ud_type_t gpr = (mop1t - OP_ALr8b) + UD_R_AL +
  823.                         (REX_B(u->pfx_rex) << 3);
  824.         if (UD_R_AH <= gpr && u->pfx_rex)
  825.             gpr = gpr + 4;
  826.         iop[0].type = UD_OP_REG;
  827.         iop[0].base = gpr;
  828.         if (mop2t == OP_I)
  829.             decode_imm(u, mop2s, &(iop[1]));
  830.         break;
  831.     }
  832.  
  833.     /* eAX..eDX, DX/I */
  834.     case OP_eAX : case OP_eCX : case OP_eDX : case OP_eBX :
  835.     case OP_eSP : case OP_eBP : case OP_eSI : case OP_eDI :
  836.         iop[0].type = UD_OP_REG;
  837.         iop[0].base = resolve_gpr32(u, mop1t);
  838.         if (mop2t == OP_DX) {
  839.             iop[1].type = UD_OP_REG;
  840.             iop[1].base = UD_R_DX;
  841.             iop[1].size = 16;
  842.         } else if (mop2t == OP_I)
  843.             decode_imm(u, mop2s, &(iop[1]));
  844.         break;
  845.  
  846.     /* ES..GS */
  847.     case OP_ES : case OP_CS : case OP_DS :
  848.     case OP_SS : case OP_FS : case OP_GS :
  849.  
  850.         /* in 64bits mode, only fs and gs are allowed */
  851.         if (u->dis_mode == 64)
  852.             if (mop1t != OP_FS && mop1t != OP_GS)
  853.                 u->error= 1;
  854.         iop[0].type = UD_OP_REG;
  855.         iop[0].base = (mop1t - OP_ES) + UD_R_ES;
  856.         iop[0].size = 16;
  857.  
  858.         break;
  859.  
  860.     /* J */
  861.     case OP_J :
  862.         decode_imm(u, mop1s, &(iop[0]));        
  863.         iop[0].type = UD_OP_JIMM;
  864.         break ;
  865.  
  866.     /* PR, I */
  867.     case OP_PR:
  868.         if (MODRM_MOD(inp_peek(u)) != 3)
  869.             u->error = 1;
  870.         decode_modrm(u, &(iop[0]), mop1s, T_MMX, NULL, 0, T_NONE);
  871.         if (mop2t == OP_I)
  872.             decode_imm(u, mop2s, &(iop[1]));
  873.         break;
  874.  
  875.     /* VR, I */
  876.     case OP_VR:
  877.         if (MODRM_MOD(inp_peek(u)) != 3)
  878.             u->error = 1;
  879.         decode_modrm(u, &(iop[0]), mop1s, T_XMM, NULL, 0, T_NONE);
  880.         if (mop2t == OP_I)
  881.             decode_imm(u, mop2s, &(iop[1]));
  882.         break;
  883.  
  884.     /* P, Q[,I]/W/E[,I],VR */
  885.     case OP_P :
  886.         if (mop2t == OP_Q) {
  887.             decode_modrm(u, &(iop[1]), mop2s, T_MMX, &(iop[0]), mop1s, T_MMX);
  888.             if (mop3t == OP_I)
  889.                 decode_imm(u, mop3s, &(iop[2]));
  890.         } else if (mop2t == OP_W) {
  891.             decode_modrm(u, &(iop[1]), mop2s, T_XMM, &(iop[0]), mop1s, T_MMX);
  892.         } else if (mop2t == OP_VR) {
  893.             if (MODRM_MOD(inp_peek(u)) != 3)
  894.                 u->error = 1;
  895.             decode_modrm(u, &(iop[1]), mop2s, T_XMM, &(iop[0]), mop1s, T_MMX);
  896.         } else if (mop2t == OP_E) {
  897.             decode_modrm(u, &(iop[1]), mop2s, T_GPR, &(iop[0]), mop1s, T_MMX);
  898.             if (mop3t == OP_I)
  899.                 decode_imm(u, mop3s, &(iop[2]));
  900.         }
  901.         break;
  902.  
  903.     /* R, C/D */
  904.     case OP_R :
  905.         if (mop2t == OP_C)
  906.             decode_modrm(u, &(iop[0]), mop1s, T_GPR, &(iop[1]), mop2s, T_CRG);
  907.         else if (mop2t == OP_D)
  908.             decode_modrm(u, &(iop[0]), mop1s, T_GPR, &(iop[1]), mop2s, T_DBG);
  909.         break;
  910.  
  911.     /* C, R */
  912.     case OP_C :
  913.         decode_modrm(u, &(iop[1]), mop2s, T_GPR, &(iop[0]), mop1s, T_CRG);
  914.         break;
  915.  
  916.     /* D, R */
  917.     case OP_D :
  918.         decode_modrm(u, &(iop[1]), mop2s, T_GPR, &(iop[0]), mop1s, T_DBG);
  919.         break;
  920.  
  921.     /* Q, P */
  922.     case OP_Q :
  923.         decode_modrm(u, &(iop[0]), mop1s, T_MMX, &(iop[1]), mop2s, T_MMX);
  924.         break;
  925.  
  926.     /* S, E */
  927.     case OP_S :
  928.         decode_modrm(u, &(iop[1]), mop2s, T_GPR, &(iop[0]), mop1s, T_SEG);
  929.         break;
  930.  
  931.     /* W, V */
  932.     case OP_W :
  933.         decode_modrm(u, &(iop[0]), mop1s, T_XMM, &(iop[1]), mop2s, T_XMM);
  934.         break;
  935.  
  936.     /* V, W[,I]/Q/M/E */
  937.     case OP_V :
  938.         if (mop2t == OP_W) {
  939.             /* special cases for movlps and movhps */
  940.             if (MODRM_MOD(inp_peek(u)) == 3) {
  941.                 if (u->mnemonic == UD_Imovlps)
  942.                     u->mnemonic = UD_Imovhlps;
  943.                 else
  944.                 if (u->mnemonic == UD_Imovhps)
  945.                     u->mnemonic = UD_Imovlhps;
  946.             }
  947.             decode_modrm(u, &(iop[1]), mop2s, T_XMM, &(iop[0]), mop1s, T_XMM);
  948.             if (mop3t == OP_I)
  949.                 decode_imm(u, mop3s, &(iop[2]));
  950.         } else if (mop2t == OP_Q)
  951.             decode_modrm(u, &(iop[1]), mop2s, T_MMX, &(iop[0]), mop1s, T_XMM);
  952.         else if (mop2t == OP_M) {
  953.             if (MODRM_MOD(inp_peek(u)) == 3)
  954.                 u->error= 1;
  955.             decode_modrm(u, &(iop[1]), mop2s, T_GPR, &(iop[0]), mop1s, T_XMM);
  956.         } else if (mop2t == OP_E) {
  957.             decode_modrm(u, &(iop[1]), mop2s, T_GPR, &(iop[0]), mop1s, T_XMM);
  958.         } else if (mop2t == OP_PR) {
  959.             decode_modrm(u, &(iop[1]), mop2s, T_MMX, &(iop[0]), mop1s, T_XMM);
  960.         }
  961.         break;
  962.  
  963.     /* DX, eAX/AL */
  964.     case OP_DX :
  965.         iop[0].type = UD_OP_REG;
  966.         iop[0].base = UD_R_DX;
  967.         iop[0].size = 16;
  968.  
  969.         if (mop2t == OP_eAX) {
  970.             iop[1].type = UD_OP_REG;    
  971.             iop[1].base = resolve_gpr32(u, mop2t);
  972.         } else if (mop2t == OP_AL) {
  973.             iop[1].type = UD_OP_REG;
  974.             iop[1].base = UD_R_AL;
  975.             iop[1].size = 8;
  976.         }
  977.  
  978.         break;
  979.  
  980.     /* I, I/AL/eAX */
  981.     case OP_I :
  982.         decode_imm(u, mop1s, &(iop[0]));
  983.         if (mop2t == OP_I)
  984.             decode_imm(u, mop2s, &(iop[1]));
  985.         else if (mop2t == OP_AL) {
  986.             iop[1].type = UD_OP_REG;
  987.             iop[1].base = UD_R_AL;
  988.             iop[1].size = 16;
  989.         } else if (mop2t == OP_eAX) {
  990.             iop[1].type = UD_OP_REG;    
  991.             iop[1].base = resolve_gpr32(u, mop2t);
  992.         }
  993.         break;
  994.  
  995.     /* O, AL/eAX */
  996.     case OP_O :
  997.         decode_o(u, mop1s, &(iop[0]));
  998.         iop[1].type = UD_OP_REG;
  999.         iop[1].size = resolve_operand_size(u, mop1s);
  1000.         if (mop2t == OP_AL)
  1001.             iop[1].base = UD_R_AL;
  1002.         else if (mop2t == OP_eAX)
  1003.             iop[1].base = resolve_gpr32(u, mop2t);
  1004.         else if (mop2t == OP_rAX)
  1005.             iop[1].base = resolve_gpr64(u, mop2t);      
  1006.         break;
  1007.  
  1008.     /* 3 */
  1009.     case OP_I3 :
  1010.         iop[0].type = UD_OP_CONST;
  1011.         iop[0].lval.sbyte = 3;
  1012.         break;
  1013.  
  1014.     /* ST(n), ST(n) */
  1015.     case OP_ST0 : case OP_ST1 : case OP_ST2 : case OP_ST3 :
  1016.     case OP_ST4 : case OP_ST5 : case OP_ST6 : case OP_ST7 :
  1017.  
  1018.         iop[0].type = UD_OP_REG;
  1019.         iop[0].base = (mop1t-OP_ST0) + UD_R_ST0;
  1020.         iop[0].size = 0;
  1021.  
  1022.         if (mop2t >= OP_ST0 && mop2t <= OP_ST7) {
  1023.             iop[1].type = UD_OP_REG;
  1024.             iop[1].base = (mop2t-OP_ST0) + UD_R_ST0;
  1025.             iop[1].size = 0;
  1026.         }
  1027.         break;
  1028.  
  1029.     /* AX */
  1030.     case OP_AX:
  1031.         iop[0].type = UD_OP_REG;
  1032.         iop[0].base = UD_R_AX;
  1033.         iop[0].size = 16;
  1034.         break;
  1035.  
  1036.     /* none */
  1037.     default :
  1038.         iop[0].type = iop[1].type = iop[2].type = UD_NONE;
  1039.   }
  1040.  
  1041.   return 0;
  1042. }
  1043.  
  1044. /* -----------------------------------------------------------------------------
  1045.  * clear_insn() - clear instruction pointer
  1046.  * -----------------------------------------------------------------------------
  1047.  */
  1048. static int clear_insn(register struct ud* u)
  1049. {
  1050.   u->error     = 0;
  1051.   u->pfx_seg   = 0;
  1052.   u->pfx_opr   = 0;
  1053.   u->pfx_adr   = 0;
  1054.   u->pfx_lock  = 0;
  1055.   u->pfx_repne = 0;
  1056.   u->pfx_rep   = 0;
  1057.   u->pfx_repe  = 0;
  1058.   u->pfx_seg   = 0;
  1059.   u->pfx_rex   = 0;
  1060.   u->pfx_insn  = 0;
  1061.   u->mnemonic  = UD_Inone;
  1062.   u->itab_entry = NULL;
  1063.  
  1064.   memset( &u->operand[ 0 ], 0, sizeof( struct ud_operand ) );
  1065.   memset( &u->operand[ 1 ], 0, sizeof( struct ud_operand ) );
  1066.   memset( &u->operand[ 2 ], 0, sizeof( struct ud_operand ) );
  1067.  
  1068.   return 0;
  1069. }
  1070.  
  1071. static int do_mode( struct ud* u )
  1072. {
  1073.   /* if in error state, bail out */
  1074.   if ( u->error ) return -1;
  1075.  
  1076.   /* propagate perfix effects */
  1077.   if ( u->dis_mode == 64 ) {  /* set 64bit-mode flags */
  1078.  
  1079.     /* Check validity of  instruction m64 */
  1080.     if ( P_INV64( u->itab_entry->prefix ) ) {
  1081.         u->error = 1;
  1082.         return -1;
  1083.     }
  1084.  
  1085.     /* effective rex prefix is the  effective mask for the
  1086.      * instruction hard-coded in the opcode map.
  1087.      */
  1088.     u->pfx_rex = ( u->pfx_rex & 0x40 ) |
  1089.                  ( u->pfx_rex & REX_PFX_MASK( u->itab_entry->prefix ) );
  1090.  
  1091.     /* whether this instruction has a default operand size of
  1092.      * 64bit, also hardcoded into the opcode map.
  1093.      */
  1094.     u->default64 = P_DEF64( u->itab_entry->prefix );
  1095.     /* calculate effective operand size */
  1096.     if ( REX_W( u->pfx_rex ) ) {
  1097.         u->opr_mode = 64;
  1098.     } else if ( u->pfx_opr ) {
  1099.         u->opr_mode = 16;
  1100.     } else {
  1101.         /* unless the default opr size of instruction is 64,
  1102.          * the effective operand size in the absence of rex.w
  1103.          * prefix is 32.
  1104.          */
  1105.         u->opr_mode = ( u->default64 ) ? 64 : 32;
  1106.     }
  1107.  
  1108.     /* calculate effective address size */
  1109.     u->adr_mode = (u->pfx_adr) ? 32 : 64;
  1110.   } else if ( u->dis_mode == 32 ) { /* set 32bit-mode flags */
  1111.     u->opr_mode = ( u->pfx_opr ) ? 16 : 32;
  1112.     u->adr_mode = ( u->pfx_adr ) ? 16 : 32;
  1113.   } else if ( u->dis_mode == 16 ) { /* set 16bit-mode flags */
  1114.     u->opr_mode = ( u->pfx_opr ) ? 32 : 16;
  1115.     u->adr_mode = ( u->pfx_adr ) ? 32 : 16;
  1116.   }
  1117.  
  1118.   /* These flags determine which operand to apply the operand size
  1119.    * cast to.
  1120.    */
  1121.   u->c1 = ( P_C1( u->itab_entry->prefix ) ) ? 1 : 0;
  1122.   u->c2 = ( P_C2( u->itab_entry->prefix ) ) ? 1 : 0;
  1123.   u->c3 = ( P_C3( u->itab_entry->prefix ) ) ? 1 : 0;
  1124.  
  1125.   /* set flags for implicit addressing */
  1126.   u->implicit_addr = P_IMPADDR( u->itab_entry->prefix );
  1127.  
  1128.   return 0;
  1129. }
  1130.  
  1131. static int gen_hex( struct ud *u )
  1132. {
  1133.   unsigned int i;
  1134.   unsigned char *src_ptr = inp_sess( u );
  1135.   char* src_hex;
  1136.  
  1137.   /* bail out if in error stat. */
  1138.   if ( u->error ) return -1;
  1139.   /* output buffer pointe */
  1140.   src_hex = ( char* ) u->insn_hexcode;
  1141.   /* for each byte used to decode instruction */
  1142.   for ( i = 0; i < u->inp_ctr; ++i, ++src_ptr) {
  1143.     sprintf( src_hex, "%02x", *src_ptr & 0xFF );
  1144.     src_hex += 2;
  1145.   }
  1146.   return 0;
  1147. }
  1148.  
  1149. /* =============================================================================
  1150.  * ud_decode() - Instruction decoder. Returns the number of bytes decoded.
  1151.  * =============================================================================
  1152.  */
  1153. unsigned int ud_decode( struct ud* u )
  1154. {
  1155.   inp_start(u);
  1156.  
  1157.   if ( clear_insn( u ) ) {
  1158.     ; /* error */
  1159.   } else if ( get_prefixes( u ) != 0 ) {
  1160.     ; /* error */
  1161.   } else if ( search_itab( u ) != 0 ) {
  1162.     ; /* error */
  1163.   } else if ( do_mode( u ) != 0 ) {
  1164.     ; /* error */
  1165.   } else if ( disasm_operands( u ) != 0 ) {
  1166.     ; /* error */
  1167.   } else if ( resolve_mnemonic( u ) != 0 ) {
  1168.     ; /* error */
  1169.   }
  1170.  
  1171.   /* Handle decode error. */
  1172.   if ( u->error ) {
  1173.     /* clear out the decode data. */
  1174.     clear_insn( u );
  1175.     /* mark the sequence of bytes as invalid. */
  1176.     u->itab_entry = & ie_invalid;
  1177.     u->mnemonic = u->itab_entry->mnemonic;
  1178.   }
  1179.  
  1180.   u->insn_offset = u->pc; /* set offset of instruction */
  1181.   u->insn_fill = 0;   /* set translation buffer index to 0 */
  1182.   u->pc += u->inp_ctr;    /* move program counter by bytes decoded */
  1183.   gen_hex( u );       /* generate hex code */
  1184.  
  1185.   /* return number of bytes disassembled. */
  1186.   return u->inp_ctr;
  1187. }
  1188.  
  1189. /* vim:cindent
  1190.  * vim:ts=4
  1191.  * vim:sw=4
  1192.  * vim:expandtab
  1193.  */
  1194.