C program to implement pass one of two pass assembler


AIM:

To write a C program to implement pass one 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: Create a symbol table.
STEP 3: Check for specified table in the symbol table.
STEP 4: If symbol is not found, show any error message or add it to SYMTAB.
STEP 5: For word increment the value by 3.
STEP 6: For RESW, operand value is incremented by 3 into locctr.
STEP 7: For BYTE, increment according to instructions.
STEP 8: Store the address value as program address.
STEP 9: Stop the program execution.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct sourcefile
{
char label[10],instr[10],operand[10];
}s;
struct pseudocd
{
char s[10];
}ps;
struct source_result
{
int address;
char label[10],instr[10],oper[10];
}res;
int find(char str[10])
{
FILE *pse;
pse=fopen("pseudo.txt","r");
while(!feof(pse))
{
fscanf(pse,"%s",ps.s);
if(strcmp(str,ps.s)==0)
{
fclose(pse);
return 1;
}
}
fclose(pse);
return 0;
}
void main()
{
FILE *ss,*ss1;
int addr,detect;
clrscr();
ss=fopen("s.txt","r+");
ss1=fopen("res.txt","w+");
while(!feof(ss))
{
fscanf(ss,"%s%s%s",s.label,s.instr,s.operand);
detect=find(s.instr);
if(detect==1)
{
if(strcmp(s.instr,"START")==0)
addr=atoi(s.operand);
else if(strcmp(s.instr,"WORD")==0)
addr=res.address+3;
else if(strcmp(s.instr,"RESW")==0)
addr=res.address+(3*atoi(s.operand));
else if(strcmp(s.instr,"RESB")==0)
addr=res.address+atoi(s.operand);
else if(strcmp(s.instr,"BYTE")==0)
addr=res.address+strlen(s.operand)-3;
else
addr=res.address+3;
strcpy(res.label,s.label);
strcpy(res.instr,s.instr);
strcpy(res.oper,s.operand);
fprintf(ss1,"%d\t%s\t%s\t%s\n",res.address,res.label,res.instr,res.oper);
res.address=addr;
}
else
{
printf("Error Encounted");
fclose(ss);
fclose(ss1);
getch();
}
}
printf("\nPass 1 Completed");
fclose(ss);
fclose(ss1);
getch();
}

0 comments:

Post a Comment