⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tm.c

📁 一个简化的编译器
💻 C
📖 第 1 页 / 共 2 页
字号:
        }      iMem[loc].iop = op;      iMem[loc].iarg1 = arg1;      iMem[loc].iarg2 = arg2;      iMem[loc].iarg3 = arg3;    }  }  return TRUE;} /* readInstructions *//********************************************/STEPRESULT stepTM (void){ INSTRUCTION currentinstruction  ;  int pc  ;  int r,s,t,m  ;  int ok ;  pc = reg[PC_REG] ;  if ( (pc < 0) || (pc > IADDR_SIZE)  )      return srIMEM_ERR ;  reg[PC_REG] = pc + 1 ;  currentinstruction = iMem[ pc ] ;  switch (opClass(currentinstruction.iop) )  { case opclRR :    /***********************************/      r = currentinstruction.iarg1 ;      s = currentinstruction.iarg2 ;      t = currentinstruction.iarg3 ;      break;    case opclRM :    /***********************************/      r = currentinstruction.iarg1 ;      s = currentinstruction.iarg3 ;      m = currentinstruction.iarg2 + reg[s] ;      if ( (m < 0) || (m > DADDR_SIZE))         return srDMEM_ERR ;      break;    case opclRA :    /***********************************/      r = currentinstruction.iarg1 ;      s = currentinstruction.iarg3 ;      m = currentinstruction.iarg2 + reg[s] ;      break;  } /* case */  switch ( currentinstruction.iop)  { /* RR instructions */    case opHALT :    /***********************************/      printf("HALT: %1d,%1d,%1d\n",r,s,t);      return srHALT ;      /* break; */    case opIN :    /***********************************/      do      { printf("Enter value for IN instruction: ") ;        fflush (stdin);        fflush (stdout);        gets(in_Line);        lineLen = strlen(in_Line) ;        inCol = 0;        ok = getNum();        if ( ! ok ) printf ("Illegal value\n");        else reg[r] = num;      }      while (! ok);      break;    case opOUT :        printf ("OUT instruction prints: %d\n", reg[r] ) ;      break;    case opADD :  reg[r] = reg[s] + reg[t] ;  break;    case opSUB :  reg[r] = reg[s] - reg[t] ;  break;    case opMUL :  reg[r] = reg[s] * reg[t] ;  break;    case opDIV :    /***********************************/      if ( reg[t] != 0 ) reg[r] = reg[s] / reg[t];      else return srZERODIVIDE ;      break;    /*************** RM instructions ********************/    case opLD :    reg[r] = dMem[m] ;  break;    case opST :    dMem[m] = reg[r] ;  break;    /*************** RA instructions ********************/    case opLDA :    reg[r] = m ; break;    case opLDC :    reg[r] = currentinstruction.iarg2 ;   break;    case opJLT :    if ( reg[r] <  0 ) reg[PC_REG] = m ; break;    case opJLE :    if ( reg[r] <=  0 ) reg[PC_REG] = m ; break;    case opJGT :    if ( reg[r] >  0 ) reg[PC_REG] = m ; break;    case opJGE :    if ( reg[r] >=  0 ) reg[PC_REG] = m ; break;    case opJEQ :    if ( reg[r] == 0 ) reg[PC_REG] = m ; break;    case opJNE :    if ( reg[r] != 0 ) reg[PC_REG] = m ; break;    /* end of legal instructions */  } /* case */  return srOKAY ;} /* stepTM *//********************************************/int doCommand (void){ char cmd;  int stepcnt=0, i;  int printcnt;  int stepResult;  int regNo, loc;  do  { printf ("Enter command: ");    fflush (stdin);    fflush (stdout);    gets(in_Line);    lineLen = strlen(in_Line);    inCol = 0;  }  while (! getWord ());  cmd = word[0] ;  switch ( cmd )  { case 't' :    /***********************************/      traceflag = ! traceflag ;      printf("Tracing now ");      if ( traceflag ) printf("on.\n"); else printf("off.\n");      break;    case 'h' :    /***********************************/      printf("Commands are:\n");      printf("   s(tep <n>      "\             "Execute n (default 1) TM instructions\n");      printf("   g(o            "\             "Execute TM instructions until HALT\n");      printf("   r(egs          "\             "Print the contents of the registers\n");      printf("   i(Mem <b <n>>  "\             "Print n iMem locations starting at b\n");      printf("   d(Mem <b <n>>  "\             "Print n dMem locations starting at b\n");      printf("   t(race         "\             "Toggle instruction trace\n");      printf("   p(rint         "\             "Toggle print of total instructions executed"\             " ('go' only)\n");      printf("   c(lear         "\             "Reset simulator for new execution of program\n");      printf("   h(elp          "\             "Cause this list of commands to be printed\n");      printf("   q(uit          "\             "Terminate the simulation\n");      break;    case 'p' :    /***********************************/      icountflag = ! icountflag ;      printf("Printing instruction count now ");      if ( icountflag ) printf("on.\n"); else printf("off.\n");      break;    case 's' :    /***********************************/      if ( atEOL ())  stepcnt = 1;      else if ( getNum ())  stepcnt = abs(num);      else   printf("Step count?\n");      break;    case 'g' :   stepcnt = 1 ;     break;    case 'r' :    /***********************************/      for (i = 0; i < NO_REGS; i++)      { printf("%1d: %4d    ", i,reg[i]);        if ( (i % 4) == 3 ) printf ("\n");      }      break;    case 'i' :    /***********************************/      printcnt = 1 ;      if ( getNum ())      { iloc = num ;        if ( getNum ()) printcnt = num ;      }      if ( ! atEOL ())        printf ("Instruction locations?\n");      else      { while ((iloc >= 0) && (iloc < IADDR_SIZE)                && (printcnt > 0) )        { writeInstruction(iloc);          iloc++ ;          printcnt-- ;        }      }      break;    case 'd' :    /***********************************/      printcnt = 1 ;      if ( getNum  ())      { dloc = num ;        if ( getNum ()) printcnt = num ;      }      if ( ! atEOL ())        printf("Data locations?\n");      else      { while ((dloc >= 0) && (dloc < DADDR_SIZE)                  && (printcnt > 0))        { printf("%5d: %5d\n",dloc,dMem[dloc]);          dloc++;          printcnt--;        }      }      break;    case 'c' :    /***********************************/      iloc = 0;      dloc = 0;      stepcnt = 0;      for (regNo = 0;  regNo < NO_REGS ; regNo++)            reg[regNo] = 0 ;      dMem[0] = DADDR_SIZE - 1 ;      for (loc = 1 ; loc < DADDR_SIZE ; loc++)            dMem[loc] = 0 ;      break;    case 'q' : return FALSE;  /* break; */    default : printf("Command %c unknown.\n", cmd); break;  }  /* case */  stepResult = srOKAY;  if ( stepcnt > 0 )  { if ( cmd == 'g' )    { stepcnt = 0;      while (stepResult == srOKAY)      { iloc = reg[PC_REG] ;        if ( traceflag ) writeInstruction( iloc ) ;        stepResult = stepTM ();        stepcnt++;      }      if ( icountflag )        printf("Number of instructions executed = %d\n",stepcnt);    }    else    { while ((stepcnt > 0) && (stepResult == srOKAY))      { iloc = reg[PC_REG] ;        if ( traceflag ) writeInstruction( iloc ) ;        stepResult = stepTM ();        stepcnt-- ;      }    }    printf( "%s\n",stepResultTab[stepResult] );  }  return TRUE;} /* doCommand *//********************************************//* E X E C U T I O N   B E G I N S   H E R E *//********************************************/main( int argc, char * argv[] ){ if (argc != 2)  { printf("usage: %s <filename>\n",argv[0]);    exit(1);  }  strcpy(pgmName,argv[1]) ;  if (strchr (pgmName, '.') == NULL)     strcat(pgmName,".tm");  pgm = fopen(pgmName,"r");  if (pgm == NULL)  { printf("file '%s' not found\n",pgmName);    exit(1);  }  /* read the program */  if ( ! readInstructions ())         exit(1) ;  /* switch input file to terminal */  /* reset( input ); */  /* read-eval-print */  printf("TM  simulation (enter h for help)...\n");  do     done = ! doCommand ();  while (! done );  printf("Simulation done.\n");  return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -