Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

DESIGN OF A SINGLE PASS ASSEMBLER

PROGRAM
#include<stdio.h>
struct symboltab {
char sym[20];
int loc;
};
void main() {
struct symboltab s[20];
char *c,*d,*symbol,*roper,*moper,*opco,*buf,*p;
int mloc,consv,start,begin,i=0,j=0,k=0,flag=0,roperval;
char *opsym[13]={"STOP","ADD","SUB","MULT","MOVER","MOVEM","COMP",
"BC","DIV","READ","PRINT","DS","DC"};
char *opcode[13]={"00","01","02","03","04","05","06","07","08","09","10","00","00"};
FILE *f,*of;
clrscr();
f=fopen("input.txt","r");
of=fopen("output.txt","w");
while(!feof(f)) {
fscanf(f,"%s",d);
if(atoi(d)!=0) {
start=atoi(d);
begin=start;
break;
}
}
while(!feof(f)) {
fscanf(f,"%s",d);
fgets(c,60,f);
if(strcmp(d,"*")) {
strcpy(s[i].sym,d);
s[i].loc=start;
i++;
}
start++;
}
rewind(f);

while(!feof(f)) {
fscanf(f,"%s%s%s%s",c,symbol,roper,moper);
if(strcmp(symbol,"END") && strcmp(symbol,"START")) {
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=0;
if(!strcmp(symbol,"DC")) {
flag=1;
p=(char *)strtok(roper,"\'");
consv=atoi(p);
}
for(k=0;k<13;k++) {
if(!strcmp(opsym[k],symbol))
strcpy(opco,opcode[k]);
}
for(j=0;j<i;j++) {
if(!strcmp(s[j].sym,moper)) {
mloc=s[j].loc;
break;
}
else
mloc=0;
if(flag==1)
mloc+=consv;
}
fprintf(of,"%d\t+\t%s\t%d\t%d\n",begin,opco,roperval,mloc);
printf("%d\t+\t%s\t%d\t%d\n",begin,opco,roperval,mloc);
begin++;
flag=0;
}}
getch();
}

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

START
MOVER
SUB
MOVEM
PRINT
STOP
DS
DC
DS
END

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

400
N
ONE
RES
RES
*
*
*
*
*

OUTPUT.TXT
400
401
402
403
404
405
406
407

+
+
+
+
+
+
+
+

04
02
05
10
00
00
00
00

1
1
1
0
0
0
0
0

405
406
407
407
0
0
2
0

You might also like