C program to implement single pass assembler

AIM:

To write a C program to implement single 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: Assembler simply generate object code as it scans the source code.
STEP 3: If the instruction operand is a symbol text, has not yet been defined, the
operands address is omitted.
STEP 4: When nearly half the program translation is over, some of the forward reference
problem are existed.
STEP 5: Combine the process to the end of the program to fill forward reference
property.
STEP 6: At the end of the program, the symbol table entries with ‘x’ are undefined.
STEP 7: Stop the program execution.

PROGRAM :

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
FILE *f1,*f2;
char ch,str[30],str1[10],str2[30],cstr[15];
int i,j,num,q,r;
clrscr();
printf("Enter your assembly instructions\n");
f1=fopen("asin","w");
while(1)
{ ch=getchar();
if(ch=='*')
break;
fputc(ch,f1);
}
fclose(f1);
f1=fopen("asin","r");
f2=fopen("asout","w");
while(1)
{
fgets(str,25,f1);
strncpy(str1,str,3);
str1[3]='\0';
j=0;
for(i=3;i<strlen(str);i++)
{s
tr2[j]=str[i];
j++;
}s
tr2[j]='\0';
if((strcmp(str1,"lda"))==0)
{
fputs("3a\t",f2);
fputs(str2,f2);
} else if((strcmp(str1,"mov"))==0)
{
fputs("47\n",f2);
} else if((strcmp(str1,"add"))==0)
{
fputs("80\n",f2);
} else if((strcmp(str1,"sub"))==0)
{
fputs("90\n",f2);
} else if((strcmp(str1,"hlt"))==0)
{
fputs("76\n",f2);
break;
} else if((strcmp(str1,"sta"))==0)
{
fputs("32\t",f2);
num=atoi(str2)
q=num/100;
r=num%100;
if(r==0)
fputs("00\t",f2);
else
fputs(itoa(r,cstr,10),f2);
fputs("\t",f2);
fputs(itoa(q,cstr,10),f2);
fputs("\n",f2);
} else
{
fputs("error\n",f2);
}}
fclose(f1);
fclose(f2);
f2=fopen("asout","r");
printf("\nTHE OBJECT CODE CONTENTS\n");
ch=fgetc(f2);
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(f2);
}
fclose(f2);
getch();
}

OUTPUT:

Input

Enter your assembly instructions
lda 5000
sub z
sta 9988
hlt
*
Output

THE OBJECT CODE CONTENTS
3a 00 50
90
32 88 99
76

0 comments:

Post a Comment