Sunteți pe pagina 1din 5

Experiment 7

Aim:
To implement data structure for Pass 1 of Two-Pass Assembler.

Theory:
Assembler​ is a program for converting instructions written in low-level assembly code
into relocatable machine code and generating along information for the loader.
It generates instructions by evaluating the mnemonics (symbols) in the operation field
and finding the value of symbols and literals to produce machine code. Now, if an
assembler does all this work in one scan then it is called a single pass assembler,
otherwise if it does in multiple scans then called multiple pass assembler.  
Forward reference problem :​ Rules for an assembly program states that the symbol
should be defined somewhere in the program. But in some cases a symbol may be
used prior to its definition. Such a reference is called forward reference. Due to this
assembler cannot assemble the instructions and such a problem is called forward
reference.
Eg : c=a + b; int a; int b; int c;
Solution to Forward Reference Problem:
The forward reference problem can be resolved by properly defining all areas before
they are referenced, using a Forward Reference Table. It is possible, but inconvenient
to do so for all data items.
The other way the forward reference problem can be resolved is by making different
passes (one pass/two pass/multi pass) over the assembler code.
Databases required for pass 1:
1) Input Source Program
2) Location Counter (LC) - to keep track of each instructions’ location and to assign
addresses for each symbol defined.
3) Machine Operation Table (MOT) - indicates the symbolic mnemonic for each
instruction and its length.
4) Pseudo Operation Table (POT) - indicates pseudo opcode and the action to be
taken in pass 1.
5) Symbol Table (ST) - used to store each label and its corresponding value.
6) Literal Table (LT) - used to store each literal encountered and its corresponding
assigned location.
7) Copy of the input to be used later by pass 2.
Flowchart :

Conclusion:
Hence, we understood why 2 passes are required in an assembler. We also understood
the databases used in an assembler and implemented pass 1 for the sample code.
Program:
#include<stdio.h>
#include<conio.h>
struct symtab
{
char sname[10];
int addr;
}stab[10];
struct littab
{
char lname[10];
int addr;
}ltab[10];

void main()
{
int lc=0,i=0,j=0,x;
FILE *fp1,*fp2;
char str[80];
char* s1[10],s2[10],s3[10];
int reg[16]={0};

fp2= fopen("out.txt","w");
fp1=fopen("prog.txt","r");

while(!feof(fp1))
{

fgets(str,80,fp1);
sscanf(str,"%s %s %s",s1,s2,s3);

if(strcmp(s2,"START")==0)
{

lc=atoi(s3);
strcpy(stab[i].sname,s1);
stab[i].addr=lc;
i++;

}
else if(strcmp(s1,"USING")==0)
reg[15]=1;
else if (strcmp(s2,"DC")==0||strcmp(s2,"DS")==0){
strcpy(stab[i].sname,s1);
stab[i].addr=lc;
i++;
lc+=4;}
else if(strcmp(s1,"END")==0){
for(x=0;x<j;x++){
ltab[x].addr=lc;
lc+=4;}}
else{
fprintf(fp2,"%d %s\n",lc,str);
if(s3[0]=='='){
strcpy(ltab[j].lname,s3);
j++;}
lc+=4;}

strcpy(s1,"");
strcpy(s2,"");
strcpy(s3,"");}
fclose(fp1);
fclose(fp2);
printf("Symbol table:\n");

for(x=0;x<i;x++)
printf("%s %d\n",stab[x].sname,stab[x].addr);
printf("literal table\n");

for(x=0;x<j;x++)
printf("%s %d\n",ltab[x].lname,ltab[x].addr);
printf("Base Registers:\n");

for(x=0;x<=15;x++)
printf("reg[%d] = %d\n",x,reg[x]);
fp2 = fopen("out.txt","r");
printf("\n########################\n");
while(!feof(fp2)){
fgets(str,80,fp2);
printf("%s",str);}
fclose(fp2);
getch();}
Input Assembly Program
PG1 START 1000
USING *,15
L 1, FOUR
A 1, ='5'
ST 1, TEMP
A 1, ='4'
FOUR DC F'4'
TEMP DS '1'F
END

OUTPUT:

S-ar putea să vă placă și