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

Design of A Two Pass Assembler - Pass I: Program

This document describes the design of the first pass of a two-pass assembler. The first pass processes the input assembly code file to generate symbol, literal and intermediate code tables which are used in the second pass. It opens input and various output files, scans the input to generate symbol and literal tables, looks up opcodes and generates intermediate code which is written to the output file.

Uploaded by

soundar23
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Design of A Two Pass Assembler - Pass I: Program

This document describes the design of the first pass of a two-pass assembler. The first pass processes the input assembly code file to generate symbol, literal and intermediate code tables which are used in the second pass. It opens input and various output files, scans the input to generate symbol and literal tables, looks up opcodes and generates intermediate code which is written to the output file.

Uploaded by

soundar23
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

DESIGN OF A TWO PASS ASSEMBLER PASS I

PROGRAM
#include<stdio.h>
void main() {
char *c,symbol[50],roper[50],moper[50],*symbol1,*d,*roper1,*moper1,*opno,*opsym,
*opty,*roperval,str[50],opcode[50],rep[50],*p,ltno[50],ltsym[50],ltadd[50],stno[50],stsym[50],
stadd[50];
FILE *f,*symf,*orf,*ltf,*opcf,*of;
int start,begin,i=0,j=1;
clrscr();
f=fopen("input.txt","r");
symf=fopen("symtab.txt","w+");
orf=fopen("org.txt","w+");
ltf=fopen("littab.txt","w+");
opcf=fopen("opcode.txt","r");
of=fopen("output.txt","w");
while(!feof(f)) {
fscanf(f,"%s",c);
if(atoi(c)!=0) {
begin=start=atoi(c);
break;
}
}
printf("SYMBOL TABLE\n");
while(!feof(f)) {
fscanf(f,"%s%s%s%s",c,symbol,roper,moper);
if(strcmp(c,"*")) {
i++;
printf("%d\t%s\t%d\n",i,c,start);
fprintf(symf,"%d\t%s\t%d\n",i,c,start);
}
fprintf(orf,"%d\t%s\t%s\t%s\n",start,symbol,roper,moper);
start++;
}
rewind(f);
printf("LITERAL TABLE\n");
while(!feof(f)) {

fscanf(f,"%s%s%s%s",c,symbol,roper,moper);
if(moper[0]=='=') {
rewind(orf);
while(!feof(orf)) {
fscanf(orf,"%s%s%s%s",d,symbol1,roper1,moper1);
if(((!strcmp(symbol1,"LTORG")) || (!strcmp(symbol1,"END")))
&& (atoi(d)>=begin)) {
if(!strcmp(symbol1,"LTORG")) {
printf("%d\t%s\t%s\n",j,moper,d);
fprintf(ltf,"%d\t%s\t%s\n",j,moper,d);
j++;
break;
}
if(!strcmp(symbol1,"END")) {
printf("%d\t%s\t%d\n",j,moper,atoi(d)+1);
fprintf(ltf,"%d\t%s\t%d\n",j,moper,atoi(d)+1);
j++;
break;
}
}
}
begin++;
}
}
rewind(f);
printf("INTERMEDIATE CODE\n");
while(!feof(f)) {
fscanf(f,"%s%s%s%s",c,symbol,roper,moper);
rewind(opcf);
while(!feof(opcf)) {
fscanf(opcf,"%s%s%s",opsym,opno,opty);
if(!strcmp(opsym,symbol)) {
strcpy(opcode,opno);
break;
}
}
if(!strcmp(roper,"AREG,"))
roperval="(1)";
else if(!strcmp(roper,"BREG,"))
roperval="(2)";

else if(!strcmp(roper,"CREG,"))
roperval="(3)";
else if(!strcmp(roper,"DREG,"))
roperval="(4)";
else
roperval="";
if(!strcmp(opty,"AD")) {
strcpy(str,"(AD,");
strcat(str,opcode);
strcat(str,")");
if(!strcmp(symbol,"START")) {
strcpy(rep,"(C,");
strcat(rep,moper);
strcat(rep,")");
}
}
else if(!strcmp(opty,"DL")) {
strcpy(str,"(DL,");
strcat(str,opcode);
strcat(str,")");
if(!strcmp(symbol,"DC")) {
p=(char*)strtok(roper,"\'");
strcpy(rep,"(C,");
strcat(rep,p);
strcat(rep,")");
}
else if(!strcmp(symbol,"DS")) {
strcpy(rep,"(C,");
strcat(rep,roper);
strcat(rep,")");
}
else {
strcpy(rep,"");
}
}
else {
strcpy(str,"(IM,");
strcat(str,opcode);
strcat(str,")");

if(strcmp(moper,"*")) {
if(moper[0]=='=') {
rewind(ltf);
while(!feof(ltf)) {
fscanf(ltf,"%s%s%s",ltno,ltsym,ltadd);
if(!strcmp(ltsym,moper)) {
strcpy(rep,"(L,");
strcat(rep,ltno);
strcat(rep,")");
}
}
}
else {
rewind(symf);
while(!feof(symf)) {
fscanf(symf,"%s%s%s",stno,stsym,stadd);
if(!strcmp(stsym,moper)) {
strcpy(rep,"(S,");
strcat(rep,stno);
strcat(rep,")");
}
}
}
}
else {
strcpy(rep,"");
}
}
printf("%s\t%s\t%s\n",str,roperval,rep);
fprintf(of,"%s\t%s\t%s\n",str,roperval,rep);
strcpy(rep,"");
}
getch();
}

OUTPUT
INPUT.TXT
*
*
*
*
*
*
*
*
N
ONE
RES
*

START
MOVER
SUB
MOVEM
PRINT
ADD
STOP
LTORG
DS
DC
DS
END

*
AREG,
AREG,
AREG,
*
AREG,
*
*
1
'2'
1
*

OPCODE.TXT
STOP
ADD
SUB
MULT
MOVER
MOVEM
COMP
BC
DIV
READ
PRINT
DS
DC
START
END
ORIGIN
EQU
LTORG

00
01
02
03
04
05
06
07
08
09
10
02
01
01
02
03
04
05

IM
IM
IM
IM
IM
IM
IM
IM
IM
IM
IM
DL
DL
AD
AD
AD
AD
AD

400
N
ONE
RES
RES
=XF1
*
*
*
*
*
*

SYMTAB.TXT
1
2
3

N
407
ONE 408
RES 409

LITTAB.TXT
1

=X'F1'

406

OUTPUT.TXT
(AD,01)
(IM,04)
(IM,02)
(IM,05)
(IM,10)
(IM,01)
(IM,00)
(AD,05)
(DL,02)
(DL,01)
(DL,02)
(AD,02)

(1)
(1)
(1)
(1)

(C,400)
(S,1)
(S,2)
(S,3)
(S,3)
(L,1)

(C,1)
(C,2)
(C,1)

You might also like