You are on page 1of 4

//NAME: NITHUN AJEETH.K REG. NO.

: 283575075

DEPT.: CSE//

//ONE PASS OF TWO PASS ASSEMBLER// #include<stdio.h> #include<string.h> #include<stdlib.h> #include<conio.h> void main() { FILE *ft1,*ft2,*ft3,*ft4; char label[10],opcode[10],operand[10],code[10],mnemonics[5]; int locctr,start,length; clrscr(); ft1=fopen("input.txt","r"); ft2=fopen("symbol.txt","w"); ft3=fopen("output.txt","w"); ft4=fopen("optab.txt","r"); fscanf(ft1,"%s\t%s\t%s\t\n",label,opcode,operand); if(strcmp(opcode,"START")==0) { start=atoi(operand); locctr=start; fprintf(ft3,"\t%s\t%s\t%s\n",label,opcode,operand); fscanf(ft1,"%s%s%s",label,opcode,operand); } else locctr=0; while(strcmp(opcode,"END")!=0) { fprintf(ft3,"%d\t",locctr); if(strcmp(label,"**")!=0) fprintf(ft2,"%s\t%d\n",label,locctr); fscanf(ft4,"%s%s",code,mnemonics); while(strcmp(code,"END")!=0) { if(strcmp(opcode,code)==0) { locctr+=3; break; } fscanf(ft4,"%s%s",code,mnemonics);

} 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(ft3,"%s\t%s\t%s\t\n",label,opcode,operand); fscanf(ft1,"%s%s%s",label,opcode,operand); } fprintf(ft3,"%d\t%s\t%s\t%s\t\n",locctr,label,opcode,operand); length=locctr-start; printf("\nThe Length of the Program is: %d",length); fclose(ft1); fclose(ft2); fclose(ft3); fclose(ft4); getch(); }

Output: The Length of the Program is: 10 Input.txt: ** ** ** ** ** ALPHA FIVE CHARZ C1 ** Optab.txt: START LDA STA LDCH STCH END Symbol.txt: ALPHA FIVE CHARZ C1 3012 3015 3018 3019 0 10 6 5 4 0 START LDA STA LDCH STCH RESW WORD BYTE RESB END 3000 FIVE ALPHA CHARZ C1 1 5 C'Z' 1 **

Output.txt: 3000 3003 3006 3009 3012 3015 3018 3019 3030 ** ** ** ** ** ALPHA FIVE CHARZ C1 ** START LDA STA LDCH STCH RESW WORD BYTE C'Z' RESB END 3000 FIVE ALPHA CHARZ C1 1 5 1 **

You might also like