#include #include #include #define MAXBYTEADDR 65535 static unsigned char mem[MAXBYTEADDR]; /* * Return the numeric value represented by the hex digit c. */ struct assoc { char c; int v; }; struct assoc hexval[] = {{'0',0},{'1',1},{'2',2},{'3',3},{'4',4}, {'5',5},{'6',6},{'7',7},{'8',8},{'9',9}, {'a',10},{'b',11},{'c',12},{'d',13},{'e',14},{'f',15}, {'A',10},{'B',11},{'C',12},{'D',13},{'E',14},{'F',15}}; int getval (char c) { int i; for (i=0; i < sizeof(hexval); i++) if (hexval[i].c == c) return hexval[i].v; return 0; } /* * Return the integer representation of the four-character string of hex digits. */ int toInt (char in[]) { int k, val; for (val=0, k=0; k<4; k++) val = val*16 + getval(in[k]); return val; } int getop(unsigned int inst) { return (inst >> 12) & 0xF; } char *opcodes[] = {"add","or","xor","and","addi","rotr","st","ld","ldi", "uu","bneg","bz","jr","uu","uu","jmp"}; int main (int argc, char* argv[]) { FILE *in; char line[256]; int addr = 0; unsigned int inst; if (argc != 2) { fprintf (stderr, "wrong number of arguments (one expected)\n"); exit (1); } in = fopen (argv[1], "r"); if (in == NULL) { fprintf (stderr, "can't open %s\n", argv[1]); exit (1); } while (fgets(line, 256, in)) { inst = toInt(line); printf("%4x: %4X %s\n", addr,inst, opcodes[getop(inst)]); addr = addr+2; } }