Friday 28 September 2012

Pass 1 of 2 pass assembler - System Software - Anna University - Experiment No: 2

Ex:2 Pass 1 of two pass assembler

#include<stdio.h>
#include<string.h>
#include<conio.h>
struct symtab
{
char sym[10];
int val;
}
s;
struct optab
{
char opc[10];
int nm;
}op;
struct pgm
{
char lab[7],opc[7],opr[7];
}p;
void main()
{
FILE *a,*b,*c;
int i,d,lc=0;
clrscr();
a=fopen("optab.txt","w");
printf("pass 1 of 2 pass assembler");
printf("\n enter the opcode table\n enter the size:");
scanf("\t%d",&d);
for(i=0;i<d;i++)
{
printf("enter the opcode and mnemonics\t");
scanf("%s\t%d",op.opc,&op.nm);
fprintf(a,"\n%s\t%d",op.opc,op.nm);
}
fclose(a);
b=fopen("pgm.txt","w");
printf("enter the program\n enter lab1,opcode,operand\n");
do
{
scanf("%s%s%s",p.lab,p.opc,p.opr);
fprintf(b,"\n%s\t%s\t%s",p.lab,p.opc,p.opr);
}
while(strcmp(p.opc,"END")!=0);
fclose(b);
a=fopen("optab.txt","r");
b=fopen("pgm.txt","r");
c=fopen("inmt.txt","w");
while(feof(a)==0)
{
fscanf(a,"%s%d",op.opc,&op.nm);
while(feof(b)==0)
{
fscanf(b,"%s%s%s",p.lab,p.opc,p.opr);
if(strcmp(p.opc,"START")==0)
lc=atoi(p.opc);

else
if(strcmp(p.opc,"WORD")==0)
lc+=3;
else
if(strcmp(p.opc,"RESW")==0)
lc+=3*atoi(p.opr);
else
if(strcmp(p.opc,"RESB")==0)
lc+=atoi(p.opr);
else
if(strcmp(p.opc,"BYTE")==0)
lc+=(strlen(p.opr));
else
if(strcmp(p.opc,"END")==0)
lc+=3;
fprintf(c,"%s\t\t%d",p.lab,lc);
}
}
fclose(c);
fclose(b);
fclose(a);
c=fopen("inmt.txt","r");
printf("the output is....\n SYMBOL TABLE\n SYMBOL\n ADDRESS\n");
while(feof(c)==0)
{
fscanf(c,"%s%d",s.sym,&s.val);
printf("%s\t\t%d\n",s.sym,s.val);
}
fclose(c);
getch();
}


Output:

pass 1 of 2 pass assembler

enter the opcode table

enter the size:3

enter the opcode and mnemonics m 7

enter the opcode and mnemonics n 4

enter the opcode and mnemonics h 8

enter the program

enter lab1,opcode,operand

A START 43

B BYTE 54

C RESW 56

D RESB 67

E END 87

the output is....

SYMBOL TABLE

SYMBOL

ADDRESS

A

0

B

2

C

170

D

237

E

240

No comments:

Don't You Think this Awesome Post should be shared ??
| Pass 1 of 2 pass assembler - System Software - Anna University - Experiment No: 2 |
Back To Top Related Posts Plugin for WordPress, Blogger...