/*
 * This subroutine attempts to find the specified opcode. If it finds it,
 * it returns 1 (TRUE) and the pointer to the array of strings that define
 * each addressing mode for that opcode. Otherwise, it returns 0 (FALSE).
 *
 * $Log: findop.c,v $
 * Revision 1.2  1997/06/04 01:09:30  david
 * ANSI-fied the program.
 *
 */

#include <stdio.h>
#include "xasm.h"

static char *RcsId = "$Id: findop.c,v 1.2 1997/06/04 01:09:30 david Exp $";

extern struct table optable[];

/* ptr =  Pointer to pointer to strings of object code */
int findop( char *opcode, struct table **ptr )
{
static max = 0; /* Max. # of opcodes in table */
int Max, Mid, Min; /* Maximum, Middle, Minimum */
int first = 1; /* Set 'first' try flag */

if ( max == 0 )
   while ( optable[max].opname != NULL )
      max++; /* Bump pointer until end of table */

Max = max; Mid = 0; Min = 0;
do { /* while ( Min + 1 < Max ) */
   if ( first )
      {
      first = 0;
      if ( strcmp( optable[0].opname, opcode ) == 0 )
	 {
	 *ptr = &optable[Mid]; /* Return pointer */
	 return( 1 );
         }
      }
   Mid = ( Max + Min ) / 2;
   if ( strcmp( optable[Mid].opname, opcode ) > 0 )
      {
      Max = Mid;
      continue;
      }
   if ( strcmp( optable[Mid].opname, opcode ) < 0 )
      {
      Min = Mid;
      continue;
      }
   *ptr = &optable[Mid]; /* Return pointer to opcode entry */
   return( 1 ); /* Yep. We found it. */
   } while ( Min + 1 < Max );
return( 0 ); /* Nope. We didn't find it */
}
