0% found this document useful (0 votes)
5 views

Ex NO -2

The document outlines the implementation of pass one of a two-pass assembler, detailing the algorithm for reading input lines, processing opcodes, and managing symbols and location counters. It includes a C program that reads an input file, processes assembly instructions, and generates an output file with the intermediate results and symbol table. The final output also includes the calculated length of the program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Ex NO -2

The document outlines the implementation of pass one of a two-pass assembler, detailing the algorithm for reading input lines, processing opcodes, and managing symbols and location counters. It includes a C program that reads an input file, processes assembly instructions, and generates an output file with the intermediate results and symbol table. The final output also includes the calculated length of the program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Ex No.

2 Implementation Of Pass One Of A Two Pass


Date Assembler

AIM:

To write a program to implement pass one of a two pass assembler.

ALGORITHM:

1.Read the input line.


2.Check to see if the opcode in the input line is “START”.
i. Find if there is any operand field after START, if so that indicates the location from
which the program has to be loaded in the memory, so initialize the LOCCTR to the
operand value.
ii. Otherwise if there is no value in the operand field the LOCCTR is set to zero and
the program starts loading from the starting address zero.
3.Write the line to the intermediate file.
4.Now start the following processing for the other lines in the program, if it is not a comment
line, until the opcode is “END”.
A) If there is a symbol in the label field.
i. Check the symbol table to see if has already been stored over there. If so then it is a
duplicate symbol, the error message should be displayed.
ii.Otherwise if the symbol is entered into the SYMTAB, along with the memory address in
which it is stored.
B) If there is an opcode in the line read.
i. Search the OPTAB to see if the opcode is present, if so increment the location counter
(LOCCTR) by three.
ii. a) If the opcode is ‘WORD’, increment the location counter by three.
b) If the opcode is ‘BYTE’ increment the location counter by one.
c) If opcode is RESW increment the location counter by the integer equivalent of operand
value *3.
d) If the opcode is RESB increment the location counter by the integer equivalent
of the operand value.
C) Write each and every line processed to the intermediate file along with their
Location counters.
5.Subtract the starting address of the program from the final value of the LOCCTR, to Calculate
the length of the program.
6.Close all the operand files and exit.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char label[10],inst[10],op1[10],op2[10];
char buff[10];
int a,b,loctr=0,ctr,flag,d[5];
FILE *fp1,*fp2,*fp3,*fp4;
clrscr();
fp1=fopen("INPUT.TXT","r");
fp2=fopen("OUTPUT.TXT","w");
fp3=fopen("OPTAB.TXT","r");
fp4=fopen("SYM.TXT","w");
fprintf(fp4,"SYMBOL TABLE\n");
fprintf(fp4,"LABEL\nADDRESS\n");
buff[0]='\0';
fscanf(fp1,"%s%s%s%s",label,inst,op1,op2);
if(strcmp(inst,"START")==0)
{
a=atoi(op1);
loctr=a;
fprintf(fp2,"\nADDRESS\tMNEMONICS");
fprintf(fp2,"\n%d\t%s\t%s\t%s\n",loctr,label,inst,op1);
}
else
loctr=0;
while(!feof(fp1))
{
fscanf(fp1,"%s%s%s%s",label,inst,op1,op2);
if(strcmp(inst,"END")!=0)
{
if(strcmp(label,"$")!=0)
fscanf(fp4,"\n%s\t%d",label,loctr);
while(!feof(fp3))
{
fscanf(fp3,"%s%d",buff,&d);
if(strcmp(inst,buff)==0)
{
flag=1;
break;
}
}
b=atoi(op1);
if(strcmp(inst,"WORD")==0)
loctr+=3;
else if(strcmp(inst,"RESW")==0)
loctr+=3*b;
else if(strcmp(inst,"RESB")==0)
loctr+=b;
else if(strcmp(inst,"BYTE")==0)
loctr+=b;
else if(flag)
loctr+=3;
}
fprintf(fp2,"%d\t%s\t%s\t%s\n",loctr,label,inst,op1);
}
fprintf(fp2,"\n\nPRGLEN=%d\n",(loctr-a));
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
}
OUTPUT:
INPUT FILES
INPUT.TXT:
COPY START 1000 $
FIRST STL RETADR $
$ STA BUFFER $
BUFFER RESB 08 $
RETADR RESW 03 $
$ END FIRST $
OPTAB.TXT:
STA 14
STL 32
LDA 03
LTB 69
JSUB 48
JEQ 30
COM P 28
STH 34
SYM.TXT:
SYMBOL TABLE
LABEL ADDRESS
FIRST 1000
BUFFER 1006
RETADR 1014
OUTPUT FILE
OUTPUT.TXT:
ADDRESS MNEMONICS
1000 COPY START 1000
1003 FIRST STL RETADR
1006 $ STA BUFFER
1014 BUFFER RESB 08
1023 RETADR RESW 03
1023 $ END FIRST
PRGLEN=23

You might also like