473,386 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

I really got to know this

Parul Bagadia
188 100+
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..
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include<stdio.h>
  3. #include<conio.h>
  4. void main()
  5. {
  6.    FILE *in,*out;
  7.    char ch;
  8.    char store[100];
  9.    char label[100];
  10.    char operand[100];
  11.    char opcode[100],directive[100];
  12.    int get=0,add=0;
  13.    int i=0,la=0,opr=0,opc=0,dir=0,once=0,accept=0;
  14.    int one=0,two=0,one1=0,one2=0;
  15.    int dir1=0,opc1=0,opr1=0,la1=0,easy=0;
  16.    int p=0,q=0,r=0,s=0,match=0;
  17.    int pass1=0,pass2=0,pass3=0;
  18.    struct mot{
  19.   char p1[80];
  20.   char q1[40];
  21.   int r1[];
  22.    }object;
  23.    clrscr();
  24.    object.r1[0]=1;
  25.    object.r1[1]=2;
  26.    object.r1[2]=3;
  27.    object.r1[3]=1;
  28.    object.r1[4]=2;
  29.    object.r1[5]=1;
  30.    object.r1[6]=2;
  31.    store[0]=NULL,label[0]=NULL,operand[0]=NULL,opcode[0]=NULL,directive[0]=NULL;
  32.    in=fopen("micro.txt","r");
  33.     if(in==NULL)
  34.        printf("\nSorry,error in opening the file.\n");
  35.     else
  36.       {
  37.   printf("\nSource code file opened successfully.\n");
  38.   printf("\nDistribution of code is as follows: \n");
  39.       while(!feof(in))
  40.         {
  41.     ch=getc(in);
  42.     store[i]=ch;
  43.     i++;
  44.      if(ch=='\n' && once==0)
  45.        {
  46.          for(accept=0;accept<i-1;accept++)
  47.   {
  48.      directive[dir]=store[accept];
  49.      dir++;
  50.   }
  51.     directive[dir]='\t';
  52.     dir++;
  53.     once=5;
  54.     i=0;
  55.     store[0]=NULL;
  56.         }
  57.       else if((ch=='\n' && once==5)|| ch==',')
  58.        {
  59.    if(ch==',')
  60.      {
  61.     for(accept=0;accept<i-1;accept++)
  62.       {
  63.          opcode[opc]=store[accept];
  64.          opc++;
  65.       }
  66.       opcode[opc]='\t';
  67.       opc++;
  68.       i=0;
  69.       store[0]=NULL;
  70.       operand[opr]=fgetc(in);
  71.       opr++;
  72.       while(easy!=2)
  73.        {
  74.            operand[opr]=fgetc(in);
  75.            if(operand[opr]=='\n')
  76.       {
  77.          operand[opr]='\t';
  78.          easy=2;
  79.       }
  80.       opr++;
  81.        }
  82.        easy=0;
  83.      }
  84.    else
  85.    {
  86.         for(accept=0;accept<i-1;accept++)
  87.           {
  88.         opcode[opc]=store[accept];
  89.         opc++;
  90.           }
  91.           i=0;
  92.           store[0]=NULL;
  93.           opcode[opc]='\t';
  94.           opc++;
  95.    }
  96.        }
  97.      if(ch==':')
  98.        {
  99.         for(accept=0;accept<i-1;accept++)
  100.    {
  101.       label[la]=store[accept];
  102.       la++;
  103.    }
  104.    label[la]='\t';
  105.    la++;
  106.    i=0;
  107.    store[0]=NULL;
  108.         }
  109.         }
  110.         printf("\nDirective");
  111.         printf("\t");
  112.         printf("Label");
  113.         printf("\t   ");
  114.         printf("Opcode");
  115.         printf("\t");
  116.         printf("Operand");
  117.         printf("\t\n");
  118.         while(one!=5 || two!=5 || one1!=5 || one2!=5)
  119.         {
  120.        while(directive[dir1]!='\t' && dir1!=dir)
  121.          {
  122.      printf("%c",directive[dir1]);
  123.      dir1++;
  124.          }
  125.          printf("\t");
  126.          if(dir1==dir)
  127.     one=5;
  128.          if(one==5)
  129.     printf("  ");
  130.          else
  131.     dir1++;
  132.         while(label[la1]!='\t' && la1<la)
  133.           {
  134.     printf("%c",label[la1]);
  135.     la1++;
  136.          }
  137.          printf("\t");
  138.          if(la1==la)
  139.           two=5;
  140.          if(two==5)
  141.      printf(" \t");
  142.          else
  143.     la1++;
  144.        while(opcode[opc1]!='\t' && opc1<opc)
  145.         {
  146.           printf("%c",opcode[opc1]);
  147.           opc1++;
  148.         }
  149.        printf("\t");
  150.        if(opc1==opc)
  151.           one1=5;
  152.        if(one1==5)
  153.          printf("     ");
  154.        else
  155.          opc1++;
  156.        while(operand[opr1]!='\t'&& opr1<opr)
  157.         {
  158.           printf("%c",operand[opr1]);
  159.           opr1++;
  160.         }
  161.         if(opr1==opr)
  162.      one2=5;
  163.         if(one2!=5)
  164.      opr1++;
  165.         printf("\n");
  166.         }
  167.       printf("\nMOT details are as follows:\n");
  168.       strcpy(object.p1,"MVI A\tMOV A\tROL BH\tDEC CH\tJNZ NEXT\tINC SI\tNOP\t\n");
  169.       strcpy(object.q1,"E8\tB6\t4A\t7D\t2F\tEF\t1C\t\n");
  170.       printf("\nAddress\t Hexcode\t Symbol\t \n");
  171.       while(1)
  172.         {
  173.     while(opcode[q]==' ')
  174.     q++;
  175.     while(object.p1[p]!='\n'&& object.p1[p]==opcode[q] && object.p1[p]!='\t' && opcode[q]!='\t')
  176.        {
  177.      p++;
  178.      q++;
  179.      continue;
  180.        }
  181.     if(object.p1[p]=='\n')
  182.          break;
  183.     else if(object.p1[p]=='\t' && opcode[q]!='\t')
  184.     match++;
  185.     else if(object.p1[p]!='\t' && opcode[q]=='\t')
  186.         {
  187.     while(object.p1[p]!=' ' && p>0)
  188.           p--;
  189.     if(object.p1[p]==' ')
  190.           p++;
  191.          }
  192.     else if(object.p1[p]==opcode[q] &&opcode[q]=='\t')
  193.          {
  194.         if(pass1==match)
  195.     {
  196.         while(object.r1[r]!='\t')
  197.      {
  198.          get=object.r1[r];
  199.          r++;
  200.       }
  201.       add=add+get;
  202.       printf("%c\t",add);
  203.          while(object.q1[s]!='\t')
  204.        {
  205.           printf("%c",object.q1[s]);
  206.           s++;
  207.        }
  208.        printf("\t");
  209.        r++;p++;q++;
  210.        s++;
  211.      }
  212.          else
  213.      {
  214.            while(pass1<match)
  215.        {
  216.            while(object.r1[r]!='\t')
  217.           r++;
  218.            while(object.q1[s]!='\t')
  219.           s++;
  220.           r++;
  221.           s++;
  222.        }
  223.      }
  224.           match++;
  225.           pass1++;
  226.          }
  227.         }
  228.       }
  229.      getch();
  230. }
  231.  
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??
Jan 24 '09 #1
3 1728
JosAH
11,448 Expert 8TB
The following doesn't make sense:

Expand|Select|Wrap|Line Numbers
  1.  
  2.   struct mot{ 
  3.   char p1[80]; 
  4.   char q1[40]; 
  5.   int r1[]; 
  6.    }object; 
  7.    clrscr(); 
  8.    object.r1[0]=1; 
  9.    object.r1[1]=2; 
  10.    object.r1[2]=3; 
  11.    object.r1[3]=1; 
  12.    object.r1[4]=2; 
  13.    object.r1[5]=1; 
  14.    object.r1[6]=2; 
  15.  
Better make that array r1 an array of seven elements.

kind regards,

Jos
Jan 24 '09 #2
Parul Bagadia
188 100+
@JosAH
But it should not affect its value in some loop where m not accessing it
Jan 25 '09 #3
JosAH
11,448 Expert 8TB
@Parul Bagadia
You already accessed it and corrupted your memory. Give that array a valid size.

kind regards,

Jos
Jan 25 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Randall Smith | last post by:
I've been programming in Python for about 2 years. I think it offers the best combination of simplicity and power of any language I have explored. As I write more and larger and complex programs,...
72
by: Mel | last post by:
Are we going backwards ? (please excuse my spelling...) In my opinion an absolute YES ! Take a look at what we are doing ! we create TAGS, things like <H1> etc. and although there are tools...
35
by: Richard | last post by:
I finally got Mozilla installed and running just last week and wow... Nice work you Mozilla developers, you! And is it just me or does Mozilla really interpret/execute CSS code really well...
6
by: Zip Code | last post by:
"The large print giveth, and the small print taketh away.", so said Tom Waites in his classic rap, "Step Right Up", a paean about come ons and rip offs. Now, we have all explored the fact that...
21
by: Hattuari | last post by:
I'm learning C++ after having spent several years in the computer industry doing both system administration and engineering. I've written code in Perl, Bash, Pascal, Ada, C, Mathematica (hundreds...
15
by: Jeannie | last post by:
Hello group! I'm in Europe, traveling with my laptop, and I don't any compilers other than Borland C++ 5.5. available. I also don't have any manuals or help files available. Sadly, more...
3
by: Chris | last post by:
Wait.. before you flame.. If someone can program in Java, or Javascript, or C, or (insert your language here that uses basically the same syntax as C#), and that person knew how to program in...
3
by: Studio P.M. | last post by:
Dear colleagues, I kindly ask the C# developers community to help me to REALLY understand the Page_Load event, so to REALLY have it under control. Reason for this question is that I cannot...
59
by: Alan Silver | last post by:
Hello, This is NOT a troll, it's a genuine question. Please read right through to see why. I have been using Vusual Basic and Classic ASP for some years, and have now started looking at...
131
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.