Sunday 25 September 2011


                                                       ONE PASS ASSEMBLER

AIM:
To write a C program to implement one pass assembler.
ALGORITHM:
Step 1: Start the program.
Step 2: Create the structure for symbol table, opcode, instruction, input and output.
Step 3: In the main function assign the opcode values.
Step 4: Get the label, opcode, operand from the user and compare the label. If it is start, execute the program.
Step 5: Then convert the operand to integer and store counter value.
Step 6: Create the symbol table from the instruction.
Step 7: Do necessary operations and display the result.
Step 8: Stop the program.

SOURCE CODE:
/* ONE PASS ASSEMBLER */
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct input
{
char label[20],opc[20],opr[20];
}ip[20];
struct symbol
{
char sym[20],val[20];
int addr;
}s[20];
struct obj
{
int addr;
char obj[20];
}ob[20];
struct table
{
int addr;
char sym[20];
}t[20];
struct optab
{
char opc[20],val[20];
}op[20];
void main()
{
int n=0,t1=0,o1=0,in=0,i,j,temp,lc,flag=0,k;
char str[20];
clrscr();
strcpy(op[1].opc,"ADD");
strcpy(op[1].val,"10");

strcpy(op[2].opc,"SUB");
strcpy(op[2].val,"20");

strcpy(op[3].opc,"MUL");
strcpy(op[3].val,"30");

strcpy(op[4].opc,"LDA");
strcpy(op[4].val,"40");

strcpy(op[5].opc,"LDX");
strcpy(op[5].val,"50");
printf("\n\t\tEnter the instruction");
printf("\nLabel\tOpcode\tOperand\n");
for(in=1;;in++)
{
scanf("\n%s\t%s\t%s",ip[in].label,ip[in].opc,ip[in].opr);
if(!(strcmp(ip[in].opc,"END")))
break;
}
if(!(strcmp(ip[1].opc,"START")))
lc=(atoi(ip[1].opr));
else
lc=0;
for(j=1;j<=in;j++)
{
temp=lc;
if(!(strcmp(ip[j].opc,"RESW")))
lc=(atoi(ip[j].opr))*3+lc;
else if(!(strcmp(ip[j].opc,"RESB")))

lc=(atoi(ip[j].opr))+lc;
else if(!(strcmp(ip[j].opc,"BYTE")))

lc=(strlen(ip[j].opr))+lc;
else if(!(strcmp(ip[j].opc,"START")));
else
lc=lc+3;
if(strcmp(ip[j].label,"-")!=0)
{
for(i=1;(i<=n)&&(strcmp(ip[j].label,s[i].sym));i++);
if(i==(n+1))
{
n++;
strcpy(s[n].sym,ip[j].label);
if((!(strcmp(ip[j].opc,"WORD"))||(!(strcmp(ip[j].opc,"BYTE")))))
strcpy(s[n].val,ip[j].opr);
else
strcpy(s[n].val,"0");
s[n].addr=temp;
}
}
flag=0;
for(i=1;i<=5;i++)
if(!(strcmp(op[i].opc,ip[j].opc)))
{
strcpy(ob[++o1].obj,op[i].val);
for(k=1;k<=n;k++)
if(!(strcmp(s[k].sym,ip[j].opr)))
{
itoa(s[k].addr,str,10);
strcat(ob[o1].obj,str);
}
flag=1;
}
if(flag==1);
else if((!(strcmp(ip[j].opc,"WORD")))||(!(strcmp(ip[j].opc,"BYTE"))))
strcpy(ob[++o1].obj,ip[j].opr);
else
if((!(strcmp(ip[j].opc,"RESW")))||(!(strcmp(ip[j].opc,"RESB")))||(!(strcmp(ip[j].opc,"START")))||(!(strcmp(ip[j].opc,"END"))))
strcpy(ob[++o1].obj,"-");
else
strcpy(ob[++o1].obj,"00");
ob[o1].addr=temp;
if((!(strcmp(ip[j].opc,"RESW")))||(!(strcmp(ip[j].opc,"RESB")))||(!(strcmp(ip[j].opc,"BYTE")))||(!(strcmp(ip[j].opc,"START")))||(!(strcmp(ip[j].opc,"END")))||(!(strcmp(ip[j].opc,"WORD"))));
else
{
for(i=1;(i<=n)&&(strcmp(ip[j].opr,s[i].sym));i++);
if(i==(n+1))
{
t1++;
strcpy(t[t1].sym,ip[j].opr);
t[t1].addr=temp;
}
}
}
if((!(strcmp(ip[1].opc,"START"))))
flag=2;
else
flag=1;
for(i=1;i<=t1;i++)
{
for(j=1;(j<=n)&&(strcmp(t[i].sym,s[j].sym));j++);
if(j!=(n+1))
{
for(k=flag;k<=o1;k++)
{
if(ob[k].addr==t[i].addr)
{
itoa(s[j].addr,str,10);
strcat(ob[k].obj,str);
}
}
}
}
printf("\n\nINPUT TABLE\n");
printf("\n\nADDRESS\tLABEL\tOPCODE\tOPERAND\n");
for(i=1;i<=in;i++)
printf("\n\n%d\t%s\t%s\t%s\t",ob[i].addr,ip[i].label,ip[i].opc,ip[i].opr);
printf("\n\nTABLE OF INCOMPLETE INSTRUCTIONS\n");
printf("\n\nADDRESS\tSYMBOL\n");
for(i=1;i<=t1;i++)
printf("\n%d\t%s\t",t[i].addr,t[i].sym);
printf("\n\nSYMBOL TABLE\n");
printf("\n\nSYMBOL\tADDRESS\tVALUE\n");
for(i=1;i<=n;i++)
printf("\n%s\t%d\t%s\t",s[i].sym,s[i].addr,s[i].val);
printf("\n\nOBJECT CODE\n");
printf("\n\nADDRESS\tOBJECTCODE\n");
for(i=1;i<=o1;i++)
printf("\n%d\t%s\t",ob[i].addr,ob[i].obj);
getch();
}

OUTPUT:

Enter the instruction
Label   Opcode  Operand
COPY    START   2000
-       ADD     ONE
-       SUB     TWO
ONE     WORD    1
TWO     WORD    2
-       END     -

INPUT TABLE


ADDRESS LABEL   OPCODE  OPERAND


2000    COPY    START   2000

2000    -       ADD     ONE

2003    -       SUB     TWO

2006    ONE     WORD    1

2009    TWO     WORD    2

2012    -       END     -

TABLE OF INCOMPLETE INSTRUCTIONS


ADDRESS SYMBOL

2000    ONE
2003    TWO

SYMBOL TABLE


SYMBOL  ADDRESS VALUE

COPY    2000    0
ONE     2006    1
TWO     2009    2

OBJECT CODE


ADDRESS OBJECTCODE

2000    -
2000    102006
2003    202009
2006    1
2009    2
2012    -

Result
Thus above program executed and output verified.

0 comments:

Post a Comment