+ All Categories
Home > Documents > System Software Lab Manual

System Software Lab Manual

Date post: 27-Oct-2014
Category:
Upload: chandrasekar-raguraman
View: 280 times
Download: 3 times
Share this document with a friend
Description:
System Software Lab Manual
74
GENERATION OF SYMBOL TABLE AIM: To write a C program to create a symbol table for the source program, Assembly language program. ALGORITHM: Step 1: Start the program. Step 2 : Initialize program counter to starting address if the address is specified is as START instruction else set to zero to program counter. Step 3 : Read the next instruction from the files. a. Check the mnemonics of the source program in opcode table. i. If found then check the label name in label field. ii. If found then print label name and address of the label, Else increment address by length of the instruction. i.e address=address+length. Step 4 : Repeat step 6 until end of the file is encountered. Step 5 : Return back both cursor pointer to starting of the file. Step 6 : Read the next instruction from the files. a. Check the operand. i. If the operand starts with = symbol, then print literal value ,length and its address. ii. Else increment address by length of the instruction, i.e address=address + length.
Transcript
Page 1: System Software Lab Manual

GENERATION OF SYMBOL TABLE

AIM:

To write a C program to create a symbol table for the source program, Assembly language program.

ALGORITHM:

Step 1: Start the program.

Step 2 : Initialize program counter to starting address if the address is specified is as START instruction else set to zero to program counter.

Step 3 : Read the next instruction from the files.a. Check the mnemonics of the source program in opcode table.

i. If found then check the label name in label field.ii. If found then print label name and address of the label,

Else increment address by length of the instruction. i.e address=address+length.

Step 4 : Repeat step 6 until end of the file is encountered.

Step 5 : Return back both cursor pointer to starting of the file.

Step 6 : Read the next instruction from the files.a. Check the operand.

i. If the operand starts with = symbol, then print literal value ,length and its address.

ii. Else increment address by length of the instruction, i.e address=address + length.

Step 7 : Stop the program.

Page 2: System Software Lab Manual

SOURCE CODE:

#include<stdio.h>#include<string.h>#include<conio.h>#include<stdlib.h>#include<alloc.h>

struct sym{char name[20];int val;char *next;}s[10];

void main(){int ch,val,j,f=0;static int i=0;char name[10];clrscr();do{printf("\n 1.insert \n 2.delete \n 3.disp \n 4.modify \n 5.search \n 6.exit");printf("\n\n enter choice");scanf("%d",&ch);switch(ch){case 1:printf("\n enter the symbol name & value");scanf("%s%d",name,&val);strcpy(s[i].name,name);s[i].val=val;s[i].next=malloc(sizeof(s[i].name));i++;break;case 2:printf("enter the symbol to delete");scanf("%s",&name);for(j=0;j<i;j++){if(strcmp(s[j].name,name)==0){while(j<i){strcpy(s[j].name,s[j+1].name);s[j].val=s[j+1].val;j++;}i--;

Page 3: System Software Lab Manual

break;}}break;case 3:printf("\n name\tvalue\taddr\n");for(j=0;j<i;j++){printf("\n%s\t",s[j].name);printf("%d\t",s[j].val);printf("%d\t",s[j].next);}break;case 4:printf("\n enter the name of the sym to modify");scanf("%s%d",name,& val);for(j=0;j<i;j++)if(strcmp(s[j].name,name)==0){s[j].val=val;}break;case 5:printf("\n enter sym to search");scanf("%s",name);for(j=0;j<i;j++)if(strcmp(s[j].name,name)==0){printf("\n sym found%d\n",j+1);f=1;}if(f==0)printf("\n sym not found");break;case 6:exit(0);}}while(ch<=5);getch();}

Page 4: System Software Lab Manual

OUTPUT

1.insert2.delete3.disp4.modify5.search6.exit enter choice 1enter the symbol name & valueint 100

enter choice 1enter the symbol name & valuefloat 200

enter choice 1enter the symbol name & valuechar 300

enter choice 3name value addrint 100 2346float 200 2370char 300 2394

enter choice 4enter the name of the sym to modifyfloat500

enter choice 3 name value addrint 100 2346float 500 2370char 300 2394

enter choice 5enter sym to search char

sym found3

Page 5: System Software Lab Manual

RESULT:

The program allocates memory space in bytes for the variables declared. Hence a C program is written and executed to create the symbol table for the given input file.

Page 6: System Software Lab Manual

TWO PASS ASSEMBLER – PASS1

AIM:To implement pass one of a two pass assembler for the given input using C language

ALGORITHM:

Step 1 : Start.

Step 2 : Read the souce program from the file.

Step 3 : Check the opcode.a. If the opcode=”START”,then initialize the location counter to starting

address specified in the Start statement.b. Write the statement into intermediate file.c. Else initialize the location counter to zero.

Step 4 : While opcode is not equal to END do the following.a. If the instruction is not a comment lineb. If the symbol is in the operand field,then do the searching in symbol table.c. If the symbol is found already,print “its duplicate symbol”d. Else insert the symbol name and its address into symbol table.

Step 5 : Read the opcode from the source program.a. If the opcode is “RESW” then add(3* value of the operand) to location

counterb. Elseif the opcode is “BYTE” then add(value of the operand) to location

counterc. Elseif the opcode is “WORD” then add 3 to location counterd. Elseif the opcode is “RESB” then find the length of the constant string

and add this length to location counter.e. Else Check the opcode in the opcode table.

i. If opcode is found then add 3 to location counterii. Else Printf “Invalid mnemonics code in the source program”iii.

Step 6 : Write the instruction into the intermediate file.

Step 7 : repeat the step 4 and 5.

Step 8 : Stop the program.

Page 7: System Software Lab Manual

SOURCE CODE:

#include<stdio.h>#include<conio.h>#include<stdlib.h>void main(){char opcode[10],mn[10],operand[10],label[10];int locctr,start,length;FILE *f1;FILE *f2;FILE *f3;FILE *f4;clrscr();f1=fopen("input.dat","r");f2=fopen("symbol.dat","w");f3=fopen("out.dat","w");f4=fopen("optab.dat","r");fscanf(f1,"%s%s%s",label,opcode,operand);if(strcmp(opcode,"START")==0){start=atoi(operand);locctr=start;fprintf(f3,"\t %s\t%s\t%s\n",label,opcode,operand);fscanf(f1,"%s%s%s",label,opcode,operand);}elselocctr=0;while(strcmp(opcode,"END")!=0){fprintf(f3,"%d\t",locctr);if(strcmp(label,"**")!=0)fprintf(f2,"\t%s\t%d\n",label,locctr);fscanf(f4,"%s%s",opcode,mn);fscanf(f4,"%s%s",opcode,mn);if(strcmp(opcode,"WORD")==0)locctr=locctr+(atoi(operand));else if((strcmp(opcode,"BYTE")==0))++locctr;else if((strcmp(opcode,"END")!=0))locctr+=3;fprintf(f3,"%s\t%s\t%s\n",label,opcode,operand);fscanf(f1,"%s%s%s",label,opcode,operand);}length=locctr-start;printf("the length of the program is %d",length);fclose(f1);fclose(f2);fclose(f3);fclose(f4);getch();

}

Page 8: System Software Lab Manual

Note: check the program results in root directory in the following files (optab.dat, out.dat, symbol.dat)SAMPLE INPUT AND OUTPUT:

INPUT:

1. FILE NAME: optab.dat

START 3700LDA 00STA 0CLDCH 50STCH 54END *

2. FILE NAME: input.dat

** START 3700** LDA THREE** STA DELTA** LDCH CHARZ** STCH D1DELTA RESW 2THREE WORD 3CHARZ BYTE C'Z'D1 RESB 4096** END **OUTPUT:the length of the program 19

1. FILE NAME: out.dat

** START 37003700 ** LDA THREE3703 ** STA DELTA3706 ** LDCH CHARZ3709 ** STCH D13712 DELTA RESW 23714 THREE WORD 33717 CHARZ BYTE C'Z'3718 D1 RESB 40962. File Name: symbol.dat

DELTA 3712THREE 3714CHARZ 3717D1 3718

Page 9: System Software Lab Manual

RESULT:Thus a C program has been written and executed for the

implementation of Pass one of a two pass assembler to generate the location of each instruction.

Page 10: System Software Lab Manual

TWO PASS ASSEMBLER – PASS 2

AIM:To implement pass two of an assembler when intermediate files

(output of pass one) are given and to generate the object code

ALGORITHM:

Step 1 : Start.

Step 2 : Read the first line from the intermediate file.

Step 3 : If opcode=START thena. Write listing lineb. Read the next input line

Step 4 : Write the header record to object program.

Step 5 : Intialize first text record.

Step 6 : While opcode is not equal to END,do the following a) If this is not a comment line then, do the searching in OPTAB for opcode. i)If the symbol is present then,do the searching in SYMTAB for operand. ii) If found then store symbol value as operand address Else set an error flag to indicate it is an undefined symbol Store 0 as operand address. iii) Else store 0 as operand address b)Assemble the object code instruction c) Else if opcode is BYTE or WORD then i) Convert constant to object code ii)If the object code will not fit to the current text record the a)Write the text record to object program

b)Intialize new text recordc)Add object code to text recordd)Write listing linee)Read next input line

Step 7 : Write last text record to object program

Step 8 : Write end record to object program

Step 9 : Write last listing line

Step 10 : Stop the program.

Page 11: System Software Lab Manual

SOURCE CODE:

#include<stdio.h>#include<string.h>void main(){int lc=0x1000,ad,address;int n,s,i=0,j;FILE *fp1,*fp2,*fp3;char lab[10],op[10],val[10],code[10];char a[15][15]={"STA","STL","LDA","LDB","J","JEQ","JSUB","COMP","STCH","ADD","SUB"};char b[20][15]={"14","32","03","69","34","30","48","28","24","16","0C"};char sym[15][10];int symadd[15];fp1=fopen("INTER.DAT","r");fp2=fopen("SYMTAB.DAT","r");fp3=fopen("OUTPUT.DAT","w");s=lc;while(!feof(fp2)){fscanf(fp2,"%s\t%x",&sym[i],&symadd[i]);i++;}n=i;while(!feof(fp1)){fscanf(fp1,"%x\t%s\t%s\t%s",&ad,lab,op,val);if(strcmp(op,"END")!=0){if(strcmp(op,"START")==0){

fprintf(fp3,"H^%s^00%x\n",lab,lc);fprintf(fp3,"T^00%x^1F",lc);

}else if(strcmp(op,"RESW")!=0&&strcmp(op,"RESB")!=0&&strcmp(op,"WORD")!=0)

{if(strcmp(op,"BYTE")==0){

for(i=2,j=0;i<strlen(val)-1;i++){code[j]=val[i];j++;}

code[j]='\0';fprintf(fp3,"^%s",code);}else

Page 12: System Software Lab Manual

{for(i=0;i<11;i++){

if(strcmp(op,a[i])==0){strcpy(code,b[i]);break;}

}for(i=0;i<n;i++)

{if(strcmp(val,sym[i])==0){address=symadd[i];break;}

}

fprintf(fp3,"^%s%x",code,address);}}}

elsebreak;}fprintf(fp3,"\nE^00%x",s);fcloseall();}

Page 13: System Software Lab Manual

INPUT:

1. INTER.DAT:

1000 COPY START 10001000 RETADR RESW 11003 BUFFER RESB 41007 EOF BYTE C`EOF`100a FIRST STA RETADR100d $ STL BUFFER1010 $ J FIRST1013 $ END STARTPROGRAMLENGTH 13

2. SYMTAB.DAT:

RETADR 1000BUFFER 1003EOF 1007FIRST 100a

OUTPUT:

1.OUTPUT.DAT:

H^COPY^001000T^001000^1F^EOF^141000^321003^34100aE^001000

Page 14: System Software Lab Manual

RESULT:Hence a C program has been written and executed for the implementation of pass two of a two pass assembler to generate the object code of the given instructions.

Page 15: System Software Lab Manual

SINGLE PASS ASSEMBLER

AIM:To write a C program to implement the working of an assembler that generates the object code of the instructions in a single pass.

ALGORITHM:

Step 1 : Start the program

Step 2 : Read the first input line

Step 3 : If opcode = START theni. Save program name and its lengthii. Initialize LOCCTR to starting addressiii. Write the line into output fileiv. Else Initialize LOCCTR to 0

Step 4 : While opcode<> END do a)If there is a symbol in label field then i) Search SYMTAB for label

If it is found thenSet an error to indicate it’s a duplicate label

elseInsert label name and address to SYMTAB

b) Search OPTAB for opcodei) If it is found then

Add 3 to LOCCTRelseif opcode=WORD then

Add 3 to LOCCTRelseif opcode=RESW then

Add(3*value of the operand)to LOCCTRElseif opcode=BYTE then

Add(3* length of the constant operand) to LOCCTRElseif opcode=RESB then

Add value of the operand to LOCCTRc) Convert mnemonics code into its equivalent machine coded) Check the operand,if the operand is a symbol operand is a symbol operand

i) Search the SYMTAB for symbol If it is found then

Replace the symbol by their addressElse convert data constants into the machine code

Step 5 : Write the opcode,symbol address,data constants into output file.

Step 6 : Read the next input line

Step 7 : Calculate the program length using LOCCTR – starting address+1

Step 8 : Stop the program

Page 16: System Software Lab Manual

SOURCE CODE:

#include<stdio.h>#include<string.h>#define q 11void main(){int lc,address,err=0;int s,num,i=0,j,n=0,line=1,f=0,f1=0,t=0,ni=0,m=0;FILE *fp1,*fp2,*fp3,*fp4;char lab[10],op[10],val[10],code[10];char a[20][15]={"STA","STL","LDA","LDB","J","JEQ","J","SUB","COMP","STCH","ADD","SUB"};char b[20][15]={"14","32","03","69","34","30","48","28","24","16","0C"};char sym[15][10];int symadd[15];clrscr();fp1=fopen("INPUT_SP.DAT","r");fp2=fopen("OBJFILE.DAT","w");fp3=fopen("ERROR.DAT","w");fp4=fopen("SYMTAB_SP.DAT","w");while((!feof(fp1))){fscanf(fp1,"%s\t%s\t%s",lab,op,val);t++;m++;if(strcmp(op,".")==0)m=0;else if(strcmp(op,"END")==0)break;}t=t-1;m--;fclose(fp1);fp1=fopen("INPUT.DAT","r");fscanf(fp1,"%s\t%s\t%x",lab,op,&lc);fprintf(fp3,"-------------------------------------\n");fprintf(fp3,"LINE NO.\t|ERROR FOUND\n");fprintf(fp3,"-------------------------------------");fprintf(fp4,"SYMBOL\tADDRESS");s=lc;fprintf(fp2,"H^%s^00%x^%x\n",lab,lc,t*3);fprintf(fp2,"T^00%x^",lc);if(m>10)fprintf(fp2,"1E");elsefprintf(fp2,"%x",m*3);

while((op,".")!=0&&(!feof(fp1))){

Page 17: System Software Lab Manual

fscanf(fp1,"%s\t%s\t%s",lab,op,val);line++;if(strcmp(lab,"$")!=0)

{for(i=0;i<n;i++)

{if(strcmp(lab,sym[i])==0)

{f=1;break;

}f=0;}

if(f==0){strcpy(sym[n],lab);symadd[n]=lc;fprintf(fp4,"\n%s\t%x",lab,lc);n++;}if(f==1){fprintf(fp3,"%d\t\t|SYMBOL ALREADY DEFINED\

n",line);err++;}}

num=atoi(val);if(strcmp(op,"RESW")==0)lc=lc+(num*3);else if(strcmp(op,"RESB")==0)lc=lc+num;else if(strcmp(op,"BYTE")==0)

{num=strlen(val)-3;lc=lc+num;for(i=2,j=0;i<strlen(val)-1;i++)

{code[j]=val[i];j++;}

code[j]='\0';fprintf(fp2,"^%s",code);ni++;}

elselc=lc+3;

if(strcmp(op,".")==0)break;

}

Page 18: System Software Lab Manual

while(strcmp(op,"END")!=0&&(!feof(fp1))){fscanf(fp1,"%s\t%s\t%s",lab,op,val);line++;if(strcmp(op,"END")==0)break;if((strcmp(lab,"$")!=0)&&((strcmp(op,"RESW")!=0||

strcmp(op,"RESB")!=0||strcmp(op,"WORD")!=0||strcmp(op,"BYTE")==0))){for(i=0;i<n;i++)

{if(strcmp(lab,sym[i])==0)

{f=1;break;}

f=0;}

if(f==0){strcpy(sym[n],lab);symadd[n]=lc;fprintf(fp4,"\n%s\t%x",lab,lc);n++;}else{fprintf(fp3,"\n%d\t\t|SYMBOL ALREADY DEFINED");err+

+;}}

else if(strcmp(op,"RESW")==0||strcmp(op,"RESB")==0||strcmp(op,"WORD")==0||strcmp(op,"BYTE")==0)

fprintf(fp3,"\n%d\t\t|Declaration not allowed here",line);if(strcmp(op,"RESW")!=0&&strcmp(op,"RESB")!

=0&&strcmp(op,"WORD")!=0&&strcmp(op,"BYTE")!=0){for(i=0;i<q;i++)

{if(strcmp(op,a[i])==0)

{strcpy(code,b[i]);f1=0;break;}

f1=1;}

if(f1==1){fprintf(fp3,"\n%d\t\t|WRONG OPCODE",line);err++;}for(i=0;i<n;i++)

{

Page 19: System Software Lab Manual

if(strcmp(val,sym[i])==0){address=symadd[i];f=0;break;}

f=1;}

if(f){fprintf(fp3,"\n%d\t\t|UNDEFINED SYMBOL",line);err++;}}

if(ni<10){fprintf(fp2,"^%s%x",code,address);ni++;}else{fprintf(fp2,"T^00%x^",lc);

if(m>10){fprintf(fp2,"1E");m=m-10;}else{fprintf(fp2,"%x",m*3);fprintf(fp2,"^%s%x",code,address);ni=0;}

}lc=lc+3;}

fprintf(fp2,"\nE^00%x",s);fprintf(fp3,"No of errors=%d\n--------------------------------------",err);printf("Output file:OBJCODE.DAT\nErrors are described in ERROR.DAT\

nSymbol table is in the file:SYMTAB.DAT");getch();fcloseall();

}

INPUT:

1. FILE NAME: input_sp.dat

Page 20: System Software Lab Manual

ARITH START 0X1000FIRST LDA ALPHA$ ADD INCR$ SUB ONE$ STA BETAONE BYTE X'F1'ALPHA RESW 1INCR RESW 1BETA RESW 1$ END FIRST

OUTPUT:

1. FILE NAME: OBJFILE.dat

H^TEN^0010^1bT^0010^1bE^0010

2. ERROR.DAT:

-------------------------------------LINE NO. |ERROR FOUND-------------------------------------5 |SYMBOL ALREADY DEFINED6 |SYMBOL ALREADY DEFINED8 |SYMBOL ALREADY DEFINEDNo of errors=3--------------------------------------

3.SYMTAB_SP.DAT:

SYMBOL ADDRESSSIX 10ANS 13- 16END 1f

Page 21: System Software Lab Manual

RESULT:

Thus the C program to implement the working of an assembler that generates the object code of the instructions in a single pass was executed successfully.

MACRO PROCESSOR – TWO PASS

AIM:

Page 22: System Software Lab Manual

      To execute the working of a macro processor to substitute one group of characters or lines for another using C. 

ALGORITHM :

Step 1: Start the program.

Step 2: Open the source program and reference program.

Step 3: Search for START.

Step 4: After finding START, search for call.

Step 5: If the string is found in the reference file, pass it to the corresponding location.

Step 6: Else print the output.

Step 7: Close the source and reference program files.

Step 8: Stop the program.

SOURCE CODE:

#include<stdio.h>

Page 23: System Software Lab Manual

#include<conio.h>#include<string.h>char check[20],check1[20],str[20],s1[20],s2[20],s3[20],ch;int j,ads,flag=0,flag1=0;FILE*f1,*f2,*f3,*fp;void main(){

clrscr();f1=fopen("macro1.dat","r");f2=fopen("macro2.dat","r");f3=fopen("macroout.dat","w");while(!feof(f1)){

fscanf(f1,"%s%s",check1,check);if(strcmp(check1,"START")==0){

flag=1;}while(flag==1&&(fscanf(f1,"%s",check))!=EOF){

flag1=0;if(strcmp(check,"CALL")==0){

fscanf(f1,"%s",str);rewind(f2);while((fscanf(f2,"%s%s%s",s1,s2,s3))!=EOF){

if(strcmp(str,s2)==0){

fprintf(f3,"%s%s",check,s3);fprintf(f3,"%c",get(f1));flag1=1;

}}

}else{

ch=getc(f1);if((ch==' ')||(strcmp(check,"END")==0)){

fprintf(f3,"%s",check);fprintf(f3,"%c",ch);

}elsefprintf(f3,"%s%c",check,ch);

}}

}fclose(f1);fclose(f2);

Page 24: System Software Lab Manual

fclose(f3);printf("\n%s source program\n");fp=fopen("macroin1.dat","r");while((ch=getc(fp))!=EOF)putchar(ch);putchar('\n');fclose(fp);getch();printf("\n%s macro function\n");fp=fopen("macroout.dat","r");while((ch=getc(fp))!=EOF)putchar('\n');fclose(fp);getch();

}

SAMPLE INPUT AND OUTPUT:

INPUT:

Page 25: System Software Lab Manual

1. FILE NAME: macro1.dat

START 0L1,55 *CALL ACALL BMUL 1,2CALL FCALL AEND

2. FILE NAME: macroin2.dat

MACRO A 250MACRO B 122

OUTPUT:

FILE NAME: Macroout.dat

L1,55 *CALL 250CALL 122MUL 1,2CALL 250END

Page 26: System Software Lab Manual

RESULT:      Hence we have implemented the working of a macro processor to

substitute one group of characters or lines for another using C.MACRO PROCESSOR – SINGLE PASS

Page 27: System Software Lab Manual

AIM:To execute the working of a macro processor one pass using c program

ALGORITHM :

Step 1: Start the program.

Step 2: Open the source program and reference program.

Step 3: Search for START.

Step 4: After finding START, search for call.

Step 5: If the string is found in the reference file, pass it to the corresponding location.

Step 6: Else print the output.

Step 7: Close the source and reference program files.

Step 8: Stop the program.

SOURCE CODE

#include<stdio.h>#include<conio.h>

Page 28: System Software Lab Manual

#include<string.h>#include<stdlib.h>#include<ctype.h>FILE *fp,*f1,*f2,*f3,*f4;char c1[20],c2[20],c3[20],name[10],alaarg[10][10];char line[20],c;char *p1,*p2,*p3,*q1;int found=0,a=0,i,j,k,l=0,m=0;void main(){clrscr();f2=fopen("mnt.txt","r");fscanf(f2,"%s%s%s",c1,c2,c3);strcpy(name,c2);fclose(f2);fp=fopen("mpass1.txt","r");f3=fopen("mpass2.txt","w");f4=fopen("ala.txt","r+");while(fscanf(fp,"%s%s%s",c1,c2,c3)!=EOF){if(strcmp(c2,name)==0){fprintf(f4,"%d\t%s\n",a,c1);strcpy(alaarg[a],c1);a++;p1=strtok(c3,",");fprintf(f4,"%d\t%s\n",a,p1);strcpy(alaarg[a],p1);a++;p2=strtok(NULL,",");fprintf(f4,"%d\t%s\n",a,p2);strcpy(alaarg[a],p2);fclose(f4);expand();}elsefprintf(f3,"%s\t%s\t%s\t\n",c1,c2,c3);}fclose(f4);fclose(f3);fclose(f1);

}expand(){f2=fopen("mdt.txt","r");while(fscanf(f2,"%s",c1)!=EOF){if(strcmp(c1,"A")==0){

Page 29: System Software Lab Manual

fscanf(f2,"%s",c2);p1=strtok(c2,",");p2=strtok(NULL,",");fprintf(f3,"%s\t%s\t%s\t%s%s\n",alaarg[0],c1,p1,alaarg[1]);}else if(strcmp(c1,"L")==0){fscanf(f2,"%s",c2);p1=strtok(c2,",");p2=strtok(NULL,",");fprintf(f3,"\t%s\t%s\n",c1,p1,alaarg[2]);}}printf("\n to see the output view the file mpass2.txt\n");getch();fclose(f2);return 0;}

OUTPUT

To see the ouput view the file mpass2.txt

Page 30: System Software Lab Manual

INPUT FILE

MNT.TXT

100 CSE 0

MDT.TXT

&LAB CSE &A1,&A2#0 A 1,#1#0 B 2,#2

ALA.TXT

0 LOOP1 D12 D2

MPASS1.TXT

PGM START 0LOOP CSE D1,D2D1 DC F’1’D2 DC F’2’

OUTPUT FILE

MPASS2.TXT

PGM START 0LOOP A 1,D1

L 2,D2D1 DC F’1’D2 DC F’2’

Page 31: System Software Lab Manual

RESULT:      Hence we have implemented the working of a macro processor to

substitute one group of characters or lines for another using C.

ABSOLUTE LOADER

Page 32: System Software Lab Manual

AIM:

To implement the working of an absolute loader that will load the entire source statement to the user specified address.

ALGORITHM:

Step 1: Start the program.

Step 2: Open the source, reference file.

Step 3: Read the base address.

Step 4: Search for START.

Step 5: After START, search for call.

Step 6: If call is found, get next string.

Step 7: If the next string is found in reference file, put the corresponding location.

Step 8: Else put subroutine not found.

Step 9: If character is \n, increment location by 3.

Step 10: Print the output.

Step 11: Stop the program.

SOURCE CODE

#include<stdio.h>#include<conio.h>#include<string.h>

Page 33: System Software Lab Manual

void main(){char input[10];int start,length,address;FILE *fp1,*fp2;clrscr();fp1=fopen("input.txt","r");fp2=fopen("output.txt","w");fscanf(fp1,"%s",input);while(strcmp(input,"E")!=0){if(strcmp(input,"H")==0){fscanf(fp1,"%d",&start);fscanf(fp1,"%d",&length);fscanf(fp1,"%s",input);}else if(strcmp(input,"T")==0){fscanf(fp1,"%d",&address);fscanf(fp1,"%s",input);fprintf(fp2,"%d\t%c%c\n",address,input[0],input[1]);fprintf(fp2,"%d\t%c%c\n",(address+1),input[2],input[3]);fprintf(fp2,"%d\t%c%c\n",(address+2),input[4],input[5]);address+=3;fscanf(fp1,"%s",input);}else{fprintf(fp2,"%d\t%c%c\n",address,input[0],input[1]);fprintf(fp2,"%d\t%c%c\n",(address+1),input[2],input[3]);fprintf(fp2,"%d\t%c%c\n",(address+2),input[4],input[5]);address+=3;fscanf(fp1,"%s",input);}}fclose(fp1);fclose(fp2);printf("FINISHED");getch();}

OUTPUT:

FINISHED

INPUT FILE

Page 34: System Software Lab Manual

INPUT.TXT

H 4000 232T 4000 142033 483039 102036T 6000 298300 230000 282030 302015E

OUTPUT FILE

OUTPUT.TXT

4000 144001 204002 334003 104004 204005 366000 296001 836002 006003 236004 006005 006006 286007 206008 306009 306010 206011 15

 

Page 35: System Software Lab Manual

RESULT:

Hence a program has been written and executed in C to implement the working of a absolute loader that is capable of loading an entire program to the specified address.

RELOCATING LOADER

AIM:

Page 36: System Software Lab Manual

To implement the working of a relocating or relocatable loader that will reload the entire source statement to the user specified address.

ALGORITHM:

Step 1:Start the program.

Step 2:Open the intermediate file in read mode and source file in the write mode.

Step 3: Get the starting address of the program to be relocated.

Step 4: Read the input of the intermediate file.

Step 5: Check if the intermediate bitmask is equal to 1.

Step 6: If equal to 1 then add the intermediate address with starting address and store it.

Step 7: Then copy the intermediate label and mnemonic.

Step 8: Stop the program.

SOURCE CODE:

#include<stdio.h>

Page 37: System Software Lab Manual

#include<conio.h>#include<string.h>char check[20],check1[20],s1[20];int ads,addr;FILE *f1,*f2;void main(){

clrscr();f1=fopen("relin.dat","r");f2=fopen("reabload.dat","w");printf("\n enter the base address");scanf("%d",&ads);fscanf(f1,"%s%s",check1,check);if(strcmp(check1,"START")==0){

fprintf(f2,"%s%d\n",check1,ads);}while(strcmp(check1,"END")!=0){

fscanf(f1,"%d%s%s",addr,check1,s1);fprintf(f2,"%d%s%s\n",ads+=3,check1,s1);

}fclose(f1);fclose(f2);getch();

}

SAMPLE INPUT AND OUTPUT:

INPUT:

Page 38: System Software Lab Manual

FILE NAME: relin.dat

START 23002303 L1,15*2306 CALL X2309 CALL Y2312 ADD 1,22315 CALL F2318 CALL X2321 END

OUTPUT:

FILE NAME: reabload.dat

Enter the base address :4230

START 42304233 L1,55*4236 CALL A4239 CALL B4232 MUL 1,24235 CALL F4238 CALL A4231 END

Page 39: System Software Lab Manual

RESULT:Hence a program has been written and executed in C to implement the

working of a relocating loader that is capable of shifting an entire program to the specified address.

DIRECT LINKING LOADER - PASS ONE

Page 40: System Software Lab Manual

AIM:To execute a C program that implements pass one of a direct linking loader thus identifying and defining all external references and extended definitions.

ALGORITHM:

Step 1: Start a program.

Step 2: Define structure object program name, address, type, length.

Step 3: Define a pointer.

Step 4: Open the input file in read mode.

Step 5: The output file is in write mode.

Step 6: Get the program address from the user.

Step 7: Print the symbol and address in the output file.

Step 8: If x=atoi(op.ad)+atoi(in.len)

Step 9: Print the symbol in the output5.txt file.

Step10: Close all the files.

Step11: Stop the program.

SOURCE CODE:

#include<stdio.h>

Page 41: System Software Lab Manual

#include<ctype.h>#include<conio.h>#include<string.h>struct input{

char type[10];char name[10];char add[10];char len[10];

}in;struct output{

char pn[10];char sn[10];char ad[10];char lgn[10];

}op;char lc[]="4000";void main(){

FILE *f1,*f2;int x,y;f1=fopen("input.dat","r");f2=fope n("output.dat","w");fprintf(f2,"CSNAME\tSNAME\tADDRESS\tLENGTH\n");while(1){

m:fscanf(f1,"%s%s%s%s",in.type,in.name,in.add,in.len);if(strcmp(in.type,"H")==0){

strcpy(op.pn,in.name);strcpy(op.ad,lc);strcpy(op.lgn,in.len);strcpy(op.sn,"_");fprintf(f2,"\n%s\t%s\t%s\t%s\n",op.pn,op.sn,op.ad,op.lgn);x=atoi(op.ad)+atoi(in.len);itoa(x,lc,10);

}if(strcmp(in.type,"D")==0){

strcpy(op.pn,"_");strcpy(op.sn,in.name);y=atoi(op.ad)+atoi(in.add);itoa(y,op.ad,10);strcpy(op.lgn,"_");fprintf(f2,"\n%s\t%s\t%s\t%s\n",op.pn,op.sn,op.ad,op.lgn);

}if(strcmp(in.type,"R")==0)

goto m;

Page 42: System Software Lab Manual

if(strcmp(in.type,"M")==0)goto m;

if(strcmp(in.type,"E")==0)goto m;

if(strcmp(in.type,"END")==0)break;}

getch();}

SAMPLE INPUT AND OUTPUT:

INPUT:

Page 43: System Software Lab Manual

FILE NAME: input.dat

H PROGA 000040 000070D LISTA 000055 -R LISTB - -T 000052 0A LISTBE 000060 - -

H PROGB 000030 000047D LISTB 000039 -R LISTA - -T 000033 09 000041M 000050 06 LISTAE 000075 - -END - - -

OUTPUT:

FILE NAME: output.dat

CSNAME SNAME ADDRESS LENGTH

PROGA - 3400 000030- LISTA 3455 -PROGB - 3500 000047- LISTB 3539 -

Page 44: System Software Lab Manual

RESULT:Hence we have written and executed a C program that implements pass one of a direct linking loader thus identifying and defining all external references and extended definitions.

DIRECT LINKING LOADER-PASS TWO

Page 45: System Software Lab Manual

AIM:To execute a C program that implements pass two of a direct linking loader thus linking all external references and extended definitions generating the complete object code

ALGORITHM:

Step 1: Start the program.

Step 2: Define structure object program name, len1, len, address, sname,type, type1.

Step 3: Define a pointer.

Step 4: Open the source and reference file.

Step 5: The input files are in the read mode.

Step 6: The output file is the write mode.

Step 7: Get the the base address from the user.

Rewind(f2)

while(!feof(f2))

Step 8: Read the line from input program.

Step 9: Print the symbol and address in the output file.

Step10: a=atoi(tarr)+atoi(nadd)

Step11: Print the symbol in the output.dat file.

Step12: Close all the files.

Step13: Stop the program.

SOURCE CODE:

#include<stdio.h>#include<conio.h>#include<string.h>

struct input{

char type[10]; char sname[20];

Page 46: System Software Lab Manual

char add[20]; char len[20]; char type1[10]; char sname1[10]; char add1[10]; char len1[20];

}in;

struct estab{

char address[10]; char name[10];

}est;

struct output{

char code[20]; char op1; char op2;

}out;

int x,y,i,len,j,a,l;

void main(){

char stadd[10],ladd[10],nadd[10],tarr[10],madd[10]; FILE *f1,*f2,*f3; f1=fopen("inputa.dat","r"); f2=fopen("inputb.dat","r"); f3=fopen("output.dat","w"); while(1) {

m: fscanf(f1,"%s%s%s%s",in.type,in.sname,in.add,in.len); if(strcmp(in.type,"END")==0) break;

if(strcmp(in.type,"H")==0) { while(!feof(f2)) { fscanf(f2,"%s%s",est.name,est.address); if(strcmp(in.sname,est.name)==0) { strcpy(stadd,est.address); break; }

}rewind(f2);

}

Page 47: System Software Lab Manual

if(strcmp(in.type,"T")==0){

x=atoi(in.sname)+atoi(stadd); itoa(x,ladd,10); strcpy(out.code,ladd); fscanf(f1,"%s%s%s%s",in.type1,in.sname1,in.add1,in.len1); y=atoi(in.sname1)+atoi(stadd); itoa(y,madd,10); while(!feof(f2)) {

fscanf(f2,"%s%s",est.name,est.address); if(strcmp(est.name,in.len1)==0) strcpy(nadd,est.address);

} rewind(f2); for(i=0;i<strlen(in.len);i=i+2) {

if(strcmp(out.code,madd)==0) {

l=atoi(in.add1); for(j=0;j<l;i++,j++) tarr[j]=in.len[i]; a=atoi(tarr)+atoi(nadd); itoa(a,tarr,10); i=(i-l); for(j=0;j<l;i++,j++) in.len[i]=tarr[j]; i=(i-l);

} out.op1=in.len[i]; out.op2=in.len[i+1]; fprintf(f3,"%s\t%c%c\n",out.code,out.op1,out.op2); x=x+1; itoa(x,out.code,10); }}

}getch();

}

Page 48: System Software Lab Manual

SAMPLE INPUT AND OUTPUT:

INPUT:

1. FILE NAME: inputa.dat

H PROGB 000030 000047D LISTB 000039 -R LISTA - -T 000033 09 000041M 000050 06 LISTAE 000075 - -END - - -

2. FILE NAME: inputb.dat

PROGA 004000LISTA 004040

Page 49: System Software Lab Manual

PROGB 004050LISTB 004070 

OUTPUT:4080 034081 404082 40 

Page 50: System Software Lab Manual

RESULT: Thus the C program that implements pass two of a direct linking loader thus linking all external references and extended definitions generating the complete object code was executed successfully.

TEXT EDITOR

AIM:To write a C program to implement the text editor operation to implement functions like opening a file, saving it, copying, cutting and pasting data

ALGORITHM:

Step 1: Start the program

Step 2: Get the choice from the user to perform different function of text editor

Step 3: To perform this function maintain a file pointer globally

Step 4: The different functions arei. creating a newfileii. open the existing fileiii. edit the fileiv. Search the string in the filev. delete a file

Step 5: //creating a new filea. Give a specific name to create file, which is pointed by file pointer.b. add the contents to the file(set the EOF condition)c. Close the file

Step 6: //open the existing filea.open the created file using file pointerb.Display the contents of the operand file

Page 51: System Software Lab Manual

c.Close the file

Step 7: //Edit the filea.Open the created file using file pointerb.Append the contents to the existing file(set of EOF condition)c.Close the file

Step 8: //search the string in filea.Get the string from user to searchb.if it is exist in the file contents then display “the string is present”c.else display “the string is not present”d.close the file

Step 9: //delete the file using file pointer

Step 10:stop

SOURCE CODE:

#include<stdio.h>#include<conio.h>#include<string.h>void del();void bksp();void save();void filopen();void sel();void cpy();void paste();void cut();void headbar();void enter();char c,c1,b[4096],b1[4096],b2[80],fl[60];int i,j,x,y,bx,nx=0,lx,ly=6,flag=0,line[40];FILE*f,*f1;void main(){

clrscr();headbar();while(1){

x=wherex();y=wherey();c=getch();if(c!=0){

switch(c){

case 13:enter();

Page 52: System Software Lab Manual

break;case 8:

bksp();break;

case 3:cpy();break;

case 22:paste();break;

case 24:cut();break;

case 27:headbar();break;

default:gettext(x,y,80,y,b);if(y!=5)

putchar(c);else gotoxy(1,6);puttext(x+1,y,79,y,b);break;

}}else{

c=getch();switch(c){

case 107:exit(0);

case 83:del();break;

case 72:(y>5)?gotoxy(x,y-1):gotoxy(x,6);break;

case 75:gotoxy(x-1,y);break;

case 77:gotoxy(x+1,y);break;

case 80:(y<ly)?gotoxy(x,y+1):gotoxy(x,y);break;

case 60:save();break;

case 61:

Page 53: System Software Lab Manual

filopen();break;

case -99:sel();break;

}}

}}

void del(){

gettext(x+1,y,80,y,b);puttext(x,y,79,y,b);

}

void bksp(){

if(y>5){

if(x!=1){

gettext(x,y,80,y,b);puttext(x-1,y,79,y,b);gotoxy(x-1,y);

}else gotoxy(line[y-1],y-1);

}else gotoxy(1,6);

}

void save(){

ly++;gotoxy(1,40);printf("enter the file name");scanf("%s",fl);f=fopen(fl,"w");for(i=6;i<=ly;i++);{

for(j=1;j<=80;j++);{

gettext(j,i,j,i,b);putc(b[0],f);

}putc('\n',f);

}gotoxy(1,40);del();gotoxy(1,6);

Page 54: System Software Lab Manual

fclose(f);}

void filopen(){

gotoxy(1,40);printf("entr the file name");scanf("%s",fl);clrscr();f1=fopen(fl,"r");headbar();while((c1=getc(f1))!=EOF)putchar(c1);fclose(f1);ly=wherey();

}

void sel(){

if(flag!=1){

nx=wherex();bx=nx;flag=1;

}elsenx++;gotoxy(x+1,y);

}

void cpy(){

gettext(bx,y,nx,y,b1);}

void paste(){

lx=x+(nx-bx);gettext(x,y,80,y,b);puttext(x,y,lx,y,b1);puttext(lx+1,y,79,y,b);gotoxy(lx+1,y);flag=0;

}

void cut(){

gettext(bx,y,nx,y,b1);gettext(nx+1,y,80,y,b);puttext(bx,y,79,y,b);

Page 55: System Software Lab Manual

gotoxy(bx,y);} void headbar(){

int k;clrscr();printf(“OPEN-F3\t SAVE-F2 \tSELECT-ALT+->\t COPY-CTRL+C”);printf(“\nCUT-CTRL+X \t PASTE-CTRL+V\n”);printf(“ CLRSCREEN-ESC CLOSE-ALT+F4\n");for(k=0;k<79;k++)

printf("_");printf("\n");gotoxy(1,39);for(k=0;k<79;k++)

printf("_");gotoxy(1,6);

}

void enter(){

line[y]=x;if((y+1)!=39){

putchar('\n');ly++;

}}

OUTPUT:

Page 56: System Software Lab Manual
Page 57: System Software Lab Manual
Page 58: System Software Lab Manual
Page 59: System Software Lab Manual
Page 60: System Software Lab Manual

RESULT:Hence a C program has been written and executed to implement the working of a user friendly text editor that performs basic operations like cut, copy, paste, saving and opening a file.


Recommended