PASS ONE OF TWO PASS ASSEMBLER
AIM:
To write a C program to implement the operation of pass1 of two pass assembler.
ALGORITHM:
Step 1: Start the program.
Step 2: Create a structure to declare the input variables and another structure for output variables.
Step 3: Create a structure for symbol table declaration.
Step 4: In the main function, get the label, operand and opcode.
Step 5: Compare the opcode with START and set the location counter.
Step 6: Compare the output address with the location counter, if true copy the inputs to the output.
Step 7: Using while compare opcode with END and if its not true compare the labels.
Step 8: Set the flag value and equals the symbol table address with location counter value.
Step 9: Compare the opcode with WORD if true add 3 to location counter.
Step 10: If the opcode is BYTE then find the length of operand and equate to location counter.
Step 11: If it is RESW, then add 3 into operand value to location counter.
Step 12: If it is RESB, then equate the value of operand value to location counter.
Step 13: Print the table.
Step 14: Stop the program.
SOURCE CODE:
/* PASS ONE OF TWO PASS ASSEMBLER */
#include<stdio.h>
#include<conio.h>
struct input
{
char label[10],opcode[10],operand[10];
}in[20];
struct output
{
char label[10],opcode[10],operand[10];
int address;
}op[20];
struct symbol
{
char label[10],value[10];
int address;
}sym[20];
int s=1,o=1,i=1,x,flag=0;
int locctr,a,temp,l,t;
char loc[10];
void main()
{
clrscr();
printf("\nEnter the label opcode and operand: ");
scanf("%s%s%s",in[i].label,in[i].opcode,in[i].operand);
if(strcmp(in[i].opcode,"START")==0)
{
locctr=atoi(in[i].operand,10);
}
else
{
locctr=0;
}
op[o].address=locctr;
strcpy(op[o].label,in[i].label);
strcpy(op[o].opcode,in[i].opcode);
strcpy(op[o].operand,in[i].operand);
o++;
i++;
temp=locctr;
printf("\nEnter the label opcode and operand: ");
scanf("%s%s%s",in[i].label,in[i].opcode,in[i].operand);
while(strcmp(in[i].opcode,"END")!=0)
{
if(strcmp(in[i].label,"-")!=0)
{
for(x=1;x<=s;x++)
{
if(strcmp(in[i].label,sym[s].label)==0)
{
flag=1;
break;
}
}
if(flag==0)
{
strcpy(sym[s].label,in[i].label);
sym[s].address=locctr;
}
else
{
printf("\nAlready exists");
}
}
if(strcmp(in[i].opcode,"WORD")==0)
{
strcpy(sym[s].value,in[i].operand);
locctr=locctr+3;
s++;
}
else if(strcmp(in[i].opcode,"BYTE")==0)
{
strcpy(sym[s].value,in[i].operand);
l=strlen(in[i].operand);
locctr=locctr+l;
s++;
}
else if(strcmp(in[i].opcode,"RESW")==0)
{
locctr=locctr+(3*atoi(in[i].operand));
s++;
}
else if(strcmp(in[i].opcode,"RESB")==0)
{
locctr=atoi(in[i].operand);
s++;
}
else
{
locctr=locctr+3;
}
op[o].address=temp;
strcpy(op[o].label,in[i].label);
strcpy(op[o].opcode,in[i].opcode);
strcpy(op[o].operand,in[i].operand);
o++;
i++;
temp=locctr;
printf("\nEnter the label opcode and operand: ");
scanf("%s%s%s",in[i].label,in[i].opcode,in[i].operand);
}
printf("\n\n\t\t\tINPUT PROGRAM\n");
printf("\nLABEL\tOPCODE\tOPERAND");
for(l=1;l<=i;l++)
{
printf("\n\n%s",in[l].label);
printf("\t%s",in[l].opcode);
printf("\t%s",in[l].operand);
}
printf("\n\n\t\t\tOUTPUT PROGRAM");
printf("\n\nADDRESS\tLABEL\tOPCODE\tOPERAND");
for(l=1;l<o;l++)
{
printf("\n\n%d",op[l].address);
printf("\t %s",op[l].label);
printf("\t %s",op[l].opcode);
printf("\t %s",op[l].operand);
}
printf("\n\n\t\tSYMBOL TABLE");
printf("\n\nADDRESS\tLABEL\tVALUE");
for(l=1;l<=s;l++)
{
printf("\n\n%d",sym[l].address);
printf("\t%s",sym[l].label);
printf("\t%s",sym[l].value);
}
getch();
}
OUTPUT:
Enter the label opcode and operand: COPY START 1000
Enter the label opcode and operand: -   ADD     ONE
Enter the label opcode and operand: -   SUB     TWO
Enter the label opcode and operand: ONE WORD    1
Enter the label opcode and operand: TWO WORD    2
Enter the label opcode and operand: -   END     1000
                        INPUT PROGRAM
LABEL   OPCODE  OPERAND
COPY    START   1000
-       ADD     ONE
-       SUB     TWO
ONE     WORD    1
TWO     WORD    2
-       END     1000
                        OUTPUT PROGRAM
ADDRESS LABEL   OPCODE  OPERAND
1000     COPY    START   1000
1000     -       ADD     ONE
1003     -       SUB     TWO
1006     ONE     WORD    1
1009     TWO     WORD    2
                SYMBOL TABLE
ADDRESS LABEL   VALUE
1006    ONE     1
1009    TWO     2
Result
Thus above program executed and output verified.
0 comments:
Post a Comment