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

Pass2 of Assembler

The document describes the algorithm for implementing pass two of an assembler. It reads opcode, label, and operand values from an intermediate file and writes the assembled machine code instructions to an output file by looking up opcodes in an operation table and symbols in a symbol table. It handles different instruction types like BYTE, WORD, and others. The source code provided implements this algorithm in C.

Uploaded by

Er Lalit Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
163 views

Pass2 of Assembler

The document describes the algorithm for implementing pass two of an assembler. It reads opcode, label, and operand values from an intermediate file and writes the assembled machine code instructions to an output file by looking up opcodes in an operation table and symbols in a symbol table. It handles different instruction types like BYTE, WORD, and others. The source code provided implements this algorithm in C.

Uploaded by

Er Lalit Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Aim

To implement of pass two of pass two assembler in CS1207 SYSTEM SOFTWARE LAB usi
ng opcode
Algorithm
Start the program
Initialize all the variables
Open a file by name
fp1=fopen("assmlist.dat","w");
fp2=fopen("symtab.dat","r");
fp3=fopen("intermediate.dat","r");
fp4=fopen("optab.dat","r");
Read the content of the file
If opcode is BYTE
if(strcmp(opcode,"BYTE")==0)
Then
fprintf(fp1,"%d\t%s\t%s\t%s\t",address,label,opcode,operand);
Else if opcode is WORD
else if(strcmp(opcode,"WORD")==0)
then
fprintf(fp1,"%d\t%s\t%s\t%s\t00000%s\n",address,label,opcode,operand,a);
Else perform
else if((strcmp(opcode,"RESB")==0)(strcmp(opcode,"RESW")==0))
fprintf(fp1,"%d\t%s\t%s\t%s\n",address,label,opcode,operand);
if it is not math anything
else
fprintf(fp1,"%d\t%s\t%s\t%s\t%d0000\n",address,label,opcode,operand,code);
Finally terminate the of pass two of pass two assembler

Source code program in c pass two of pass two assembler


#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char a[10],ad[10],label[10],opcode[10],operand[10],mnemonic[10],symbol[10];
int i,address,code,add,len,actual_len;
FILE *fp1,*fp2,*fp3,*fp4;
clrscr();
fp1=fopen("assmlist.dat","w");
fp2=fopen("symtab.dat","r");
fp3=fopen("intermediate.dat","r");
fp4=fopen("optab.dat","r");
fscanf(fp3,"%s%s%s",label,opcode,operand);
if(strcmp(opcode,"START")==0)
{
fprintf(fp1,"\t%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp3,"%d%s%s%s",&address,label,opcode,operand);
}
while(strcmp(opcode,"END")!=0)
{
if(strcmp(opcode,"BYTE")==0)
{
fprintf(fp1,"%d\t%s\t%s\t%s\t",address,label,opcode,operand);
len=strlen(operand);
actual_len=len-3;
for(i=2;i<(actual_len+2);i++)
{
itoa(operand[i],ad,16);
fprintf(fp1,"%s",ad);
}
fprintf(fp1,"\n");
}
else if(strcmp(opcode,"WORD")==0)
{
len=strlen(operand);
itoa(atoi(operand),a,10);
fprintf(fp1,"%d\t%s\t%s\t%s\t00000%s\n",address,label,opcode,operand,a);
}
else if((strcmp(opcode,"RESB")==0)(strcmp(opcode,"RESW")==0))
{
fprintf(fp1,"%d\t%s\t%s\t%s\n",address,label,opcode,operand);
}
else
{
rewind(fp4);
fscanf(fp4,"%s%d",mnemonic,&code);
while(strcmp(opcode,mnemonic)!=0)
fscanf(fp4,"%s%d",mnemonic,&code);
if(strcmp(operand,"**")==0)
{
fprintf(fp1,"%d\t%s\t%s\t%s\t%d0000\n",address,label,opcode,operand,code);
}
else
{
rewind(fp2);
fscanf(fp2,"%s%d",symbol,&add);
while(strcmp(operand,symbol)!=0)
{
fscanf(fp2,"%s%d",symbol,&add);
}
fprintf(fp1,"%d\t%s\t%s\t%s\t%d%d\n",address,label,opcode,operand,code,add);
}
}
fscanf(fp3,"%d%s%s%s",&address,label,opcode,operand);
}
fprintf(fp1,"%d\t%s\t%s\t%s\n",address,label,opcode,operand);
printf("Finished");
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
getch();
}

INPUT FILES
INTERMEDIATE.DAT
** 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'EOF'
2019 C1 RESB 1
2020 ** END **
OPTAB.DAT
LDA 33
STA 44
LDCH 53
STCH 57
END *
SYMTAB.DAT
ALPHA 2012
FIVE 2015
CHARZ 2018
C1 2019
OUTPUT FILE :
ASSMLIST.DAT
** START 2000
2000 ** LDA FIVE 332015
2003 ** STA ALPHA 442012
2006 ** LDCH CHARZ 532018
2009 ** STCH C1 572019
2012 ALPHA RESW 1
2015 FIVE WORD 5 000005
2018 CHARZ BYTE C'EOF' 454f46
2019 C1 RESB 1
2020 ** END **

You might also like