Sie sind auf Seite 1von 35

INDEX

EXPERIMENT NO 1 2 3 4 5 6 7 8 9 10 NAME OF THE EXPERIMENT GENERATION OF SYMBOL TABLE DESIGN OF 2 PASS ASSEMBLER(PASS-1) DESIGN OF 2 PASS ASSEMBLER(PASS-2) IMPLEMENTATION OF ONE PASS ASSEMPLER IMPLEMENTATION OF MACROPROCESSOR IMPLEMENTATION OF PROGRAM RELOCATION IMPLEMENTATION OF ABSOLUTE LOADER DESIGN OFDIRECT LINKING LOADER(PASS1) DESIGN OFDIRECT LINKING LOADER(PASS2) TEXT EDITOR PAGE NO 1 4 7 10 13 16 19 22 25 29

Ex.No: 1 DATE:

GENERATION OF SYMBOL TABLE

AIM: ALGORITHM:

To write a C program to generate a symbol table

Step 1: Start the process Step 2: Initialize the structure variable OPTAB with valid mnemonic code and length. Step 3: Score assembly language input program in a data file. Step 4: Read the instruction from the input file until end of file is read. Step 5: Read the instruction one by one match it with the instruction in OPTAB. Step 6: If found, then check whether there is a symbol in the label field. Step 7: If found, then store the label and its location in the symbol table, else go to step 5. Step 8: If instruction not found goto step 5. Step 9: Stop the process.

PROGRAM: #include<stdio.h> #include<conio.h> #include<string.h> struct table { char inst[15]; int len; }; struct table tab[4]={{"MOV AX,BX",2},{"ADD AX,BX",2}, {"MUL AX,BX",2},{"SUB AX,BX",2}}; void main() { int flag=0,i,j,addr=1000; char *strp,str[20],strinst[20],strlab[20]; FILE *fp1; clrscr(); fp1=fopen("sample.dat","r"); printf("\tSYMBOL TABLE"); printf("\n_______________________________\n"); printf("\nADDRESS \tLABEL \tLENGTH\n"); printf("-------------------------------\n"); while(!feof(fp1)) { flag=0; fscanf(fp1,"%[^\n]\n",str); strp=strstr(str,"\t"); if(strp!=NULL) { for(i=1,j=0;strp[i]!='\0';i++,j++) strinst[j]=strp[i]; strinst[j]='\0'; for(i=0;i<5;i++) { if(strcmp(strinst,tab[i].inst)==0) { for(j=0;str[j]!='\t';j++) strlab[j]=str[j]; strlab[j]='\0'; flag=1; printf("\n %d \t\t %s \t %d\n",addr,strlab,tab[i].len); addr=addr+tab[i].len; } } } else { for(i=0;i<5;i++) { if(strcmp(str,tab[i].inst)==0)

{ flag=1; addr=addr+tab[i].len; } } } if(flag==0) printf("\n%s INVALID INSTRUCTION \n",str); } fclose(fp1); getch(); } Input: /*sample.dat*? 11 12 13 14 MOV ADD MUL SUB AX,BX AX,BX AX,BX AX,BX

Output: SYMBOL TABLE _______________________________ ADDRESS LABEL LENGTH ------------------------------1000 1002 1004 1006 MOV AX,BX ADD AX,BX MUL AX,BX DIV AL,BL 2 2 2 INVALID INSTRUCTION

RESULT:

Thus a C program was written to generate a symbol table and the output was verified. Ex.No: 2 DATE: AIM: To write a C program to design two pass assembler (pass-1). ALGORITHM: Step 1: Step 2: Step 3: Step 4: Input the source program. Initialize the LOCCTR to starting address of program. Read source program code using LOCCTR. Check whether there is a symbol in the label field. If it is a label store it with the value of LOCCTR in the symbol table and change the LOCCTR value. Step 5: Else search the OPTAB for opcode if it is present then add instruction length and LOCCTR. a) else if opcode =WORD,add 3 to LOCCTR. b) else if opcode =RESW,add 3 * #[operand] to LOCCTR. c) else if opcode =RESB,add # [operand] to LOCCTR. d) else if opcode =BYTE,add length of constant to LOCCTR e) else set error flag (Invalid operation code). f) write line to intermediate file goto Step 3. g) Write last line of the source code to intermediate file. h) Stop the process. DESIGN OF PASS-1 OF A TWO PASS ASSEMBLER

PROGRAM: #include<stdio.h> #include<conio.h> #include<string.h> struct OPTAB { char mne[10]; char mcode[10]; }; struct SYMTAB { char sym[10]; int addr; }; struct OPTAB op[8]={{"STL","14"},{"JSUB","48"}, {"JEQ","30"},{"LDA","00"}, {"LDX","04"},{"STA","0C"}, {"ADD","4D"},{"JLT","38"}}; struct SYMTAB sym1[10]; void main() { FILE *fip,*fintr,*fsym; char opc[10],oper[10],flag,label[10]; int i,lc,curadd; clrscr(); fip=fopen("input1.dat","r"); fintr=fopen("inter1.dat","w"); fsym=fopen("symtab1.dat","w"); fscanf(fip,"%s\t%s\t%s\n",label,opc,oper); if(strcmp(opc,"START")==0) { lc=atoi(oper); fscanf(fip,"%s\t%s\t%s\n",label,opc,oper); } else lc=0; while(strcmp(opc,"END")!=0) { flag='F'; curadd=lc; if(strcmp(label,"NULL")!=0) fprintf(fsym,"%s\t%d\n",label,curadd); for(i=0;i<8;i++) { if(strcmp(opc,op[i].mne)==0) flag='T'; }

if(flag=='T') lc+=3; else if(strcmp(opc,"RESW")==0) lc+=3+atoi(oper); else if(strcmp(opc,"RESB")==0) lc+=atoi(oper); else if(strcmp(opc,"BYTE")==0) lc+=strlen(oper); else if(strcmp(opc,"WORD")==0) lc+=3; else printf("%d\t%s\t%s\t%s\t INVALID\n",curadd,label,opc,oper); fprintf(fintr,"%d\t%s\t%s\t%s\n",curadd,label,opc,oper); fscanf(fip,"%s\t%s\t%s\n",label,opc,oper); } fprintf(fintr,"\tNULL\tEND"); }

Input: /*input1.dat*/ NULL START NULL LDX REPEAT NULL STA ZERO BYTE ALPHA RESW GAMMA RESB NULL END 1000 ZERO LDA ALPHA GAMMA 0 1 1

/*inter1.dat*/ 1000 1003 1006 1009 1010 1014 NULL LDX REPEAT NULL STA ZERO BYTE ALPHA RESW GAMMA RESB NULL END ZERO LDA ALPHA GAMMA 0 1 1

Output: /*symtab1.dat*/ REPEAT 1003 ZERO 1009 ALPHA 1010 GAMMA 1014

RESULT: Thus a C program was written to implement pass-1 of a two pass assembler and the output was verified.

Ex.no: 3 DATE:
AIM:

DESIGN OF PASS-2 OF A TWO PASS ASSEMBLER

To write a C program to design two pass assembler (pass-2). ALGORITHM: Step 1: Step 2: Step 3: Step 4: Step 5: Step 6: Step 7: Step 8: Step 9: Input the copy of source program given to pass-1. Read the input line from intermediate file generated during Pass-1. Write header record to object program and initialize the text record. Search OPTAB for OPCODE, if present check for operand file, if present search SYMTAB for OPERAND. If operand is found in symbol table, then store symbol Value As operand address else set operand address =0 Assemble the object code instruction. If OPCODE = BYTE or WORD then converts to object code. Write listing line in the output file and read the next input line goto step-4. Write last text record into object program. Step 10: Write end record to object program. Step 11: Write last listing line. Step 12: Stop the process.

PROGRAM: #include<stdio.h> #include<conio.h> #include<string.h> struct OPTAB { char mne[10]; char mcode[10]; }; struct OPTAB op[8]={{"STL","14"},{"JSUB","48"}, {"JEQ","30"},{"LDA","00"}, {"LDX","04"},{"STA","0C"}, {"ADD","4D"},{"JLT","38"}}; void main() { FILE *fint,*fsym,*fout; char opc[10],oper[10],obj[10],label[10],mne[10],addr[10],symbol[10],addr1[10]; int i; clrscr(); fint=fopen("inter1.txt","r"); fsym=fopen("symtab1.txt","r"); fout=fopen("output1.txt","w"); fscanf(fint,"%s\t%s\t%s\t%s\n",addr,label,opc,oper); while(!feof(fint)) { strcpy(obj,""); for(i=0;i<5;i++) { if(strcmp(opc,op[i].mne)==0) strcpy(obj,op[i].mcode); } rewind(fsym); while(!feof(fsym)) { fscanf(fsym,"%s\t%s\n",symbol,addr1); if(strcmp(symbol,oper)==0) strcat(obj,addr1); } fprintf(fout,"%s\t%s\t%s\t%s\n",addr,label,opc,oper,obj); fscanf(fint,"%s\t%s\t%s\n",addr,label,opc,oper); } }

Input: /*inter1.dat*/ 1000 1003 1006 1009 1010 1014 NULL LDX REPEAT NULL STA ZERO BYTE ALPHA RESW GAMMA RESB NULL END ZERO LDA ALPHA GAMMA 0 1 1

/*symtab1.dat*/ REPEAT 1003 ZERO 1009 ALPHA 1010 GAMMA 1014 OUTPUT: /*output1.dat*/ 1000 1003 1006 1009 1010 1014 NULL LDX REPEAT NULL STA ZERO BYTE ALPHA RESW GAMMA RESB NULL END ZERO 041009 LDA ALPHA 001010 GAMMA 0c1013 0 1 1

10

RESULT: Thus a C program was written to implement pass-2 of a two pass assembler and the output was verified.

Ex.no:4 DATE:
AIM:

DESIGN OF ONE PASS ASSEMBLER

To write a C program to design of one pass assembler ALGORITHM: Step 1: Step 2: Step 3: Step 4: Step 5: Step 6: Step 7: Step 8: Step 9: Start the process. Initialize structure variable with valid mnemonics, Opcode and length. Store input assembly language program in data file. Read the instruction from input file. Compare the instruction with values mnemonics available in structure. If both are equal then print address, mnemonic and OPCODE and its length. OPCODE the address based on the instruction length. Else display invalid instruction. Stop.

11

PROGRAM: #include<stdio.h> #include<conio.h> struct table { char inst[25]; char opc[20]; int len; }; main() { struct table tab[3]={{"add ax,bx","8a",2}, {"mov ax,bx","7c",2}, {"hlt","3c",1}}; int addr=1000,flag,i; char str[25]; FILE *fp; clrscr(); fp=fopen("sam.dat","r"); printf("\t------------------------------------------\n"); printf("\n\tADDRESS\tINSTRUCTION\tOPCODE\tLENGTH\n"); printf("\t------------------------------------------\n"); while(!feof(fp)) { flag=0; fscanf(fp,"%[^\n]\n",str); for(i=0;i<5;i++) { if(strcmp(str,tab[i].inst)==0) { flag=1; printf("\n\t%d\t%s\t%s\t%d\n",addr,str,tab[i].opc,tab[i].len); addr=addr+tab[i].len; } } if(flag==0) printf("\n\t\t%s \tINVALID INSTRUCTION\n",str); } fclose(fp); }

12

INPUT: /*sam.dat*/ ADD AX,BX MOV AX,BX HLT SUB AX,BX OUTPUT: -----------------------------------------ADDRESS INSTRUCTION OPCODE LENGTH -----------------------------------------1000 1002 1004 ADD AX,BX MOV AX,BX HLT 3c 8a 7c 1 INVALID INSTRUCTION 2 2

SUB AX,BX

13

RESULT: Thus a C program was written to implement one pass assembler and the output was verified.

Ex.no: 5 DATE:
AIM:

IMPLEMENTATION OF MACRO PROCESSOR

To write a C program to implement a macro processor. ALGORITHM: Step 1: Step 2: Step 3: Step 4: Step 5: Step 6: Step 7: Step 8: Step 9: Step 10: Expanding ! = False While opcode ! =End do Getline the processline and then end. Enter the macr into namtab prototype in DEFTAB Expanding ! = True Get first line of macro definition from DEFTAB Set up arguments from macro invocation in ARGTAB Write macro invocation to expanded file as comment While not end of macro definition do getline and process line Expanding ! = False Stop the program.

14

PROGRAM: #include<stdio.h> #include<conio.h> #include<string.h> main () { FILE *fp1,*fp2; char str1[20],str2[20],mname[20],*strp; int i,j; clrscr(); fp1=fopen("macro1.dat","r"); fp2=fopen("macro1.dat","r"); while(!feof(fp1)) { fscanf(fp1,"%[^\n]\n",str1); if(strcmp(str1,"MACRO")==0) { fscanf(fp1,"%[^\n]\n",str1); if(strcmp(str1,"MEND")!=0) { fscanf(fp1,"%[^\n]\n",str1); } } fscanf(fp1,"[^\n]\n",str1); fscanf(fp1,"[^\n]\n",str1); strp=strstr(str1,"\t"); for(i=1,j=0;strp[i]!='\0';i++,j++) mname[j]=strp[i]; mname[j]='/0'; if(strp!=NULL) { rewind(fp2); { fscanf(fp2,"%[^\n]\n",str2); if(strcmp(str2,"MACRO")==0) { fscanf(fp2,"%[^\n]\n",str2); if(strcmp(str2,"mname")==0) { fscanf(fp2,"%[^\n]\n",str2); while(strcmp(str2,"MEND")!=0) { printf("\n%s",str2);

15

fscanf(fp2,"%[^\n]\n",str2); } } } } } else printf("\n%s",str1); } getch(); }

Input: MACRO ADDITION LDA ALPHA STA GAMMA MEND START CALL ADDITION END OUTPUT: LDA ALPHA STA GAMMA MEND START CALL ADDITION END

16

RESULT: Thus a C program was written to implement macro processor and the output was verified.

Ex.no: 6 DATE:
AIM:

IMPLEMENTATION OF ABSOLUTE LOADER

To write a C program to implement absolute loader. ALGORITHM: Step Step Step Step Step Step Step Step Step 1: Start the process. 2: 3: 4: 5: 6: 7: 8: 9: Input the object code program. Process the header record. Process the text record. Record type is not equal to E and then do step 6 to step 8 else goto step 9. Convert the char into internal representation. Move these object code into specified location. Read next text record. Stop the process.

17

PROGRAM: #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str[80],str1[80]; char rtype[3],subs[8],tlength[5],eaddr[8],taddr[8]; int i=0,j=0,k,curaddr,saddr,locctr; FILE *fp,*fp1; fp=fopen("input4.dat","r"); fp1=fopen("output4.dat","w"); clrscr(); strcpy(rtype," "); strcpy(eaddr," "); fscanf(fp,"%c",rtype); rtype[1]='\0'; fscanf(fp,"%[^\n]\n",str); if(strcmp(rtype,"H")==0) { for(i=6;i<=10;i++) subs[j++]=str[i]; subs[j]='\0'; } saddr=atoi(subs); j=0; while(!feof(fp)) { fscanf(fp,"%c",rtype); rtype[1]='\0'; if(strcmp(rtype,"T")==0) { fscanf(fp,"%[^\n]\n",str); for(i=0,j=0;i<=5;i++,j++) taddr[j]=str[i]; taddr[j]='\0'; curaddr=atoi(taddr); for(i=6,j=0;i<=7;i++,j++) tlength[j]=str[i]; tlength[j]='\0'; while(str[i]!='\0')

18

{ for(k=0;k<2;k++) { str1[k]=str[i]; i++; } str1[k]='\0'; fprintf(fp1,"\n%06d\t %2s",curaddr,str1); curaddr+=1; } } else { fscanf(fp,"%[^\n]\n",str); if(strcmp(rtype,"E")==0) { for(i=0,j=0;str[i]!='\0';i++,j++) eaddr[j]=str[i]; eaddr[j]='\0'; } } fprintf(fp1,"\n"); } if(strcmp(eaddr,"NULL")!=0) fprintf(fp1,"LOCATION COUNTER [%d]",atoi(eaddr)); else fprintf(fp1,"LOCATION COUNTER [%d]",atoi(saddr)); getch(); } INPUT: /*input6.txt*/ HCOPY 2010 T001000141033482D364B6F T002010205D30203F8205D281030 E001000 Output: /*Output6.txt*/ 001000 001001 001002 001003 001004 001005 001006 002010 002011 002012 002013 002014 10 33 48 2D 36 4B 6F 5D 30 20 3F 82

19

002015 002016 002017 002018 002019

05 D2 81 03 0

LOCATION COUNTER [1000]

RESULT: Thus a C program was written to implement relocating loader and the output was verified.

Ex.No:7 DATE:
AIM:

IMPLEMENTATION OF RELOCATING LOADER

To implement the relocation loader. ALGORITHM: Step 1: Step 2: Step 3: Step 4: Step 5: Step 6: Step 7: Step 8: Step 9: Start the program. Input the object code program. Processing the header record and get the relocating address. Processing the text record. Record type is equal to E and then do step 6 to step 8 else goto step 9. Convert the object code into internal representation. Relocate these objects code through the relocating address to step 3. Read the next text record. End the process.

20

PROGRAM: #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str[80],str1[80]; char rtype[3],subs[8],tlength[5],eaddr[8],taddr[8]; int i=0,j=0,k,curaddr,saddr,len,locctr; FILE *fp,*fp1; fp=fopen("input7.txt","r"); fp1=fopen("output7.txt","w"); clrscr(); /* /* PROCESSING HEADER RECORD */ ------------------------ */

fscanf(fp,"%c",str); rtype[i]='\0'; printf("Enter the starting address scanf("%d",&saddr); curaddr=saddr; j=0; /* /*

");

PROCESSING TEXT RECORD */ ---------------------- */

while(!feof(fp)) { fscanf(fp,"%[^\n]\n",str); for(i=0;i<1;i++) rtype[i]=str[i]; rtype[i]='\0'; if(strcmp(rtype,"T")==0) { for(i=1;i<6;i++);

21

for(i=7,j=0;i<=8;i++,j++) tlength[j]=str[i]; tlength[j]='\0'; while(str[i]!='\0') { k=0; for(;k<2;k++) { str1[k]=str[i]; i++; } str1[k]='\0'; fprintf(fp1,"\n%d \t %2s",curaddr,str1); curaddr+=1; } } else { if(strcmp(rtype,"E")==0) { for(i=0,j=0;str[i]!='\0';i++,j++) eaddr[j]=str[i]; eaddr[j]='\0'; } } fprintf(fp1,"\n"); } fprintf(fp1,"LOCATION COUNTER [ %d ]",saddr); } Input: /*Input7.txt*/ HCOPY 2010 T001000141033482D364B6F T002010205D30203F8205D281030 E001000 Output: /*output7.txt*/ 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 10 33 48 2D 36 4B 6F 5D 30 20 3F 82

22

2092 2093 2094 2095 2096 2097 2098 2099 2100

05 D2 81 03 0 99 2 0

LOCATION COUNTER [ 2080 ] RESULT: Thus a C program was written to implement absolute loader and the output was verified.

Ex.no:8 DATE: AIM:

IMPLEMENTATION OF PASS-1 OF A DIRECT LINKING LOADER

To write a C program to implement pass one of a direct linking loader. ALGORITHM: 1. 2. 3. 4. 5. 6. 7. 8. 9. Start the program. Get the program address. Set the program address to os address. Read input from input file. Set control section length to CSLTH Search ESTAB for control section name If it found means enter the cs name into ESTAB with cs address. If record type is not equal to end means read input record. If record type = D (definition read)then for each in record search ESTAB for symbol name. 10. If it found means enter that symbol into ESTAB with corresponding address value (cs address + symbol original address) 11. Then add cs length to cs address again goto step 5. 12. End the program.

23

PROGRAM: #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> void main() { int value,csaddr,progaddr,cslth; char s1[10],s2[10],s3[10],s4[10]; FILE *fp,*fp1,*fp2; clrscr(); printf("\n\t ENTER THE STARTING ADDRESS:"); scanf("%d",&progaddr); csaddr=progaddr; fp=fopen("ob1.txt","r"); fp1=fopen("estab.txt","w"); fp2=fopen("address.txt","w"); fprintf(fp2,"%d",progaddr); fclose(fp2); fscanf(fp,"%s%s%s%s",s1,s2,s3,s4); while(!feof(fp)) { cslth=atoi(s3); fprintf(fp1,"%s\t.......\t%d\t%s\n",s2,csaddr,s3); do { fscanf(fp,"%s%s%s%s",s1,s2,s3,s4); if(strcmp(s1,"d")==0) { value=csaddr+atoi(s3); fprintf(fp1,"...\t%s\t%d\t_\n",s2,value); } } while(strcmp(s1,"e")!=0);

24

csaddr=csaddr+cslth; fscanf(fp,"%s%s%s%s",s1,s2,s3,s4); } fclose(fp); fclose(fp1); getch(); }

Input: /*ob1.txt*/ b d d t t t m m e h d d t t t m e proga a1 a 0000 0000 0000 0006 0006 0000 progb b1 b2 0000 0003 0006 0006 0000 9 _ 6 _ 9 _ 000006 010006 000006 06 +b2 06 _ _ _ 9 _ 000006 010006 010006 000006 06 -a1 06 +a2 _ _

_ _ -b1

_ _ _ _

output: /*address.txt*/ 3000 /*estab.txt*/ proga ....... ... a1 3006 ... a2 3009 progb ....... 3000 _ _ 3009 9 9

25

... ...

b1 b2

3015 _ 13015 _

RESULT: Thus a C program was written to implement direct linking loader [pass-1] and the output was verified.

Ex.no: 9 DATE: AIM:

IMPLEMENTATION OF PASS-2 OF A DIRECT LINKING LOADER

To write C programs to implement pass -2 of a direct linking loader. ALGORITHM: 1. 2. 3. 4. 5. 6. 7. 8. 9. Start the program. Set the progaddr to csaddr Set progaddr to exeaddr. If it is not end of input do the following. Read next input record. Set control section length of CSLTH. If record type not equal to end then read next input record (actual processing starts). If record type =T then move object code to specified location. Else if record type =M then search ESTABfor modifying symbol name. 10. If it found then add or subtract value to location counter value else set errorflag. 11. If an address is specified in end record then set exeaddr to (csaddre + specified addr). 12. Then add cslth + csaddr ( to get next starting addr)

26

13. Jump to location given be Exeaddr. 14. Stop the program.

PROGRAM: #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> void main() { int i=0,j,k,value; int ob1[10],addr[10],progaddr; int csaddr,execaddr,cslth,q,d=0; char s1[10],s2[10],s3[10],s4[10]; FILE *fp,*fp1; int search(char sym[10]); fp=fopen("ob1.txt","r"); fp1=fopen("address.txt","r"); clrscr(); fscanf(fp1,"%d",&progaddr); fclose(fp1); csaddr=progaddr; execaddr=progaddr; fscanf(fp,"%s\t%s\t%s\t%s\n",s1,s2,s3,s4); while(!feof(fp)) { cslth=atoi(s3); while(strcmp(s1,"e")!=0) { fscanf(fp,"%s\t%s\t%s\t%s\n",s1,s2,s3,s4); if(strcmp(s1,"t")==0) { addr[i]=atoi(s2)+csaddr; ob1[i]=atoi(s3); i++;

27

} else if(strcmp(s1,"m")==0); { char op=s4[10]; for(k=1;k<strlen(s4);k++) s4[k-1]=s4[k]; s4[k-1]=0; value=search(s3); q=d+atoi(s3)/3; if(op=='+') ob1[q]=ob1[q]+value; else ob1[q]=ob1[q]-value; } } d=cslth/3; execaddr=csaddr+atoi(s3); csaddr=csaddr+cslth; fscanf(fp,"%s%s%s%s",s1,s2,s3,s4); } fclose(fp); q=(execaddr-progaddr)/3; printf("\n***OUTPUT***\n"); printf("\n\n\nprogram located in memory ready for execution"); printf("\n\n\tADDRESS\t\t\tDATA\n"); for(j=0;j<i;j++) printf("\n\t%d\t\t\t%d",addr[j],ob1[j]); gotoxy(q,q+5); getch(); } int search(char sym[10]) { char s1[10],s2[10],s3[10],s4[10]; FILE *fp4; fp4=fopen("estab1.txt","r"); while(!feof(fp4)) { fscanf(fp4,"%s%s%s%s",s1,s2,s3,s4); if(strcmp(s2,sym)!=0) { fclose(fp4); return(atoi(s3)); } } return(0); }

Input: /*ob1.txt*/ b d proga 9 a1 6 _ _

28

d t t t m m e h d d t t t m e

a 0000 0000 0000 0006 0006 0000 progb b1 b2 0000 0003 0006 0006 0000

9 _ 000006 010006 000006 06 +b2 06 _ _ _ 9 _ 000006 010006 010006 000006 06 -a1 06 +a2 _ _

_ _ -b1

_ _ _ _

/*address.txt*/ 3000

***OUTPUT***

program located in memory ready for execution ADDRESS 3000 3000 3000 3009 3012 3015 DATA 6 10006 6 10006 6 6

29

RESULT: Thus a C program was written to implement direct linking loader [pass-2] and the output was verified.

Ex.no:10 DATE:
Aim:

SIMPLE TEXT EDITOR

To write a c program for implementing the text editor. Algorithm: 1. Start the program. 2. display the option in screen as OPEN, SAVE, QUIT, DELETE 3. OPEN-F3 SAVE-F2 QUIT-ESC DELETE-EXIT,DE 4. Get option from the user If the option F3 then open the file and display if the option F2 then save the content from dummy file to the original file If the option is ESC then exit the program If the option is DELETE

30

then delete the file 5. Start the program.

PROGRAM: #include<stdio.h> #include<conio.h> #include<ctype.h> #include<string.h> int pg=1; void main() { FILE *txtin,*txtout; char filename[50]; static int i,j,a,col,row; char txt[75][80]={" "},ch; unsigned char keypress; void prl(int col,int row); clrscr(); row=col=1; prl(col,row); for( ; ; ) { col=wherex(); row=wherey(); keypress=getch(); switch(keypress) { case 13: /*ENTER*/ { row++; col=1;

31

prl(col,row); break; } case 27: /*EXIT*/ exit(0); case 60: /*SAVE*/ { gotoxy(1,25); printf("\nEnter the filename:"); gets(filename); txtin=fopen(filename,"wt"); if(fopen(filename,"wt")==NULL) { printf("Error"); exit(0); } clrscr(); for(i=1;i<24;i++) { for(j=1;j<80;j++) { fputc(txt[i][j],txtin); gotoxy(j,i); putch(txt[i][j]); } fputc('\n',txtin); } fclose(txtin); prl(i,j); gotoxy(i,j); break; } case 61: /*OPEN*/ { gotoxy(1,25); printf("\nEnter the filename:"); gets(filename); txtout=fopen(filename,"r"); i=j=1; clrscr(); gotoxy(1,1); for(i=1;i<=24;i++) { for(j=1;j<=80;j++) { txt[i][j]=fgetc(txtout); gotoxy(j,i); putch(txt[i][j]); } } fclose(txtout); prl(1,1); break; } case 71: /*HOME*/ {

32

col=1; prl(col,row); break; } case 72: /*UPWARD ARROW*/ { row--; prl(col,row); break; } case 75: /*LEFT ARROW*/ { col--; prl(col,row); break; } case 77: /*RIGHT ARROW*/ { row=row+1; prl(col,row); break; } case 83: /*DELETE*/ { a=wherex(); j=80; while(isspace(txt[row][j]!=0)) j--; for( ;a<j;a++) { txt[row][a]=txt[row][a+1]; gotoxy(a,row); putch(txt[row][a]); } txt[row][a]=' '; prl(col,row); break; } default: if(isprint(keypress)!=0) { putch(keypress); txt[row][col]=keypress; col++; prl(col,row); if(col>=80) { row=row+1; col=1; prl(col,row); } } } } } void prl(int col,int row)

33

{ if(col<1) col=1; if(col>80) { row=row++; col=1; } if(row<1) row=1; if(row>=24) row--; gotoxy(5,24); printf("(%d%d)",row,col); gotoxy(15,24); printf("ESC_EXIT"); gotoxy(30,24); printf("F2_SAVE"); gotoxy(45,24); printf("F3_OPEN"); gotoxy(col,row); }

OUTPUT: Esc-EXIT: F2-SAVE: F3-OPEN ENTER THE FILENAME: mac. dat Test program Esc-EXIT: F2-SAVE: F3-OPEN ENTER THE FILENAME: mac1.dat MACRO ADDITION LDA ALPHA ADD BETA STA GAMMA MEND Esc-EXIT: F2-SAVE: F3-OPEN ENTER THE FILENAME: mac1.dat

RESULT:

34

Thus a C program was written to implement a simple Text Editor and the output was verified.

35

Das könnte Ihnen auch gefallen