#include<stdio.h> #include<string.h> #include<stdlib.h> void chk_label(); void chk_opcode(); void READ_LINE(); struct optab { char code[10],objcode[10]; }myoptab[3]={ {"LDA","00"}, {"JMP","01"}, {"STA","02"} }; struct symtab{ char symbol[10]; int addr; }mysymtab[10]; int startaddr,locctr,symcount=0,length; char line[20],label[8],opcode[8],operand[8],programname[10]; void PASS1() { FILE *input,*inter; input=fopen("input.txt","r"); inter=fopen("inter.txt","w"); printf("LOCATION LABEL\tOPERAND\tOPCODE\n"); printf("_____________________________________"); fgets(line,20,input); READ_LINE(); if(!strcmp(opcode,"START")) { startaddr=atoi(operand); locctr=startaddr; strcpy(programname,label); fprintf(inter,"%s",line); fgets(line,20,input); } else { programname[0]='\0'; startaddr=0; locctr=0; } printf("\n %d\t %s\t%s\t %s",locctr,label,opcode,operand); while(strcmp(line,"END")!=0) { READ_LINE(); printf("\n %d\t %s \t%s\t %s",locctr,label,opcode,operand); if(label[0]!='\0') chk_label(); chk_opcode(); fprintf(inter,"%s %s %s\n",label,opcode,operand); fgets(line,20,input); } printf("\n %d\t\t%s",locctr,line); fprintf(inter,"%s",line); fclose(inter); fclose(input); } void READ_LINE() { char buff[8],word1[8],word2[8],word3[8]; int i,j=0,count=0; label[0]=opcode[0]=operand[0]=word1[0]=word2[0]=word3[0]='\0'; for(i=0;line[i]!='\0';i++) { if(line[i]!=' ') buff[j++]=line[i]; else { buff[j]='\0'; strcpy(word3,word2); strcpy(word2,word1); strcpy(word1,buff); j=0; count++; } } buff[j-1]='\0'; strcpy(word3,word2); strcpy(word2,word1); strcpy(word1,buff); switch(count) { case 0:strcpy(opcode,word1); break; case 1:strcpy(opcode,word2); strcpy(operand,word1); break; case 2:strcpy(label,word3); strcpy(opcode,word2); strcpy(operand,word1); break; } } void chk_label() { int k,dupsym=0; for(k=0;k<symcount;k++) if(!strcmp(label,mysymtab[k].symbol)) { mysymtab[k].addr=-1; dupsym=1; break; } if(!dupsym) { strcpy(mysymtab[symcount].symbol,label); mysymtab[symcount++].addr=locctr; } } void chk_opcode() { int k=0,found=0; for(k=0;k<3;k++) if(!strcmp(opcode,myoptab[k].code)) { locctr+=3; found=1; break; } if(!found) { if(!strcmp( opcode,"WORD")) locctr+=3; else if (!strcmp(opcode,"RESW")) locctr+=(3*atoi(operand)); else if(!strcmp(opcode,"RESB")) locctr+=atoi(operand); } } int main() { PASS1(); length=locctr-startaddr; }
The translation performed by an assembler is essentially a collection of substitutions: * machine operation code for mnemonic * machine address for symbolic * machine encoding of a number for its character representation, etc. Except for one factor these substitutions could all be performed in one sequential pass over source text.The factor is the forward reference(reference to an instruction which has not yet been scanned by assembler). Now it's that the separate passes of two pass assemblers are required to handle forward references without restriction. Now if we impose certain restriction that means handling forward references without making two passes. These different sets of restrictions lead to one pass assembler. And these one-pass assembler are particularly attractive when secondary storage is either slow or missing entirely, as on many small machines.
#include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> struct object_code { int locctr; char add[10]; }obcode[300]; void main() { char input[100][16],output[100][16],binary[20],address[20],stloc[10]; int len,bitmask,loc,tlen=0,tloc,textloc,i=0,location,j,k,count=0,start,n,num=0,inc=0; FILE *fp1,*fp2; clrscr(); fp1=fopen("linput.dat","r"); fp2=fopen("reloadout.dat","w"); printf("Enter the location where the program has to be loaded:"); scanf("%s",stloc); start=atoi(stloc); location=start; tloc=start; fscanf(fp1,"%s",input[i]); while(strcmp(input[i],"T")!=0) { strcpy(output[i],input[i]); i++; fscanf(fp1,"%s",input[i]); strcpy(output[i],input[i]); } itoa(start,output[2],10); while(strcmp(input[i],"E")!=0) { strcpy(output[i],input[i]); if(strcmp(input[i],"T")==0) { for(j=0;j<3;j++) { i++; fscanf(fp1,"%s",input[i]); strcpy(output[i],input[i]); } bitmask=atoi(output[i]); itoa(bitmask,binary,2); strcpy(output[i],NULL); textloc=atoi(output[i-2]); textloc=textloc+start; itoa(textloc,output[i-2],10); for(n=0;n<(textloc-(tloc+tlen));n++) { strcpy(obcode[inc].add,"xx"); obcode[inc++].locctr=location++; } tlen=atoi(output[i-1]); tloc=textloc; k=0; } else { if(binary[k]==1) { num=0; len=strlen(output[i]); strcpy(address,NULL); for(j=2;j<len;j++) { address[num]=output[i][j]; output[i][j]='\0'; num++; } loc=atoi(address); loc=loc+start; itoa(loc,address,10); strcat(output[i],address); } k++; len=strlen(output[i]); num=0; for(n=0;n<len;n++) { obcode[inc].add[num++]=output[i][n]; if(num>1) { obcode[inc++].locctr=location++; num=0; } } } i++; fscanf(fp1,"%s",input[i]); } strcpy(output[i],input[i]); i++; fscanf(fp1,"%s",input[i]); loc=atoi(input[i]); loc=loc+start; strcpy(output[i],itoa(loc,address,10)); count=0; i=0; n=0; fprintf(fp2,"%d\t",obcode[n].locctr); for(n=0;n<inc;n++) { fprintf(fp2,"%s",obcode[n].add); i++; if(i>3) { fprintf(fp2,"\t"); i=0; count++; } if(count>3) { fprintf(fp2,"\n%d\t",obcode[n+1].locctr); count=0; } } getch(); }
#include#include#includevoid main(){char opcode[10],operand[10],label[10],code[10][10],ch;char mnemonic[10][10]={"START","LDA","STA","LDCH","STCH","END"};int locctr,start,len,i=0,j=0;FILE *fp1,*fp2,*fp3;clrscr();fp1=fopen("INPUT.DAT","r");fp2=fopen("SYMTAB.DAT","w");fp3=fopen("OUT.DAT","w");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);}elselocctr=0;while(strcmp(opcode,"END")!=0){fprintf(fp3,"%d",locctr);if(strcmp(label,"**")!=0)fprintf(fp2,"%s\t%d\n",label,locctr);strcpy(code[i],mnemonic[j]);while(strcmp(mnemonic[j],"END")!=0){if(strcmp(opcode,mnemonic[j])==0){locctr+=3;break;}strcpy(code[i],mnemonic[j]);j++;}if(strcmp(opcode,"WORD")==0)locctr+=3;else if(strcmp(opcode,"RESW")==0)locctr+=(3*(atoi(operand)));else if(strcmp(opcode,"RESB")==0)locctr+=(atoi(operand));else if(strcmp(opcode,"BYTE")==0)++locctr;fprintf(fp3,"\t%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);fcloseall();printf("\n\nThe contents of Input Table :\n\n");fp1=fopen("INPUT.DAT","r");ch=fgetc(fp1);while(ch!=EOF){printf("%c",ch);ch=fgetc(fp1);}printf("\n\nThe contents of Output Table :\n\n\t");fp3=fopen("OUT.DAT","r");ch=fgetc(fp3);while(ch!=EOF){printf("%c",ch);ch=fgetc(fp3);}len=locctr-start;printf("\nThe length of the program is %d.\n\n",len);printf("\n\nThe contents of Symbol Table :\n\n");fp2=fopen("SYMTAB.DAT","r");ch=fgetc(fp2);while(ch!=EOF){printf("%c",ch);ch=fgetc(fp2);}fcloseall();getch();}INPUT FILE:INPUT.DAT** START 2000** LDA FIVE** STA ALPHA** LDCH CHARZ** STCH C1ALPHA RESW 1FIVE WORD 5CHARZ BYTE C'Z'C1 RESB 1** END **-Fabianski Benjamin
#include<stdio.h> #include<string.h> #include<stdlib.h> void chk_label(); void chk_opcode(); void READ_LINE(); struct optab { char code[10],objcode[10]; }myoptab[3]={ {"LDA","00"}, {"JMP","01"}, {"STA","02"} }; struct symtab{ char symbol[10]; int addr; }mysymtab[10]; int startaddr,locctr,symcount=0,length; char line[20],label[8],opcode[8],operand[8],programname[10]; void PASS1() { FILE *input,*inter; input=fopen("input.txt","r"); inter=fopen("inter.txt","w"); printf("LOCATION LABEL\tOPERAND\tOPCODE\n"); printf("_____________________________________"); fgets(line,20,input); READ_LINE(); if(!strcmp(opcode,"START")) { startaddr=atoi(operand); locctr=startaddr; strcpy(programname,label); fprintf(inter,"%s",line); fgets(line,20,input); } else { programname[0]='\0'; startaddr=0; locctr=0; } printf("\n %d\t %s\t%s\t %s",locctr,label,opcode,operand); while(strcmp(line,"END")!=0) { READ_LINE(); printf("\n %d\t %s \t%s\t %s",locctr,label,opcode,operand); if(label[0]!='\0') chk_label(); chk_opcode(); fprintf(inter,"%s %s %s\n",label,opcode,operand); fgets(line,20,input); } printf("\n %d\t\t%s",locctr,line); fprintf(inter,"%s",line); fclose(inter); fclose(input); } void READ_LINE() { char buff[8],word1[8],word2[8],word3[8]; int i,j=0,count=0; label[0]=opcode[0]=operand[0]=word1[0]=word2[0]=word3[0]='\0'; for(i=0;line[i]!='\0';i++) { if(line[i]!=' ') buff[j++]=line[i]; else { buff[j]='\0'; strcpy(word3,word2); strcpy(word2,word1); strcpy(word1,buff); j=0; count++; } } buff[j-1]='\0'; strcpy(word3,word2); strcpy(word2,word1); strcpy(word1,buff); switch(count) { case 0:strcpy(opcode,word1); break; case 1:strcpy(opcode,word2); strcpy(operand,word1); break; case 2:strcpy(label,word3); strcpy(opcode,word2); strcpy(operand,word1); break; } } void chk_label() { int k,dupsym=0; for(k=0;k<symcount;k++) if(!strcmp(label,mysymtab[k].symbol)) { mysymtab[k].addr=-1; dupsym=1; break; } if(!dupsym) { strcpy(mysymtab[symcount].symbol,label); mysymtab[symcount++].addr=locctr; } } void chk_opcode() { int k=0,found=0; for(k=0;k<3;k++) if(!strcmp(opcode,myoptab[k].code)) { locctr+=3; found=1; break; } if(!found) { if(!strcmp( opcode,"WORD")) locctr+=3; else if (!strcmp(opcode,"RESW")) locctr+=(3*atoi(operand)); else if(!strcmp(opcode,"RESB")) locctr+=atoi(operand); } } int main() { PASS1(); length=locctr-startaddr; }
The translation performed by an assembler is essentially a collection of substitutions: * machine operation code for mnemonic * machine address for symbolic * machine encoding of a number for its character representation, etc. Except for one factor these substitutions could all be performed in one sequential pass over source text.The factor is the forward reference(reference to an instruction which has not yet been scanned by assembler). Now it's that the separate passes of two pass assemblers are required to handle forward references without restriction. Now if we impose certain restriction that means handling forward references without making two passes. These different sets of restrictions lead to one pass assembler. And these one-pass assembler are particularly attractive when secondary storage is either slow or missing entirely, as on many small machines.
#include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> struct object_code { int locctr; char add[10]; }obcode[300]; void main() { char input[100][16],output[100][16],binary[20],address[20],stloc[10]; int len,bitmask,loc,tlen=0,tloc,textloc,i=0,location,j,k,count=0,start,n,num=0,inc=0; FILE *fp1,*fp2; clrscr(); fp1=fopen("linput.dat","r"); fp2=fopen("reloadout.dat","w"); printf("Enter the location where the program has to be loaded:"); scanf("%s",stloc); start=atoi(stloc); location=start; tloc=start; fscanf(fp1,"%s",input[i]); while(strcmp(input[i],"T")!=0) { strcpy(output[i],input[i]); i++; fscanf(fp1,"%s",input[i]); strcpy(output[i],input[i]); } itoa(start,output[2],10); while(strcmp(input[i],"E")!=0) { strcpy(output[i],input[i]); if(strcmp(input[i],"T")==0) { for(j=0;j<3;j++) { i++; fscanf(fp1,"%s",input[i]); strcpy(output[i],input[i]); } bitmask=atoi(output[i]); itoa(bitmask,binary,2); strcpy(output[i],NULL); textloc=atoi(output[i-2]); textloc=textloc+start; itoa(textloc,output[i-2],10); for(n=0;n<(textloc-(tloc+tlen));n++) { strcpy(obcode[inc].add,"xx"); obcode[inc++].locctr=location++; } tlen=atoi(output[i-1]); tloc=textloc; k=0; } else { if(binary[k]==1) { num=0; len=strlen(output[i]); strcpy(address,NULL); for(j=2;j<len;j++) { address[num]=output[i][j]; output[i][j]='\0'; num++; } loc=atoi(address); loc=loc+start; itoa(loc,address,10); strcat(output[i],address); } k++; len=strlen(output[i]); num=0; for(n=0;n<len;n++) { obcode[inc].add[num++]=output[i][n]; if(num>1) { obcode[inc++].locctr=location++; num=0; } } } i++; fscanf(fp1,"%s",input[i]); } strcpy(output[i],input[i]); i++; fscanf(fp1,"%s",input[i]); loc=atoi(input[i]); loc=loc+start; strcpy(output[i],itoa(loc,address,10)); count=0; i=0; n=0; fprintf(fp2,"%d\t",obcode[n].locctr); for(n=0;n<inc;n++) { fprintf(fp2,"%s",obcode[n].add); i++; if(i>3) { fprintf(fp2,"\t"); i=0; count++; } if(count>3) { fprintf(fp2,"\n%d\t",obcode[n+1].locctr); count=0; } } getch(); }