SOFTWARE LABORATORY
We need ic.txt , st.txt , lt.txt for this program generated by assembler pass I
You con watch http://www.engineersvision.com/2013/01/design-suitable-data-structures-and.html
put that ic.txt , st.txt , lt.txt in same folder where you want your assembler pass 2 program .
Code for you :
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct mottab
{
char name[8];
int class;
char info[4];
};
struct symtab
{
char sym[8];
int address;
int len;
}st[20];
struct littab
{
int lit;
int address;
}lt[20];
int pt[5]={0};
struct ictab
{
int lc;
int class1,no1;
int class2,no2;
int class3,no3;
}ic[50];
int ipt=0,ilt=0,ist=0,iic=0;
static struct mottab
mot[30]={{"STOP",1,"00"},{"ADD",1,"01"},{"SUB",1,"02"},{"MULT",1,"03"},{"MOVEM",1,"04"},{"MOVER",1,"05"},{"COMP",1,"06"},{"BC",1,"07"},{"DIV",1,"08"},{"READ",1,"09"},{"PRINT",1,"10"},{"START",3,"01"},{"END",3,"02"},{"ORIGIN",3,"03"},{"EQU",3,"04"},{"LTORG",3,"05"},{"DS",2,"01"},{"DC",2,"02"},{"AREG",4,"01"},{"BREG",4,"02"},{"CREG",4,"03"},{"EQ",5,"01"},{"LT",5,"02"},{"GT",5,"03"},{"LE",5,"04"},{"GE",5,"05"},{"NE",5,"06"},{"ANY",5,"07"}};
int main()
{
FILE * fp;
char buffer[60],s1[10],s2[10],s3[10],s4[10],s5[8],s6[8],s7[8];
int i,lc;
fp=fopen("st.txt","r");
while(fgets(buffer,40,fp)!=0)
{
sscanf(buffer,"%s%s%s",s1,s2,s3);
strcpy(st[ist].sym,s1);
st[ist].address=atoi(s2);
st[ist++].len=atoi(s3);
}
fclose(fp);
fp=fopen("lt.txt","r");
while(fgets(buffer,20,fp)!=0)
{
sscanf(buffer,"%s%s",s1,s2);
lt[ilt].lit=atoi(s1);
lt[ilt++].address=atoi(s2);
}
fclose(fp);
fp=fopen("ic.txt","r");
while(fgets(buffer,40,fp)!=0)
{
sscanf(buffer,"%s%s%s%s%s%s%s",s1,s2,s3,s4,s5,s6,s7);
ic[iic].lc=atoi(s1);
ic[iic].no1=atoi(s2);
ic[iic].class1=atoi(s3);
ic[iic].no2=atoi(s4);
ic[iic].class2=atoi(s5);
ic[iic].no3=atoi(s6);
ic[iic++].class3=atoi(s7);
}
fclose(fp);
printf("\n\t**************Target Code ******************");
for(i=0;i<iic;i++)
{
if((ic[i].class1==3 && ic[i].no1==1) || (ic[i].class1==3 && ic[i].no1==3) ) //start or origin
{
lc = ic[i].no2;
continue;
}
if(ic[i].class1==1) //ie imperative statement
{
printf("\n\t\t %d )\t %d",ic[i].lc,ic[i].no1);
if(ic[i].class2==4)
printf("\t%d",ic[i].no2);
else
printf("\t%d",st[ic[i].no2].address);
if(ic[i].class3==4)
printf("\t%d",ic[i].no3);
else
if(ic[i].class3!=0)
if(ic[i].class3==7)
printf("\t%d",st[ic[i].no3].address);
else
printf("\t%d",lt[ic[i].no3].address);
}
else
{
printf("\n\t\t %d )\t 0 \t00 \t00",ic[i].lc);
}
}
}
run above programm as :
root@prashant-HP-ENVY-4-Notebook-PC:~/SPOS Programs/ass pass2# gcc ap2.c
root@prashant-HP-ENVY-4-Notebook-PC:~/SPOS Programs/ass pass2# ./a.out
**************Target Code ******************
200 ) 5 1 208
201 ) 5 2 217
202 ) 5 3 216
203 ) 5 2 220
204 ) 5 3 221
205 ) 1 2 1
206 ) 2 2 3
207 ) 4 209 2
208 ) 0 00 00
209 ) 0 00 00
214 ) 10 209
215 ) 0 00 00
218 ) 0 00 00
219 ) 0 00 00
This comment has been removed by the author.
ReplyDelete