LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   gcc, segmentation fault, though compiles... (https://www.linuxquestions.org/questions/programming-9/gcc-segmentation-fault-though-compiles-256504/)

scratch09 11-18-2004 01:06 PM

gcc, segmentation fault, though compiles...
 
I'm doing my college assignment on Assembler for a simplified architecture of a virtual computer.
I have written the code, I used Borlandc (windows) to compile it, I was able to run there.
But in linux gcc just compiles the code but while executing generates an segmentation fault...
Please help, any body.

itsme86 11-18-2004 01:14 PM

How can we tell you what's causing the seg fault if you don't show us the code?

jinksys 11-18-2004 08:34 PM

Well if it runs under a win9x box and not under linux it probebly has to due with
memory protections. Does your process try to access other processes memory
areas?

Chrax 11-18-2004 08:57 PM

Hint: if you're going to ask questions about code, give a snippet of code that we can look at. You've given us nothing to work with. Don't take this hard or anything, but when you ask a question, consider for a moment everything you'd ask someone you're trying to help, and give us the answers ahead of time to avoid the need to actually ask them, and we can get on to problem solving.

So here's some preliminary questions: what was the code, what were your compile commands, and did you make sure to check that you renamed anything that might involve files to be appropriate to the system it's being run on (i.e. c:\... or /...), anything else I should know?

scratch09 11-20-2004 02:22 PM

Ok, sorry i was late. I'm providing the whole code here:
NOTE: All the required instructions are on structure OPTAB.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h> //for int toascii(int)
#define opsz 20
#define symsz 1000

struct table{
char symbol[10];
int value;
};
struct table optab[opsz]; //operation table
struct table symtab[symsz]; //Symbol table

//fucntion prototyping
void createoptab(struct table[]);
void initsymtab(struct table[]);
int fetch(char*,char*,char*,char*,char*);
int passone(char*,char*);
int passtwo(char*,char*);

int main(int argc,char* argv[]){
char *str;
char fld1[8],fld2[8],fld3[8],fld4[8];
if(argc<2){
puts("ERR: Syntax. \nPass Source Code as cmdline argument");
exit(0);
}
puts("PASS 1");
passone(argv[1],"FILE.INT");
puts("Intermediate file: FILE.INT");

puts("PASS 2");
passtwo("FILE.INT","FILE.OJB");
puts("Object file: FILE.OJB");
puts("DONE");
return 1;
}

int passtwo(char *inter,char *bin){
FILE *in, *out; //io streams
char fld1[8], fld2[8], fld3[8], fld4[8]; //tmp str for fetch
char loc[8], lbl[8], opc[8], args[8]; //str for respective values
char str[40], buff1[50], buff2[50]; //tmp str during decoding lang.
char tmpstr[30]; //tmpstr
int start, found=0;
int i, count; //tmp
long j, recstart;
in = fopen(inter,"r"); //intermediate file
out= fopen(bin,"w+"); //binary file
puts("\tHeader Record");
//Header Record Processing...
fgets (str, 40, in); //1. get first line of code
fetch (fld1, fld2, fld3, fld4, str); //2. fetch the fields
fprintf(out, "%-1s", "H"); //3. set col1 = H
fprintf(out, "%-6s", fld2); //4. set col2-7= program name, the label with START
start= atoi(fld1);
fprintf(out,"00%-4s\n",itoa(start,tmpstr,16)); //5. set col8-13= starting add of program
puts("\tText Record");
//Text Record Processing...
strcpy(buff1,"\0"); //flushing,
strcpy(buff2,"\0");
count=0;
recstart=start;
while(!feof(in)){ //up to end-of-file
fgets(str,40,in); //fetch line by line
count++; //increment counter of lines
//flushing:
strcpy(loc,"\0"); strcpy(lbl,"\0");
strcpy(opc,"\0"); strcpy(args,"\0");
fetch(fld1,fld2,fld3,fld4,str); //fetch fields
//find loc,lbl,opc,args.
if(fld2[0]==':'){ //contains label
strcpy(loc,fld1); strcpy(lbl,fld2);
strcpy(opc,fld3); strcpy(args,fld4);
}
else{ //no label
strcpy(loc,fld1);
strcpy(opc,fld2);
strcpy(args,fld3);
}
if(!strcmp(opc,"END")){
puts("\nEND encountered");
break; //program ended
}
//find instruction opcode in OPTAB
found=0;
for(i=0;i<opsz;i++) {
if(!strcmp(optab[i].symbol,opc)) { //found
found=1; break;
} //endif
}//endfor

if(found!=0){ //success, OPCODE found in OPTAB.
strcat(buff2,itoa(optab[i].value,tmpstr,16)); //append opcode to buffer buff2
if(strlen(args)>0) //if instruction has argument
{
found=0; //find value of args from symtab
for(i=0;i<symsz;i++){ //serach arg,in symtab.
if(!strcmp(symtab[i].symbol,args))
{found=1; break;}
}
if(found==0) { printf("ERR:Undefined Symbol\n%s %s %s<--",lbl,opc,args);
exit(0);
}
//else:buff2=opcode+arg
if(symtab[i].value>16) //if not a register fill spaces with zeroes
{
if(symtab[i].value>0xfff) strcat(buff2,""); //for 4 digit
else if(symtab[i].value>0xff) strcat(buff2,"0"); //3 digit
else if(symtab[i].value>0xf) strcat(buff2,"00"); //for 2 digit
else strcat(buff2,"000"); //1 digit
} //end filling spaces
//add OPCODE tobuffer.
strcat(buff2,itoa(symtab[i].value,tmpstr,16));
} //endif, with argument.
}// endif opcode found

else{ //OPCODE not found, then it is a directive.
if(!strcmp(opc,"BYTE")){
if(args[0]=='C'||args[0]=='c'){ //if char, convert to ascii value.
for(i=2;i<(strlen(args)-1);i++){ // args[0]=C,[1]=' and arg[n]='
j=toascii(args[i]); //#ctype.h
strcat(buff2,itoa(j,tmpstr,16));
}//end for
}//endif, char*? check
else{ //if number,store the value
j=atoi(args);
if(j<0x10){ //single digit number
strcat(buff2,"0"); //outof 2 col of a byte, fill first by 0
strcat(buff2,itoa(j,tmpstr,16)); //write ascii of number
}
else if(j<0x100){//double digit number
strcat(buff2,itoa(j,tmpstr,16)); //just write in ascii
}
else{//greater than a byte? Possibly error,
printf("ERR: Can't Accomodate in a byte. %d",j);
exit(0);
}
} //end -isnum
}//endif, opc==BYTE

else if(!strcmp(opc,"WORD")){
if(args[0]=='C'||args[0]=='c'){ //if char, convert to ascii value.
for(i=2;i<(strlen(args)-1);i++) // args[0]=C,[1]=' and arg[n]='
{
strcat(buff2,"00"); //char uses 2 byte, fill first 2 byte of word with zeroes.
j=toascii(args[i]); //#ctype.h
strcat(buff2,itoa(j,tmpstr,16));
}//end for
}//endif, char*? check
else{ //if number,store the value
j=atoi(args);
if(j<0x10) strcat(buff2,"000"); //single digit number, outof 4 col of a word, fill 3 by 0s
else if(j<0x100) strcat(buff2,"00");//double digit number, first 2=00
else if(j<0x1000) strcat(buff2,"0"); //3 digit
else if(j<0x10000){/*donothing*/} //4 digit, just write in ascii
else{//greater than a byte? Possibly error,
printf("ERR: Can't Accomodate in a byte. %d",j);
exit(0);
}
strcat(buff2,itoa(j,tmpstr,16)); //now write the number in ascii
} //endelse, number
}//endif, opc==WORD
else if((!strcmp(opc,"RESB"))||(!strcmp(opc,"RESW"))) {} //do nothing
else printf("UNKnERR: Unknown Error in intermediate file\%s not recognized",opc);
}//endelse, OPCODE not found

if((strlen(buff2))>70) //if object code>50 byte, write TEXT Record
{
strcpy(buff1,"T"); //1. col1 = T
//col 2-6, //filling spaces with zeroes.
if(recstart>0xfff) strcat(buff1,"00"); //for 4 digit
else if(recstart>0xff) strcat(buff1,"000"); //3 digit
else if(recstart>0xf) strcat(buff1,"0000"); //for 2 digit
else if(recstart<=0xf) strcat(buff1,"00000"); // 1 digit, including 0

strcat(buff1,itoa(recstart,tmpstr,16)); //2. col 2-6 =start loc of this record, current-6*3
strcat(buff1,itoa(strlen(buff2),tmpstr,16)); //3. col =size of this record
strcat(buff1,buff2); // buff1+=buff2
fprintf(out,"%s\n",buff1);
count=0;
recstart=atoi(loc);
//flush buffers
strcpy(buff1,"\0");
strcpy(buff2,"\0");
}
}//endwhile

if(strlen(buff2)>0) //write the remaining instructioncode, if remaining.
{
strcpy(buff1,"T"); //COL 1

//COL 2-6 //filling spaces with zeroes.
if(recstart>0xfff) strcat(buff1,"00"); //for 4 digit
else if(recstart>0xff) strcat(buff1,"000"); //3 digit
else if(recstart>0xf) strcat(buff1,"0000"); //for 2 digit
else if(recstart<=0xf) strcat(buff1,"00000"); // 1 digit, including 0
strcat(buff1,itoa(recstart,tmpstr,16)); //start loc
strcat(buff1,itoa(strlen(buff2)/2,tmpstr,16)); //object code length of this record
strcat(buff1,buff2);
fprintf(out,"%s",buff1);
}

//end record

fprintf(out,"%s","\nE");
fprintf(out,"00%s",itoa(start,tmpstr,16));

fclose(in);
fclose(out);

puts("Calculating size of object program");
out=fopen(bin,"r+"); //for appending size of object code
fgets(str,40,out); //first string
fseek(out,0,0); //move to first
fseek(out,strlen(str),0); //move to end of this line

// fprintf(out,"FS:%s",itoa(sizeof(out),tmpstr,16)); //write the size.

return 1;
}//ends passtwo()

int passone(char *src,char *inter)
{
FILE *in,*out;
char fld1[8],fld2[8],fld3[8],fld4[8];
char lbl[8],opc[8],args[8];
char str[40];
int LOCCTR,symcnt=9;
int found;
int i; //tmp

in=fopen(src,"r");
out=fopen(inter,"w+");
if(in==NULL){
puts("ERR: Specified source does not exist");
exit(0);
}
createoptab(optab);
initsymtab(symtab);

fgets(str,40,in); //get first ins from in stream
fetch(fld1,fld2,fld3,fld4,str); //fetch fields.
if(fld1[0]==':'){ //has got label
strcpy(lbl,fld1);
strcpy(opc,fld2);
strcpy(args,fld3);
}//endif label
else{ //no label?
strcpy(lbl,"\0");
strcpy(opc,fld1);
strcpy(args,fld2);
} //end else nolabel.
if(!strcmp(opc,"START")) LOCCTR=atoi(args);//if OPCode is START
else{ //if no START,
LOCCTR=0;
fputs("0 :PRG START 0 \n",out);
}
fseek(in,0,0); //move to start
do{
fgets(str,40,in); //get ins line from in stream
fetch(fld1,fld2,fld3,fld4,str); //fetch fields
if(str[0]!='.'){ //if not a .comment continue
fprintf(out,"%s ",itoa(LOCCTR,fld4,10)); //location in DEC.
if(fld1[0]==':'){ //has got label
strcpy(lbl,fld1);
strcpy(opc,fld2);
strcpy(args,fld3);
//Check n UPDATE SYMTAB
for(i=0;i<=symcnt;i++){
if(!strcmp(lbl,symtab[i].symbol)) {
printf("\nERR: Multiple Label Declaration\n%s",lbl);
exit(0);
}//endif, multiple
} //search-end, for loop
//since new label, add to "symtab" and write to intermed. file
strcpy(symtab[symcnt].symbol,lbl);
symtab[symcnt].value=LOCCTR;
symcnt++;
fprintf(out,"%s ",lbl);
} //endif label
else{ //no label
strcpy(lbl,"\0");
strcpy(opc,fld1);
strcpy(args,fld2);
}//end else

found=0; //CHECK INSTRUCTION-validity
for(i=0;i<=opsz;i++){
if(!strcmp(opc,optab[i].symbol)){ //if match found
found=1; break;
}//endif
} //endfor
if(found) LOCCTR+=3;
else if(!strcmp(opc,"WORD")) LOCCTR+=3;
else if(!strcmp(opc,"RESW")) LOCCTR+=atoi(args)*3;
else if(!strcmp(opc,"RESB")) LOCCTR+=atoi(args);
else if(!strcmp(opc,"BYTE")) LOCCTR+=strlen(args);
else if(!strcmp(opc,"START")){} //do nothing now, already done
else if(!strcmp(opc,"END")){} //do nothing
else { printf("\nERR:Unknow Instruction %s",opc);
exit(0);
}
fprintf(out,"%s %s \n",opc,args);//write to file
} //endif .nocomment in line
}while(!feof(in)); //end-of-file encountered.

fclose(in);
fclose(out);
return 1;
}//end passone()

int fetch(char *fld1,char *fld2, char *fld3, char *fld4,char *str)
{
char one[8],two[8],three[8],four[8];
char ch;
int i=0,j=0;
//flushing with null chars
strcpy(one,"\0"); strcpy(two,"\0");
strcpy(three,"\0"); strcpy(four,"\0");

for(;i<strlen(str);i++,j++){ //loop ONE
ch=str[i];
if((ch==32)||(ch=='\t')||(ch=='\n')) { //a white space encountered?
i++; one[j]='\0'; //string terminate
break;
} //endif
one[j]=ch;
} //end for ONE

for(j=0;i<strlen(str);i++,j++) { //loop 4 TWO
ch=str[i];
if((ch==32)||(ch=='\t')||(ch=='\n')) { //white space encounter.
i++; two[j]='\0'; //string terminate
break;
} //endif
two[j]=ch;
} //end for TWO

for(j=0;i<strlen(str);i++,j++) { //loop 4 THREE
ch=str[i];
if((ch==32)||(ch=='\t')||(ch=='\n')) { //White Space?
i++; three[j]='\0'; //string terminate
break;
} //endif
three[j]=ch;
} //endfor THREE

for(j=0;i<strlen(str);i++,j++) { //loop 4 FOUR
ch=str[i];
if((ch==32)||(ch=='\t')||(ch=='\n')) { //a WHITE SPACE
i++; four[j]='\0';//terminate string with NULL
break;
} //endif
four[j]=ch;
} //endfor FOUR
//copy respective fields.
strcpy(fld1,one);
strcpy(fld2,two);
strcpy(fld3,three);
strcpy(fld4,four);
return 1;
}

void createoptab(struct table tab[]){
puts("\tCreating OPTAB...");
strcpy(tab[0].symbol,"ADD"); tab[0].value=0x18;
strcpy(tab[1].symbol,"AND"); tab[1].value=0x40;
strcpy(tab[2].symbol,"CLEAR"); tab[2].value=0xB4;
strcpy(tab[3].symbol,"COMP"); tab[3].value=0x28;
strcpy(tab[4].symbol,"DIV"); tab[4].value=0x24;
strcpy(tab[5].symbol,"J"); tab[5].value=0x3C;
strcpy(tab[6].symbol,"JEQ"); tab[6].value=0x30;
strcpy(tab[7].symbol,"JGT"); tab[7].value=0x34;
strcpy(tab[8].symbol,"JLT"); tab[8].value=0x38;
strcpy(tab[9].symbol,"JSUB"); tab[9].value=0x48;
strcpy(tab[10].symbol,"LDA"); tab[10].value=0x00;
strcpy(tab[11].symbol,"LDL"); tab[11].value=0x08;
strcpy(tab[12].symbol,"MUL"); tab[12].value=0x20;
strcpy(tab[13].symbol,"OR"); tab[13].value=0x44;
strcpy(tab[14].symbol,"RD"); tab[14].value=0xD8;
strcpy(tab[15].symbol,"RSUB"); tab[15].value=0x4C;
strcpy(tab[16].symbol,"STA"); tab[16].value=0x0C;
strcpy(tab[17].symbol,"SUB"); tab[17].value=0x1C;
strcpy(tab[18].symbol,"TIX"); tab[18].value=0x2C;
strcpy(tab[19].symbol,"TD"); tab[19].value=0xE0;
}

void initsymtab(struct table tab[]){
int i;
puts("\tInitializing SYMTAB...");
//flushing
for(i=0;i<100;i++){
strcpy(tab[i].symbol,"\0"); tab[i].value=-1;
}
//Registers and values
strcpy(tab[0].symbol,"A"); tab[0].value=0;
strcpy(tab[1].symbol,"X"); tab[1].value=1;
strcpy(tab[2].symbol,"L"); tab[2].value=2;
strcpy(tab[3].symbol,"PC");tab[3].value=8;
strcpy(tab[4].symbol,"SW");tab[4].value=9;
strcpy(tab[5].symbol,"B"); tab[5].value=3;
strcpy(tab[6].symbol,"S"); tab[6].value=4;
strcpy(tab[7].symbol,"T"); tab[7].value=5;
strcpy(tab[8].symbol,"F"); tab[8].value=6;
}

Tinkster 11-20-2004 05:11 PM

Very nice ... and now put it in code-tags and
indent it properly ... :)


All times are GMT -5. The time now is 09:27 AM.