home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / a / apxa2.c / assm1.c < prev   
C/C++ Source or Header  |  2020-01-01  |  5KB  |  260 lines

  1. #include "stdio.h"
  2. #include "assm.d1"
  3. #include "assm.d2"
  4.  
  5. #define CPMEOF EOF
  6.  
  7. /*
  8.  *  Two changes to version 1.4 have been made to "port" as6502 to CP/M(tm).
  9.  *  A "tolower()" function call was add to the command line processing
  10.  *  code to (re)map the command line arguments to lower case (CP/M
  11.  *  converts all command line arguments to upper case).  The readline()
  12.  *  function has code added to "ignore" the '\r' character (CP/M includes
  13.  *  the \r character along with \n).
  14.  *
  15.  *  Also, the ability to process multiple files on the command line has been
  16.  *  added.  Now one can do, for example:
  17.  *
  18.  *    as6502 -nisvo header.file source.file data.file ...
  19.  *
  20.  *    George V. Wilder
  21.  *    IX 1A-360 x1937
  22.  *    ihuxp!gvw1
  23.  */
  24.  
  25. main(argc, argv)
  26.    int    argc;
  27.    char *argv[];
  28. {
  29.     char    c;
  30.     int    cnt;
  31.     int    i;
  32.     int    ac;
  33.     char    **av;
  34.  
  35.     while (--argc > 0 && (*++argv)[0] == '-') {
  36.         for (i = 1; (c =  ((*argv)[i])) != '\0'; i++) {
  37.             c |= 0x20 ;  /* make it lower case - tm */
  38.             if (c == 'd')        /* debug flag */
  39.                 dflag++;
  40.             if (c == 'i')        /* ignore .nlst flag */
  41.                 iflag++;
  42.             if (c == 'l')        /* disable listing flag */
  43.                 lflag--;
  44.             if (c == 'n')        /* normal/split address mode */
  45.                 nflag++;
  46.             if (c == 'o')        /* object output flag */
  47.                 oflag++;
  48.             if (c == 's')        /* list symbol table flag */
  49.                 sflag++;
  50.             if (c == 'v')        /* print assembler version */
  51.                 fprintf(stdout, "as6502 - version 4.1b - 11/22/84 - JHV [gvw]\n");
  52.         }
  53.     }
  54.     ac = argc;
  55.     av = argv;
  56.     pass = FIRST_PASS;
  57.     for (i = 0; i < 128; i++)
  58.         hash_tbl[i] = -1;
  59.     errcnt = loccnt = slnum = 0;
  60.     while (pass != DONE) {
  61.         initialize(ac, av, argc);
  62.         if(pass == LAST_PASS && ac == argc)
  63.             errcnt = loccnt = slnum = 0;
  64.         while (readline() != -1)
  65.             assemble();
  66.         if (errcnt != 0) {
  67.             pass = DONE;
  68.             fprintf(stderr, "Terminated with error counter = %d\n", errcnt);
  69.         }
  70.         switch (pass) {
  71.         case FIRST_PASS:
  72.             --ac;
  73.             ++av;
  74.             if(ac == 0) {
  75.                 pass = LAST_PASS;
  76.                 if (lflag == 0)
  77.                     lflag++;
  78.                 ac = argc;
  79.                 av = argv;
  80.             }
  81.             break;
  82.         case LAST_PASS:
  83.             --ac;
  84.             ++av;
  85.             if(ac == 0) {
  86.                 pass = DONE;
  87.                 if (sflag != 0)
  88.                     stprnt();
  89.             }
  90.         }
  91.         wrapup();
  92.         if ((dflag != 0) && (pass == LAST_PASS)) {
  93.             fprintf(stdout, "nxt_free = %d\n", nxt_free);
  94.             cnt = 0;
  95.             for (i = 0; i < 128; i++)
  96.                 if (hash_tbl[i] == -1)
  97.                     cnt++;
  98.             fprintf(stdout, "%d unused hash table pointers out of 128\n", cnt);
  99.         }
  100.     }
  101.     return(0);
  102. }
  103.  
  104. /*****************************************************************************/
  105.  
  106. /* initialize opens files */
  107.  
  108. initialize(ac, av, argc)
  109.    int    ac;
  110.    char *av[];
  111.    int  argc;
  112. {
  113.  
  114.     if (ac == 0) {
  115.         fprintf(stderr, "Invalid argument count (%d).\n", argc);
  116.         exit(1);
  117.     }
  118.     if ((iptr = fopen(*av, "r")) == NULL) {
  119.         fprintf(stderr, "Open error for file '%s'.\n", *av);
  120.         exit(1);
  121.     }
  122.     if ((pass == LAST_PASS) && (oflag != 0) && ac == argc) {
  123.         if ((optr = fopen("6502.out", "w")) == NULL) {
  124.             fprintf(stderr, "Create error for object file 6502.out.\n");
  125.             exit(1);
  126.         }
  127.         else fprintf(optr,"CALL -151"); /* let monitor do loading */
  128.     }
  129. }
  130.  
  131. /* readline reads and formats an input line    */
  132.  
  133. int    field[] =
  134. {
  135.     SFIELD,
  136.     SFIELD + 8,
  137.     SFIELD + 14,
  138.     SFIELD + 23,
  139.     SFIELD + 43,
  140.     SFIELD + 75
  141. };
  142.  
  143. readline()
  144. {
  145.     int    i;        /* pointer into prlnbuf */
  146.     int    j;        /* pointer to current field start    */
  147.     int    ch;        /* current character        */
  148.     int    cmnt;        /* comment line flag    */
  149.     int    spcnt;        /* consecutive space counter    */
  150.     int    string;        /* ASCII string flag    */
  151.     int    temp1;        /* temp used for line number conversion */
  152.  
  153.     temp1 = ++slnum;
  154.     for (i = 0; i < LAST_CH_POS; i++)
  155.         prlnbuf[i] = ' ';
  156.     i = 3;
  157.     while (temp1 != 0) {    /* put source line number into prlnbuf */
  158.         prlnbuf[i--] = temp1 % 10 + '0';
  159.         temp1 /= 10;
  160.     }
  161.     i = SFIELD;
  162.     cmnt = spcnt = string = 0;
  163.     j = 1;
  164.     while ((ch = getc(iptr)) != '\n') {
  165.         if(ch == '\r')
  166.             continue;
  167.         prlnbuf[i++] = ch;
  168.         if ((ch == ' ') && (string == 0)) {
  169.             if (spcnt != 0)
  170.                 --i;
  171.             else if (cmnt == 0) {
  172.                 ++spcnt;
  173.                 if (i < field[j])
  174.                     i = field[j];
  175.                 if (++j > 3) {
  176.                     spcnt = 0;
  177.                     ++cmnt;
  178.                 }
  179.             }
  180.         }
  181.         else if (ch == '\t') {
  182.             prlnbuf[i - 1] = ' ';
  183.             spcnt = 0;
  184.             if (cmnt == 0) {
  185.                 if (i < field[j])
  186.                     i = field[j];
  187.                 if (++j > 3)
  188.                     ++cmnt;
  189.             }
  190.             else i = (i + 8) & 0x78;
  191.         }
  192.         else if ((ch == ';') && (string == 0)) {
  193.             spcnt = 0;
  194.             if (i == SFIELD + 1)
  195.                 ++cmnt;
  196.             else if (prlnbuf[i - 2] != '\'') {
  197.                 ++cmnt;
  198.                 prlnbuf[i-1] = ' ';
  199.                 if (i < field[3])
  200.                     i = field[3];
  201.                 prlnbuf[i++] = ';';
  202.             }
  203.         }
  204.         else if (ch == EOF || ch == CPMEOF)
  205.             return(-1);
  206.         else {
  207.             if ((ch == '"' || ch == '\'' ) && (cmnt == 0))
  208.                 string = string ^ 1;
  209.             spcnt = 0;
  210.             if (i >= LAST_CH_POS - 1)
  211.                 --i;
  212.         }
  213.     }
  214.     prlnbuf[i] = 0;
  215.     return(0);
  216. }
  217.  
  218. /*
  219.  * wrapup() closes the source file
  220.  */
  221.  
  222. wrapup()
  223. {
  224.  
  225.     fclose(iptr);
  226.     if ((pass == DONE) && (oflag != 0)) {
  227.         fputc('\n', optr);
  228.         fclose(optr);
  229.     }
  230.     return;
  231. }
  232.  
  233. /* symbol table print
  234.  */
  235.  
  236. stprnt()
  237. {
  238.     int    i;
  239.     int    j;
  240.     int    k;
  241.  
  242.     fputc('\014', stdout);
  243.     fputc('\n', stdout);
  244.     i = 0;
  245.     while    ((j = symtab[i++]) != 0) {
  246.         for (k = j; k > 0; k--)
  247.             fputc(symtab[i++], stdout);
  248.         for (k = 20 - j; k > 0; k--)
  249.             fputc(' ', stdout);
  250.         ++i;
  251.         j = (symtab[i++] & 0xff);
  252.         j += (symtab[i++] << 8);
  253.         hexcon(4, j);
  254.         fprintf(stdout, "\t%c%c:%c%c\t%c%c%c%c\n",
  255.             hex[3], hex[4], hex[1], hex[2],
  256.             hex[1], hex[2], hex[3], hex[4]);
  257.         i += 2;
  258.     }
  259. }
  260.