I really got to know this

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Parul Bagadia
    New Member
    • Mar 2008
    • 188

    #1

    I really got to know this

    Hello,
    I guess it may be related with some sort of memory organisation, but am not sure.
    Nywaz, the situation is , am working in Turbo C++ compiler..am making a program to simulate an assembler, where my input is a file named "micro", which has assembly code in it and needs to be converted in assembly language by no. of steps, out of which m done with some of them and working with other steps.
    Here I have taken a structure named MOT having 3 arrays out of which 1 is int and during program execution values of integer array gets change in certain areas of code, it generally happens when one array limit is exceeded but its not the case over here , value of object.r1 changes from 1 digit to 5 digits for certain indices and for certain indices it gets added by only 4-5 nos...
    Here is the code..
    Code:
     
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
       FILE *in,*out;
       char ch;
       char store[100];
       char label[100];
       char operand[100];
       char opcode[100],directive[100];
       int get=0,add=0;
       int i=0,la=0,opr=0,opc=0,dir=0,once=0,accept=0;
       int one=0,two=0,one1=0,one2=0;
       int dir1=0,opc1=0,opr1=0,la1=0,easy=0;
       int p=0,q=0,r=0,s=0,match=0;
       int pass1=0,pass2=0,pass3=0;
       struct mot{
      char p1[80];
      char q1[40];
      int r1[];
       }object;
       clrscr();
       object.r1[0]=1;
       object.r1[1]=2;
       object.r1[2]=3;
       object.r1[3]=1;
       object.r1[4]=2;
       object.r1[5]=1;
       object.r1[6]=2;
       store[0]=NULL,label[0]=NULL,operand[0]=NULL,opcode[0]=NULL,directive[0]=NULL;
       in=fopen("micro.txt","r");
        if(in==NULL)
           printf("\nSorry,error in opening the file.\n");
        else
          {
      printf("\nSource code file opened successfully.\n");
      printf("\nDistribution of code is as follows: \n");
          while(!feof(in))
            {
        ch=getc(in);
        store[i]=ch;
        i++;
         if(ch=='\n' && once==0)
           {
             [B]for(accept=0;accept<i-1;accept++)[/B]
    [B]  {[/B]
    [B]     directive[dir]=store[accept];[/B]
    [B]     dir++;[/B]
    [B]  }[/B]
        directive[dir]='\t';
        dir++;
        once=5;
        i=0;
        store[0]=NULL;
            }
          else if((ch=='\n' && once==5)|| ch==',')
           {
       if(ch==',')
         {
        for(accept=0;accept<i-1;accept++)
          {
             opcode[opc]=store[accept];
             opc++;
          }
          opcode[opc]='\t';
          opc++;
          i=0;
          store[0]=NULL;
          operand[opr]=fgetc(in);
          opr++;
          while(easy!=2)
           {
               operand[opr]=fgetc(in);
               if(operand[opr]=='\n')
          {
             operand[opr]='\t';
             easy=2;
          }
          opr++;
           }
           easy=0;
         }
       else
       {
            for(accept=0;accept<i-1;accept++)
              {
            opcode[opc]=store[accept];
            opc++;
              }
              i=0;
              store[0]=NULL;
              opcode[opc]='\t';
              opc++;
       }
           }
         if(ch==':')
           {
            for(accept=0;accept<i-1;accept++)
       {
          label[la]=store[accept];
          la++;
       }
       label[la]='\t';
       la++;
       i=0;
       store[0]=NULL;
            }
            }
            printf("\nDirective");
            printf("\t");
            printf("Label");
            printf("\t   ");
            printf("Opcode");
            printf("\t");
            printf("Operand");
            printf("\t\n");
            while(one!=5 || two!=5 || one1!=5 || one2!=5)
            {
           while(directive[dir1]!='\t' && dir1!=dir)
             {
         printf("%c",directive[dir1]);
         dir1++;
             }
             printf("\t");
             if(dir1==dir)
        one=5;
             if(one==5)
        printf("  ");
             else
        dir1++;
            while(label[la1]!='\t' && la1<la)
              {
        printf("%c",label[la1]);
        la1++;
             }
             printf("\t");
             if(la1==la)
              two=5;
             if(two==5)
         printf(" \t");
             else
        la1++;
           while(opcode[opc1]!='\t' && opc1<opc)
            {
              printf("%c",opcode[opc1]);
              opc1++;
            }
           printf("\t");
           if(opc1==opc)
              one1=5;
           if(one1==5)
             printf("     ");
           else
             opc1++;
           while(operand[opr1]!='\t'&& opr1<opr)
            {
              printf("%c",operand[opr1]);
              opr1++;
            }
            if(opr1==opr)
         one2=5;
            if(one2!=5)
         opr1++;
            printf("\n");
            }
          printf("\nMOT details are as follows:\n");
          strcpy(object.p1,"MVI A\tMOV A\tROL BH\tDEC CH\tJNZ NEXT\tINC SI\tNOP\t\n");
          strcpy(object.q1,"E8\tB6\t4A\t7D\t2F\tEF\t1C\t\n");
          printf("\nAddress\t Hexcode\t Symbol\t \n");
          while(1)
            {
        while(opcode[q]==' ')
        q++;
        while(object.p1[p]!='\n'&& object.p1[p]==opcode[q] && object.p1[p]!='\t' && opcode[q]!='\t')
           {
         p++;
         q++;
         continue;
           }
        if(object.p1[p]=='\n')
             break;
        else if(object.p1[p]=='\t' && opcode[q]!='\t')
        match++;
        else if(object.p1[p]!='\t' && opcode[q]=='\t')
            {
        while(object.p1[p]!=' ' && p>0)
              p--;
        if(object.p1[p]==' ')
              p++;
             }
        else if(object.p1[p]==opcode[q] &&opcode[q]=='\t')
             {
            if(pass1==match)
        {
            while(object.r1[r]!='\t')
         {
             get=object.r1[r];
             r++;
          }
          add=add+get;
          printf("%c\t",add);
             while(object.q1[s]!='\t')
           {
              printf("%c",object.q1[s]);
              s++;
           }
           printf("\t");
           r++;p++;q++;
           s++;
         }
             else
         {
               while(pass1<match)
           {
               while(object.r1[r]!='\t')
              r++;
               while(object.q1[s]!='\t')
              s++;
              r++;
              s++;
           }
         }
              match++;
              pass1++;
             }
            }
          }
         getch();
    }
    It can also be the case that garbadge value is getting inseted, but i dont agree with it because when the loop which i have shown in bold starts executing then only value is increased so there must be some better reason ehich i don't know.
    The remedyof it is to initialise the array when used as i have done with other members of MOT..,ill do that.
    But i want to know the reason why itincreases so tremendously only in that loop and only with certain indices?
    Whats the logic behind this??
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    The following doesn't make sense:

    Code:
     
      struct mot{ 
      char p1[80]; 
      char q1[40]; 
      int r1[]; 
       }object; 
       clrscr(); 
       object.r1[0]=1; 
       object.r1[1]=2; 
       object.r1[2]=3; 
       object.r1[3]=1; 
       object.r1[4]=2; 
       object.r1[5]=1; 
       object.r1[6]=2;
    Better make that array r1 an array of seven elements.

    kind regards,

    Jos

    Comment

    • Parul Bagadia
      New Member
      • Mar 2008
      • 188

      #3
      Originally posted by JosAH
      The following doesn't make sense:

      Code:
       
        struct mot{ 
        char p1[80]; 
        char q1[40]; 
        int r1[]; 
         }object; 
         clrscr(); 
         object.r1[0]=1; 
         object.r1[1]=2; 
         object.r1[2]=3; 
         object.r1[3]=1; 
         object.r1[4]=2; 
         object.r1[5]=1; 
         object.r1[6]=2;
      Better make that array r1 an array of seven elements.

      kind regards,

      Jos
      But it should not affect its value in some loop where m not accessing it

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Parul Bagadia
        But it should not affect its value in some loop where m not accessing it
        You already accessed it and corrupted your memory. Give that array a valid size.

        kind regards,

        Jos

        Comment

        Working...