home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OBJASM.ZIP / OOUTPUT.C < prev    next >
Text File  |  1990-10-02  |  4KB  |  174 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "o.h"
  4.  
  5. /*
  6. ** Local Variables
  7. */
  8. int tab_at = 0;
  9. static int col_cnt = 0;
  10.  
  11. /*
  12. ** Local Prototypes
  13. */
  14. void tab_count( char * );
  15. void tab_seek( int );
  16. void tab_next( void );
  17. void out_label( char * );
  18. void out_opcode( char * );
  19. void out_operand( char * );
  20. void out_comment( char * );
  21. void out_endline( void );
  22. void out_newline( void );
  23. void out_directive( char * );
  24. void out_line( char *, char *, char *, char * );
  25.  
  26. void tab_count( text )
  27.     char    *text;
  28. {
  29.     int     count;
  30.  
  31.     count = strlen( text );
  32.     while ( count  ) {
  33.         if ( *text == '\t' ) {
  34.             col_cnt = (col_cnt | 0x07) + 1;
  35.         } else {
  36.             col_cnt++;
  37.         }
  38.         text++;
  39.         --count;
  40.     }
  41.     tab_at = (col_cnt >> 3) + 1;
  42. }
  43.  
  44.  
  45. void tab_seek( tab_count )
  46.     int     tab_count;
  47. {
  48.     if ( tab_count <= tab_at ) {
  49.         putc( '\n', stdout );
  50.         tab_count -= tab_offset;
  51.         tab_offset = 0;
  52.         tab_at = 0;
  53.         col_cnt = 0;
  54.     }
  55.     if ( tab_at == 0 ) {
  56.         tab_at = 1;
  57.     }
  58.  
  59.     while( tab_at < tab_count ) {
  60.         putc( '\t', stdout );
  61.         tab_at++;
  62.         col_cnt = (tab_at - 1) << 3;
  63.     }
  64. }
  65.  
  66. void tab_next()
  67. {
  68.     tab_seek( tab_at + 1 );
  69. }
  70.  
  71. void out_label( label )
  72.     char    *label;
  73. {
  74.     tab_seek( 1 );                  /* Start a new line if needed */
  75.     printf( "%s", label );
  76.     tab_count( label );
  77. }
  78.  
  79. void out_opcode( opcode )
  80.     char    *opcode;
  81. {
  82.     tab_seek( 2 + tab_offset );     /* Indent to first tab stop */
  83.     printf( "%s", opcode );
  84.     tab_count( opcode );
  85. }
  86.  
  87. void out_operand( operand )
  88.     char    *operand;
  89. {
  90.     if ( strcmp(operand,"") ) {
  91.         tab_next();                 /* Indent to next tab stop */
  92.         printf( "%s", operand );
  93.         tab_count( operand );
  94.     }
  95. }
  96.  
  97. void out_comment( comment )
  98.     char    *comment;
  99. {
  100.     if ( strcmp(comment,"") ) {
  101.         tab_seek( 7 );          /* Indent to next tab stop */
  102.         printf( "; %s", comment );
  103.         col_cnt += 2;
  104.         tab_count( comment );
  105.     }
  106. }
  107.  
  108. void out_endline()
  109. {
  110.     putc( '\n', stdout );
  111.     tab_offset = 0;
  112.     tab_at = 0;
  113.     col_cnt = 0;
  114. }
  115.  
  116. void out_newline()
  117. {
  118.     if ( tab_at != 0 ) {          /* Finish off previous line if needed */
  119.         out_endline();
  120.     }
  121.     out_endline();
  122. }
  123.  
  124. void out_directive( direct )
  125.     char    *direct;
  126. {
  127.     tab_next();
  128.     if ( tab_at < 2 ) {
  129.         tab_next();
  130.     }
  131.     printf( "%s", direct );
  132.     tab_count( direct );
  133. }
  134.  
  135. void out_line( label, opcode, operand, comment )
  136.     char    *label;
  137.     char    *opcode;
  138.     char    *operand;
  139.     char    *comment;
  140. {
  141.     out_label( label );
  142.     out_directive( opcode );
  143.     out_operand( operand );
  144.     out_comment( comment );
  145. }
  146.  
  147. char *out_hexize( offset, text, bytes )
  148.     unsigned long   offset;
  149.     char            *text;
  150.     int             bytes;
  151. {
  152.     char            temp[15];
  153.  
  154.     switch( bytes ) {
  155.         case 1: sprintf( temp, "%02Xh", (unsigned char)offset );
  156.                 break;
  157.         case 2: sprintf( temp, "%04Xh", (unsigned short)offset );
  158.                 break;
  159.         case 4: sprintf( temp, "%08lXh", offset );
  160.                 break;
  161.         default:
  162.                 fmt_error( "Internal error calling out_hexize()" );
  163.                 break;
  164.     }
  165.  
  166.     if ( temp[0] >= '0' && temp[0] <= '9' ) {
  167.         strcpy( text, temp );
  168.     } else {
  169.         sprintf( text, "0%s", temp );
  170.     }
  171.     return( text );
  172. }
  173. 
  174.