home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / c / cug292wk.zip / PCDSK2 / ASLIST.C < prev    next >
C/C++ Source or Header  |  1993-01-22  |  8KB  |  420 lines

  1. /* aslist.c */
  2.  
  3. /*
  4.  * (C) Copyright 1989,1990
  5.  * All Rights Reserved
  6.  *
  7.  * Alan R. Baldwin
  8.  * 721 Berkeley St.
  9.  * Kent, Ohio  44240
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <setjmp.h>
  14. #include <string.h>
  15. #include <alloc.h>
  16. #include "asm.h"
  17.  
  18. /*
  19.  * Copy a block of source and code to
  20.  * the listing file. If no listing file or
  21.  * the current line is not to be listed
  22.  * just return. Multiple code bytes get
  23.  * put out on extra lines after the source
  24.  * line.
  25.  */
  26. VOID
  27. list()
  28. {
  29.     register char *wp;
  30.     register int *wpt;
  31.     register nb;
  32.  
  33.     if (lfp == NULL || lmode == NLIST)
  34.         return;
  35.     slew(lfp);
  36.     while (ep < &eb[NERR])
  37.         *ep++ = ' ';
  38.     fprintf(lfp, "%.2s", eb);
  39.     if (lmode == SLIST) {
  40.         fprintf(lfp, "%24s%5u %s\n", "", line, ib);
  41.         return;
  42.     }
  43.     if (xflag == 0) {        /* HEX */
  44.         fprintf(lfp, " %04X", laddr);
  45.         if (lmode == ALIST) {
  46.             fprintf(lfp, "%19s%5u %s\n", "", line, ib);
  47.             return;
  48.         }
  49.         wp = cb;
  50.         wpt = cbt;
  51.         nb = (int) (cp - cb);
  52.         list1(wp, wpt, nb, 1);
  53.         fprintf(lfp, " %5u %s\n", line, ib);
  54.         while ((nb -= 6) > 0) {
  55.             wp += 6;
  56.             wpt += 6;
  57.             slew(lfp);
  58.             fprintf(lfp, "%7s", "");
  59.             list1(wp, wpt, nb, 0);
  60.             putc('\n', lfp);
  61.         }
  62.     } else
  63.     if (xflag == 1) {        /* OCTAL */
  64.         fprintf(lfp, " %06o", laddr);
  65.         if (lmode == ALIST) {
  66.             fprintf(lfp, "%17s%5u %s\n", "", line, ib);
  67.             return;
  68.         }
  69.         wp = cb;
  70.         wpt = cbt;
  71.         nb = (int) (cp - cb);
  72.         list1(wp, wpt, nb, 1);
  73.         fprintf(lfp, " %5u %s\n", line, ib);
  74.         while ((nb -= 4) > 0) {
  75.             wp += 4;
  76.             wpt += 4;
  77.             slew(lfp);
  78.             fprintf(lfp, "%9s", "");
  79.             list1(wp, wpt, nb, 0);
  80.             putc('\n', lfp);
  81.         }
  82.     } else
  83.     if (xflag == 2) {        /* DECIMAL */
  84.         fprintf(lfp, "  %05u", laddr);
  85.         if (lmode == ALIST) {
  86.             fprintf(lfp, "%17s%5u %s\n", "", line, ib);
  87.             return;
  88.         }
  89.         wp = cb;
  90.         wpt = cbt;
  91.         nb = (int) (cp - cb);
  92.         list1(wp, wpt, nb, 1);
  93.         fprintf(lfp, " %5u %s\n", line, ib);
  94.         while ((nb -= 4) > 0) {
  95.             wp += 4;
  96.             wpt += 4;
  97.             slew(lfp);
  98.             fprintf(lfp, "%9s", "");
  99.             list1(wp, wpt, nb, 0);
  100.             putc('\n', lfp);
  101.         }
  102.     }
  103. }
  104.  
  105. /*
  106.  * Send bytes of code to the listing.
  107.  * A subroutine of `list'.
  108.  */
  109. VOID
  110. list1(wp, wpt, nb, f)
  111. register char *wp;
  112. register int nb, *wpt;
  113. {
  114.     register i;
  115.  
  116.     if (xflag == 0) {        /* HEX */
  117.         if (nb > 6)
  118.             nb = 6;
  119.         for (i=0; i<nb; ++i) {
  120.             list2(*wpt++);
  121.             fprintf(lfp, "%02X", (*wp++)&0377);
  122.         }
  123.         if (f) {
  124.             while (i < 6) {
  125.                 fprintf(lfp, "   ");
  126.                 ++i;
  127.             }
  128.         }
  129.     } else
  130.     if (xflag == 1) {        /* OCTAL */
  131.         if (nb > 4)
  132.             nb = 4;
  133.         for (i=0; i<nb; ++i) {
  134.             list2(*wpt++);
  135.             fprintf(lfp, "%03o", (*wp++)&0377);
  136.         }
  137.         if (f) {
  138.             while (i < 4) {
  139.                 fprintf(lfp, "    ");
  140.                 ++i;
  141.             }
  142.         }
  143.     } else
  144.     if (xflag == 2) {        /* DECIMAL */
  145.         if (nb > 4)
  146.             nb = 4;
  147.         for (i=0; i<nb; ++i) {
  148.             list2(*wpt++);
  149.             fprintf(lfp, "%03u", (*wp++)&0377);
  150.         }
  151.         if (f) {
  152.             while (i < 4) {
  153.                 fprintf(lfp, "    ");
  154.                 ++i;
  155.             }
  156.         }
  157.     }
  158. }
  159.  
  160. /*
  161.  * Send relocation type to the listing.
  162.  * A subroutine of `list1'.
  163.  */
  164. VOID
  165. list2(t)
  166. register int t;
  167. {
  168.     register int c;
  169.  
  170.     c = ' ';
  171.     if (fflag == 1) {
  172.         if (t & R_RELOC) {
  173.             c = '`';
  174.         }
  175.     } else
  176.     if (fflag >= 2) {
  177.         if (t & R_RELOC) {
  178.             if (t & (R_PAG0|R_PAG)) {
  179.                 c = '*';
  180.             } else if (t & R_USGN) {
  181.                 c = 'u';
  182.             } else if (t & R_PCR) {
  183.                 c = 'p';
  184.             } else {
  185.                 c = 'r';
  186.             }
  187.             if (t & R_HIGH) c += 1;
  188.         }
  189.     }
  190.     putc(c, lfp);
  191. }
  192.  
  193. /*
  194.  * Increment the count of lines on the
  195.  * page. If the page overflows:
  196.  * 1) put out a page skip,
  197.  * 2) a title,
  198.  * 3) a subtitle,
  199.  * 4) and reset the line count.
  200.  */
  201. VOID
  202. slew(fp)
  203. FILE *fp;
  204. {
  205.     if (lop++ >= NLPP) {
  206.         fprintf(fp, "\fASxxxx Assembler %s  (%s), page %u.\n",
  207.             VERSION, cpu, ++page);
  208.         fprintf(fp, "%s\n", tb);
  209.         fprintf(fp, "%s\n\n", stb);
  210.         lop = 5;
  211.     }
  212. }
  213.  
  214. /*
  215.  * Symbol and Area Table Output
  216.  */
  217. VOID
  218. lstsym(fp)
  219. FILE *fp;
  220. {
  221.     register int c, i, j, k;
  222.     register char *ptr;
  223.     int nmsym, narea;
  224.     struct sym *sp;
  225.     struct sym **p;
  226.     struct area *ap;
  227.  
  228.     /*
  229.      * Symbol Table Header
  230.      */
  231.     strcpy(stb, &symtbl[0]);
  232.     lop = NLPP;
  233.     if (fp == tfp)
  234.         page = 0;
  235.     slew(fp);
  236.  
  237.     /*
  238.      * Find number of symbols
  239.      */
  240.     nmsym = 0;
  241.     for (i=0; i<NHASH; i++) {
  242.         sp = symhash[i];
  243.         while (sp) {
  244.             if (sp != &dot)
  245.                 ++nmsym;
  246.             sp = sp->s_sp;
  247.         }
  248.     }
  249.     if (nmsym == 0)
  250.         goto atable;
  251.  
  252.     /*
  253.      * Allocate space for an array of pointers to symbols
  254.      * and load array.
  255.      */
  256.     if ((p = (struct sym **) malloc(sizeof((struct sym *) sp)*nmsym))
  257.         == NULL) {
  258.         fprintf(fp, "Insufficient space to build Symbol Table.\n");
  259.         return;
  260.     }
  261.     nmsym = 0;
  262.     for (i=0; i<NHASH; i++) {
  263.         sp = symhash[i];
  264.         while (sp) {
  265.             if (sp != &dot)
  266.                 p[nmsym++] = sp;
  267.             sp = sp->s_sp;
  268.         }
  269.     }
  270.  
  271.     /*
  272.      * Bubble Sort on Symbol Table Array
  273.      */
  274.     j = 1;
  275.     c = nmsym - 1;
  276.     while (j) {
  277.         j = 0;
  278.         for (i=0; i<c; ++i) {
  279.             if (strcmp(&p[i]->s_id[0],&p[i+1]->s_id[0]) > 0) {
  280.                 j = 1;
  281.                 sp = p[i+1];
  282.                 p[i+1] = p[i];
  283.                 p[i] = sp;
  284.             }
  285.         }
  286.     }
  287.  
  288.     /*
  289.      * Symbol Table Output
  290.      */
  291.     for (i=0; i<nmsym;) {
  292.         sp = p[i];
  293.         if (sp->s_area) {
  294.             j = sp->s_area->a_ref;
  295.             if (xflag == 0) {
  296.                 fprintf(fp, " %2X ", j);
  297.             } else
  298.             if (xflag == 1) {
  299.                 fprintf(fp, "%3o ", j);
  300.             } else
  301.             if (xflag == 2) {
  302.                 fprintf(fp, "%3u ", j);
  303.             }
  304.         } else {
  305.             fprintf(fp, "    ");
  306.         }
  307.         ptr = &sp->s_id[0];
  308.         while (ptr < &sp->s_id[NCPS]) {
  309.             if ((c = *ptr++) != 0) {
  310.                 putc(c, fp);
  311.             } else {
  312.                 putc(' ', fp);
  313.             }
  314.         }
  315.         if (sp->s_flag & S_ASG) {
  316.             putc('=', fp);
  317.         } else {
  318.             putc(' ', fp);
  319.         }
  320.         if (sp->s_type == S_NEW) {
  321.             if (xflag == 0) {
  322.                 fprintf(fp, "  **** ");
  323.             } else
  324.             if (xflag == 1) {
  325.                 fprintf(fp, "****** ");
  326.             } else
  327.             if (xflag == 2) {
  328.                 fprintf(fp, " ***** ");
  329.             }
  330.         } else {
  331.             j = sp->s_addr;
  332.             if (xflag == 0) {
  333.                 fprintf(fp, "  %04X ", j);
  334.             } else
  335.             if (xflag == 1) {
  336.                 fprintf(fp, "%06o ", j);
  337.             } else
  338.             if (xflag == 2) {
  339.                 fprintf(fp, " %05u ", j);
  340.             }
  341.         }
  342.         j = 0;
  343.         if (sp->s_flag & S_GBL) {
  344.             putc('G', fp);
  345.             ++j;
  346.         }
  347.         if (sp->s_area != NULL) {
  348.             putc('R', fp);
  349.             ++j;
  350.         }
  351.         if (sp->s_type == S_NEW) {
  352.             putc('X', fp);
  353.             ++j;
  354.         }
  355.         if (++i % 3 == 0) {
  356.             putc('\n', fp);
  357.             slew(fp);
  358.         } else
  359.         if (i < nmsym) {
  360.             while (j++ < 4)
  361.                 putc(' ', fp);
  362.             fprintf(fp, "| ");
  363.         }
  364.     }
  365.     putc('\n', fp);
  366.  
  367.     /*
  368.      * Area Table Header
  369.      */
  370.  
  371. atable:
  372.     strcpy(stb, &aretbl[0]);
  373.     lop = NLPP;
  374.     slew(fp);
  375.  
  376.     /*
  377.      * Area Table Output
  378.      */
  379.     narea = 0;
  380.     ap = areap;
  381.     while (ap) {
  382.         ++narea;
  383.         ap = ap->a_ap;
  384.     }
  385.     for (i=0; i<narea; ++i) {
  386.         ap = areap;
  387.         for (j=i+1; j<narea; ++j)
  388.             ap = ap->a_ap;
  389.         j = ap->a_ref;
  390.         if (xflag == 0) {
  391.             fprintf(fp, "  %2X ", j);
  392.         } else
  393.         if (xflag == 1) {
  394.             fprintf(fp, " %3o ", j);
  395.         } else
  396.         if (xflag == 2) {
  397.             fprintf(fp, " %3u ", j);
  398.         }
  399.         ptr = &ap->a_id[0];
  400.         while (ptr < &ap->a_id[NCPS]) {
  401.             if ((c = *ptr++) != 0) {
  402.                 putc(c, fp);
  403.             } else {
  404.                 putc(' ', fp);
  405.             }
  406.         }
  407.         j = ap->a_size;
  408.         k = ap->a_flag;
  409.         if (xflag==0) {
  410.             fprintf(fp, "   size %4X   flags %X\n", j, k);
  411.         } else
  412.         if (xflag==1) {
  413.             fprintf(fp, "   size %6o   flags %o\n", j, k);
  414.         } else
  415.         if (xflag==2) {
  416.             fprintf(fp, "   size %5u   flags %u\n", j, k);
  417.         }
  418.     }        
  419. }