home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / appleii / appxas.3 / assm2.c next >
C/C++ Source or Header  |  2020-01-01  |  9KB  |  430 lines

  1. #include "stdio.h"
  2. #include "assm.d1"
  3. #include "assm.d2"
  4.  
  5. extern    int    optab[];
  6. extern    int    step[];
  7.  
  8. /* translate source line to machine language */
  9.  
  10. assemble()
  11. {
  12.     int    flg;
  13.     int    i;        /* prlnbuf pointer */
  14.  
  15.     if ((i = prlnbuf[SFIELD]) == ';' || i == 0 || i == '.' )  {
  16.         if (pass == LAST_PASS)
  17.             println();
  18.         return;
  19.     }
  20.     lablptr = -1;
  21.     i = SFIELD;
  22.     udtype = UNDEF;
  23.     if (colsym(&i) != 0 && (lablptr = stlook()) == -1)
  24.         return;
  25.     while (prlnbuf[++i] == ' ');    /* find first non-space */
  26.     if ((flg = oplook(&i)) < 0) {    /* collect operation code */
  27.         labldef(loccnt);
  28.         if (flg == -1)
  29.             error("Invalid operation code");
  30.         if ((flg == -2) && (pass == LAST_PASS)) {
  31.             if (lablptr != -1)
  32.                 loadlc(loccnt, 1, 0);
  33.             println();
  34.         }
  35.         return;
  36.     }
  37.     else if (noasm) {
  38.         if (opflg == PSEUDO && opval <= ENDCOPVAL && opval >= IFEQOPVAL )
  39.         pseudo(&i);
  40.         else if (pass == LAST_PASS)
  41.             println();
  42.         return;
  43.         }
  44.     else if (opflg == PSEUDO)
  45.         pseudo(&i);
  46.     else if (labldef(loccnt) == -1)
  47.         return;
  48.     else {
  49.         if (opflg == CLASS1)
  50.             class1();
  51.         else if (opflg == CLASS2)
  52.             class2(&i);
  53.         else class3(&i);
  54.     }
  55. }
  56.  
  57. /****************************************************************************/
  58.  
  59. /* printline prints the contents of prlnbuf */
  60.  
  61. println()
  62. {
  63.     if (lflag > 0)
  64.         fprintf(stdout, "%s\n", prlnbuf);
  65. }
  66.  
  67. /* colsym() collects a symbol from prlnbuf into symbol[],
  68.  *    leaves prlnbuf pointer at first invalid symbol character,
  69.  *    returns 0 if no symbol collected
  70.  */
  71.  
  72. colsym(ip)
  73.     int *ip;
  74. {
  75.     int    valid;
  76.     int    i;
  77.     char    ch;
  78.  
  79.     valid = 1;
  80.     i = 0;
  81.     while (valid == 1) {
  82.         ch = prlnbuf[*ip];
  83.         /* drop : in labels */
  84.         if (i > 1 && ch == ':' ) ch = prlnbuf[++(*ip)] ;
  85.         if (ch == '_' || ch == '.');
  86.         else if (ch >= 'a' && ch <= 'z');
  87.         else if (ch >= 'A' && ch <= 'Z');
  88.         else if (i >= 1 && ch >= '0' && ch <= '9');
  89.         else if (i == 1 && ch == '=');
  90.         else valid = 0;
  91.         if (valid == 1) {
  92.             if (i < SBOLSZ - 1)
  93.                 symbol[++i] = ch;
  94.             (*ip)++;
  95.         }
  96.     }
  97.     if (i == 1) {
  98.         switch (symbol[1]) {
  99.         case 'A': case 'a':
  100.         case 'X': case 'x':
  101.         case 'Y': case 'y':
  102.             error("Symbol is reserved (A, X or Y)");
  103.             i = 0;
  104.         }
  105.     }
  106.     symbol[0] = i;
  107.     return(i);
  108. }
  109.  
  110. /* symbol table lookup
  111.  *    if found, return pointer to symbol
  112.  *    else, install symbol as undefined, and return pointer
  113.  */
  114.  
  115. stlook()
  116. {
  117.     int    found;
  118.     int    hptr;
  119.     int    j;
  120.     int    nptr;
  121.     int    pptr;
  122.     int    ptr;
  123.  
  124.     hptr = 0;
  125.     for (j = 0; j < symbol[0]; j++)
  126.         hptr += symbol[j];
  127.     hptr %= 128;
  128.     ptr = hash_tbl[hptr];
  129.     if (ptr == -1) {        /* no entry for this link */
  130.         hash_tbl[hptr] = nxt_free;
  131.         return(stinstal());
  132.     }
  133.     while (symtab[ptr] != 0) {    /* 0 count = end of table */
  134.         found = 1;
  135.         for (j = 0; j <= symbol[0]; j++) {
  136.             if (symbol[j] != symtab[ptr + j]) {
  137.                 found = 0;
  138.                 pptr = ptr + symtab[ptr] + 4;
  139.                 nptr = (symtab[pptr + 1] << 8) + (symtab[pptr] & 0xff);
  140.                 nptr &= 0xffff;
  141.                 if (nptr == 0) {
  142.                     symtab[ptr + symtab[ptr] + 4] = nxt_free & 0xff;
  143.                     symtab[ptr + symtab[ptr] + 5] = (nxt_free >> 8) & 0xff;
  144.                     return(stinstal());
  145.                 }
  146.                 ptr = nptr;
  147.                 break;
  148.             }
  149.         }
  150.         if (found == 1)
  151.             return(ptr);
  152.     }
  153.     error("Symbol not found");
  154.     return(-1);
  155. }
  156.  
  157. /*  instal symbol into symtab
  158.  */
  159. stinstal()
  160. {
  161. register    int    j;
  162. register    int    ptr1;
  163. register    int    ptr2;
  164.  
  165.     ptr1 = ptr2 = nxt_free;
  166.     if ((ptr1 + symbol[0] + 6) >= STABSZ) {
  167.         error("Symbol table full");
  168.         return(-1);
  169.     }
  170.     for (j = 0; j <= symbol[0]; j++)
  171.         symtab[ptr1++] = symbol[j];
  172.     symtab[ptr1] = udtype;
  173.     nxt_free = ptr1 + 5;
  174.     return(ptr2);
  175. }
  176.  
  177. /* operation code table lookup
  178.  *    if found, return pointer to symbol,
  179.  *    else, return -1
  180.  */
  181.  
  182. oplook(ip)
  183.    int    *ip;
  184. {
  185. register    char    ch;
  186. register    int    i;
  187. register    int    j;
  188.     int    k;
  189.     int    temp[2];
  190.  
  191.     i = j = 0;
  192.     temp[0] = temp[1] = 0;
  193.     while((ch=prlnbuf[*ip])!= ' ' && ch!= 0 && ch!= '\t' && ch!= ';') {
  194.         if (ch >= 'A' && ch <= 'Z')
  195.             ch &= 0x1f;
  196.         else if (ch >= 'a' && ch <= 'z')
  197.             ch &= 0x1f;
  198.         else if (ch == '.')
  199.             ch = 31;
  200.         else if (ch == '*')
  201.             ch = 30;
  202.         else if (ch == '=')
  203.             ch = 29;
  204.         else return(-1);
  205.         temp[j] = (temp[j] * 0x20) + (ch & 0xff);
  206.         if (ch == 29)
  207.             break;
  208.         ++(*ip);
  209.         if (++i >= 3) {
  210.             i = 0;
  211.             if (++j >= 3) {
  212.                 return(-1);
  213.             }
  214.         }
  215.     }
  216.     if ((j = temp[0]^temp[1]) == 0)
  217.         return(-2);
  218.     k = 0;
  219.     i = step[k] - 3;
  220.     do {
  221.         if (j == optab[i]) {
  222.             opflg = optab[++i];
  223.             opval = optab[++i];
  224.             return(i);
  225.         }
  226.         else if (j < optab[i])
  227.             i -= step[++k];
  228.         else i += step[++k];
  229.     } while (step[k] != 0);
  230.     return(-1);
  231. }
  232.  
  233. /* error printing routine */
  234.  
  235. error(stptr)
  236.    char *stptr;
  237. {
  238.     loadlc(loccnt, 0, 1);
  239.     loccnt += 3;
  240.     loadv(0,0,0);
  241.     loadv(0,1,0);
  242.     loadv(0,2,0);
  243.     fprintf(stderr, "%s\n", prlnbuf);
  244.     fprintf(stderr, "***** %s\n", stptr);
  245.     errcnt++;
  246. }
  247.  
  248. /* load 16 bit value in printable form into prlnbuf */
  249.  
  250. loadlc(val, f, outflg)
  251.     int val;
  252.     int f;
  253.     int outflg;
  254. {
  255.     int    i;
  256.  
  257.     i = 6 + 7*f;
  258.     hexcon(4, val);
  259.     if (nflag == 0) {
  260.         prlnbuf[i++]  = hex[3];
  261.         prlnbuf[i++]  = hex[4];
  262.         prlnbuf[i++]  = ':';
  263.         prlnbuf[i++]  = hex[1];
  264.         prlnbuf[i] = hex[2];
  265.     }
  266.     else {
  267.         prlnbuf[i++] = hex[1];
  268.         prlnbuf[i++] = hex[2];
  269.         prlnbuf[i++] = hex[3];
  270.         prlnbuf[i] = hex[4];
  271.     }
  272.     if ((pass == LAST_PASS)&&(oflag != 0)&&(objcnt <= 0)&&(outflg != 0)) {
  273.          /* rearrange the next for franklin monitor tm */
  274.         fprintf(optr, "\n%c%c%c%c:", hex[1], hex[2], hex[3], hex[4]);
  275.         objcnt = 16;
  276.     }
  277. }
  278.  
  279.  
  280.  
  281. /* load value in hex into prlnbuf[contents[i]] */
  282. /* and output hex characters to obuf if LAST_PASS & oflag == 1 */
  283.  
  284. loadv(val,f,outflg)
  285.    int    val;
  286.    int    f;        /* contents field subscript */
  287.    int    outflg;        /* flag to output object bytes */
  288. {
  289.  
  290.     hexcon(2, val);
  291.     prlnbuf[13 + 3*f] = hex[1];
  292.     prlnbuf[14 + 3*f] = hex[2];
  293.     if ((pass == LAST_PASS) && (oflag != 0) && (outflg != 0)) {
  294.         fputc(hex[1], optr);
  295.         fputc(hex[2], optr);
  296.         fputc(' ', optr);   /* add space for franklin monitor tm */
  297.         --objcnt;
  298.     }
  299. }
  300.  
  301. /* convert number supplied as argument to hexadecimal in hex[digit] (lsd)
  302.         through hex[1] (msd)        */
  303.  
  304. hexcon(digit, num)
  305.     int digit;
  306.    int    num;
  307. {
  308.  
  309.     for (; digit > 0; digit--) {
  310.         hex[digit] = (num & 0x0f) + '0';
  311.         if (hex[digit] > '9')
  312.             hex[digit] += 'A' -'9' - 1;
  313.         num >>= 4;
  314.     }
  315. }
  316.  
  317. /* assign <value> to label pointed to by lablptr,
  318.  *    checking for valid definition, etc.
  319.  */
  320.  
  321. labldef(lval)
  322.     int lval;
  323. {
  324.     int    i;
  325.  
  326.     if (lablptr != -1) {
  327.         lablptr += symtab[lablptr] + 1;
  328.         /* debug 
  329. fprintf(stderr,"L =%d Flag=%o val=%o%o next=%o%o\n",lablptr,symtab[lablptr],
  330. symtab[lablptr+1],symtab[lablptr+2],symtab[lablptr+3],symtab[lablptr+4]);
  331.         */
  332.         if (pass == FIRST_PASS  ) {
  333.             if (symtab[lablptr] == UNDEF) {
  334.                 symtab[lablptr + 1] = lval & 0xff;
  335.                 i = symtab[lablptr + 2] = (lval >> 8) & 0xff;
  336.                 if (i == 0)
  337.                     symtab[lablptr] = DEFZRO;
  338.                 else    symtab[lablptr] = DEFABS;
  339.             }
  340.             else if (symtab[lablptr] == UNDEFAB) {
  341.                 symtab[lablptr] = DEFABS;
  342.                 symtab[lablptr + 1] = lval & 0xff;
  343.                 symtab[lablptr + 2] = (lval >> 8) & 0xff;
  344.             }
  345.             else {
  346.                 symtab[lablptr] = MDEF;
  347.                 symtab[lablptr + 1] = 0;
  348.                 symtab[lablptr + 2] = 0;
  349.                 error("Label multiply defined");
  350.                 return(-1);
  351.             }
  352.         }
  353.         else {
  354.             i = (symtab[lablptr + 2] << 8) +
  355.                 (symtab[lablptr+1] & 0xff);
  356.             i &= 0xffff;
  357.             if (i != lval ) {
  358.                 error("Sync error");
  359.                 return(-1);
  360.             }
  361.         }
  362.     }
  363.     return(0);
  364. }
  365. /* assign <value> to label pointed to by lablptr
  366.  */
  367.  
  368. labldef2(lval)
  369.     int lval;
  370. {
  371.     int    i;
  372.  
  373.     if (lablptr != -1) {
  374.         lablptr += symtab[lablptr] + 1;
  375.         /* debug 
  376. fprintf(stderr,"L =%d Flag=%o val=%o%o next=%o%o\n",lablptr,symtab[lablptr],
  377. symtab[lablptr+1],symtab[lablptr+2],symtab[lablptr+3],symtab[lablptr+4]);
  378.         */
  379.             if (symtab[lablptr] == UNDEF) {
  380.                 symtab[lablptr + 1] = lval & 0xff;
  381.                 i = symtab[lablptr + 2] = (lval >> 8) & 0xff;
  382.                 if (i == 0)
  383.                     symtab[lablptr] = DEFZRO;
  384.                 else    symtab[lablptr] = DEFABS;
  385.             }
  386.             else if (symtab[lablptr] == UNDEFAB) {
  387.                 symtab[lablptr] = DEFABS;
  388.                 symtab[lablptr + 1] = lval & 0xff;
  389.                 symtab[lablptr + 2] = (lval >> 8) & 0xff;
  390.             }
  391.             else {
  392.                 i = (symtab[lablptr + 2] << 8) +
  393.                 (symtab[lablptr+1] & 0xff);
  394.                 i &= 0xffff;
  395.                 if (i != lval ) {
  396.                     error("Sync error");
  397.                     return(-1);
  398.                     }
  399.                 }
  400.     }
  401.     return(0);
  402. }
  403.  
  404. /* determine the value of the symbol,
  405.  * given pointer to first character of symbol in symtab
  406.  */
  407.  
  408. symval(ip)
  409.     int *ip;
  410. {
  411.     int    ptr;
  412.     int    svalue;
  413.  
  414.     svalue = 0;
  415.     colsym(ip);
  416.     if ((ptr = stlook()) == -1)
  417.         undef = 1;        /* no room error */
  418.     else if (symtab[ptr + symtab[ptr] + 1] == UNDEF)
  419.         undef = 1;
  420.     else if (symtab[ptr + symtab[ptr] + 1] == UNDEFAB)
  421.         undef = 1;
  422.     else svalue = ((symtab[ptr + symtab[ptr] + 3] << 8) +
  423.         (symtab[ptr + symtab[ptr] + 2] & 0xff)) & 0xffff;
  424.     if (symtab[ptr + symtab[ptr] + 1] == DEFABS)
  425.         zpref = 1;
  426.     if (undef != 0)
  427.         zpref = 1;
  428.     return(svalue);
  429. }
  430.