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