C program to implement pass two of two pass assembler

AIM:

To write a C program to implement pass two of two pass assembler.

ALGORITHM:

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJfShSmc4uzJ32B_Cnh6KDE7EeSGsUpqwnmXpsZMSrMSp0wjgo7gGWZu_JIDHC_zUiWwWkPBhm3Pz_vS0k-tjHPGbd4J7sxNkEVv5q-YbMtId0PtXLQqi2n_k4Dee10jKN6F-IOkz4Wqgg/s1600/c+and+c%252B%252B+meansofmine.jpg

STEP 1: Start the program execution.
STEP 2: Assemble the instructions and generate object program.
STEP 3: Search SYMTAB for operand value, provided symbol is found in the operand
field.
STEP 4: Opcodes in ‘BYTE’ or ‘WORD’ is converted from constant to object code.
STEP 5: Write object program and assembly listing.
STEP 6: Stop the program execution.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct opc
{
int len;
char mnemonic[10],code[3];
}opcode;
struct opd
{
int address;
char code[10];
}op;
struct source_result
{
int address;
char label[10],instr[10],operand[10];
}res;
struct res
{
int a;
char c[10];
}s;
void main()
{
FILE *r,*o,*result,*symb;
int i,j,found=0,l;
char s1[10];
clrscr();
r=fopen("re.txt","r");
o=fopen("opcode.txt","r");
result=fopen("output.txt","w");
symb=fopen("symbol.txt","r");
while(!feof(r))
{
fscanf(r,"%d%s%s%s",&res.address,&res.label,&res.instr,&res.operand);
rewind(o);
rewind(symb);
found = 0;
while(!feof(o))
{
fscanf(o,"%d%s%s",&opcode.len,&opcode.mnemonic,&opcode.code);
if(strcmp(res.instr,opcode.mnemonic)==0)
{
op.address=res.address;
strcpy(op.code,opcode.code);
fprintf(result,"%d\t%s",op.address,op.code);
found=1;
break;
}
}
if(found==0)
continue;
while(!feof(symb))
{
fscanf(symb,"%d%s",&s.a,&s.c);
if(strcmp(res.operand,s.c)==0)
{
fprintf(result,"%d\n",s.a);
break;
}
else if(strcmp(res.operand,"NULL")==0)
{
fprintf(result,"0000");
break;
}
else if(res.operand[0]=='#')
{
strcpy(s1,res.operand);
l=strlen(s1)-1;
for(i=4;i>l;i--)
fprintf(result,"0");
for(j=1;j<=l;j++)
fprintf(result,"%c",s1[j]);
fprintf(result,"\n");
break;
}
}
}
printf("Pass 2 completed!");
fcloseall();
getch();
}

OUTPUT:

opcode.txt

1 READ 1F
1 LDA 00
re.txt
0 NULL START 1000
1000 A RESB 100
1100 LOOP READ A
1103 B BYTE C"INDIA"
1107 C WORD INDIA
1110 G RESW 5
1125 CLOOP LDA #5
1128 NULL END NULL
symbol.txt
1000 A
1100 LOOP
1107 C
1110 G
1125 CLOOP

Output

Pass 2 completed!
output.txt
1100 1F1000
1125 000005

0 comments:

Post a Comment