逐行读取文件并在c中进行比较

时间:2014-09-17 05:40:17

标签: c unix binary hex

我不善于解释问题,但这是一次尝试。我必须创建一个c程序,它读取用汇编语言编写的文件(用于LC-2 ISA)并生成程序的十六进制表示。

所以,我的汇编程序应该能够提供基于LC-2的指令的翻译, 例如:LEA,AND,ADD,LDR,BR(它们有自己的二进制表示)。

基本上,您需要做的就是逐行读取文件并将二进制文件转换为十六进制。到目前为止,我只能读取第一行的前两个字符。这就是我的尝试(如果我不清楚这个问题,我会道歉) -

(我的问题是,我如何逐行阅读文件" File.asm"然后比较它们是' LEA'或者' ADD&#39 ;或者' AND'然后基于比较将二进制转换为十六进制?

#

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);
    }

1 个答案:

答案 0 :(得分:1)

请检查一下。

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

因此,您只获得第一行的前两个字符,并停止阅读更多文件。

您可以稍微使用fgets()并将其检查为null。

当fgets()给出null时,它就是文件的结尾。

您可以查看以下示例,该示例使用fgets()读取文件行并循环直到文件结尾。

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

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

然后你必须从该行获得前两个字符,然后你必须检查你的指示。