Pass 1 of SIC Assembler Simulation

C Program for Pass 1 of SIC Assembler Simulation

Note: - use input.txt --SIC input program              Use optab.txt--opcode table of SIC

 Output:-symtab.txt--symbol table                interm.txt- intermediate code

Here addresses are in decimal format
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
int  main()
{
FILE *f1,*f2,*f3,*f4,*f5;
int lc,sa,l,op1,o,len;
char m1[20],la[20],op[20],otp[20];
f1=fopen("input.txt","r");
f3=fopen("symtab.txt","w");
f4=fopen("interm.txt","w");
f5=fopen("length.txt","w");
fscanf(f1,"%s%s%d",la,m1,&op1);
if(strcmp(m1,"START")==0)
{
sa=op1;
lc=sa;
fprintf(f4,"%d\t\%s\t%s\t%d\n",op1,la,m1,op1);
}
else
{
lc=0;
}
fscanf(f1,"%s%s",la,m1);
while(!feof(f1))
{
fscanf(f1,"%s",op);
fprintf(f4,"\n%d\t%s\t%s\t%s\n",lc,la,m1,op);
if(strcmp(la,"-")!=0)
{
fprintf(f3,"\n%d\t%s\n",lc,la);
}
f2=fopen("optab.txt","r");
fscanf(f2,"%s%d",otp,&o);
while(!feof(f2))
{
if(strcmp(m1,otp)==0)
{
lc=lc+3;
break;
}
fscanf(f2,"%s%d",otp,&o);
}
fclose(f2);
if(strcmp(m1,"WORD")==0)
{
lc=lc+3;
}
else if(strcmp(m1,"RESW")==0)
{
op1=atoi(op);
lc=lc+(3*op1);
}
else if(strcmp(m1,"BYTE")==0)
{
if(op[0]=='X')
{
lc=lc+1;
}
else
{
len=strlen(op)-2;
lc=lc+len;
}
}
else if(strcmp(m1,"RESB")==0)
{
op1=atoi(op);
lc=lc+op1;
}
fscanf(f1,"%s%s",la,m1);
}
if(strcmp(m1,"END")==0)
{
fprintf(f5,"%d",lc-sa);
}
fclose(f1);
fclose(f4);
fclose(f5) ;
fclose(f3);
      
}

Input files
optab.txt( use tab key between mnemonic code and opcode)
ADD 18
AND 40
COMP 28
DIV         24
FIX         C4
J         3C
JEQ         30
JGT          34
JLT          38
JSUB  48
LDA 00
LDB 68
LDCH 50
LDL 08
LDX 04
MUL D0
OR          44
RD          D8
RSUB  4C
STA           0C
STB           78
STCH 54
STL            14
STX           10
SUB            1C
TD             E0
TIX           2C
WD               DC

input.txt (if symbol is not in the label field place a - mark)
copy    START   1000 
-       OR      ALPHA
-       ADD     ONE
-       SUB     TWO
-       STA     BETA
- LDA     ALPHA
-       ADD     ONE
-       SUB     TWO
-       STA     BETA
- LDA     ALPHA
-       ADD     ONE
-       SUB     TWO
-       STA     BETA
- LDA     ALPHA
-       ADD     ONE
-       SUB     TWO
-       STA     BETA
ALPHA   BYTE    C'KLNCE
ONE     RESB    2
TWO     WORD    5
BETA    RESW    1
_       END     _

Output
symtab.txt

1048 ALPHA

1053 ONE

1055 TWO

1058 BETA

1061 _

interm.txt

1000 copy START 1000

1000 - OR ALPHA

1003 - ADD ONE

1006 - SUB TWO

1009 - STA BETA

1012 - LDA ALPHA

1015 - ADD ONE

1018 - SUB TWO

1021 - STA BETA

1024 - LDA ALPHA

1027 - ADD ONE

1030 - SUB TWO

1033 - STA BETA

1036 - LDA ALPHA

1039 - ADD ONE

1042 - SUB TWO

1045 - STA BETA

1048 ALPHA BYTE C'KLNCE

1053 ONE RESB 2

1055 TWO WORD 5

1058 BETA RESW 1

1061 _ END _



No comments:

Post a Comment