You are on page 1of 5

#include<stdio.

h>
#include<string.h>
#include<ctype.h>
#include<math.h>
FILE *fp1,*fp2,*fp3;
char progaddr[10];
int csaddr,cslth,addr,execaddr,v;
char str[100][20],hex[10],b[20];
typedef struct memory
{
int lo;
char obj[20];
}memory;
typedef struct est
{
int wq;
char nam[20];
}es;
es cont[20];
memory load[200];
void firstpass(void);
void secondpass(void);
int substring(char *);
int search(char *);
int hextodec(char *);
void dectohex(int );
int main()
{
printf("Enter the memory location from where u wanna load:\n");
scanf("%s",progaddr);
firstpass();
secondpass();
return 0;
}
void firstpass()
{
int k,m,l1,l2,i ;
char buff[100],name[20];
fp1=fopen("input1.dat","r");
fp2=fopen("estab1.dat","w");
csaddr=hextodec(progaddr);v=0;
while(!feof(fp1))
{
fgets(buff,100,fp1);
if(buff[0]=='H')
{
k=substring(buff);
strcpy(cont[v].nam,str[1]);
cont[v].wq=csaddr;
v++;
cslth=hextodec(str[k-2]);
}
while(buff[0]!='E')
{
fgets(buff,100,fp1);
if(buff[0]=='D')
{
k=substring(buff);
m=1;
while(m<k-1)

{
strcpy(name,str[m++]);
l2=m%2;
if(l2==0)
{
l1=hextodec(str[m++]);
addr=csaddr+l1;
}
strcpy(cont[v].nam,name);
cont[v].wq=addr;
v=v+1;
}
}
}
csaddr=cslth+csaddr;
}
for(i=0;i<v;i++)
fprintf(fp2,"%s\t\t%x\n",cont[i].nam,cont[i].wq);
fcloseall();
}
int substring(char *a)
{
int l,count=0,j=0,i;
l=strlen(a);
for(i=0;i<l;i++)
{
if(a[i]!='^')
{
str[count][j]=a[i];
j++;
}
else
{
str[count][j]='\0';
j=0;
count++;
}
}
return(count+1);
}
void secondpass()
{
int k,loc,n,l,j,i,q=0,p,t,u,w,m,s[3],x=0;
char buff[100],c,new[20],new1[3],str1[10];
fp3=fopen("input1.dat","r");
fp1=fopen("result1.dat","w");
fprintf(fp1,"ADDRESS\tCONTENTS\n\n");
csaddr=hextodec(progaddr);
while(!feof(fp3))
{
fgets(buff,100,fp3);
if(buff[0]=='H')
{
k=substring(buff);
cslth=hextodec(str[k-2]);
}
while(buff[0]!='E')
{
fgets(buff,100,fp3);
if(buff[0]=='T')

{
k=substring(buff);
n=3;
loc=csaddr+hextodec(str[1]);
while(n<k-1)
{
l=strlen(str[n]);
for(i=0;i<l;i=i+2)
{
new[0]=str[n][i];
new[1]=str[n][i+1];
new[2]='\0';
load[q].lo=loc;
strcpy(load[q].obj,new);
q=q+1;
loc=loc+1;
}
n=n+1;
}
}
if(buff[0]=='M')
{
k=substring(buff);
c=str[k-2][0];u=0;
l=strlen(str[k-2]);w=0;
for(i=1;i<l;i++)
{
str1[w]=str[k-2][i];
w++;
}
str1[w]='\0';
p=search(str1);
loc=csaddr+hextodec(str[1]);
for(i=0;i<q;i++)
{
if(loc==load[i].lo)
{
u=hextodec(load[i].obj)*
10000+hextodec(load[i+1].obj)*100+hextodec(load[i+2].obj);
/*else{
n=atoi(load[i+2]
.obj);n=n/10;
u=hextodec(load[i].ob
j)*1000+hextodec(load[i+1].obj)*10+n;} */
if(c=='+')
u=u+p;
else
u=u-p;
new[2]='\0';
t=u%100;u=u/100;
new1[1]=(t%10)+48;t=t/10;
new1[0]=(t%10)+48;
strcpy(load[i+2].obj,new1);
t=u%100;u=u/100;
new1[1]=(t%10)+48;t=t/10;
new1[0]=(t%10)+48;
strcpy(load[i+1].obj,new1);
t=u%100;u=u/100;
new1[1]=(t%10)+48;t=t/10;
new1[0]=(t%10)+48;

strcpy(load[i].obj,new1);
break;
}
}
}
}
s[x]=q;
x=x+1;
csaddr=csaddr+cslth;
}
for(i=0;i<q;i++)
{
fprintf(fp1,"%x\t%s\n",load[i].lo,load[i].obj);
for(j=0;j<x;j++)
{
if(i==s[j])
fprintf(fp1,"\n\n");
}
}
fcloseall();
}
int search(char *abc)
{
char buf[20];
int i;
for(i=0;i<v;i++)
{
if(strcmp(abc,cont[i].nam)==0)
break;
}
return(cont[i].wq);
}
int hextodec(char* hex)
{
int decimal=0,i,l,t;
l=strlen(hex);
for(i=0;i<l;i++){
t=(int)pow(16,(l-i-1));
if((hex[i]-48)<10)
decimal+=(hex[i]-48)*t;
else if(hex[i]=='A')
decimal+=10*t;
else if(hex[i]=='B')
decimal+=11*t;
else if(hex[i]=='C')
decimal+=12*t;
else if(hex[i]=='D')
decimal+=13*t;
else if(hex[i]=='E')
decimal+=14*t;
else if(hex[i]=='F')
decimal+=15*t;
else{
printf("Character not found:\n");
break;
}
}
return decimal;
}

void dectohex(int a)
{
int r,j=0,i,d[10],p;
char x[20],y[10];
while(a)
{
r=a%16;
d[j]=r;
j++;
a=a/16;
}
for(i=0;i<j;i++)
{
if(d[i]==10)
x[i]='A';
else if(d[i]==11)
x[i]='B';
else if(d[i]==12)
x[i]='C';
else if(d[i]==13)
x[i]='D';
else if(d[i]==14)
x[i]='E';
else if(d[i]==15)
x[i]='F';
else
x[i]=d[i];
}
p=j;
r=0;
for(i=0;i<p;i++)
{
y[i]=x[p-1];
p=p-1;
}
for(i=0;i<6-j;i++)
b[i]=0;
for(i=6-j;i<6;i++)
{
b[i]=y[r];
r=r+1;
}
b[6]='\0';
}

You might also like