Hodson 0 Newbie Poster

I've got to write an application in C which can take an input file containing RISC instructions and then output (both to screen and an output file) the encoded version of these instructions.

I will be working with the following op-classes:

0
1A
2A
2B
2C
3A
3B
3C
3D

Now, when encoding op-class 3A I am using the following forumla:

code = valuewrd[0] * 1024 + valuewrd[1] * 64 + valuewrd[3]

I am lost though when it comes to encoding the other op-classes as I am guessing they will have a different formula?!

Could anyone help me out here by pointing me in the right direction.

Current code (doesn't output to file yet but I should be fine with adding that in myself):

/**************************************************************************/
/* Program to read into a buffer a string of text and break into words    */
/* Uses global data:                                                      */
/*     begwrd  -- array, holds start positions of words within string     */
/*     lenwrd  -- array, holds length of each word                        */
/*     numwrd  -- integer, holding number of words detected in string     */
/*                                                                        */
/* The buffer is searched for words until onreof the following is found:  */
/*        end-of-line found (EOL)                                         */ 
/*        end-of-file found (EOF)                                         */
/*        semi-colon found, indicates comments which are ignored          */
/* A word is delimited by one or more spaces, which include blanks and    */
/*  horizontal tabs. Or a word is delimited when one of the following     */
/*  special single-character words are found:                             */
/*        comma                                                           */
/*        hyphen                                                          */
/*        opening bracket                                                 */
/*        closing bracket                                                 */
/*        asterisk (star)                                                 */
/**************************************************************************/
                             /* declare library headers */
      #include <stdio.h>
      #include <string.h>
      
                             /* declare prototypes */
      int  findwords( char buffer[], int * pnumwrd );
      void identify( char buffer[] );
      
                             /* define constant data */
      #define MAXWRD 20      /* maximum number of words allowed */
      #define size 65
                             /* declare global data */
      int numwrd;            /* number of words found in buffer */
      int begwrd[MAXWRD];    /* start position of each word found in buffer */
      int lenwrd[MAXWRD];    /* length of each word found in buffer */
      char typewrd[MAXWRD][13];
      int valuewrd[MAXWRD];
      
      
      
      char symbol[size][13] = {
           "CLR", "DEC", "INC", "NEG", "CPL", 
           "ADD", "ADC", "SUB", "SBC", "CMP", 
           "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8",
           "R9", "R10", "R11", "R12", "SP", "PC", "FR",
           "," , "-" , "*" , "(" , ")" };
      
      char type[size][13] = {
           "2A", "2A", "2A", "2A", "2A",
           "3A", "3A", "3A", "3A", "3A",    
           "REG", "REG", "REG", "REG", "REG", "REG", "REG", "REG", "REG",
           "REG", "REG", "REG", "REG", "REG", "REG", "REG",
           "COMMA" , "HYPHEN" , "STAR" , "OPENBRA" , "CLOSEBRA" };

      int value[size] = {
           116, 117, 118, 119, 120, 
           132, 133, 134, 135, 136,  
           200, 201, 202, 203, 204, 205, 206, 207, 208,
           209, 210, 211, 212, 211, 212, 213, 
           401, 402, 403, 404, 405  };

      
      
      

/**************************************************************************/
/* Main function                                                          */
/* Prompts for an reads in a line of text. Which is then seached for its  */
/*  individual words.                                                     */
/**************************************************************************/
  int main( void)
  {
      char buffer[256];      /* declare sufficiently big buffer */
      int line;              /* declare line number */
      char filename[60];     /* declare text string space for file name */
      FILE * stream;         /* declare input stream name */
      int status;            /* declare a status variable */
      int i,j;               /* declare loop counter */
      char wordy[20];        /* declare test character array */
        int code;    
/* -----------------------------------------------------------------------*/
                             /* prompt for and read in file name */
      printf("\nPlease enter name of file, including extension >");
      scanf("%s", filename );
      fflush( stdin );
      
                             /* attempt to open file */
      stream = fopen( filename, "rt" );
                             /* finish if cannot open file, reporting */
      if( stream == NULL )
      {
          printf("\nUnable to open file %s", filename );
          return 1;
      }

                             /* else file is open, reset line number count */
      line = 0;
     
                             /* enter while loop with test condition to */
                             /* continue being next line is not EOF */
      while( ! feof( stream ) )
      {
                             /* read in next line to buffer*/
          fgets( buffer, 255, stream );
                             /* break into words */
          status = findwords( buffer, &numwrd );         
          identify( buffer );
          if( strcmp( typewrd[0], "3A" ) == 0 )
                code = valuewrd[0] * 1024 + valuewrd[1] * 64 + valuewrd[3] ;
 
                            /* output line read into buffer */
          printf("\nline[%d]: %s", line, buffer );
          
                            /* if status is not 0 then report full */
          if( status != 0 )
              printf("\nToo many words in line");
                            /* else output word information */
          else
          {
                            /* output number of words formed */
              printf("\nNumber of words formed is %d", numwrd );
                            /* take each word in turn and output its */
                            /* starting position and length */
                            /* plus the word itself */
              for( i=0; i<numwrd; i++ )
              {
                            /* load word into temp buffer */
                   wordy[0]= '\0';
                   if( lenwrd[i] > 0 )
                   {
                       for( j=0; j<lenwrd[i]; j++ )
                       {
                            wordy[j]=buffer[begwrd[i]+j];
                       }
                       wordy[ lenwrd[i] ] = '\0' ;
                   }  
                           /* output information */
                   printf("\nWord %2d:  Starts %4d  Len %4d type %s  value %d, code %X,  %s",
                       i, begwrd[i], lenwrd[i], typewrd[i], valuewrd[i], code, wordy );
              }
          }
          
                            /* increment line number for next line */
          line = line + 1;
                            /* pause to view output */
          printf("\n\nPress enter for next line");
          getchar();            
      }

                             /* finished reading file */
      printf("\nFinished reading file");
                            /* pause to view output */
      printf("\n\nPress enter for next line");
      getchar();            

      return 0;
  }
/********************************************************************/
/* identify                                                         */
/* identifys found words                                           */
/*********************************************************************/
  void identify( char buffer[] )
  {
       int i, j;
       char wordy[13];
       
/* -------------------------------------------------------- */
      for(i=0; i<numwrd; i++)
      {
                                            /* set defaults */
                   strcpy( typewrd[i] , "UNKNOWN" );
                   valuewrd[i] = 0;
                   
                                           /* load word into temp buffer */
                   wordy[0]= '\0';
                   if( lenwrd[i] > 0 )
                   {
                       for( j=0; j<lenwrd[i]; j++ )
                       {
                            wordy[j]=buffer[begwrd[i]+j];
                       }
                       wordy[ lenwrd[i] ] = '\0' ;
                   }
                   
                   
                                   /* search for a symbol match */  
             for( j=0; j<size; j++ )
             {
                  if( strcmp( wordy, symbol[j] )== 0 )
                  {
                         strcpy( typewrd[i] , type[j] ) ;
                         valuewrd[i] = value[j] ;
                   }
              }  

       }
       
       
       
   }





/**********************************************************************/
/* Function findwords                                                 */
/*                                                                    */
/* Search given Buffer for words - returns status 0=OK, 110=FULL      */
/*   buffer  - array holding string to be searched                    */
/*   pnumwrd - number of words formed (pointer to) - returned         */
/* The buffer is searched for words until the end of the buffer is    */
/* reached or when one of the following are found:                    */
/*    comment symbol (semi-colon)  (';')                              */
/*    end of line symbol           (10 or 13)                         */
/*    end of file reached                                             */
/* A word is deliminated by one or more spaces,including horizontal   */
/* tabs, or when one of the following special, single character,      */
/* words are found:                                                   */
/*    comma                                                           */
/*    hyphen                                                          */
/*    open bracket                                                    */
/*    closing bracket                                                 */
/*    asterisk (star)                                                 */
/* Uses Global information (updated)                                  */
/*   begwrd   - Character number where each word begins.              */
/*   lenwrd   - Number of characters in each word (thier lengths)     */
/**********************************************************************/
int  findwords( char buffer[], int * pnumwrd )
{
    int status;              // status to be returned
                             // 0=OK, else 110=FULL too many words
    int inword;              // used as a flag to say if in word or not.
                             // 0=Not-in-word. 1=In-word
    int numwrd;              // number of words formed
    int length;              // length of characters in buffer
    int i;                   // loop counter
/*--------------------------------------------------------------------*/

    status = 0;              // reset status to be OK
    numwrd = 0;              // reset number of words
    inword = 0;              // reset flag to Not-in-word state

                             // find number of characters in buffer
    length = strlen( buffer );
                             // take each character of the buffer in turn
    for( i=0; i<length; i++ )
    {
                             // end search if end of line character
        if( buffer[i] == 10 )break;
                             // end search if carriage return character
        if( buffer[i] == 13 )break;
                             // end search if strat of comment
        if( buffer[i] == ';' )break;

                             // enter switch structure based on character
        switch( buffer[i] )
        {
                             // case where char is blank or horizontal tab
                             // this just sets word flag to Not-in-word
         case ' ':
         case   9: inword = 0;
                   break;
                             // special single words of comma, hyphen,
                             // open bracket, close bracket, and star
                             // These create a new word of length 1, but
                             // resets word flag into Not-in-word state
         case ',':
         case '-':
         case '(':
         case ')':
         case '*': if( numwrd < MAXWRD )
                   {
                             // record location & set length of 1
                       begwrd[numwrd] = i;
                       lenwrd[numwrd] = 1;
                             // increment number of words
                       numwrd = numwrd +1;
                   }
                             // else no room for more words
                   else
                   {
                       status = 110;
                   }
                             // always force word flag into Not-in-word state
                   inword = 0;
                   break;
                             // Otherwise its found a character, test to
         default :
                             // if word flag is in Not-in-word state,
                             // create a new word with initially length 1
                   if( inword == 0 )
                   {
                       if( numwrd < MAXWRD )
                       {
                             // record location & set length of 1
                           begwrd[numwrd] = i;
                           lenwrd[numwrd] = 1;
                             // increment number of words
                           numwrd = numwrd +1;
                            // and set word flag as In-word
                           inword = 1;
                       }
                            // else no room for more words
                       else
                       {
                           status = 110;
		       }
                   }
                            // else word flag is In-word, merely extend the
                            // length of the existing word
                            // But only if valid word
                   else
                   {
                       if( status == 0 )lenwrd[numwrd-1]++;
                   }
                   break;
        }
    }
                            // finished, return number of words via
                            // its given address
    *pnumwrd = numwrd;
                            // return status
    return status;
}

/**********************************************************************/