Code:
int main(int argc,char *argv[])
{
int valid=0,k=0,l=0,i,x,y,z;
FILE *inp, *outp, *tpin, *tpout;
char ch, temp[SIZE], decl[SIZE];
nodetype *list,*newnode,*prev;
prev=NULL;
//fflush(outp);
intro("This program takes a program written in ‘C’ and converts it into a program written in assembly language.");
//valid=readC(inp,argv[1]);
//if(valid==1)
//DEBUG
inp=fopen(argv[1],"r");
if(inp!=NULL)
{
outp=fopen("asmcode.txt","w");
fprintf(outp,".file\t\"main.c\"\n\t.section\t.rodata\n");
tpin=fopen("declarein.txt","w"); //Stores input placeholder information
tpout=fopen("declareout.txt","w"); //Stores output placeholder information
while(!feof(inp)) //grabs a line from the input file
{
//do{
fgets(temp,SIZE,inp);
//temp[i]=ch;
//i++;
//}while(ch!='\n');
//temp[i]='\0';
printf("jjj\n");
if((temp[0]=='s')&&(temp[1]=='c')&&(temp[2]=='a')&&(temp[3]=='n')&&(temp[4]=='f')&&(temp[5]=='('))
{
for(x=6,y=0;x<10,y<4;x++,y++)
decl[y]=temp[x];
fprintf(outp,".LC%d:\n\t.string\t",l); //Stores input assembly declarations in main output file
fprintf(tpin,"%s",decl); //Keeps a copy of the placeholder in declarein.txt
fputs("\n",tpin);
l++;
for(z=0;z<4;z++)
fprintf(outp,"%s\n\t.text\n",decl[z]);
}
else if((temp[0]=='p')&&(temp[1]=='r')&&(temp[2]=='i')&&(temp[3]=='n')&&(temp[4]=='t')&&(temp[5]=='f')&&(temp[6]=='('))
{
for(x=7,y=0;x!='\0';x++,y++)
decl[y]=temp[x];
decl[y]='\0';
fprintf(outp,".LC%d:\n\t.string\t",k); //Stores output assembly declarations in main output file
fprintf(tpout,"%s",decl); //Keeps a copy of the placeholder in declarein.txt
fputs("\n",tpout);
k++;
for(z=0;z<4;z++)
fprintf(outp,"%s\n\t.text\n",decl[z]);
}
else for(x=0;x!='\n';x++)
if(temp[x]=='=')
{
newnode=(nodetype*)malloc(sizeof(nodetype));
newnode->next=list;
list=newnode;
for(y=0,z=0;y<x;y++,z++)
newnode->nm[z]=temp[y];
newnode->val=temp[x++];
}
}
fclose(tpin);
fclose(tpout);
fprintf(outp,".globl\tmain\n\t.type\tmain@function\nmain:\n\tpushl\t%cebp\n\tmov\t%cesp,%cebp\n",'%','%','%');
fseek(inp,0L,0);
while(!feof(inp))
{
fgets(temp,SIZE,inp);
if((temp=="int main()")||(temp[0]=='#')||(temp[0]==' ')||(temp[0]=='{')||(temp[0]=='}'))
fseek(inp,1L,1);
//convert(inp,outp,list);
fseek(inp,1L,1);
}
fprintf(outp,"\n\tmov\t$0,%ceax\n\tleave\n\tret\n\t.size\tmain,.-main\n\t.ident\t\"GCC:(Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2\"\n\t.section\t.note.GNU-stack,\"\",@progbits",'%');
}
else {
printf("\n\nInvalid input file");
exit(1);
}
fclose(outp);
fclose(inp);
terminate();
return 0;
}