home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 363_01 / listing.c < prev    next >
C/C++ Source or Header  |  1991-12-17  |  10KB  |  270 lines

  1. /***********************************************************************
  2.  *
  3.  *      LISTING.C
  4.  *      Listing File Routines for 68020 Assembler
  5.  *
  6.  *    Function: initList()
  7.  *      Opens the specified listing file for writing. If the file cannot be 
  8.  *      opened, then the routine prints a message and exits.
  9.  *
  10.  *      listLine()
  11.  *      Writes the current listing line to the listing file. If the line is 
  12.  *      not a continuation, then the routine includes the source line,
  13.  *      formatted into fields, as the last part of the listing line. If an 
  14.  *      error occurs during the writing, the routine prints a message and 
  15.  *      exits.
  16.  *
  17.  *      listLoc()
  18.  *      Starts the process of assembling a listing line by printing the 
  19.  *      location counter value into listData and initializing listPtr.
  20.  *
  21.  *      listObj()
  22.  *      Prints the data whose size and value are specified in the object field 
  23.  *      of the current listing line. Bytes are printed as two digits, words as 
  24.  *      four digits, and longwords as eight digits. A space follows each group 
  25.  *      of digits. 
  26.  *           If the item to be printed will not fit in the object code field, 
  27.  *      and cexFlag is TRUE, then the remainder of the data will be printed
  28.  *      on a continuation line. Otherwise, elipses ("...") will be printed to 
  29.  *      indicate the omission of values from the listing, and the excessive 
  30.  *      data will not be shown in the listing file. 
  31.  *
  32.  *   Usage: initList(char *name)
  33.  *
  34.  *      listLine()
  35.  *
  36.  *      listLoc()
  37.  *
  38.  *      listObj(int data, int size)
  39.  *
  40.  *      Author: Paul McKee
  41.  *      ECE492    North Carolina State University, 12/13/86
  42.  *
  43.  *      Modified A.E. Romer. Version 1.0
  44.  *          17 March 1991:  ANSI functions, braces layout.
  45.  *          Summer   1991:  Numerous small changes.
  46.  *
  47.  ************************************************************************/
  48.  
  49. #include <stdio.h>
  50. #include <ctype.h>
  51. #include "asm.h"
  52.  
  53. #define     DATA_SIZE       44      /* maximum size of object code */
  54. #define     ELLIPSIS        5       /* size of ellipsis (" ... ") */
  55.  
  56. /* listing fields widths, excluding following space */
  57.  
  58. #define     LINENO_FIELD    5
  59. #define     LOC_FIELD       8
  60. #define     LABEL_FIELD     9
  61. #define     OPCODE_FIELD    8
  62. #define     OPERAND_FIELD   16
  63.  
  64. /* Declarations of global variables */
  65. extern long loc;
  66. extern char pass2, cexFlag, continuation;
  67. extern char line[256];
  68. extern FILE *listFile;
  69. extern int lineNum;
  70. extern char noOperand;       /* TRUE if the instruction has no operand.
  71.                               * Used by listLine. */
  72.  
  73. static char listData[DATA_SIZE];
  74.                              /* Buffer in which location-and-object-code 
  75.                               * field of the listing lines is assembled */
  76.  
  77. extern char *listPtr;        /* Pointer to above buffer (this pointer is
  78.                               * global because it is actually manipulated
  79.                               * by equ() and set() to put specially 
  80.                               * formatted information in the listing) */
  81.  
  82. int initList(char *name)
  83.     {
  84.  
  85.     listFile = fopen(name, "w");
  86.     if (!listFile)
  87.         {
  88.         puts("Can't open listing file");
  89.         exit(0);
  90.         }
  91.  
  92.     return NORMAL;
  93.     }
  94.  
  95.  
  96. int listLine(void)
  97.     {
  98.     int i, j;
  99.  
  100.     if (!continuation)
  101.         fprintf(listFile, "%0*d ", LINENO_FIELD, lineNum);
  102.     else
  103.         for (i = 0; i < LINENO_FIELD+1; i++)
  104.             fputc(' ', listFile);               /* do not print line number
  105.                                                  * on continuation line */
  106.     fprintf(listFile, "%-*.*s", DATA_SIZE, DATA_SIZE, listData);
  107.     if (!continuation)
  108.         {
  109.         i = 0;                              /* 'line' character count */
  110.         j = 0;                              /* listing line character count */
  111.         if (line[0] != '*' && line[0] != '\n')
  112.             {
  113.             while (!isspace(line[i]))           /* if label exists */
  114.                 {
  115.                 putc(line[i++], listFile);      /* copy label to list file */
  116.                 j++;
  117.                 }
  118.             while (line[i] == '\t' || line[i] == ' ')
  119.                 i++;                            /* skip blanks */
  120.             if (line[i] != '\n' && line[i] != '\0')
  121.                 {
  122.                 while (j < LABEL_FIELD)
  123.                     {
  124.                     putc(' ', listFile);
  125.                                         /* fill remaining space with blanks */
  126.                     j++;
  127.                     }
  128.                 putc(' ', listFile);        /* add field separating blank */
  129.                 j++;
  130.                 }
  131.  
  132.             while (!isspace(line[i]) && line[i] != '\0')    /* opcode string */
  133.                 {
  134.                 putc(line[i++], listFile);      /* copy opcode string */
  135.                 j++;
  136.                 }
  137.             while (line[i] == '\t' || line[i] == ' ')
  138.                 i++;                            /* skip blanks */
  139.             if (line[i] != '\n' && line[i] != '\0')
  140.                 if (noOperand == TRUE)
  141.                     {
  142.                     while (j < LABEL_FIELD + OPCODE_FIELD + OPERAND_FIELD)
  143.                         {
  144.                         putc(' ', listFile);
  145.                                     /* fill remaining space with blanks */
  146.                         j++;
  147.                         }
  148.                     putc(' ', listFile);   /* add field separating blank */
  149.                     j++;
  150.  
  151.                     while (line[i] != '\n' && line[i] != '\0')
  152.                                                         /* comment string */
  153.                         putc(line[i++], listFile);    /* copy comment string */
  154.                     }
  155.                 else                /* noOperand == FALSE */
  156.                     {
  157.                     while (j < LABEL_FIELD + OPCODE_FIELD)
  158.                         {
  159.                         putc(' ', listFile);
  160.                                     /* fill remaining space with blanks */
  161.                         j++;
  162.                         }
  163.                     putc(' ', listFile);   /* add field separating blank */
  164.                     j++;
  165.  
  166.                     while (!isspace(line[i]) && line[i] != '\0')
  167.                                                         /* operand string */
  168.                         if (line[i] == CHAR_DELIMITER)  
  169.                                                     /* quoted string start */
  170.                             {
  171.                             putc(line[i++], listFile);
  172.                             j++;
  173.                             while (line[i] != CHAR_DELIMITER
  174.                                     && line[i] != '\0' && line[i] != '\n')
  175.                                 { 
  176.                                 putc(line[i++], listFile);
  177.                                 j++;
  178.                                 }
  179.                             if (line[i]== CHAR_DELIMITER)
  180.                                 {
  181.                                 putc(line[i++], listFile);
  182.                                 j++;
  183.                                 }
  184.                             } 
  185.                         else
  186.                             {
  187.                             putc(line[i++], listFile);
  188.                                                     /* copy operand string */
  189.                             j++;
  190.                             }
  191.                     while (line[i] == '\t' || line[i] == ' ')
  192.                         i++;                            /* skip blanks */
  193.                     if (line[i] != '\n' && line[i] != '\0')
  194.                         {
  195.                         while (j < LABEL_FIELD + OPCODE_FIELD + OPERAND_FIELD)
  196.                             {
  197.                             putc(' ', listFile);
  198.                                         /* fill remaining space with blanks */
  199.                             j++;
  200.                             }
  201.                         putc(' ', listFile);   /* add field separating blank */
  202.                         j++;
  203.                         }
  204.  
  205.                     while (line[i] != '\n' && line[i] != '\0')
  206.                                                         /* comment string */
  207.                         putc(line[i++], listFile);    /* copy comment string */
  208.                     }
  209.             putc('\n', listFile);
  210.             }
  211.         else            /* line[0]=!= '*' || line[0] == '\n' */
  212.             fprintf(listFile, "%s", line);      /* copy lines without code */
  213.         }
  214.     else                /* continuation */
  215.         putc('\n', listFile);
  216.     if ferror(listFile)
  217.         {
  218.         fputs("Error writing to listing file\n", stderr);
  219.         exit(0);
  220.         }
  221.  
  222.     return NORMAL;
  223.     }
  224.  
  225.  
  226. int listLoc(void)
  227.     {
  228.     sprintf(listData, "%0*lX ", LOC_FIELD, loc);
  229.     listPtr = listData + LOC_FIELD + 1;
  230.  
  231.     return NORMAL;
  232.     }
  233.  
  234.  
  235. int listObj(long data, int size)
  236.     {
  237.     int i;
  238.     if (listPtr - listData + 2*size + ELLIPSIS > DATA_SIZE)
  239.         if (cexFlag)
  240.             {
  241.             listLine();
  242.             listPtr = listData;
  243.             for (i = 0; i < LOC_FIELD+1; i++)
  244.                 *listPtr++ = ' ';
  245.             continuation = TRUE;
  246.             }
  247.         else
  248.             {
  249.             strcpy(listPtr, "...");
  250.             return NORMAL;
  251.             }
  252.     switch (size)
  253.         {
  254.         case BYTE : sprintf(listPtr, "%02X ", data & 0xFF);
  255.                     listPtr += 3;
  256.                     break;
  257.         case WORD : sprintf(listPtr, "%04X ", data & 0xFFFF);
  258.                     listPtr += 5;
  259.                     break;
  260.         case LONG : sprintf(listPtr, "%08lX ", data);
  261.                     listPtr += 9;
  262.                     break;
  263.         default   : printf("LISTOBJ: INVALID SIZE CODE!\n");
  264.                     exit(0);
  265.         }
  266.  
  267.     return NORMAL;
  268.     }
  269.  
  270.