Friday 17 August 2012

System software program 1 :



Implementation of one pass of a two pass assembler:


program:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char opcode[10],mnemonic[10],operand[10],label[10],code[10];
int locctr,start,length;
FILE *fp1,*fp2,*fp3,*fp4;
clrscr();
fp1=fopen("Input.dat","r");
fp2=fopen("Symtab.dat","w");
fp3=fopen("Out.dat","w");
fp4=fopen("optab.dat","r");
fscanf(fp1,"%s%s%s",label,opcode,operand);
if(strcmp(opcode,"Start")==0)
{
start=atoi(operand);
locctr=start;
fprintf(fp3,"%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp1,"%s%s%s",label,opcode,operand);
}
else
locctr=start;
while(strcmp(opcode,"END")!=0)
{
fprintf(fp3,"%d\t",locctr);
if(strcmp(label,"**")!=0)
fprintf(fp2,"%d\t%d\n",locctr,label);
recoind(fp4)
fscanfa(fp4,"%s",mnemonic);
while(strcmp(mnemonic,"END")!=0)
{
if(strcmp(opcode,mnemonic)==0)
{
locctr+=3;
break;
}
fscanf(fp4,"%s",mnemonic);
}
if(strcmp(opcode,"WORD")==0)
locctr+=3;
else if(strcmp(opcode,"RESW")==0)
locctr+=(3*(atoi(operand)));
else if(strcmp(opcode,"BYTE")==0)
++locctr;
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",locctr,label,opcode,operand);
length=locctr-start;
printf("\nThe length of the program is %d",length);
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
getch();
}

Input file:

input.txt

** start 2000
** LDA five
** STA five
** LDCH charz
** STCH c1
alpha RESW 1
five WORD 1
charz BYTE c'z'
c1 RESB 1
** END **

optab.txt

Start
LDA
STA
LDCH
STCH
END

Output file:

symtab.txt

alpha 2012
five 2015
charz 2018
c1 2019

out.txt

** start 2000
2000 ** LDA five
2003 ** STA five
2006 ** LDCH charz
2009 ** STCH c1
2012 alpha RESW 1
2015 five WORD 5
2018 char BYTE c'z'
2019 c1 RESB 1
2019 ** END **

output:

The length of the program is 19

No comments:

Post a Comment