Sunday 25 September 2011


                                        PASS TWO OF TWO PASS ASSEMBLER

AIM:
To write a c program to perform pass 2 of two pass assembler.
ALGORITHM:
Step 1: Start the program.
Step 2: Create structure for input, symtab, optab, final table.
Step 3: In the main function, assign values for instructions.
Step 4: Get the input from the user and check if OPCODE=START then begin assembler.
Step 5: Calculate object code for input.
Step 6: Display the output in the table format.
Step 7: Stop the program.
SOURCE CODE:
/* PASS TWO OF TWO PASS ASSEMBLER */
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct input
{
char label[10],opcode[10],operand[10],address[20];
}in[20];
struct optab
{
char opcode[10],mnemonics[10];
}op[20];
struct symbol
{
char address[10],label[10],value[10];
}sym[20];
struct finaltable
{
char address[10],opcode[10],operand[10];
}fin[10];
void main()
{
int i,k,o,s,f,flag,x,n,y;
clrscr();
o=1;
strcpy(op[1].mnemonics,"ADD");
strcpy(op[1].opcode,"10");
strcpy(op[2].mnemonics,"SUB");
strcpy(op[2].opcode,"20");
strcpy(op[3].mnemonics,"MUL");
strcpy(op[3].opcode,"30");
strcpy(op[4].mnemonics,"DIV");
strcpy(op[4].opcode,"40");
strcpy(op[5].mnemonics,"LDA");
strcpy(op[5].opcode,"50");
o=5;
printf("\nEnter the number of entries in the symbol table:\t");
scanf("%d",&n);
printf("\nAddress\tLabel\tValue\n");
for(s=1;s<=n;s++)
scanf("%s\t%s\t%s",sym[s].address,sym[s].label,sym[s].value);
i=1;
s=1;k=1;
printf("\nEnter the address:");
scanf("%s",in[i].address);
printf("\nEnter the label:");
scanf("%s",in[i].label);
printf("\nEnter the opcode:");
scanf("%s",in[i].opcode);
printf("\nEnter the operand:");
scanf("%s",in[i].operand);
while(strcmp(in[i].opcode,"END")!=0)
{
f=0;
flag=0;
for(x=1;x<=o;x++)
{
if(strcmp(in[i].opcode,op[x].mnemonics)==0)
{
flag=1;
break;
}
}
if(flag==1)
{
strcpy(fin[k].opcode,op[x].opcode);
strcpy(fin[k].address,in[i].address);
for(y=1;y<=n;y++)
{
if(strcmp(in[i].operand,sym[y].label)==0)
{
f=1;
break;
}
else
{
f=0;
}
}
if(f==1)
strcat(fin[k].operand,sym[y].address);
else if(f==0)
printf("\nSymbol Table Entry is Wong");
}
else if(strcmp(in[i].opcode,"WORD")==0||strcmp(in[i].opcode,"BYTE")==0)
strcpy(fin[k].opcode,in[i].operand);
else if(strcmp(in[i].opcode,"RESW")==0||strcmp(in[i].opcode,"RESB")==0)
strcpy(fin[k].opcode,"");
strcpy(fin[k].address,in[i].address);
i++;
k++;
printf("\nEnter the address:");
scanf("%s",in[i].address);
printf("\nEnter the label:");
scanf("%s",in[i].label);
printf("\nEnter the opcode:");
scanf("%s",in[i].opcode);
printf("\nEnter the operand:");
scanf("%s",in[i].operand);
}
printf("\n\nInput Program");
printf("\nADDRESS\tLABEL\tOPCODE\tOPERAND");
for(f=1;f<=i;f++)
printf("\n%s\t%s\t%s\t%s",in[f].address,in[f].label,in[i].opcode,in[f].operand);
printf("\nSYMBOL TABLE");
printf("\nLABEL\tADDRESS\tVALUE");
for(f=1;f<=n;f++)
printf("\n%s\t%s\t%s",sym[f].label,sym[f].address,sym[f].value);
printf("\n\nFINAL TABLE");
printf("\nADDRESS\tOBJECTCODE");
for(f=1;f<=k;f++)
{
printf("\n%s\t%s%s",fin[f].address,fin[f].opcode,fin[f].operand);
}
getch();

}



OUTPUT:

Enter the number of entries in the symbol table:        2

Address Label   Value
1006    ONE     1
1009    TWO     2

Enter the address:1000

Enter the label:COPY

Enter the opcode:START

Enter the operand:1000

Enter the address:1000

Enter the label:-

Enter the opcode:ADD

Enter the operand:ONE

Enter the address:1003

Enter the label:-

Enter the opcode:ADD

Enter the operand:TWO

Enter the address:1006

Enter the label:ONE

Enter the opcode:WORD

Enter the operand:1

Enter the address:1009

Enter the label:TWO

Enter the opcode:WORD

Enter the operand:2

Enter the address:-

Enter the label:-

Enter the opcode:END

Enter the operand:-


Input Program
ADDRESS LABEL   OPCODE  OPERAND
1000    COPY    END     1000
1000    -       END     ONE
1003    -       END     TWO
1006    ONE     END     1
1009    TWO     END     2
-       -       END     -
SYMBOL TABLE
LABEL   ADDRESS VALUE
ONE     1006    1
TWO     1009    2

FINAL TABLE
ADDRESS OBJECTCODE
1000
1000    101006
1003    101009
1006    1
1009    2

Result
Thus above program executed and output verified.

0 comments:

Post a Comment