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

Pass 2 Assembler

1. The document describes the algorithm for implementing pass one of a two-pass assembler. It reads source code lines and determines locations in memory for instructions and data by incrementing the location counter (LOCCTR). It also builds a symbol table by inserting labels and their memory locations. 2. The key steps are reading lines from an input file, searching for labels and symbols to insert into the symbol table, looking up opcodes to determine instruction lengths, and incrementing LOCCTR accordingly for each line to calculate memory locations. 3. It writes the lines along with their calculated memory locations to an output file, and outputs the length of the assembled program.

Uploaded by

lekhaperumal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
290 views

Pass 2 Assembler

1. The document describes the algorithm for implementing pass one of a two-pass assembler. It reads source code lines and determines locations in memory for instructions and data by incrementing the location counter (LOCCTR). It also builds a symbol table by inserting labels and their memory locations. 2. The key steps are reading lines from an input file, searching for labels and symbols to insert into the symbol table, looking up opcodes to determine instruction lengths, and incrementing LOCCTR accordingly for each line to calculate memory locations. 3. It writes the lines along with their calculated memory locations to an output file, and outputs the length of the assembled program.

Uploaded by

lekhaperumal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

IMPLEMENTATION OF PASS ONE OF A TWO PASS ASSEMBLER

AIM OF THE EXPERIMENT:


To write a program to implement pass one of a two pass assembler.
ALGORITHM
1.begin
2 read first input line;
3 if OPCODE = 'START' then
begin
i. save #[OPERAND] as starting address
ii. initialized LOCCTR to starting address
iii. write line to intermediate file
iv. read next input line
end {if START}
4 else
initialized LOCCTR to 0
5 while OPCODE != 'END' do
begin
a. i. if this is not a comment line then
ii. begin
iii. if there is a symbol in the LABEL field then
begin
1. search SYMTAB for LABEL
2. if found then
3. set error flag (duplicate symbol)
4. else
5. insert (LABEL, LOCCTR) into SYMTAB
end {if symbol}
iv. search OPTAB for OPCODE
v. if found then
add 3 {instruction length} to LOCCTR
vi. else if OPCODE = 'WORD' then
add 3 to LOCCTR
vii. else if OPCODE = 'RESW' then
add 3 * #[OPERAND] to LOCCTR
viii. else if OPCODE = 'RESB' then
add #[OPERAND] to LOCCTR
ix. else if OPCODE = 'BYTE' then
begin
1. find length of constant in bytes
2. add length to LOCCTR
end {if BYTE}
x. else
set error flag (invalid operation code)
end {if not a comment}
6 write line to intermediate file
7 read next input line
8 end {while not END}
9 write last line to intermediate file
10 save (LOCCTR - starting address) as program length
11 end
c) Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
char opcode[10],mnemonic[3],operand[10],label[10],code[10];
int locctr,start,length;
FILE *fp1,*fp2,*fp3,*fp4;
clrscr();
fp1=fopen("input.txt","r");
fp2=fopen("symtbl.txt","w");
fp3=fopen("out.txt","w");
fp4=fopen("optab.txt","r");
fscanf(fp1,"%s%s%s",label,opcode,operand);
if(strcmp(opcode,"START")==0)
{
start=atoi(operand);
locctr=start;
fprintf(fp3,"\t%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp1,"%s%s%s",label,opcode,operand);
}
else
locctr=0;
while(strcmp(opcode,"END")!=0)
{
fprintf(fp3,"%d\t",locctr);
if(strcmp(label,"**")!=0)
fprintf(fp2,"%s\t%d\n",label,locctr);
fscanf(fp4,"%s%s",code,mnemonic);
while(strcmp(code,"END")!=0)
{
if(strcmp(opcode,code)==0)
{
locctr+=3;
break;
}
fscanf(fp4,"%s%s",code,mnemonic);
}
if(strcmp(opcode,"WORD")==0)
locctr+=3;
else if(strcmp(opcode,"RESW")==0)
locctr+=(3*(atoi(operand)));
else if(strcmp(opcode,"RESB")==0)
locctr+=(atoi(operand));
else if(strcmp(opcode,"BYTE")==0)
++locctr;
fprintf(fp3,"%s\t%s\t%s\t\n",label,opcode,operand);
fscanf(fp1,"%s%s%s",label,opcode,operand);
}
fprintf(fp3,"%d\t%s\t%s\t%s\n",locctr,label,opcode,operand);
length=locctr-start;
printf("The length of the program is %d",length);
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
getch();
}


d) Output:
INPUT FILES:

input.txt

** START 2000
** LDA FIVE
** STA ALPHA
** LDCH CHARZ
** STCH C1
ALPHA RESW 1
FIVE WORD 5
CHARZ BYTE C'Z'
C1 RESB 1
** END **
Dr.NNCE I T / V Sem SS Lab - LM


14
optab.txt

START *
LDA 03
STA 0f
LDCH 53
STCH 57
END *

OUTPUT FILES:

The length of the program is 20
symtab.txt
ALPHA 2012
FIVE 2015
CHARZ 2018
C1 2019

ouput.txt
** START 2000
2000 ** LDA FIVE
2003 ** STA ALPHA
2006 ** LDCH CHARZ
2009 ** STCH C1
2012 ALPHA RESW 1
2015 FIVE WORD 5
2018 CHARZ BYTE C'Z'
2019 C1 RESB 1
2020 ** END **

You might also like