#include<stdio.h>
#include<conio.h>
#include<string.h>
struct symbol
{
char sn[35];
int add;
}s[35];
void main()
{
int symerr=0,oper=0,i=1,j=1,lc,k,sa,p1,f,op,l,q,d,x=1;
char opcode[50],lb[25],operand[25];
char optab[10][10]={"LDA","LDB","STA","ADD","COMP","J","JEQ","SUB","STA"};
char value[10][10]={"00","b8","0c","18","28","3C","30","lc","14","78"};
FILE *fp1,*fp2;
clrscr();
fp1=fopen("input1.txt","r");
fp2=fopen("inter.txt","w");
fscanf(fp1,"%s %s",&lb,&opcode);
if(strcmp(opcode,"START")==0)
{
fscanf(fp1,"%d",&lc);
sa=lc;
fprintf(fp2,"%d\t %s\t %s\t %d\n",lc,lb,opcode,lc);
fscanf(fp1,"%s %s",&lb,&opcode);
}
while(!feof(fp1))
{
d=lc;
if(strcmp(".",lb)!=0)
{
if(strcmp("-",lb)!=0)
{
for(i=1;i<=j;i++)
{
if(strcmp(s[i].sn,lb)==0)
symerr=1;
break;
}
if(symerr==0)
{
strcpy(s[j].sn,lb);
s[j].add=lc;
j++;
}
}
for(k=0;k<10;k++)
for(l=0;l<10;l++)
if(optab[k][l]==opcode)
{
fscanf(fp1,"%s",&operand);
lc=lc+3;
x=0;
}
if(strcmp(opcode,"RESW")==0)
{
fscanf(fp1,"%s",&operand);
op=atoi(operand);
lc=lc+(op*3);
}
else if(strcmp(opcode,"RESB")==0)
{
fscanf(fp1,"%s",&operand);
op=atoi(operand);
lc=lc+op;
}
else if(strcmp(opcode,"BYTE")==0)
{
fscanf(fp1,"%s",&operand);
f=strlen(operand);
lc=lc+(f-3);
}
else if(strcmp(opcode,"WORD")==0)
{
fscanf(fp1,"%s",&operand);
lc=lc+3;
}
else
{
if(x==1)
{
fscanf(fp1,"%s",operand);
lc=lc+3;
}
}
}
fprintf(fp2,"\n%d\t%s\t%s\t%s\n",d,lb,opcode,operand);
if(symerr==1)
fprintf(fp2,"\n**DUPLICATE SYMBOL**\n");
if(oper==1)
fprintf(fp2,"\n**INVALID OPERATION CODE**\n");
fscanf(fp1,"%s\t%s",&lb,&opcode);
symerr=0;
oper=0;
}
p1=lc-sa;
fprintf(fp2,"\nThe program length is %d",p1);
printf("\n symbol table\tlabeL address\n");
for(q=1;q<j;q++)
printf("\n%s\t%d",s[q].sn,s[q].add);
fcloseall();
getch();
}
Pass 1
Assign addresses to all statements in the program
Save the values assigned to all labels for use in Pass 2
Perform some processing of assembler directives
Pass 2
Assemble instructions
Generate data values defined by BYTE, WORD
Perform processing of assembler directives not done in
Pass 1
Write the object program and the assembly listing
pass 1&pass 2 block diagram assembler
The output of an assembler is a part or all of a product. An assembler can work in a variety of manufacturing operations with the right training.
In the first pass of a two-pass assembler, it will "prepare" for the second pass, ie. it'll build the symbol table which is later used in the second pass to generate code.
In order to build a two pass assembler a assembly programming language should support macro processing, file inclusion. Its definition should be independent of any particular hardware or operating system.
what are the elements of assembly language programming?
FORTRAN, Assembler, to name two. Effectively, any language that allows you to reference symbols before they are declared.
Two main options for the design of assembler are: 1. One pass assembler 2. Multi-pass assembler One pass assemblers generally have problem of "forward referencing" which is resolved by using mulitpasses
In the first pass of a two-pass assembler, it will "prepare" for the second pass, ie. it'll build the symbol table which is later used in the second pass to generate code.
design procedure of two pass assebler
A two-pass assembler reads through the source code twice. Each read-through is called a pass. On pass one the assembler doesn't write any code. It builds up a table of symbolic names against values or addresses. On pass two, the assembler generates the output code, using the table to resolve symbolic names, enabling it to enter the correct values. The advantage of a two-pass assember is that it allows forward referencing in the source code because when the assembler is generating code it has already found all references.
The output of an assembler is a part or all of a product. An assembler can work in a variety of manufacturing operations with the right training.
input file for the single pass assembler
pass 1 assembler is assembler which convert assembly level language into machine level language in one pass only
multipass assembler means more than one pass is used by assembler.multipass assembler is used to eliminate forward references in sybol definition.it creates a number of passes that is necessary to process the definition of symbols•Multi pass assembler:-Does the work in two pass-Resolves the forward references•First pass:-Scans the code-Validates the tokens-Creates a symbol table•Second Pass:-Solves forward references-Converts the code to the machine code
Single Pass Assembler A single pass assembler scans the program only once and creates the equivalent binary program. The assembler substitute all of the symbolic instruction with machine code in one pass. AdvantagesEvery source statement needs to be processed once.DisadvantagesWe cannot use any forward reference in our program. Forward ReferenceForward reference means; reference to an instruction which has not yet been encountered by the assembler. In order to handle forward reference, the program needs to be scanned twice. In other words a two pass assembler is needed.
Ans: If we talk about multi-pass or say three pass assembler it afford user with lots of additional features in solving any problem for example where two pass assembler fails for example in instructions like these given below:ALFA EQU BETADELTA EQU ALFA..BETA EQU 24Where two pass assembler could only resolve forward reference upto ALFA equals BETA after getting value of BETA which is defined afterwards in the program which is forward reference but could be inefficient to allot value to DELTA simultaneously.In such conditions multi pass assembler like three pass assembler could easily resolve the issue in its third pass after getting value of ALFA in second pass and assigning it to DELTA (forward reference) in third pass.Though it could become somehow complex using this type of assembler even takes longer time too but its more reliable in programming than one or two pass assemblers which could not guarantee to solve our problem with best efficiency.
In the first pass of a two-pass assembler, it will "prepare" for the second pass, ie. it'll build the symbol table which is later used in the second pass to generate code.
In order to build a two pass assembler a assembly programming language should support macro processing, file inclusion. Its definition should be independent of any particular hardware or operating system.