home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / AS11.ZIP / AS.C next >
C/C++ Source or Header  |  1986-11-01  |  4KB  |  188 lines

  1. char mapdn();
  2. char *alloc();
  3. /*
  4.  *      as ---  cross assembler main program
  5.  */
  6. main(argc,argv)
  7. int     argc;
  8. char    **argv;
  9. {
  10.  char    **np;
  11.  FILE    *fopen();
  12.  
  13.  if(argc < 2){
  14.   printf("Usage: %s [files]\n",*argv);
  15.   exit(1);
  16.   }
  17.  Gargv = argv;    /* make names globally accessible */
  18.  N_files = argc-1;
  19.  initialize();
  20.  
  21.  Cfn = 0;
  22.  np = argv;
  23.  while( ++Cfn <= N_files )
  24.   if((Fd = fopen(*++np,"r")) == NULL)
  25.    printf("as: can't open %s\n",*np);
  26.   else{
  27.    Line_num = 0; /* reset line number */
  28.    make_pass();
  29.    fclose(Fd);
  30.   }
  31.  if( Err_count == 0 ){
  32.   Pass++;
  33.   re_init();
  34.   Cfn = 0;
  35.   np = argv;
  36.   while( ++Cfn <= N_files)
  37.    if((Fd = fopen(*++np,"r")) != NULL){
  38.     Line_num = 0;
  39.     make_pass();
  40.     fclose(Fd);
  41.     }
  42.   fprintf(Objfil,"S9030000FC\n"); /* at least give a decent ending */
  43.   }
  44.  printf("Errors: %d\n",Err_count);/* necessary for brain-damaged system */
  45.  exit(Err_count);
  46. }
  47.  
  48. initialize()
  49. {
  50.  FILE    *fopen();
  51.  
  52. #ifdef DEBUG
  53.  printf("Initializing\n");
  54. #endif
  55.  Err_count = 0;
  56.  Pc        = 0;
  57.  Pass      = 1;
  58.  Lflag     = 1;
  59.  Cflag     = 0;
  60.  Line[MAXBUF-1] = NEWLINE;       /* guard against garbage input */
  61.  
  62.  if( (Objfil = fopen(Obj_name,"w")) == NULL)
  63.   fatal("Can't create object file");
  64.  fwdinit();      /* forward ref init */
  65.  localinit();    /* target machine specific init. */
  66. }
  67.  
  68. re_init()
  69. {
  70. #ifdef DEBUG
  71.  printf("Reinitializing\n");
  72. #endif
  73.  Pc      = 0;
  74.  E_total = 0;
  75.  P_total = 0;
  76.  Lflag   = 1;
  77.  Cflag   = 0;
  78.  Ctotal  = 0;
  79.  fwdreinit();
  80. }
  81.  
  82. make_pass()
  83. {
  84. #ifdef DEBUG
  85.  printf("Pass %d\n",Pass);
  86. #endif
  87.  while( getaline() ){
  88.   P_force = 0;    /* No force unless bytes emitted */
  89.   if(parse_line())
  90.    process();
  91.   if(Pass == 2 && Lflag)
  92.    print_line();
  93.   P_total = 0;    /* reset byte count */
  94.   Cycles = 0;     /* and per instruction cycle count */
  95.   }
  96.  f_record();
  97. }
  98.  
  99. /*
  100.  *      getaline --- collect (possibly continued) an input line
  101.  */
  102. getaline()
  103. {
  104.  char *fgets();
  105.  register char *p = Line;
  106.  int remaining = MAXBUF-2;       /* space left in Line */
  107.  int len;                        /* line length */
  108.  
  109.  while( fgets(p,remaining,Fd) != (char *)NULL ){
  110.   Line_num++;
  111.   if((len = strlen(p)-2)<=0)
  112.    return(1);      /* just an empty line */
  113.   p += len;
  114.   if( *p != '\\' )
  115.    return(1);      /* not a continuation */
  116.   remaining -= len+2;
  117.   if(remaining<3)
  118.    warn("Continuation too long");
  119.   }
  120.  return(0);
  121. }
  122.  
  123. /*
  124.  *      parse_line --- split input line into label, op and operand
  125.  */
  126. parse_line()
  127. {
  128.  register char *ptrfrm = Line;
  129.  register char *ptrto = Label;
  130.  char *skip_white();
  131.  
  132.  if( *ptrfrm == '*' || *ptrfrm == '\n' )
  133.   return(0);      /* a comment line */
  134.  
  135.  while( delim(*ptrfrm)== NO )
  136.   *ptrto++ = *ptrfrm++;
  137.  if(*--ptrto != ':')ptrto++;     /* allow trailing : */
  138.  *ptrto = EOS;
  139.  
  140.  ptrfrm = skip_white(ptrfrm);
  141.  
  142.  ptrto = Op;
  143.  while( delim(*ptrfrm) == NO)
  144.   *ptrto++ = mapdn(*ptrfrm++);
  145.  *ptrto = EOS;
  146.  
  147.  ptrfrm = skip_white(ptrfrm);
  148.  
  149.  ptrto = Operand;
  150.  while( *ptrfrm != NEWLINE )
  151.   *ptrto++ = *ptrfrm++;
  152.  *ptrto = EOS;
  153.  
  154. #ifdef DEBUG
  155.  printf("Label-%s-\n",Label);
  156.  printf("Op----%s-\n",Op);
  157.  printf("Operand-%s-\n",Operand);
  158. #endif
  159.  return(1);
  160. }
  161.  
  162. /*
  163.  * process --- determine mnemonic class and act on it
  164.  */
  165. process()
  166. {
  167.  register struct oper *i;
  168.  struct oper *mne_look();
  169.  
  170.  Old_pc = Pc;            /* setup `old' program counter */
  171.  Optr = Operand;         /* point to beginning of operand field */
  172.  
  173.  if(*Op==EOS){           /* no mnemonic */
  174.   if(*Label != EOS)
  175.    install(Label,Pc);
  176.   }
  177.  else if( (i = mne_look(Op))== NULL)
  178.   error("Unrecognized Mnemonic");
  179.  else if( i->class == PSEUDO )
  180.   do_pseudo(i->opcode);
  181.  else{
  182.   if( *Label )install(Label,Pc);
  183.   if(Cflag)Cycles = i->cycles;
  184.   do_op(i->opcode,i->class);
  185.   if(Cflag)Ctotal += Cycles;
  186.   }
  187. }
  188.