0

I'm doing some c system programming in ubuntu. I originally wrote the code in windows and it works just fine. The goal is to make an assembler written in C based on the SIC/XE format. I transferred it over to ubuntu and I'm now running to errors. I'm getting the Segmentation fault (core dumped) error because of this function.

void pass1P(char string[256]){
//f1 = test.txt /readable
//f2 = optab.txt /readable
//f3 = symtab.txt /writable
//f4 = errorLog.txt /writable
FILE *f1,*f2,*f3,*f4,*f5;

int lc,sa,l,op1,o,len,errorLen;
int j = 0;
char m1[20],la[20],op[20],otp[20];
char hex[20];
f1=fopen(string,"r");
f3=fopen("symtab.txt","w");
f4=fopen("errorLog.txt","w");


fscanf(f1,"%s %s %d",la,m1,&op1);

if(strcmp(m1,"START")==0){
        sa=op1;
        lc=sa;
        sprintf(hex, "%x", op1);
        printf("\t%s\t%s\t%s\n",la,m1,hex);
}
else{
    lc=0;
    fprintf(f4,"\nMissing START directive\n");
}


fscanf(f1,"%s %s",la,m1);

while(!feof(f1)){

    fscanf(f1,"%s",op);

    sprintf(hex, "%x", lc);

    printf("\n%s\t%s\t%s\t%s\n",hex,la,m1,op);
printf("Made It");

    if(strcmp(la,"-")!=0){
        sprintf(hex, "%x", lc);
        fprintf(f3,"\n%s\t%s\n",hex,la);
        errorLen++;
    }

    f2=fopen("optab.txt","r");
    fscanf(f2,"%s %x",otp,&o);
    while(!feof(f2)){
        if(strcmp(m1,otp)==0){
            lc=lc+3;
            j = 1;
            break;
        }

        if((strcmp(m1,"RESW")==0)||(strcmp(m1,"WORD")==0)||(strcmp(m1,"END")==0)||(strcmp(m1,"BYTE")==0)||(strcmp(m1,"RESB")==0)){
            j = 1;
            break;
        }

        fscanf(f2,"%s %x",otp,&o);
    }
    if(j==0){
            fprintf(f4,"\nIllegal Operand at line %s\n",hex);
        }
    j=0;
    fclose(f2);
    if(strcmp(m1,"WORD")==0){
        if(strcmp(op,"-")==0){
            fprintf(f4,"\nMissing operand on data storage directive at line %s\n",hex);
        }
        else{
            lc=lc+3;
        }

    }
    else if(strcmp(m1,"RESW")==0){
        if(strcmp(op,"-")==0){
            fprintf(f4,"\nMissing operand on data storage directive at line %s\n",hex);
        }
        else{
            op1=atoi(op);
            lc=lc+(3*op1);
        }
    }

    else if(strcmp(m1,"BYTE")==0){
        if(strcmp(op,"-")==0){
            fprintf(f4,"\nMissing operand on data storage directive at line %s\n",hex);
        }
        else{
            if(op[0]=='X')
                lc=lc+1;
            else{
                len=strlen(op)-2;
                lc=lc+len;}
        }
    }

    else if(strcmp(m1,"RESB")==0){
        if(strcmp(op,"-")==0){
            fprintf(f4,"\nMissing operand on data storage directive at line %s\n",hex);
        }
        else{
            op1=atoi(op);
            lc=lc+op1;
        }
    }

    fscanf(f1,"%s%s",la,m1);
}
f5=fopen("programLen.txt","w");
if(strcmp(m1,"END")==0){
    printf("\nProgram length = %d\n\n",lc-sa);
    fprintf(f5,"%d",lc-sa);
}
else{
    printf("\nProgram length = %d\n\n",lc-sa);
    fprintf(f4,"\nEND Directive not found at line %s\n",hex);
    fprintf(f5,"%d",lc-sa);
}
if(errorLen>500)
    fprintf(f4,"\nExceeded the 500 max distinct labels at line %s\n",hex);

fclose(f1);
fclose(f3);
fclose(f4);
fclose(f5);}

The function is suppose to read a text file and create a symbol table based on the content on the file. When running it, it would only print about two lines before crashing. I've tried correcting the problem my self but I'm keep hitting a brick wall. What is the correct way to fix this segmentation fault error?

  • 2
    Check the return values from `fopen` and `fscanf` for a start – Ed Heal Oct 25 '17 at 03:13
  • 1
    Also read https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong - i.e. your while condition is wrong – Ed Heal Oct 25 '17 at 03:14
  • That the code appears to do what you expect when compiled by one compiler and run on one system is not proof that the code is conforming or correct. – John Bollinger Oct 25 '17 at 03:36
  • Works on windows, not on linux? It may be a line ending problem (`\n` vs `\r\n`) – Mathieu Oct 25 '17 at 07:05
  • Also, read [how to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Mathieu Oct 25 '17 at 07:23

0 Answers0