You are here:

C++/linking loader...no errors but it doesn't produce the right output

Advertisement


Question
QUESTION: hello.....i have some  problem with the code below. it has no errors but does not produce the right output. the output is suppose to be in a form of a symbol table in my bin file but it does not. pls help me out


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
struct instruction
{
char iden[10];
char name[10];
  }in;


void main()
{
FILE *fin,*fesym;
int v1,v2,val,*cslth,val1,val2,c,d;

char *id1,*id2,name2[10],v[10];
int csaddr=0,progaddr,temp1;

clrscr();

fin=fopen("pass1.c","r");
fesym=fopen("symtable.c","w");

 printf("         Creation of external symbol table
");
 printf("Enter the relocation address");
 scanf("%d",&progaddr);

 csaddr=progaddr;

   printf("Starting address:%d",progaddr);
   printf("
First control section address: %d",csaddr);

    fprintf(fesym,"____External symbol table___");
    fscanf(fin,"%s%s",in.iden,in.name);

     while(!feof(fin))
  {
    if(strcmp(in.iden,"H")==0)
      {
        int t;

        fscanf(fin,"%s%s",id1,val);
        fprintf(fesym,"
Control section name   symbolname   address   length");
        fprintf(fesym,"
%s          %d      %s",in.name,csaddr,val);
      val=cslth;
     }


      else if(strcmp(in.iden,"H1")==0)
        {
         int t;

         fscanf(fin,"%s%s",id1,val1);
         fprintf(fesym,"
%s          %d      %s",in.name,c,csaddr,val1);

         val1=cslth;
         t=atoi(cslth);
         csaddr=t+csaddr;

         printf("
Last control section address:%d",csaddr);
        }

         else if(strcmp(in.iden,"H2")==0)
         {
         int t;
         fscanf(fin,"%s%s",id1,val2);
         fprintf(fesym,"
%s          %d   %s",in.name,c,csaddr,val2);

         val2=cslth;
         t=atoi(cslth);
         csaddr=t+csaddr;
        printf("%d",csaddr);
         }

         else if(strcmp(in.iden,"D")==0)
         {
         int n,m,temp,temp1;
         fscanf(fin,"%s%s%s",id1,name2,id2);

         n=atoi(id1);
         m=atoi(id2);

         temp=csaddr+n;
         temp1=csaddr+m;

         fprintf(fesym,"
        %s      %d
        %s      %d
",in.name,temp,name2,temp1);
         c=atoi(cslth);
         csaddr=c+csaddr;

         }
         fscanf(fin,"%s%s",in.iden,in.name);
         } fclose(fesym);
         getch();
}

ANSWER: Hello Suzzie.

The program has a number of problems. At the end of my message is a list of warnings produced by the Microsoft compiler. The line numbers may be slightly different from your code, but you should fix the warnings. Actually, many of the warnings are logic errors in the code. You should turn on all warnings in your compiler.

I suppose the file opened for reading should be an object file or executable. The program seems to be opening a C file (pass1.c) for input.

The program is not using fscanf correctly. The destinations of fscanf should be addresses. For example
fscanf(fin,"%s%s",id1,val1);
For the above line to work, id1, and val1 must be character arrays instead of integers.
If id1 and val1 are integers, then the line should read
fscanf(fin,"%d%d", &id1, &val1);

Feel free to post a follow-up question if you need more help.

Best regards.
Zlatko

1>c:\zlatko\visual studio 2005\projects\test\main.cpp(47) : warning C4047: '=' : 'int' differs in levels of indirection from 'int *'
1>c:\zlatko\visual studio 2005\projects\test\main.cpp(58) : warning C4047: '=' : 'int' differs in levels of indirection from 'int *'
1>c:\zlatko\visual studio 2005\projects\test\main.cpp(59) : warning C4133: 'function' : incompatible types - from 'int *' to 'const char *'
1>c:\zlatko\visual studio 2005\projects\test\main.cpp(71) : warning C4047: '=' : 'int' differs in levels of indirection from 'int *'
1>c:\zlatko\visual studio 2005\projects\test\main.cpp(72) : warning C4133: 'function' : incompatible types - from 'int *' to 'const char *'
1>c:\zlatko\visual studio 2005\projects\test\main.cpp(89) : warning C4133: 'function' : incompatible types - from 'int *' to 'const char *'
1>c:\zlatko\visual studio 2005\projects\test\main.cpp(18) : warning C4101: 'v' : unreferenced local variable
1>c:\zlatko\visual studio 2005\projects\test\main.cpp(16) : warning C4101: 'v1' : unreferenced local variable
1>c:\zlatko\visual studio 2005\projects\test\main.cpp(16) : warning C4101: 'v2' : unreferenced local variable
1>c:\zlatko\visual studio 2005\projects\test\main.cpp(19) : warning C4101: 'temp1' : unreferenced local variable
1>c:\zlatko\visual studio 2005\projects\test\main.cpp(16) : warning C4101: 'd' : unreferenced local variable
1>c:\zlatko\visual studio 2005\projects\test\main.cpp(42) : warning C4101: 't' : unreferenced local variable
1>c:\zlatko\visual studio 2005\projects\test\main.cpp(44) : warning C4700: uninitialized local variable 'val' used
1>c:\zlatko\visual studio 2005\projects\test\main.cpp(44) : warning C4700: uninitialized local variable 'id1' used
1>c:\zlatko\visual studio 2005\projects\test\main.cpp(47) : warning C4700: uninitialized local variable 'cslth' used
1>c:\zlatko\visual studio 2005\projects\test\main.cpp(55) : warning C4700: uninitialized local variable 'val1' used
1>c:\zlatko\visual studio 2005\projects\test\main.cpp(68) : warning C4700: uninitialized local variable 'val2' used
1>c:\zlatko\visual studio 2005\projects\test\main.cpp(80) : warning C4700: uninitialized local variable 'id2' used

---------- FOLLOW-UP ----------

QUESTION: thanks.....i did just that. i guess i have to check my pass1.c and symtable.c program since am reading and writing into those.

ANSWER: Hello Suzzie.

Perhaps I misunderstood what the program is supposed to do when I said that the input file should be an object file or executable. I suppose now that pass1.c is a text file.

In any case, if you are having more difficulty, then post a copy of pass1.c, and your question and I'll be happy to help.

Best regards
Zlatko

---------- FOLLOW-UP ----------

QUESTION: thanks again. this is the code for pass1.c

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
FILE *f1,*f2;
char ch,str[30],str1[10],str2[30],cstr[15];
int i,j,num,q,r;
clrscr();
printf("enter your assembly instructions\n");
f1=fopen("asin","w");
while(1)
{
ch=getchar();
if(ch=='*')
break;
fputc(ch,f1);
}
fclose(f1);
f1=fopen("asin","r");
f2=fopen("asout","w");
while(1)
{
fgets(str,25,f1);
strncpy(str1,str,3);
str1[3]='\0';
j=0;
for(i=3;i<strlen(str);i++)
{
str2[j]=str[i];
j++;
}
str2[j]='\0';
if((strcmp(str1,"lda"))==0)
{
fputs("3a\t",f2);
fputs(str2,f2);
}
else if((strcmp(str,"mov"))==0)
{
fputs("47\n",f2);
}
else if((strcmp(str1,"add"))==0)
{
fputs("80\n",f2);
}
else if((strcmp(str1,"sub"))==0)
{
fputs("90\n",f2);
}
else if((strcmp(str1,"hlt"))==0)
{
fputs("\76\n",f2);
break;
}
else if((strcmp(str1,"sta"))==0)
{
fputs("32\t",f2);
num=atoi(str2);
q=num/100;
r=num%100;
if(r==0)
fputs("00\t",f2);
else
fputs(itoa(r,cstr,10),f2);
fputs("\t",f2);
fputs(itoa(q,cstr,10),f2);
fputs("\n",f2);
}
else
{
fputs("erro\n",f2);
}
}
fclose(f1);
fclose(f2);
f2=fopen("asout","r");
printf("\n THE OBJECT CODE CONTENTS\n");
ch=fgetc(f2);
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(f2);
}
fclose(f2);
getch();
}  

Answer
Hello Suzzie.

I'm certain that you don't intend your first program to read pass1.c. Maybe it's supposed to read the output of pass1.c or some the pass1.o object file. I don't know.

Look, your original program opens a file, then does
fscanf(fin,"%s%s",in.iden,in.name);

indidcating that it's expecting some kind of table of two columns where the first column is H or H1 or H2 or D. That is not what pass1.c is.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2016 About.com. All rights reserved.