首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >逐行读取文件,并在c中进行比较。

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

Stack Overflow用户
提问于 2014-09-17 13:40:17
回答 1查看 948关注 0票数 1

我不太擅长解释问题,但我可以试一试。我必须创建一个c程序来读取用汇编语言编写的文件(对于LC-2 ISA),并产生该程序的十六进制表示。

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

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

(同样,我的问题是,我如何逐行读取文件"File.asm“,然后比较它们是”LEA“、”ADD“还是”and“,然后根据比较将二进制转换为十六进制?

代码语言:javascript
复制
#

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

回答 1

Stack Overflow用户

发布于 2014-09-17 16:55:06

看看这个。

代码语言:javascript
复制
// 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

然后,您必须从该行中获取前两个字符,然后您必须检查您的指令。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25883045

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档