/*  test to see if fields work */

#include "libc.h"

struct lst {
	char name[5];
	unsigned val;
}
olist[] = {
	{"cjp",3 },
	{"cjpp", 11 },
	{"cjs",  1 },
	{"cjv", 6 },
	{"cont", 14 },
	{"crtn", 10 },
	{"equ", 0 },
	{"jmap" , 2 },
	{"jrp", 7 },
	{"jsrp", 5 },
	{"jz" , 0 },
	{"ldct", 12 },
	{"loop", 13 },
	{"org", 1 },
	{"push", 4 },
	{"rfct", 8 },
	{"rpct", 9 },
	{"twb", 15 },
};

#define OLSIZE (sizeof(olist) / sizeof(struct lst))

main()
{
    char *buf[50];
    struct lst *binary(), ;
    int p;

    while(gets(buf)){
	if((strcmp(buf,"end") == 0))
	    break;
	if((p = binary(buf, olist, OLSIZE)) == -1)
	    printf("%s not found\n",buf);
  	else
	    printf("%s     op = %d\n",buf,olist[p].val);
    }
}

binary(word,tab,n)
char *word;
struct lst tab[];
int n;
{
    int cond,low,high,mid;

    low = 0;
    high = n-1;
    while (low <= high) {
	mid = (low+high) / 2;
	if ((cond =  strcmp(word,&tab[mid].name)) < 0)
	    high = mid - 1;
	else if (cond > 0)
	    low = mid + 1;
	else
	    return(mid);
	}
	return(-1);
}
