Program working in Dev C++ but fails in Turbo C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kocchumon
    New Member
    • Jan 2012
    • 1

    Program working in Dev C++ but fails in Turbo C

    A Sic 2 pass assembler which is coded using Dev c++ gets stuck in Turbo C...guys plz help


    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<stdlib.h>
    
    
    struct stmt
    {
           char add[32];
           char lab[32];
           char opc[32];
           char opr[32];
    };
    
    
    struct optab
    {
           char opc[32];
           int val;
    };
    
    struct symtab
    {
           char sym[32];
           int  loc;
    };
    
    
    long locctr=0,strt_addr=0,len=0;
    
    
    int search_symtab(char *a)                        //function to search symtab......
    {
        FILE *fsym;
        char b[32];
        strcpy(b,a);
        
        int i;
        for(i=0;a[i]!=','&&a[i]!='\0';i++);
        a[i]='\0';
                
        fsym=fopen("symtab.txt","r");
        symtab symb;
        while(!feof(fsym))
        {
                          fscanf(fsym,"%s\t%x",symb.sym,&symb.loc);
                          if(strcmp(symb.sym,a)==0)
                          { 
                           strcpy(a,b);
                           return symb.loc;
                          }
                          
        }
        strcpy(a,b);
        return -1;
    }                      
           
         
    int search_optab(char a[100])                    //function to search optab....
    {
        FILE *fsym;
        fsym=fopen("optab.txt","r");
        optab op;
        while(!feof(fsym))
        {
                          fscanf(fsym,"%s\t%x",op.opc,&op.val);
                          if(strcmp(op.opc,a)==0)
                           return op.val;
                          
        }
        
        return -1;
    } 
    
    
    long hextoint(char *a)           //converting hex to integer
    {
         
        int len=strlen(a);
        long temp=0,t;
        for(int i=len-1,j=0;i>=0;i--,j++)
        {
                if((int)a[i]>=48&&(int)a[i]<=56)
                t=(int)a[i]-48;
                else
                t=(int)a[i]-53;
                
                temp=temp+t*(pow(16,j));  
        }         
                         
        return temp;
    }
    
    
    void add_sym(char *a,int loc,FILE *fous)       //function to add symbol to symtab............
    {
         fprintf(fous,"%s\t%05X\n",a,loc);
    }
    
    
    stmt read_stmt(FILE *fins,int p)                 //function to read single line from source file.....
    {
        stmt tem;
        char s[100],t[4][40];
        int i,j,k,l,fl=0;
      
        fgets(s,100,fins);
             
        k=0;
        for(i=0;i<2+p;i++)
        {
          l=0;
          for(j=k;s[j]!='\t'&&s[j]!='\n';j++)
          {       
               if(s[j]=='.')
               {
                            fl=1;
                            break;
               }
               else                                 
               {
                            t[i][l]=s[j];
                            l++;
               }
               
          }
          
          if(fl==1)
          break;
                                              
          t[i][l]='\0';
          k=j+1;
                                              
                                              
        }
        
        if(fl==1)
        {
                    tem.lab[0]='.';
        }
        else if(p==1)        
        {
                    strcpy(tem.lab,t[0]);
                    strcpy(tem.opc,t[1]);
                    strcpy(tem.opr,t[2]);  
        }
        else
        {
                    strcpy(tem.add,t[0]);
                    strcpy(tem.lab,t[1]);
                    strcpy(tem.opc,t[2]);
                    strcpy(tem.opr,t[3]);
        }
             
                              
                              
        return tem;
    }  
    
    
    void inslen(int d)
    {
         char c,s[100];
         FILE *fon;
         fon=fopen("obj.txt","r+");
    
         
         int fl=0;
         
         while(!feof(fon))
         {
                          
                          c=fgetc(fon);
    
                          if(c=='T')
                           fl=1;
                          
                          if((c==' ')&&(fl==1))
                          {
                                  
                                  fseek(fon,-1,SEEK_CUR);
                                  fprintf(fon,"%02X",d);
                                  break;
                          }
                          
         }
         
         fclose(fon);
            
    }     
    
             
    int getlen( char *a)
    {
        int k;
        for(k=2;a[k]!='\'';k++);
        if(a[0]=='C')                    
          return k-2;
        else
          return (k-2)/2; 
    }
    
       
    void pass1()
    {
         FILE *fino,*foui,*fous,*fins;
         fins=fopen("source.txt","r");
         foui=fopen("inter.txt","w");
         fino=fopen("optab.txt","r");
         fous=fopen("symtab.txt","w");
         
         stmt temp;
         temp=read_stmt(fins,1);
         
         long lineno=1,ln=0,k;
         
         locctr=hextoint(temp.opr);
         
         
         if(strlen(temp.lab)==0)
          cout<<"Program name not defined!!\n";
         else
          add_sym(temp.lab,locctr,fous);
         if(strlen(temp.opr)>0)
          {
                      
                      strt_addr=locctr;
                      fprintf(foui,"%05X\t%s\t%s\t%s\n",locctr,temp.lab,temp.opc,temp.opr);
          }
         else
         {
             locctr=0;
             strt_addr=locctr;
         }
         
         temp=read_stmt(fins,1);
         
         while(strcmp(temp.opc,"END")!=0&&!feof(fins))
         {
                       
                     ln=locctr;
                     if(temp.lab[0]!='.')             //if not a comment line
                     {
                            if(strlen(temp.lab)!=0)
                            {
                                    if(search_symtab(temp.lab)!=-1)
                                    {
                                              cout<<"Error at line : "<<lineno<<"\n";
                                              getch();
                                    }
                                    else
                                    {
                                                 add_sym(temp.lab,locctr,fous);
                                                 
                                    }
                            } 
                            if(search_optab(temp.opc)!=-1)
                            {
                            
                                    locctr=locctr+3;
                            }
                            else if(strcmp(temp.opc,"WORD")==0)
                            {
                                    locctr=locctr+3;
                            }
                            else if(strcmp(temp.opc,"RESW")==0)
                            {
                                    locctr=locctr+3*atoi(temp.opr);
                            }
                            else if(strcmp(temp.opc,"RESB")==0)
                            {
                                    locctr=locctr+atoi(temp.opr);
                            }
                            else if(strcmp(temp.opc,"BYTE")==0)
                            {
                                     k=getlen(temp.opr);
                                     locctr=locctr+k;    
                            }
                            else
                             cout<<"Error at line : "<<lineno<<"\n";
                     }
                     lineno++;
                     if(temp.lab[0]!='.')
                     {
                     
                                         fprintf(foui,"%05X\t%s\t%s\t%s\n",ln,temp.lab,temp.opc,temp.opr);
                     }
                                                     
                     
                     temp=read_stmt(fins,1);
         }    
                                                                             
          fprintf(foui,"\t%s\t%s\t%s\n",temp.lab,temp.opc,temp.opr);                                  
          len=locctr-strt_addr;                                                       
                                   
          fclose(fino);
          fclose(foui);
          fclose(fins);
          fclose(fous);                                       
                                                                                               
    }
     
     
    void pass2()
    {
         FILE *fini,*fouo;
         char buff[100];
         int lim=0,lim1=0,op,sm,st;
         st=strt_addr;
         
         long ob,i=0;
         fini=fopen("inter.txt","r");
         fouo=fopen("obj.txt","w");
         stmt temp=read_stmt(fini,2);
         
         if(strcmp(temp.opc,"START")==0)
            fprintf(fouo,"%c^%-6s^%06X^%06X\n",'H',temp.lab,strt_addr,len);
         temp=read_stmt(fini,2);
         fprintf(fouo,"%c^%06X^  ",'T',st);
         
         while(strcmp(temp.opc,"END")!=0&&!feof(fini))
         {
                  
                  op=search_optab(temp.opc);
                  if(op!=-1)
                  {
                            
                            if(strlen(temp.opr)>1)
                            {
                                  
                                  sm=search_symtab(temp.opr);
                                  if(sm==-1)
                                  {   
                                      cout<<"Symbol "<<temp.opr<<" not defined!!";
                                      sm=0;
                                  }
                            }
                            else
                                 sm=0;
                                 
                            int stl;
                            stl=strlen(temp.opr);
                            
                            if(temp.opr[stl-1]!='X')
                                ob=sm;
                            else
                            {
                                ob=sm+32768;
                            }
                               
       
                            if((lim+3)>30)
                            {
                                fflush(fouo);
                                inslen(lim1);
                                st=st+lim;
                                lim1=0;
                                lim=0;
                                fprintf(fouo,"\n%c^%06X^  ",'T',st);
                                
                            }
                            
                            fprintf(fouo,"^%02X%04X",op,(int)ob);
                            lim=lim+3;
                            lim1=lim1+3;
                         
                  }
                  
                  else if(strcmp(temp.opc,"BYTE")==0)
                  {
                       
                       int l=getlen(temp.opr);
                       
                      if(temp.opr[0]=='C')
                      {
                         
                          if((lim+l)>30)
                            {
                                fflush(fouo);
                                inslen(lim1);
                                st=st+lim;
                                lim1=0;
                                lim=0;
                                fprintf(fouo,"\n%c^%06X^  ",'T',st);
                            }
                          fprintf(fouo,"^"); 
                          for(int k=2;temp.opr[k]!='\'';k++)
                          {
                                  fprintf(fouo,"%X",(int)temp.opr[k]);
                                  
                          }
                         
                      }
                      else if(temp.opr[0]=='X')
                      {
                           int k;
                          char s[32];
                          
                          if((lim+l)>30)
                            {
                                fflush(fouo);
                                inslen(lim1);
                                st=st+lim;
                                lim1=0;
                                lim=0;
                                fprintf(fouo,"\n%c^%06X^  ",'T',st);
                            }
                            
                          for(k=2;temp.opr[k]!='\'';k++)
                           s[k-2]=temp.opr[k];
                          s[k-2]='\0';
                          fprintf(fouo,"^%s",s);
                      }
                   
                      lim=lim+l;
                      lim1=lim1+l;
                  }
                  else if(strcmp(temp.opc,"WORD")==0)
                  {
                       if((lim+3)>30)
                            {
                                fflush(fouo);
                                inslen(lim1);
                                st=st+lim;
                                lim1=0;
                                lim=0;
                                
                                fprintf(fouo,"\n%c^%06X^  ",'T',st);
                                
                            }
                            
                            fprintf(fouo,"^%06X",atoi(temp.opr));
                            lim=lim+3;
                            lim1=lim1+3;
                  }
                       
                  
                  else if(strcmp(temp.opc,"RESB")==0)
                  {
                           
                           int ak=atoi(temp.opr);
                           
                           lim=lim+ak;
                           
                           
                  }
                  
                  else if(strcmp(temp.opc,"RESW")==0)
                  {
                           
                           int ak=atoi(temp.opr);
                           lim=lim+3*ak;
                       
                  }
                
                  temp=read_stmt(fini,2);
                  
         }
         fflush(fouo);
         inslen(lim1);
         fprintf(fouo,"\n%c^%06X",'E',strt_addr);
         
         fclose(fouo);
         fclose(fini);
         
    }
                 
    
    int main()
    {
        
        pass1();
        cout<<"Pass 1 completed.....intermediate file created!!\n";
        pass2();
        cout<<"Pass 2 completed.....object file created!!\n";
        getch();
        return 0;
        
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    What do you mean by "getting stuck"?

    a) the compiler hangs?
    b) the executable hangs?
    c) there is a compiler error produced?
    d) the linker fails?
    etc...

    I can't debug 481 lines of code for you.

    Comment

    Working...