home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / emulate / x_comp / 6800 / asmsrc / instlook.c < prev    next >
Text File  |  1988-10-14  |  3KB  |  115 lines

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