分段故障fgets()两个传递汇编程序

时间:2016-05-13 14:43:12

标签: c

 #include<string.h>

typedef struct{
        char Mnemonic[7];   
        int code;
        } code;

typedef struct{
        char label[10];   
        unsigned int location;
        } symbolTab;

int opLook(char * mnemonic, code * optable)
{   
    int i = 0;
    int value = 0;
    for(i ; i<25 ; i++)
    {
        if(strcmp(mnemonic, optable[i].Mnemonic) == 0)
        {
            value = optable[i].code;
            return value;
        }
    }

    return value;

}


int opValid(char * mnemonic, code * optable)
{   
    int i = 0;
    for(i ; i<25 ; i++)
    {
        if(strcmp(mnemonic, optable[i].Mnemonic) == 0)
        {
            return 1;
        }
    }

    return 0;

}

int labelcheck(char * label, symbolTab * Table,  int counter)
{
    int i = 0;
    int flag = 0;

    if (counter == 0)
    {
        return flag;
    }
    else
        {
            for(i; i <counter; i ++)
            {
                if(strcmp(label, Table[i].label) == 0)
                {
                    flag = 1;
                }

            }

            return flag;
        }
}




unsigned int labelVal(char * label, symbolTab * Table,  int counter)
{
    int i = 0;

    for(i; i <counter; i ++)
        {
            if(strcmp(label, Table[i].label) == 0)
                {
                    return Table[i].location;
                }
        }


}





void Assemble(char* filename)
{
    unsigned int locctr = 0;  /*location counter*/
    unsigned int prolen = 0;    /*program length*/
    int mnemoVal;  /*mnemonic Value in Int*/ 
    int labelctr = 0;
    code Opta[25] =  {{"ADD", 24},{"AND",88},{"COMP",40},{"DIV",36},{"J",60},
                   {"JEQ",48},{"JGT",52},{"JLT",56},{"JSUB",72},{"LDA",00},
                   {"LDCH",80},{"LDL", 8},{"LDX", 4},{"MUL",32},{"OR",68},
                   {"RD",216},{"RSUB",76},{"STA",12},{"STCH",84},{"STL",20},
                   {"STX",16},{"SUB",28},{"TD",224},{"TIX",44},{"WD",220}};
    symbolTab symTab[500];             
    char  buffer[255];
    FILE * source;
    FILE * interFile;
    FILE * objF;
    FILE * ListFile;
    interFile = fopen("intermidiateFile.txt", "w+");
    ListFile = fopen("ListingFile.txt", "w+");
    objF = fopen("ObjectFile.txt", "w+");
    source = fopen("source.txt", "r");
    char * lab;       /*label*/
    char * mnemo;   /*mnemonic*/
    char * operand;
    char * address = "adress";
    unsigned int opeaddress;



    fgets(buffer, 255, source);

    if (buffer[0] != '.')
    {
        fprintf(interFile, "%s", buffer);
    }

     /*Getting first line and initialization of Location Counter*/

    if(buffer[0] == '.')               /* if the line is a comment continue with the next iteration of the loop*/
        {
            locctr = 0;
        }
    else
    {
        if(buffer[0] == ' ' || buffer[0] == '\t')
        {
            mnemo = strtok(buffer, " \t");
            operand = strtok(NULL, " \t");
            if(strcmp(mnemo, "START") == 0)
            {
                locctr = strtol(operand, NULL, 16);
            }

            else
            {
                    /*error*/
            }

        }

        else
        {
            lab =strtok(buffer, " \t");
            mnemo = strtok(NULL, " \t");
            operand = strtok(NULL, " \t");

            if(strcmp(mnemo, "START") == 0)
            {
                locctr = strtol(operand, NULL, 16);
            }

            else
            {
                    /*error*/
            }


        }

    }


/* End of the location counter initialization */


  /*start while loop*/

    while(!feof(source))
    {   
            memset(lab, '\0', strlen(lab));
            memset(mnemo, '\0', strlen(mnemo));
            memset(operand, '\0', strlen(operand));

            fgets(buffer, 255, source);

            fprintf(interFile, "%s", buffer);

            if(buffer[0] == '.')               /* if the line is a comment continue with the next iteration of the loop*/
            {
                continue;
            }

            else            /* Else for... If it is not a comment, then check if it start with a character or a space*/
            {

                if(buffer[0] == ' ' || buffer[0] == '\t')    /* If it start with a space, then it is just a mnemonic or mnemonic & operand*/
                {
                    mnemo = strtok(buffer, " \t\n\r");
                    if (strcmp(mnemo, "END") == 0)
                    {
                    break;
                    }

                    if(strcmp(mnemo, "RSUB") == 0)
                    {
                        mnemoVal = opLook(mnemo, Opta);
                        fprintf(interFile, "%x %02x\n", locctr, mnemoVal);
                    }

                    else
                    {
                        operand = strtok(NULL, " \t\r\n");
                        mnemoVal = opLook(mnemo, Opta);
                        fprintf(interFile, "%x %02x %s\n", locctr, mnemoVal, operand);
                    }

                }

                else    

                {   

                    lab = strtok(buffer, " \t\n\r");   /* it has a label, mnemonic and operand*/

                    if(labelcheck(lab, symTab, labelctr) == 0)    /* check if the label is already in the symTab, if not, add it, otherwise it is an error*/
                    {                   
                        strcpy(symTab[labelctr].label, lab);    
                        symTab[labelctr].location = locctr;
                        labelctr++;
                        mnemo = strtok(NULL, " \t\n\r");
                        if (strcmp(mnemo, "END") == 0)
                        {   
                            break;
                        }
                        operand = strtok(NULL, " \t\n\r");
                        mnemoVal = opLook(mnemo, Opta);
                    }   


                    else
                    {
                        mnemo = strtok(NULL, " \t\n\r");
                        if (strcmp(mnemo, "END") == 0)
                        {
                            break;
                        }
                        operand = strtok(NULL, " \t\n\r");
                        mnemoVal = opLook(mnemo, Opta);

                    }

                    fprintf(interFile, "%x %s %02x %s\n", locctr, lab, mnemoVal, operand);


                }



            }


            if(strcmp(mnemo, "WORD") == 0 )
            {
                locctr = locctr + 3;
            }


            else if(strcmp(mnemo, "BYTE") == 0 )
            {
                unsigned int val;
                if (operand[0] =='C')
                { 
                    val = strlen(operand) - 3;
                    locctr = locctr + val;
                }

                else
                {
                    val = (strlen(operand) - 3)/2;
                    locctr = locctr + val;
                }
            }

            else if(strcmp(mnemo, "RESB") == 0)
            {
                locctr = locctr + atoi(operand);
            }

            else if(strcmp(mnemo, "RESW") == 0)
            {
                locctr = locctr + (3*atoi(operand));
            }


            else 
            {
                locctr= locctr + 3;
            }



    }


            /* End of While loop*/

        prolen = locctr - symTab[0].location;

        fprintf(interFile, "\n%x", prolen);




    fclose(source);
    fclose(interFile);



    interFile = fopen("intermidiateFile.txt", "r");

    /*Start the Listing File and Object File        ---------------------Pass 2-----------    */

        fgets(buffer, 255, interFile);


    if(buffer[0] == '.')               /* if the line is a comment continue with the next iteration of the loop*/
        {
            locctr = 0;

            /*Error missung Start or Misplaced*/
        }
    else
    {
        if(buffer[0] == ' ' || buffer[0] == '\t')
        {
            mnemo = strtok(buffer, " \t");
            operand = strtok(NULL, " \t");
            if(strcmp(mnemo, "START") == 0)
            {
                locctr = strtol(operand, NULL, 16);
                strcpy(address, operand);

                fprintf(ListFile, "%X %s %s\n", locctr, mnemo, operand);
            }

            else
            {
                    /*error*/
            }

        }

        else
        {
            lab =strtok(buffer, " \t");
            mnemo = strtok(NULL, " \t");
            operand = strtok(NULL, " \t");

            if(strcmp(mnemo, "START") == 0)
            {
                locctr = strtol(operand, NULL, 16);
                fprintf(ListFile, "%x %s %s %s\n", locctr, lab, mnemo, operand);

                fprintf(objF, "H%s__%06x%06x\n", lab, locctr, prolen);

            }

            else
            {
                /*error*/
            }
        }
    }



    while(!feof(interFile))
    {

        memset(lab, '\0', strlen(lab));
        memset(mnemo, '\0', strlen(mnemo));
        memset(operand, '\0', strlen(operand));
        memset(address, '\0', strlen(address));
        memset(buffer, '\0', strlen(buffer));

        fgets(buffer, 255, interFile);

        if (buffer[0] == '\r')
        {
            continue;
        }



        if(buffer[0] == ' ' || buffer[0] == '\t')
        {
            mnemo = strtok(buffer, " \t\n\r");
            if (strcmp(mnemo, "END") == 0)
            {
                break;
            }

            if(strcmp(mnemo, "RSUB") == 0)
            {
                memset(buffer, '\0', strlen(buffer));
                fgets(address, 255, interFile);

                mnemoVal = opLook(mnemo, Opta);
                fprintf(ListFile, "%s %s %X0000", address, mnemo, mnemoVal);
            }
            else
            {
                operand = strtok(NULL, " \t\r\n");
                mnemoVal = opLook(mnemo, Opta);
                memset(buffer, '\0', strlen(buffer));
                fgets(address, 255, interFile);
                if (labelcheck(operand, symTab, labelctr) == 1)
                {
                    opeaddress = labelVal(operand, symTab, labelctr);
                }

                else 
                {
                    opeaddress = 0;
                    /* error*/
                }


                fprintf(ListFile, "%s %s %s %02X%04X", address, mnemo, operand, mnemoVal, opeaddress);
            }           


        }


        else if (buffer[0] == '.')
        {
            fprintf(ListFile, "%s\n", buffer);
        }


        else
        {
            lab = strtok(buffer, " \t\n\r");
            mnemo = strtok(NULL, " \t\n\r");
            operand = strtok(NULL, " \t\n\r");
            mnemoVal = opLook(mnemo, Opta);  
            memset(buffer, '\0', strlen(buffer));
            fgets(address, 255, interFile);
            if (labelcheck(operand, symTab, labelctr) == 1)
                {
                    opeaddress = labelVal(operand, symTab, labelctr);
                }

            else 
                {
                    opeaddress = 0;
                    /* error*/
                }
            fprintf(ListFile, "%s %s %s %s %02X%04X", address, lab, mnemo, operand, mnemoVal, opeaddress);  


        }

    }






    fclose(interFile);
    fclose(objF);
    fclose(ListFile);
}

执行fgets()时,在最后一个while循环中发生错误; 我看过是否有任何变量或语法错误,但一切都很好。 这是两次传球组合的一部分。错误在最后一个while循环中的fgets之间变化。起初我以为这是memset函数,但是当我将它们标记为注释时错误仍然发生

1 个答案:

答案 0 :(得分:1)

因为

char * address = "adress";

address指向只读内存

尝试修改string literals(在您的情况下为"adress")会调用undefined behavior

然后在你的最后一个循环中

  • strcpy(address, operand);
  • memset(address, '\0', strlen(address));
  • fgets(address, 255, interFile);

错了。

您可以将该变量更改为一个简单的字符数组,并为您提供足够的空间,例如:

char address[128];