home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 363_01 / instlook.c < prev    next >
C/C++ Source or Header  |  1991-12-17  |  4KB  |  122 lines

  1. /***********************************************************************
  2.  *
  3.  *      INSTLOOKUP.C
  4.  *      Instruction Table Lookup Routine for 68020 Assembler
  5.  *
  6.  *    Function: instLookup()
  7.  *      Parses an instruction and looks it up in the instruction table. The 
  8.  *      input to the function is a pointer to the instruction on a line of 
  9.  *      assembly code. The routine scans the instruction and notes the size 
  10.  *      code if present. It then (binary) searches the instruction table for 
  11.  *      the specified opcode. If it finds the opcode, it returns a pointer to 
  12.  *      the instruction table entry for that instruction (via the instPtrPtr 
  13.  *      argument) as well as the size code or 0 if no size was specified (via 
  14.  *      the sizePtr argument). If the opcode is not in the instruction table, 
  15.  *      then the routine returns INV_OPCODE.
  16.  *
  17.  *      (which is not used - A.E.Romer)
  18.  *
  19.  *      The routine returns an error value via the standard mechanism. 
  20.  *
  21.  *   Usage: char *instLookup(char *p, instruction *(*instPtrPtr),
  22.  *                                          char *sizePtr, int *errorPtr)
  23.  *
  24.  *  Errors: SYNTAX
  25.  *          INV_OPCODE
  26.  *          INV_SIZE_CODE
  27.  *
  28.  *      Author: Paul McKee
  29.  *      ECE492    North Carolina State University,  9/24/86
  30.  *
  31.  *      Modified A.E. Romer. Version 1.0
  32.  *          16 March 1991   ANSI functions, braces layout.
  33.  *          29 May   1991   opcode string storage changed to allow numeric
  34.  *                          characters (for CAS2 etc.) 
  35.  *
  36.  ************************************************************************/
  37.  
  38.  
  39. #include <stdio.h>
  40. #include <ctype.h>
  41. #include "asm.h"
  42.  
  43. #define     INST_CHARS      8           /* size of instruction string store */
  44.  
  45. extern instruction instTable[];
  46. extern int tableSize;
  47.  
  48.  
  49. char *instLookup(char *p, instruction *(*instPtrPtr), char *sizePtr,
  50.                                                             int *errorPtr)
  51.     {
  52.     char opcode[INST_CHARS];
  53.     int i, hi, lo, mid, cmp;
  54.  
  55. /*  printf("InstLookup: Input string is \"%s\"\n", p); */
  56.  
  57.     i = 0;
  58.     do
  59.         {
  60.         if (i < INST_CHARS - 1)
  61.             opcode[i++] = *p;
  62.         p++;
  63.         } while (isalnum(*p));  /* store opcode string in opcode[] */
  64.  
  65.     opcode[i] = '\0';
  66.     if (*p == '.')
  67.         if (isspace(p[2]) || p[2] == '\0')     /* end of instruction string */
  68.             {
  69.             if (p[1] == 'B')
  70.                 *sizePtr = BYTE;
  71.             else if (p[1] == 'W')
  72.                 *sizePtr = WORD;
  73.             else if (p[1] == 'L')
  74.                 *sizePtr = LONG;
  75.             else if (p[1] == 'S')
  76.                 *sizePtr = SHORT;
  77.             else
  78.                 {
  79.                 *sizePtr = 0;
  80.                 NEWERROR(*errorPtr, INV_SIZE_CODE);
  81.                 }
  82.             p += 2;
  83.             }
  84.         else
  85.             {
  86.             NEWERROR(*errorPtr, SYNTAX);
  87.             return NULL;
  88.             }
  89.     else if (!isspace(*p) && *p != '\0')
  90.         {
  91.         NEWERROR(*errorPtr, SYNTAX);
  92.         return NULL;
  93.         }
  94.     else
  95.         *sizePtr = 0;
  96.  
  97.     lo = 0;
  98.     hi = tableSize - 1;
  99.     do
  100.         {
  101.         mid = (hi + lo) / 2;
  102.         cmp = strcmp(opcode, instTable[mid].mnemonic);
  103.         if (cmp > 0)
  104.             lo = mid + 1;
  105.         else if (cmp < 0)
  106.             hi = mid - 1;
  107.         } while (cmp && (hi >= lo));
  108.     if (cmp == 0)                       /* match */
  109.         {
  110.         *instPtrPtr = &instTable[mid];
  111.         return p;
  112.         }
  113.     else
  114.         {
  115.         NEWERROR(*errorPtr, INV_OPCODE);
  116.         return NULL;
  117.         }
  118.  
  119.     return NORMAL;                /* can the program ever reach this point? */
  120.     }
  121.  
  122.