/* Problem 4--OOPS
   This looked harder than it was.  All you have to do is translate the
   hex codes into the commmands as described in the problem! */

#include <stdio.h>
#include <stdlib.h>

FILE *in, *out;

int main(int argc, char **argv);
void ProcessArg (void);

int main(int argc, char **argv) {

  char cstr[2];
  int args=0, i;

 in = fopen ("prob4.in","r");
 out = fopen ("prob4.out","w");
 while (fscanf (in,"%1s",cstr) != EOF) {
  switch (cstr[0]) { /* Process the commands */
   case '0': fprintf (out,"ADD "); args = 2;break;
   case '1': fprintf (out,"SUB "); args = 2;break;
   case '2': fprintf (out,"MUL "); args = 2;break;
   case '3': fprintf (out,"DIV "); args = 2;break;
   case '4': fprintf (out,"MOV "); args = 2;break;
   case '5': fprintf (out,"BREQ "); args = 1;break;
   case '6': fprintf (out,"BRLE "); args = 1;break;
   case '7': fprintf (out,"BRLS "); args = 1;break;
   case '8': fprintf (out,"BRGE "); args = 1;break;
   case '9': fprintf (out,"BRGR "); args = 1;break;
   case 'A': fprintf (out,"BRNE "); args = 1;break;
   case 'B': fprintf (out,"BR "); args = 1;break;
   case 'C': fprintf (out,"AND "); args = 3;break;
   case 'D': fprintf (out,"OR "); args = 3;break;
   case 'E': fprintf (out,"XOR "); args = 3;break;
   case 'F': fprintf (out,"NOT "); args = 1;break;
  }
  for (i=0;i < args;i++) { /* Process the arguments */
   ProcessArg ();
   fprintf (out,i==args-1?"\n":",");
  }
 }
 fclose (in);
 fclose (out);
 return EXIT_SUCCESS;
}

/* ProcessArg reads 4 bytes from the input file and prints out the
   appropriate value. */
void ProcessArg (void) {

  char arg[4],cstr[2];
  int i, code, a;

 for (i=0; i < 4; i++) {
  fscanf (in,"%1s",cstr); /* Convert to standard integer */
  arg[i] = cstr[0] <= '9' ? cstr[0]-'0' : cstr[0]-'A'+10;
 }
 code = arg[0]/4;
 a = (((arg[0]%4)*16+arg[1])*16+arg[2])*16+arg[3];
 switch (code) { /* Print appropriate code */
  case 0 : fprintf (out,"R");break;
  case 1 : fprintf (out,"$");break;
  case 2 : fprintf (out,"PC+");break;
 }
 fprintf (out,"%d",a);
}
