4 Replies - 8774 Views - Last Post: 06 January 2012 - 07:43 AM Rate Topic: -----

#1 kocchumon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-January 12

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

Posted 04 January 2012 - 04:50 AM

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


#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;
    
}

     






Is This A Good Question/Topic? 0
  • +

Replies To: Program working in Dev C++ but fails in Turbo C

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4075
  • View blog
  • Posts: 12,578
  • Joined: 25-December 09

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

Posted 04 January 2012 - 06:53 AM

why are you trying to use Turbo-C or Dev C++ these are two of the most outdated compilers available. You should switch your compiler to a new standard compliant compiler. There are several available free to download. You may want to try WX-Dev-C++, Code::Blocks, Pelles C to just name a few. These compilers are a much better choice than either of the ancient compilers you are trying to use.

As to your problem you will have to be much more specific as to the problem. "gets stuck in Turbo C" does not tell us anything. Most people do not have either of these compilers, and this code will not compile on modern compilers without change, so you must tell us exactly what is wrong.

Does this code compile without warnings or errors? If you get warnings or errors post the complete warning messages exactly as they appear in your development environment.

Jim

This post has been edited by jimblumberg: 04 January 2012 - 06:54 AM

Was This Post Helpful? 2
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1347
  • View blog
  • Posts: 4,642
  • Joined: 19-February 09

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

Posted 05 January 2012 - 10:16 PM

Is it because the following is C++ code and Turbo C is a C compiler?

001	#include<iostream.h>
...
475	    cout<<"Pass 1 completed.....intermediate file created!!\n";
...
477	    cout<<"Pass 2 completed.....object file created!!\n";


Was This Post Helpful? 0
  • +
  • -

#4 sunilchintu2468  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 37
  • Joined: 29-May 11

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

Posted 06 January 2012 - 01:41 AM

View Postkocchumon, on 04 January 2012 - 04:50 AM, said:

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


#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;
    
}

     






First of all there is a compilation error in the line no 50:
error: B was not declared in this scope

I don't see any declaration for B
Was This Post Helpful? 0
  • +
  • -

#5 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1678
  • View blog
  • Posts: 3,180
  • Joined: 30-May 10

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

Posted 06 January 2012 - 07:43 AM

> First of all there is a compilation error in the line no 50:
> error: B was not declared in this scope
It's useful to know that this forum has a bug when it comes to dealing with lowercase 'b' in posted code (namely turning it into B).
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1