简体   繁体   中英

Reading a file line by line and compare in c

I am not very good at explaining problems, but here's a try. I have to create ac program that reads a file written in assembly language (for the LC-2 ISA) and produces a hexadecimal representation of the program.

So, my assembler should be able to provide translations for the LC-2 based instructions, for eg: LEA, AND, ADD, LDR, BR (and they have their own binary representation).

So basically, all you need to do is read a file line by line and convert the binary to hexadecimal. So far, I have been able to only read the first two characters of the first line. Here's what I have attempted (and I apologize if I have not been clear with the question - again) -

(again my question is, how do I read the file "File.asm" line by line and then compare if they are 'LEA' or 'ADD' or 'AND' and then based on the comparison convert the binary to hexadecimal?

#

include<stdio.h>
    #include<stdlib.h>



    #define LEA "1110"
    #define AND "0101"
    #define ADD "0001"
    #define LDR "0110"
    #define TRAP "1111"
    #define BR "0000"
    #define Base "0011"
    #define ST "0011"
    #define DR "10"
    #define SR "010"
    #define OFF9 "01"
    #define SR1 "0101"
    #define SR2 "0111"
    #define index6 "0101"
    #define nato "0000"
    #define zato "0001"
    #define pato "0000"
    #define TVECT "00100101"
    #define OFF5 "0101"



    void asm_reader();
    void convert(char binaryNumber[1000]);

    int main()
    {
       asm_reader();


       return 0;
    }


    void asm_reader()
    { 
        FILE *myFile;
        myFile = fopen("File.asm", "r");
        char line[80];
        char array[2];
        int i;
             if (myFile == NULL)
             {
                printf("Error Reading File\n");
                exit (0);
             }
      while(fgets(array, 80, myFile) != NULL)
      {
    // Checks first 2 instruction characters
        for (i = 0; i < 2; i++)
           {
              fscanf(myFile, "%c,", &array[i] );
           }


        printf("Number is: %c", array[0]);

    // LEA condition________________________________________
      if ( array[0] == 'L'&& array[1]=='E')
    {
       //get whole line of instruction convert to binary and then to hex
       char x[50]=LEA;
       char y[50]=DR;
       char z[50]=OFF9;

       convert(x);
       convert(y);
       convert(z);

    }

    // AND* condition________________________________________
      if ( array[0] == 'A'&& array[1]=='N'){


       char x[10]=AND;
       char y[10]=SR1;
       char z[10]=SR2;

       convert(x);
       convert(y);
       convert(z);
     }

    // LDR condition________________________________________
      if ( array[0] == 'L'&& array[1]=='D'){
    //get whole line of instruction convert to binary and then to hex

    char x[10]=LDR;
    char y[10]=DR;
    char t[10]=Base;
    char z[10]=index6;

    convert(x);
    convert(y);
    convert(t);
    convert(z);

        }

    //____________________________________________________

    // BR condition________________________________________
      if ( array[0] == 'B'&& array[1]=='R'){
    //get whole line of instruction convert to binary and then to hex

    char x[10]=BR;
    char n1[10]=nato;
    char z1[10]=zato;
    char p1[10]=pato;
    char y[10]=OFF9;


    convert(x);
    convert(n1);
    convert(z1);
    convert(p1);
    convert(y);
        }


    //____________________________________________________

    // ADD* condition________________________________________
      if ( array[0] == 'A'&& array[1]=='D'){
    //get whole line of instruction convert to binary and then to hex

    char x[10]=ADD;
    char n1[10]=DR;
    char z1[10]=SR1;
    char y[10]=OFF5;


    convert(x);
    convert(n1);
    convert(z1);
    convert(y);

        }



    //____________________________________________________


    // TRAP condition________________________________________
      if ( array[0] == 'T'&& array[1]=='R'){
    //get whole line of instruction convert to binary and then to hex
    char x[10]=TRAP;
    char n1[10]=DR;
    char z1[10]=SR1;
    char y[10]=OFF5;


    convert(x);
    convert(n1);
    convert(z1);
    convert(y);
        }



    //____________________________________________________

    // ST condition________________________________________
      if ( array[0] == 'S'&& array[1]=='T'){
    //get whole line of instruction convert to binary and then to hex
    char x[10]=ST;
    char n1[10]=SR;
    char z1[10]=OFF9;


    convert(x);
    convert(n1);
    convert(z1);
    }

    //____________________________________________________

        fclose(myFile);


     }
    }

    //Converter Method. From binary to Hex
    void convert(char binaryNumber[1000]){


     int temp;
    long int i=0,j=0;

    char hexaDecimal[1000]; 

     while(binaryNumber[i]){
          binaryNumber[i] = binaryNumber[i] -48;
          ++i;
       }

       --i;
       while(i-2>=0){
           temp =  binaryNumber[i-3] *8 + binaryNumber[i-2] *4 +  binaryNumber[i-1] *2 + binaryNumber[i] ;
           if(temp > 9)
                hexaDecimal[j++] = temp + 55;
           else
                hexaDecimal[j++] = temp + 48;
           i=i-4;
       }

    if(i ==1)
          hexaDecimal[j] = binaryNumber[i-1] *2 + binaryNumber[i] + 48 ;
       else if(i==0)
          hexaDecimal[j] =  binaryNumber[i] + 48 ;
        else
          --j;

       printf("Equivalent hexadecimal value: ");
    FILE *opFile;
    opFile = fopen("mama.asm", "a");
       while(j>=0){

    fprintf(opFile,"%s",&hexaDecimal[j]);

          printf("%c",hexaDecimal[j--]);

       }
    fclose(opFile);
    }

Just check this out.

// Checks first 2 instruction characters
for (i = 0; i < 2; i++)
{
    fscanf(myFile, "%c,", &array[i] );
}

Because of this you are getting only first two character of only first line and stopped reading further file.

You can use somewhat fgets() and check it for null.

When the fgets() gives null it is the end of file.

You can check the following example which makes the use of fgets() to read file line And loop until end of file.

http://www.phanderson.com/files/file_read.html

http://www.mathworks.in/help/matlab/ref/fgets.html

Then you have to get first two characters from that line and then you have to check for your instructions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM