0% found this document useful (0 votes)
51 views3 pages

Implematation of Pass 1

This C program implements the first pass of a two-pass assembler. It takes source code from an input file, determines instruction locations, and writes symbol tables and object code to output files. It reads operation codes and operands, looks up instructions in a code table, calculates instruction lengths, and increments the program location counter accordingly. The length of the assembled program is then printed.

Uploaded by

agnicollege
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views3 pages

Implematation of Pass 1

This C program implements the first pass of a two-pass assembler. It takes source code from an input file, determines instruction locations, and writes symbol tables and object code to output files. It reads operation codes and operands, looks up instructions in a code table, calculates instruction lengths, and increments the program location counter accordingly. The length of the assembled program is then printed.

Uploaded by

agnicollege
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

IMPLEMENTATION OF PASS ONE OF A TWO PASS ASSEMBLER.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
char opcode[10],mnemonic[3],operand[10],label[10],code[10];
int loc,start,length;
FILE *fp1,*fp2,*fp3,*fp4;
fp1=fopen("input.dat","r");
fp2=fopen("symtab.dat","w");
fp3=fopen("out.dat","w");
fp4=fopen("opcode.dat","r");
fscanf(fp1,"%s%s%s",label,opcode,operand);
if(strcmp(opcode,"START")==0)
{
start=atoi(operand);
loc=start;
fprintf(fp3,"\t%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp1,"%s%s%s",label,opcode,operand);
}
else
loc=0;
while(strcmp(opcode,"END")!=0)
{
fprintf(fp3,"%d\t",loc);
if(strcmp(label,"**")!=0)
fprintf(fp2,"%s\t%d\n",label,loc);
fscanf(fp4,"%s%s",code,mnemonic);
while(strcmp(code,"END")!=0)
{

if(strcmp(opcode,code)==0)
{
loc+=3;
break;
}
fscanf(fp4,"%s%s",code,mnemonic);
}
if(strcmp(opcode,"WORD")==0)
loc+=3;
else if(strcmp(opcode,"RESW")==0)
loc+=(3*(atoi(operand)));
else if(strcmp(opcode,"RESB")==0)
loc+=(atoi(operand));
else if(strcmp(opcode,"BYTE")==0)
++loc;
fprintf(fp3,"%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp1,"%s%s%s",label,opcode,operand);
}
fprintf(fp3,"%d\t%s\t%s\t%s\n",loc,label,opcode,operand);
length=loc-start;
printf("The length of the program is : %d",length);
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
getch();
}

INPUT FILES :
input.dat
**

START

2000

**
**
**
**
ALPHA
FIVE
CHARZ
C1
**

LDA
STA
LDCH
STCH
RESW
WORD
BYTE
RESB
END

opcode.dat
START
LDA
STA
LDCH
STCH
END

*
03
0F
53
57

FIVE
ALPHA
CHARZ
C1
1
5
C'Z'
1
**

You might also like