Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1
SRINIVASAN ENGINEERING COLLEGE
COMPUTER SCIENCE AND ENGINEERING
ANNA UNIVERSITY OF TECHNOLOGY TRICHIRAPALLI
REGULATION 2009
III CSE (2010-2014)
CS2308
SYSTEM SOFTWARE LABORATORY
LAB MANUAL
BY
C.SARANYA, Assistant Professor.
S.BANUPRIYA, Assistant Professor.
SEC CSE/V SS LAB MANUAL
2
Preface
This laboratory manual is prepared by the Department of Computer Science and
engineering for System Software Laboratory (CS-2308). This lab manual can be used as
instructional book for students, staff and instructors to assist in performing and understanding
the experiments. In the first part of the manual, experiments as per syllabus are described and in
the second part of the manual, experiments that are beyond the syllabus but expected for
university laboratory examination are displayed. This manual will be available in electronic form
from Colleges official website, for the betterment of students.
Acknowledgement
We would like to express our profound gratitude and deep regards to the support
offered by the Chairman Shri. A.Srinivasan. We also take this opportunity to express a deep
sense of gratitude to our Principal Dr.B.Karthikeyan,M.E,Ph.D, for his valuable
information and guidance, which helped us in completing this task through various stages.
We extend our hearty thanks to our head of the department Mrs.S.Jayanthi M.E, for her
constant encouragement and constructive comments.
Finally the valuable comments from fellow faculty and assistance provided
by the department are highly acknowledged.
SEC CSE/V SS LAB MANUAL
3
TABLE OF CONTENTS
CHAPTER NO TOPIC PAGE NO
I
Introduction to the language
Main Features
Advantages
Limitations
Applications
4
II Syllabus 6
III
System Requirements 7
IV
Description about each experiment
Aim
Learning Objective
General procedure to execute programs
8
V Experiments
1
Implement a symbol table with functions to create, insert,
modify, search, and display.
10
2 Implement pass one of a two pass assembler. 18
3
Implement pass two of a two pass assembler.
24
4
Implement a single pass assembler.
30
5
Implement a two pass macro processor.
40
6
Implement a single pass macro processor.
46
7
Implement an absolute loader.
53
8
Implement a relocating loader.
59
9
Implement pass one of a direct-linking loader
65
10 Implement pass two of a direct-linking loader
70
11
Implement a simple text editor with features like
insertion / deletion of a character, word, and sentence.
75
12
Implement a symbol table with suitable hashing
.
81
VII
Experiments beyond the syllabus
85
SEC CSE/V SS LAB MANUAL
4
INTRODUCTION OF THE LANGUAGE (Turbo C language)
MAIN FEATURES
It is a robust language whose rich set of built-in functions and operators can be used to
write any complex program
C is highly portable. This means that C programs written for one computer can be
run on another with little or no modification.
C language is well suited for structured programming, thus requiring the user to
think of a problem in terms of function modules or blocks.
Programs written in C are efficient and fast. This is due to its variety of data types
and powerful operators. It is many times faster than BASIC.
C source code can be optimized much more than higher-level languages because the
language set is relatively small and very efficient
Weak text processing capabilities - C's string library is extremely primitive (it
doesn't even define an actual string type), and text processing is labor-intensive
and error-prone.
Security and safety issues- several library functions (gets() being the most
notorious) provide easy exploits for malware, and anything involving pointers is
going to be unsafe (it's hard to guard against being passed an invalid pointer);
No built-in collections library - C doesn't provide readymade collections (lists, queues, stacks,
etc.), meaning you have to roll your own.
Search(),Display()
OBJECTIVE
The main objective is to write a program to implement a symbol table which can
create,insert,modify,search and display the character
HOW IT IS BEING ACHIEVED?
It is achieved by extracting character from the expression using built in functions in C, such
as is switch(),while() etc.
SYNTAXES & KEYWORDS
switch ()-A switch statement allows a variable to be tested for equality against a list of values.
while ()-test expression is checked. If it is true, codes inside the body of while loop.
HOW TO EXECUTE THE PROGRAM
To compile and run the program.
REQUIREMENTS FOR EXECUTION
S.No. Facilities
required
Quantity
1 System 1
2 OS Turboc
SEC CSE/V SS LAB MANUAL
11
EXPECTED OUTPUT AND ITS FORM
This program takes a source program as input, and it is expected to produce a
functions as output.
LIMITATIONS
The switch statement is that execute one by one with in a case condition
The limitations are while condition is code/s inside parentheses are executed and again
the test expression is checked and process continues until the test expression becomes
false.
APPLICATIONS
Easy to create,modify,seach,and display the character with the help of case statement.
ALGORITHM
1.Design a menu through which we can create a symbol table and perform
operations as insert, modify, search and display.
2.Create a symbol table with fields as variable and value using create option. Enteries
may be added to the table while it is created itself.
3.Append new contents to the symbol table with the constraint that there is no duplication of
entries, using insert option.
4.Modify existing content of the table using modify option.
5.Use display option to the contents of the table.
SEC CSE/V SS LAB MANUAL
12
PROGRAM
PROGRAM NAME: SYMT.TXT
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
fpos_t pos;
int ch,val,v,flag,count;
char sym[30],s[40];
clrscr();
do
{
printf("Enter your chioce\n");
printf("\n 1.insertion\n2.modification\n3.search\n4.display\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
fp=fopen("SYMT.TXT","a+");
printf("Enter your symbol you want to insert and its value\n");
scanf("%s%d",sym,&val);
fprintf(fp,"%s\t%d\n",sym,val);
fclose(fp);
break;
case 2:
fp=fopen("SYMT.TXT","r+");
printf("Enter the symbol that you want to modify\n");
scanf("%s",&s);
printf("Enter the new value\n");
scanf("%d",&v);
while(!feof(fp))
{
fgetpos(fp,&pos);
fscanf(fp,"%s\t%d\n",&sym,&val);
if(strcmp(sym,s)==0)
{
fsetpos(fp,&pos);
fprintf(fp,"%s\t%d\n",s,v);
SEC CSE/V SS LAB MANUAL
13
printf("\nThe symbol table has been modified\n"); break;
}
}
fclose(fp); break;
case 3:
fp=fopen("SYMT.TXT","r");
printf("\nEnter the symbol you want to search\n");
scanf("%s",&sym);
while(!feof(fp))
{
fscanf(fp,"%s\f%d",&s,&v);
if(strcmp(sym,s)==0)
{
flag=1; break;
}
}
if(flag==1)
printf("The symbol%s is founded it has value%d\n",s,v);
else printf("The symbol%s is not found",sym);
fclose(fp);
break;
case 4:
fp=fopen("SYMT.TXT","r");
while(!feof(fp))
{
fscanf(fp,"%s\t%d\n",s,&v);
printf("%s\t%d\n",s,v);
}
fclose(fp); break;
}
printf("\n DO you want to continue(y-1/n-0)\n");
scanf("%d",&count);
}
while(count==1);
getch();
}
OUTPUT:
Enter your chioce
1.insertion
2.modification
SEC CSE/V SS LAB MANUAL
14
3.search
4.display
1
Enter your symbol you want to insert and its value
! 1
DO you want to continue(y-1/n-0)
1
Enter your chioce
1.insertion
2.modification
3.search
4.display
1
Enter your symbol you want to insert and its value
@ 2
DO you want to continue(y-1/n-0)
1
Enter your chioce
1.insertion
2.modification
3.search
4.display
1
Enter your symbol you want to insert and its value
# 3
DO you want to continue(y-1/n-0)
1
Enter your chioce
1.insertion
2.modification
3.search
4.display
4
! 1
@ 2
# 3
DO you want to continue(y-1/n-0)
1
Enter your chioce
1.insertion
2.modification
3.search
SEC CSE/V SS LAB MANUAL
15
4.display
2
Enter the symbol that you want to modify
#
Enter the new value
6
The symbol table has been modified
DO you want to continue(y-1/n-0)
1
Enter your chioce
1.insertion
2.modification
3.search
4.display
3
Enter the symbol you want to search
@
The symbol@ is founded it has value2
DO you want to continue(y-1/n-0)
0
Exit.
SYMT.TXT
! 1
@ 2
# 6
RESULT
The program has been executed to the symbol table in C was implemented and the above
output is obtained
VIVA QUESTIONS
1. Define system software.
It consists of variety of programs that supports the operation of the
computer. This software makes it possible for the user to focus on the other problems to
be solved with out needing to know how the machine works internally.
Eg: operating system, assembler, loader.
2.What is a symbol table?
SEC CSE/V SS LAB MANUAL
16
A symbol table is a data structure containing a record for each identifier, with
fieldsfor the attributes of the identifier. The data structure allows us to find the record for
eachidentifier quickly and to store or retrieve data from that record quickly. Whenever an
identifier is detected by a lexical analyzer, it is entered into the symbol table. The attributes of
an identifier cannot be determined by the lexical analyzer.
3 . Define loader.
Loader is a set of program that loads the machine language translated by the translator
into the main memory and makes it ready for execution.
4. Give some applications of operating system.
. to make the computer easier to use
. to manage the resources in computer
. process management
. data and memory management
. to provide security to the user.
Operating system acts as an interface between the user and the system
Eg:windows,linux,unix,dos
5.What is a compiler?
A compiler is a program that reads a program written in one languagethe source
language and translates it into an equivalent program in another language-the target
language.The compiler reports to its user the presence of errors in the source program.
POSSILBLE QUESTIONS
1. Implement a symbol table only with insert and delete function- a C program
2.Implement a symbol table only with search- a C program
3.Implement a symbol table only with modify - a C program
SEC CSE/V SS LAB MANUAL
17
EX.NO:2
DATE:
IMPLEMENT PASS ONE OF A TWO PASS ASSEMBLER.
DESCRIPTION
Translating mnemonic operation codes to their machine language
equivalents. Assigning machine addresses to symbolic labels used by the programmer by one
pass.
OBJECTIVE
The main objective is to write a program to implement pass one of a two pass assembler.
HOW IT IS BEING ACHIEVED?
It is achieved by extracting character from the expression using built in functions in C, such
as SYMTAB,OPTAB etc
SYNTAXES & KEYWORDS
SYMTAB-The symbol table includes the name and value for each symbol in the
source program, together with flags to indicate error conditions
OPTAB-The operation code table contains the mnemonic operation code and its
Machine language equivalent
HOW TO EXECUTE THE PROGRAM
To compile and run the program.
REQUIREMENTS FOR EXECUTION
S.No. Facilities
required
Quantity
1 System 1
2 OS Turboc
EXPECTED OUTPUT AND ITS FORM
This program takes a source program as input, and it is expected to produce a
Opcode and symbol table as output.
SEC CSE/V SS LAB MANUAL
18
LIMITATIONS
It is necessary or desirable to avoid a second pass over the source program
The external storage for the intermediate file between two passes is slow or is
inconvenient to use
APPLICATIONS
Easy to implement the second pass of assembler.
operands to their machine addresses;
Use proper addressing modes and formats to build efficient machine
ALGORITHM
1.Read the input line.
2.Check to see if the opcode in the input line is START.
i. Find if there is any operand field after START, if so that indicates the location from
which the program has to be loaded in the memory, so initialize the LOCCTR to the
operand value.
For eg .If START 2000,is the first statement, the LOCCTR value is set to 2000.
ii. Otherwise if there is no value in the operand field the LOCCTR is set to zero and
the program starts loading from the starting address zero.
3.Write the line to the intermediate file.
4.Now start the following processing for the other lines in the program, if it is not a comment
line, until the opcode is END.
A) If there is a symbol in the label field.
i. Check the symbol table to see if has already been stored over there. If so then it is a
duplicate symbol, the error message should be displayed.
ii.Otherwise if the symbol is entered into the SYMTAB, along with the memory
address in which it is stored.
B) If there is an opcode in the line read.
i. Search the OPTAB to see if the opcode is present, if so increment the location
counter (LOCCTR) by three.
ii. a) If the opcode is WORD, increment the location counter by three.
SEC CSE/V SS LAB MANUAL
19
b) If the opcode is BYTE increment the location counter by one.
c) If opcode is RESW increment the location counter by the integer equivalent of
operand value *3.
d) If the opcode is RESB increment the location counter by the integer equivalent
of the operand value.
C) Write each and every line processed to the intermediate file along with their
Location counters.
5.Subtract the starting address of the program from the final value of the LOCCTR, to
Calculate the length of the program.
6.Close all the operand files and exit.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char label[10],inst[10],op1[10],op2[10];
char buff[10];
int a,b,loctr=0,ctr,flag,d[5];
FILE *fp1,*fp2,*fp3,*fp4;
clrscr();
fp1=fopen("INPUT.TXT","r");
fp2=fopen("OUTPUT.TXT","w");
fp3=fopen("OPTAB.TXT","r");
fp4=fopen("SYM.TXT","w");
fprintf(fp4,"SYMBOL TABLE\n");
fprintf(fp4,"LABEL\nADDRESS\n");
buff[0]='\0';
fscanf(fp1,"%s%s%s%s",label,inst,op1,op2);
if(strcmp(inst,"START")==0)
{
a=atoi(op1);
loctr=a;
fprintf(fp2,"\nADDRESS\tMNEMONICS");
fprintf(fp2,"\n%d\t%s\t%s\t%s\n",loctr,label,inst,op1);
SEC CSE/V SS LAB MANUAL
20
}
else
loctr=0;
while(!feof(fp1))
{
fscanf(fp1,"%s%s%s%s",label,inst,op1,op2);
if(strcmp(inst,"END")!=0)
{
if(strcmp(label,"$")!=0)
fscanf(fp4,"\n%s\t%d",label,loctr);
while(!feof(fp3))
{
fscanf(fp3,"%s%d",buff,&d);
if(strcmp(inst,buff)==0)
{
flag=1;
break;
}
}
b=atoi(op1);
if(strcmp(inst,"WORD")==0)
loctr+=3;
else if(strcmp(inst,"RESW")==0)
loctr+=3*b;
else if(strcmp(inst,"RESB")==0)
loctr+=b;
else if(strcmp(inst,"BYTE")==0)
loctr+=b;
else if(flag)
loctr+=3;
}
fprintf(fp2,"%d\t%s\t%s\t%s\n",loctr,label,inst,op1);
}
fprintf(fp2,"\n\nPRGLEN=%d\n",(loctr-a));
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
}
INPUT FILES:
SEC CSE/V SS LAB MANUAL
21
INPUT.TXT:
COPY START 1000 $
FIRST STL RETADR $
$ STA BUFFER $
BUFFER RESB 08 $
RETADR RESW 03 $
$ END FIRST $
OPTAB.TXT:
STA 14
STL 32
LDA 03
LTB 69
JSUB 48
JEQ 30
COM P 28
STH 34
SYM.TXT:
SYMBOL TABLE
LABEL ADDRESS
FIRST 1000
BUFFER 1006
RETADR 1014
OUTPUT FILE:
OUTPUT.TXT:
ADDRESS MNEMONICS
1000 COPY START 1000
1003 FIRST STL RETADR
1006 $ STA BUFFER
1014 BUFFER RESB 08
1023 RETADR RESW 03
1023 $ END FIRST
PRGLEN=23
RESULT
The program has been executed to the symbol table in C was implemented and the above
output is obtained
SEC CSE/V SS LAB MANUAL
22
VIVA QUESTIONS
1. Define the basic functions of assembler.
* Translating mnemonic operation codes to their machine language
equivalents.
* Assigning machine addresses to symbolic labels used by the
programmer.
2. What is meant by assembler directives? Give example.
These are the statements that are not translated into machine instructions, but they provide
instructions to assembler itself. example START,END,BYTE,WORD,RESW and RESB.
3. What is the need of SYMTAB (symbol table) in assembler?
The symbol table includes the name and value for each symbol in the source
program,together with flags to indicate error conditions. Some times it may contain details
about the data area. SYMTAB is usually organized as a hash table for efficiency of insertion
and retrieval.
4. What is the need of OPTAB (operation code table) in
assembler?
The operation code table contains the mnemonic operation code and its Machine language
equivalent. Some assemblers it may also contain information about instruction format and
length. OPTAB is usually organized as a hash table, with mnemonic operation code as the
key.
5.What is meant by one pass assembler?
A one pass assembler that generates their object code in memory for immediate execution
no object program is return out and the no loader is needed.
POSSILBLE QUESTIONS:
1.Implement of pass two of one pass assembler?
2.Implement of pass two assembler?
SEC CSE/V SS LAB MANUAL
23
EX.NO:3
DATE:
IMPLEMENT A TWO PASS OF A TWO PASS ASSEMBLER
DESCRIPTION
Translating mnemonic operation codes to their machine language
equivalents. Assigning machine addresses to symbolic labels used by the programmer two
pass .
OBJECTIVE
The main objective is to write a program to implement pass two of a two pass assembler.
HOW IT IS BEING ACHIEVED?
It is achieved by extracting character from the expression using built in functions in C, such
as SYMTAB,OPTAB etc
SYNTAXES & KEYWORDS
SYMTAB-The symbol table includes the name and value for each symbol in the
source program, together with flags to indicate error conditions
OPTAB-The operation code table contains the mnemonic operation code and its
Machine language equivalent
HOW TO EXECUTE THE PROGRAM
To compile an run the program.
REQUIREMENTS FOR EXECUTION
S.No. Facilities
required
Quantity
1 System 1
2 OS Turboc
SEC CSE/V SS LAB MANUAL
24
EXPECTED OUTPUT AND ITS FORM
This program takes a source program as input, and it is expected to produce a
Opcode and symbol table as output.
LIMITATIONS
Main problem: Forward references to both data and instructions
Forward jump to instruction items cannot be easily eliminated.
APPLICATIONS
Easy to implement the second pass of assembler.
operands to their machine addresses;
Use proper addressing modes and formats to build efficient machine
ALGORITHM
1.Read the first line from the intermediate file.
2.Check to see if the opcode from the first line read is START
If so then write the label,opcode and operand field values of the corresponding
statement directly to the final output file.
3.Start the following processing, for the other lines in the intermediate file if it is not a
comment line until an END statement is reached
i. Start writing the location counter,opcode and operand fields of the corresponding
statement to the output file, along with the object code. The object code is found
by assembling each statement opcodes machine equivalent with the label address.
ii. If there is no symbol/label in the operand field,then the operand address is assigned
as zero, and it is assembled with the object code of the instruction.
iii. If the opcode is BYTE,WORD,RESB,etc convert the constants to the object code.
4.Close the operand files and exit.
SEC CSE/V SS LAB MANUAL
25
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
FILE *fp1,*fp2,*fp3,*fp4;
void main()
{
char code[2],addr[6],buff[10],add[5],inst[10],op1[10],op2[10];
char symval[5],lab[10],obj[6],a[6],label[10];
int flag;
fp1=fopen("D1.TXT","r");
fp2=fopen("D2.TXT","w");
buff[0]='\0';
fscanf(fp1,"%s%s%s%s%s",&addr,&label,&inst,&op1,&op2);
strcpy(a,op1);
if(strcmp(inst,"START")==0)
{
fprintf(fp2,"\nADDRESS\tMNEMONICS\tINSTRUCTION\n");
fprintf(fp2,"%s\t%s\t%s\t%s\n",addr,label,inst,op1);
}
while(!feof(fp1))
{
flag=0;
fscanf(fp1,"%s%s%s%s%s",&addr,&label,&inst,&op1,&op2);
if(strcmp(inst,"END")!=0)
{
fp3=fopen("OPTAB.TXT","r");
while(!feof(fp3))
{
fscanf(fp3,"%s%s",buff,code);
if(strcmp(inst,buff)==0)
{
flag=1;
break;
}
}fclose(fp3);
if(flag)
{
if(strcmp(op1,"$")!=0)
{
SEC CSE/V SS LAB MANUAL
SEC CSE/V SS LAB MANUAL
26
fp4=fopen("SYM.TXT","r");
while(!feof(fp4))
{
fscanf(fp4,"%s%s",lab,add);
if(strcmp(op1,lab)==0)
{
strcpy(symval,add);
break;
}
}
}
else
strcpy(symval,"");
fclose(fp4);
}
if(strcmp(label,"$")==0||strcpy(label,""))
obj[0]=code[0];
obj[1]=code[1];
obj[2]=symval[0];
obj[3]=symval[1];
obj[4]=symval[2];
obj[5]=symval[3];
obj[6]='\0';
if((strcmp(inst,"WORD")==0)||(strcmp(inst,"BYTE")==0))
fprintf(fp2,"%s\t%s\t%s\t%s\t%s\n",addr,label,inst,op1,op2);
else if((strcmp(inst,"RESB")==0)||(strcmp(inst,"RESB")==0))
fprintf(fp2,"%s\t%s\t%s\t%s\t%s\n",addr,label,inst,op1,op2);
else
fprintf(fp2,"%s\t%s\t%s\t%s\t%s\n",addr,label,inst,symval,obj);
}
}
fprintf(fp2,"%s\t%s\t%s\t%s\t%s\n",addr,label,inst,op1,op2);
fclose(fp1);
fclose(fp2);
}
INPUT FILES:
SEC CSE/V SS LAB MANUAL
27
D2.TXT:
1000 COPY START 1000 $
1000 FIRST STL RETADR $
1003 $ STA BUFFER $
1006 BUFFER RESB 08 $
1014 RETADR RESW 03 $
1023 $ END FIRST $
OPTAB.TXT:
STA 14
STL 32
LDA 03
LDB 69
JSUD 48
JEQ 30
COMP 28
STCH 34
SYM.TXT:
SYMBOL TABEL
LABEL ADDRESS
FIRST 1000
BUFFER 1006
RETADR 1014
OUTPUT FILE:
D2.TXT:
ADDRESS MNEMONICS INSTRUCTION
1000 COPY START 1000
1000 STL 1014 321014
1003 $ STA 1006 141006
1006 RESB 08 $
1014 RESW 03 $
1023 $ END FIRST $
RESULT
SEC CSE/V SS LAB MANUAL
28
The program has been executed to the symbol table in C was implemented and the above
output is obtained
VIVA QUESTIONS:
1.What is meant by Two pass Assembler?
A two pass assembler is a translator that contains a conventional machine language
program. The assembler goes through the entire program one line at a time where a code is
generated with that line's instructions.
2. What are the three different records used in object program?
The header record, text record and the end record are the three different records used
in object program.The header record contains the program name, starting address and length
of the program.
Text record contains the translated instructions and data of the program.
End record marks the end of the object program and specifies the address
in the program where execution is to begin.
3. Write the steps required to translate the source program to
object program.
. Convert mnemonic operation codes to their machine language
equivalents.
. Convert symbolic operands to their equivalent machine addresses
. Build the machine instruction in the proper format.
. Convert the data constants specified in the source program into their
internal machine
representation
. Write the object program and assembly listing.
POSSIBLE QUESTIONS:
1. Implement a two pass of a one pass assembler?
2. Implement a pass one assembler?
SEC CSE/V SS LAB MANUAL
29
EX.NO:4
DATE:
IMPLEMENT A SINGLE PASS ASSEMBLER.
DESCRIPTION
Describes the function of object structure, symbol structure and the code structure
OBJECTIVE
It is achieved by extracting character from the expression using built in functions in C, such
as struct obj,struct sym,struct mot functions
SYNTAXES & KEYWORDS
Struct obj-describes the structure of the object
Struct sym-describes the structure of the symbol
Struct mot-describes the stucture of the code
HOW TO EXECUTE THE PROGRAM
To compile and run the program.
REQUIREMENTS FOR EXECUTION
S.No. Facilities
required
Quantity
1 System 1
2 OS Turboc
SEC CSE/V SS LAB MANUAL
30
EXPECTED OUTPUT AND ITS FORM
This program takes a source program as input, and it is expected to produce a
Opcode structure and symbol structure and the object structure as output.
LIMITATIONS
Two-pass assemblers create a table with all symbols and their values in the first pass,
then use the table in a second pass to generate code.
The assembler must at least be able to determine the length of each instruction on the
first pass so that the addresses of symbols can be calculated.
APPLICATIONS
It shares the program only once
Creates a equivalent binary program
The assembler substitute all the symbolic instructions with in the machine code
ALGORITHM
1.Set the structure functions for object,code and for symbol
2.Set the charcter values for all the functions
3.Create the main functions and set the pointer values for eacf variables
4.Check the case condition using the switch statement
5.Read the character and the code ,object to get the no of instructions.
6.Get the lable code operandsand also check the else if condition with WORD,BYTE,RESW
etc
7.Finally give the source file for getting the answer.
8.Close all the files and exit.
SEC CSE/V SS LAB MANUAL
31
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct obj /* object structure */
{
int address2;
char label2[10];
char op2[10];
char oper2[10];
char object2[10];
};
struct sy /* symbol structure */
{
char lab[10];
int ad;
};
struct mot /* opcode structure */
{
char mne[6];
int size;
char op[3];
}m[]={{"LDA",3,"00"},
{"STA",3,"0C"},
{"SUB",2,"1C"},
{"ADD",2,"18"},
SEC CSE/V SS LAB MANUAL
32
{"J",3,"3C"}};
void create(); /* function declaration */
FILE *sr,*ad,*sy; /* file pointer declaration */
void main() /* main program*/
{
int choice;
clrscr();
do
{
printf("\n1.Create\n2.Exit");
printf("\nEnter the choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:create();break;
default : exit(0);
}
{
struct obj ob[100];
struct sy s[10];
int tot,i,j,add,loc=0,z=0,x,y,x1,ad2;
char l[10],la[10],op[10],oper[10],object[20],add1[10],la2[10];
clrscr();
printf("\nEnter Total number of instruction : ");
scanf("%d",&tot);
sr=fopen("source.txt","w");
ad=fopen("address.txt","w");
SEC CSE/V SS LAB MANUAL
33
sy=fopen("symbol.txt","w");
printf("Enter the Label Opcode Operand\n");
for(i=0;i<tot;i++)
{
scanf("%s%s%s",la,op,oper);
fprintf(sr,"%s\t%s\t%s\n",la,op,oper);
if(strcmp(op,"START")==0)
{
add=atoi(oper);
2
}
}
fclose(sr);
sr=fopen("source.txt","r");
ob[0].address2=add;
while(fscanf(sr,"%s\t%s\t%s",la,op,oper)!=EOF)
{
for(j=0;j<5;j++)
{
if(strcmp(la,"-")!=0)
{
strcpy(ob[z].label2,la);
strcpy(s[z].lab,ob[z].label2);
}
else
{
strcpy(ob[z].label2,NULL);
SEC CSE/V SS LAB MANUAL
34
strcpy(s[z].lab,ob[z].label2);
}
if(strcmp(op,m[j].mne)==0)
{
ob[z].address2=ob[z-1].address2+m[j].size;
s[z].ad=ob[z].address2;
strcpy(ob[z].object2,m[j].op);
strcpy(ob[z].op2,op);
strcpy(ob[z].oper2,oper);
break;
}
else if(strcmp(op,"WORD")==0)
{
ob[z].address2=ob[z-1].address2+3;
s[z].ad=ob[z].address2;
strcpy(ob[z].object2,oper);
strcpy(ob[z].op2,op);
strcpy(ob[z].oper2,oper);
break;
}
else if(strcmp(op,"BYTE")==0)
{
ob[z].address2=ob[z-1].address2+1;
s[z].ad=ob[z].address2;
strcpy(ob[z].object2,oper);
strcpy(ob[z].op2,op);
strcpy(ob[z].oper2,oper);
SEC CSE/V SS LAB MANUAL
35
break;
}
else if(strcmp(op,"RESW")==0)
{
ob[z].address2=ob[z-1].address2+3*atoi(oper);
s[z].ad=ob[z].address2;
strcpy(ob[z].object2,"0");
strcpy(ob[z].op2,op);
strcpy(ob[z].oper2,oper);
break;
}
else if(strcmp(op,"RESB")==0)
{
ob[z].address2=ob[z-1].address2+atoi(oper);
s[z].ad=ob[z].address2;
strcpy(ob[z].object2,"0");
strcpy(ob[z].op2,op);
strcpy(ob[z].oper2,oper);
break;
}
}
z++;
}
3
for(x1=0;x1<z;x1++)
{
if(strcmp(ob[x1].label2,NULL)!=0)
SEC CSE/V SS LAB MANUAL
36
{
fprintf(sy,"%d\t%s\n",s[x1-1].ad,s[x1].lab);
}
}
fclose(sy);
sy=fopen("symbol.txt","r");
for(x=1;x<z;x++)
{
fscanf(sy,"%d\t%s",&ad2,la2);
for(y=0;y<z;y++)
{
if(strcmp(ob[x].oper2,la2)==0)
{
strcat(ob[x].object2,itoa(ad2,add1,10));
break;
}
}
fprintf(ad,"%d\t%s\t%s\t%s\t%s\n",
ob[x-1].address2,ob[x].label2,ob[x].op2,ob[x].oper2,ob[x].object2);
}
fclose(ad);
fclose(sr);
}
INPUT
SOURCE.TXT
- START 200
- LDA AL
SEC CSE/V SS LAB MANUAL
37
- STA BL
- ADD CL
AL BYTE 2
BL RESW 3
CL RESB 4
OUTPUT
SYMBOL.TXT
208 AL
209 BL
218 CL
ADDRESS.TXT
200 LDA AL 00208
203 STA BL 0C209
206 ADD CL 18218
208 AL BYTE 2 2
209 BL RESW 3 0
218 CL RESB 4 0
RESULT
The program has been executed to the symbol table in C was implemented and the above
output is obtained
VIVA QUESTION:
1. Define load and go assembler.
One pass assembler that generates their object code in memory for Immediate execution is
known as load and go assembler. Here no object programmer is written out and hence no
need for loader.
SEC CSE/V SS LAB MANUAL
38
2. What are the two different types of jump statements used in
MASM assembler?
. Near jump
A near jump is a jump to a target in the same segment and it is assembled by using a current
code segment CS.
. Far jump
A far jump is a jump to a target in a different code segment and it is assembled by using
different segment registers
18. What is meant by machine independent assembler features?
The assembler features that do not depend upon the machine architecture are known as
machine independent assembler features.
Eg: program blocks, Literals.
POSSIBLE QUESTION:
1.Implement the single pass assembler for finding the size of the located variable?
SEC CSE/V SS LAB MANUAL
39
EX.NO:5
DATE:
IMPLEMENT A TWO PASS MACRO PROCESSOR
DESCRIPTION
Describes the function of macro processor
OBJECTIVE
The main objective is to write a program to implement two pass macro processor
HOW IT IS BEING ACHIEVED?
It is achieved by extracting character from the expression using built in functions in C, such
as fgets,strcmp.
SYNTAXES & KEYWORDS
fgets-get the file from the loader
Strcmp-compare the string
HOW TO EXECUTE THE PROGRAM
To compile and run the program.
REQUIREMENTS FOR EXECUTION
S.No. Facilities
required
Quantity
1 System 1
2 OS Turboc
EX.NO:6
DATE:
IMPLEMENT A SINGLE PASS MACRO PROCESSOR
SEC CSE/V SS LAB MANUAL
46
EXPECTED OUTPUT AND ITS FORM
This program takes a source program as input, and it is expected to produce a
macro processor and symbol structure and the object structure as output.
LIMITATIONS
The size of the program. The reason is, the pre-processor will replace all the macros
in the program by its real definition prior to the compilation process of the program.
APPLICATIONS
Macro processors have been developed for some high-level programming languages
These special-purpose macro processors are similar in general function and approach;
however, the details differ from language to language
ALGORITHM
1.Get the statement from the input file.
2.From the line read,check if the opcode is the directive MACRO ,if so then the
number of macro n must be incremented.
3.Repeat step 1 and 2 until an end of the file is encountered.
4.Open n number of files in write mode. These files will later hold the body of the n
macros respectively.
5.Rewind the input file pointer.
6.If the opcode is MACRO
i. Enter the macro name present in the operand field into the array name m.
ii. Write the line to the expanded output file.
iii. Enter lines in the body of each macro into the corresponding files already opened in
step 4.
iv. Write the body of each macro to the expanded output file also, until a mend is
reached.
7.Otherwise if the opcode is call the line must be a macro invocation statement so, the
SEC CSE/V SS LAB MANUAL
47
macro body is retrieved from the corresponding file and written to the expanded
output file.
8.Write all the remaining lines directly to the expanded file.
PROGRAM
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
char ch,c,str[8],str1[10],str2[10],if1[12],ps[2],ka[2];
FILE *fi,*fo,*md,*mf,*fp;
int co=0,i=0,flag;
void main()
{
void mac();
void file();
clrscr();
printf("\nEnter the input file name:");
scanf("%s",&if1);
fp=fopen(if1,"r");
printf("\nContent of the file\n");
while(!feof(fp))
{
fgets(str,'\n',fp);
printf("%s",str);
}
printf("\n\nPress enter to see output");
getch();
fi=fopen(if1,"r");
printf("\n**********OUTPUT**********\n\n");
while(!feof(fi))
{
fscanf(fi,"%s",str);
co=0;
file();
if(co==0)
mac();
else
{
printf("%s",str);
if((c=fgetc(fi))=='\n')
{
SEC CSE/V SS LAB MANUAL
48
do
{
c=fgetc(fp);
if(c==-1)
goto loop;
}
while(c!=-1);
}
while((c=fgetc(fi))!='\n')
printf("%s\n",c);
}
}
loop:
fclose(fi);
fclose(fp);
getch();
}
void file()
{
mf=fopen("MNE1.TXT","r");
while(!feof(mf))
{
fscanf(mf,"%s",str1);
if(strcmp(str,str1)==0)
co++;
while((ch=fgetc(mf))!='\n')
{
if(ch==-1)
{
return;
}
}
fclose(mf);
}
}
void mac()
{
md=fopen("MAC.TXT","r");
while(!feof(md))
{
SEC CSE/V SS LAB MANUAL
49
fscanf(md,"%s",str2);
if(strcmp(str,str2)==0)
{
fgets(str2,'\n',md);
while(strcmp(str2,"END\n")!=0)
{
printf("%s",str2);
fgets(str2,'\n',md);
}
return;
}
else
{
flag=1;
}
}
if(flag==1)
printf("%s",str);
fclose(md);
getch();
}
INPUT FILES:
IP.TXT:
LDA 02
ADD I
STA 2000
SUB I
STA 3000
HLT
MAC.TXT:
LDA
MOV
ADD
STA
HLT
OUTPUT:
SEC CSE/V SS LAB MANUAL
50
Enter the input file name:IP.TXT
Content of the file
LDA 02
ADD I
STA 2000
SUB I
STA 3000
HLT
Press enter to see output
*************OUTPUT************
I
MOV A,B
ADD A
MOV B,A
ADD A
STA 2000
MOV C,V
SUB C
MOV B,A
ADD A
STA 3000
VIVA QUESTION:
1.What is meant by macro processor?
A macro instruction (abbreviated to macro) is simply a notational convenience for the
programmer.
A macro represents a commonly used group of statements in the source programming
language
2.What is the basic macro functions?
Basic Macro Processor Functions
Macro invocations and subroutine calls are different
Note also that the macro instructions have been written so that the body of the macro
contains no label
It is easy to design a two-pass macro processor
Pass 1:
SEC CSE/V SS LAB MANUAL
51
All macro definitions are processed
Pass 2:
All macro invocation statements are expanded
3.what is two pass macro processor?
A two-pass macro processor would not allow the body of one macro instruction to contain
definitions of other macros.
4.What are the features of Machine independent macro processor?
Machine-Independent Macro
Processor Feature
Concatenation of Macro Parameters
Generation of Unique Labels
Conditional Macro Expansion
Keyword Macro Parameters
POSSIBLE QUESTION:
1.Implement a one pass of single macro processor?
2.Implement a two pass of single macro processor?
SEC CSE/V SS LAB MANUAL
52
DESCRIPTION
Describes the functions of loader that brings the object program into memory for execution
OBJECTIVE
It is achieved by extracting character from the expression using built in functions in C, such
as numtok,struct loccont.
SYNTAXES & KEYWORDS
Numtok-it generates the token number
Struc loc cont- structure of the local content varaible
HOW TO EXECUTE THE PROGRAM
To compile and run the program
REQUIREMENTS FOR EXECUTION
S.No. Facilities
required
Quantity
1 System 1
2 OS Turboc
EXPECTED OUTPUT AND ITS FORM
This program takes a source program as input, and it is expected to produce a
loader function of absolute loader as output.
EX.NO:7
DATE:
IMPLEMENT AN ABSOLUTE LOADER.
SEC CSE/V SS LAB MANUAL
53
LIMITATIONS
The programmer must specify the assembler the address where the
program is to be loaded.
If there are multiple subroutines , the programmer must remember the
address of each
APPLICATIONS
The source program translators produce compatible object program deck formats and it is
possible to write subroutines in several different languages since the object decks to be
processed by the loader will all be in the same language that is in machine language.
ALGORITHM:
1.Read header record and filter the starting location and other details.
2.Read first text record.
3.Do the following processing until an end record is reached.
i. If object code is in character form convert it to internal hexadecimal representation.
ii. Move object codes to specified locations in memory.
iii. Write the starting location counter value of a block of object code,and the
corresponding internal hexadecimal representations to the output file.
iv. Read next text record from the input file.
4.Close all the opened files and exit.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
char line[5][20];
void numtok();
int cur=0;
SEC CSE/V SS LAB MANUAL
54
int lth;
void main()
{
FILE *fin;
int i=0,m=0,k=0,l,j;
int prgstart,prglen,textaddr,disp;
char str[70];
struct loccontent
{
int addr;
char content[3];
} mem[200];
clrscr();
for(i=0;i<200;++i)
{
mem[i].content[0]='x';
mem[i].content[1]='x';
mem[i].content[2]='\0';
}
fin=fopen("loadin.txt","r");
while(!feof(fin))
{
fgets(str,70,fin);
numtok(str);
if(strcmp(line[0],"H")==0)
{
printf("program name is %s\n",line[1]);
prgstart=atoi(line[2]);
prglen=atoi(line[3]);
j=0;
for(i=prgstart;i<=prgstart+prglen;++i)
{
mem[j].addr=i;
j++;
}
}
else if(strcmp(line[0],"T")==0)
{
textaddr=atoi(line[1]);
disp=textaddr-prgstart;
for(m=3;m<lth;++m)
{
for(i=0;i<5;i+=2)
SEC CSE/V SS LAB MANUAL
55
{
mem[disp].addr=textaddr++;
mem[disp].content[0]=line[m][i];
mem[disp].content[1]=line[m][i+1];
mem[disp].content[2]='\0';
disp++;
}
}
}
else if(strcmp(line[0],"E")==0)
break;
k=0;
}
for(i=0;i<prglen;i+=16)
{
printf("%d",mem[k].addr);
for(l=0;l<4;l++)
{
for(j=0;j<4;j++)
{
printf("%c%c",mem[k].content[0],mem[k].content[1]);
k++;
}
printf("\t");
}
printf("\n");
}
getch();
}
void numtok(char str[70])
{
int i=0,j=0,k=0;
while(str[j]!='\0')
{
line[i][k]=str[j];
if(str[j]=='^')
{
line[i][k]='\0';
k=-1;
i++;
}
SEC CSE/V SS LAB MANUAL
56
k++;
j++;
}
line[i][k]='\0';
lth=i+1;
}
INPUT FILE:
LOADIN.TXT:
H^COPY^1000^115
T^1000^12^141033^482039^001036^281030
T^1012^9^345890^581056^454645
T^1000^12^141033^482039^001036^281030
E^1000
OUTPUT:
program name is COPY
100014103348 20390010 36281030 34589058
101610564546 45xxxxxx xxxxxxxx xxxxxxxx
1032xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
1048xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
1064xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
1080xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
1096xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
1112xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
RESULT
The program has been executed to the symbol table in C was implemented and the above
output is obtained
VIVA QUESTION:
1. What are the basic functions of loaders?
. Loading brings the object program into memory for execution
SEC CSE/V SS LAB MANUAL
57
. Relocation modifies the object program so that it can be loaded at an
Address different from the location originally specified
. Linking combines two or more separate object programs and also
supplies the information needed to reference them.
2. Define absolute loader.
The loader, which is used only for loading, is known as absolute loader.
e.g. Bootstrap loader
3. What is meant by bootstrap loader?
This is a special type of absolute loader which loads the first program to
be run by the computer. (usually an operating system)
4.Functions of Absolute loader?
There are four functions involving in the Absolute loading
Allocation
Linking
Relocation
Loading
POSSIBLE QUESTION:
1. Implement an bootsrap loader?
2. Implement an bootstarp loader with functions?
SEC CSE/V SS LAB MANUAL
58
DESCRIPTION
Describes the functions of a loader that brings the object program into memory for execution
for relocating the variable
OBJECTIVE
It is achieved by extracting character from the expression using built in functions in C, such
as numtok,struct loccont.
SYNTAXES & KEYWORDS
Numtok-it generates the token number
Struc loc cont- structure of the local content varaible
HOW TO EXECUTE THE PROGRAM
To compile and run the program.
REQUIREMENTS FOR EXECUTION
S.No. Facilities
required
Quantity
1 System 1
2 OS Turboc
EX.NO:8
DATE:
IMPLEMENT A RELOCATING LOADER.
SEC CSE/V SS LAB MANUAL
59
EXPECTED OUTPUT AND ITS FORM
This program takes a source program as input, and it is expected to produce a
loader function of relocating loader as output.
LIMITATIONS
The BSS loader allows many procedure segments but only one data
(common)segment.
The BSS loader scheme is often used on computers with a fixed-length
direct address instruction format.
APPLICATIONS
These segments need to be corrected, when the executable has been loaded
into memory. The EXE loader uses a relocation table to find the segments
which the data is loaded in to the memory.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
char line[5][20];
void numtok();
int cur=0;
int lth;
void main()
{
FILE *fin;
int i=0,m=0,k=0,t=0,l,j,prevsize;
int prgstart,prglen,textaddr,disp;
char str[70];
struct loccontent
{
int addr;
char content[3];
} mem[200];
clrscr();
for(i=0;i<200;++i)
{
mem[i].content[0]='x';
mem[i].content[1]='x';
mem[i].content[2]='\0';
}
SEC CSE/V SS LAB MANUAL
60
fin=fopen("loadin.txt","r");
while(!feof(fin))
{
fgets(str,70,fin);
numtok(str);
if(strcmp(line[0],"H")==0)
{
printf("program name is %s\n",line[1]);
prgstart=random(10000);
prglen=atoi(line[3]);
j=0;
for(i=prgstart;i<=prgstart+prglen;++i)
{
mem[j].addr=i;
j++;
}
}
else if(strcmp(line[0],"T")==0)
{
if(t==0)
{
textaddr=prgstart;
t=1;
}
disp=textaddr-prgstart;
for(m=3;m<lth;++m)
{
for(i=0;i<5;i+=2)
{
mem[disp].addr=textaddr++;
mem[disp].content[0]=line[m][i];
mem[disp].content[1]=line[m][i+1];
mem[disp].content[2]='\0';
disp++;
}
}
}
else if(strcmp(line[0],"E")==0)
break;
k=0;
}
for(i=0;i<prglen;i+=16)
{
SEC CSE/V SS LAB MANUAL
61
printf("%d\t",mem[k].addr);
for(l=0;l<4;l++)
{
for(j=0;j<4;j++)
{
printf("%c%c",mem[k].content[0],mem[k].content[1]);
k++;
}
printf("\t");
}
printf("\n");
}
getch();
}
void numtok(char str[70])
{
int i=0,j=0,k=0;
while(str[j]!='\0')
{
line[i][k]=str[j];
if(str[j]=='^')
{
line[i][k]='\0';
k=-1;
i++;
}
k++;
j++;
}
line[i][k]='\0';
lth=i+1;
}
INPUT FILE:
LOADIN.TXT:
H^COPY^1000^115
T^1000^12^141033^482039^001036^281030
T^1012^9^345890^581056^454645
T^1000^12^141033^482039^001036^281030
E^1000
OUTPUT:
SEC CSE/V SS LAB MANUAL
62
program name is COPY
105 14103348 20390010 36281030 34589058
121 10564546 45141033 48203900 10362810
137 30xxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
153 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
169 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
185 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
201 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
217 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
RESULT
The program has been executed to the symbol table in C was implemented and the above
output is obtained
VIVA QUESTION:
1. What are relative (relocative) loaders?
Loaders that allow for program relocation are called relocating (relocative) loaders.
2. What is the use of modification record?
Modification record is used for program relocation. Each modification record specifies the
starting address and the length of the field whose value is to be altered and also
describes the modification to be performed.
3. What are the 2 different techniques used for relocation?
Modification record method and relocation bit method.
4. Define Relocation bit method.
If the relocation bit corresponding to a word of object code is set to 1, the programs
starting address is to be added to this word when the program is relocated. Bit value 0
indicates no modification is required.
SEC CSE/V SS LAB MANUAL
63
POSSIBLE QUESTION:
1.To implement the relocation loader with static function?
2.To implement the relocation loader with dynamic function?
SEC CSE/V SS LAB MANUAL
64
DESCRIPTION
Describes the functions of loader that brings the object program into memory for execution
for loading the variable with in direct linking of one pass.
OBJECTIVE
The main objective is to write a program to implement pass one of a direct linking loader
HOW IT IS BEING ACHIEVED?
It is achieved by extracting character from the expression using built in functions in C, such
as prgmaddr,csaddr
SYNTAXES & KEYWORDS
prgmaddr-specifies the address of the program
csaddr-specifies the count address
HOW TO EXECUTE THE PROGRAM
To compile and run the program.
REQUIREMENTS FOR EXECUTION
S.No. Facilities
required
Quantity
1 System 1
2 OS Turboc
EXPECTED OUTPUT AND ITS FORM
This program takes a source program as input, and it is expected to produce a
loader function of direct-linking loader as output.
EX.NO:9
DATE:
IMPLEMENT PASS ONE OF A DIRECT-LINKING LOADER.
SEC CSE/V SS LAB MANUAL
65
LIMITATIONS
Allocate, relocate, link, and load all the subroutines each time in order to
execute a program.
Furthermore, even though loader program smaller than assembler, it
absorb considerable amount of memory.
APPLICATIONS
The list of symbols not defined in the current segment but used in the current
segment are stored in a data structure called USE table.
The lists of symbols defined in the current segment and referred by the other
segments are stored in a data structure called DEFINITION table.
Used in both list and definition table.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
#include<string.h>
void main()
{
int progaddr,csaddr;
char s1[2],s2[10],s3[10],s4[10];
FILE *fp,*fp1,*fp2;
clrscr();
printf("\n\tENTER THE STARTING ADDRESS\n\n\t");
scanf("%d",&progaddr);
csaddr=progaddr;
fp=fopen("OBJECT.TXT","r");
fp1=fopen("ESTAB.TXT","w");
fp2=fopen("ADDRESS.TXT","w");
fprintf(fp2,"%d",progaddr);
fclose(fp2);
fscanf(fp,"%s\t%s\t%s\t%s",s1,s2,s3,s4);
while(!feof(fp))
{
int cs1st=atoi(s3);
fprintf(fp1,"%s\t_\t%d\t%s\t%s\n",s2,csaddr,s3);
SEC CSE/V SS LAB MANUAL
66
do
{
fscanf(fp,"%s\t%s\t%s\t%s",s1,s2,s3,s4);
if(strcmp(s1,"D")==0)
{
int value=csaddr+atoi(s3);
fprintf(fp1,"_\t%s\t%d\t_\n",s2,value);
}
}
while(strcmp(s1,"E")!=0);
csaddr=csaddr+cs1st;
fscanf(fp,"%s\t%s\t%s\t%s",s1,s2,s3,s4);
}
fclose(fp);
fclose(fp1);
printf("\n\tPROGRAM EXECUTED SUCCESSFULLY");
getch();
}
OUTPUT:
ENTER THE STARTING ADDRESS
2025
PROGRAM EXECUTED SUCCESSFULLY
INPUT FILE:
OBJECT.TXT:
H PROGA 9 _
D A1 6 _
D A2 9 _
T 0000 000006 _
T 0003 010006 _
T 0006 000006 _
M 0006 06 _B1
M 0006 06 +B2
E 0000 _ _
H PROGB 9 _
D B1 6 _
D B2 9 _
T 0000 000006 _
T 0003 010006 _
T 0006 000006 _
SEC CSE/V SS LAB MANUAL
67
M 0006 06 _A1
M 0006 06 +A2
E 0000 _ _
OUTPUT FILES:
ADDRESS.TXT:
2025
ESTAB.TXT:
PROGA _ 2025 9
_ A1 2031 _
_ A2 2034 _
PROGB _ 2034 9
_ B1 2040 _
_ B2 2043 _
RESULT
The program has been executed to the symbol table in C was implemented and the above
output is obtained
VIVA QUESTION:
1. Write the two passes of a linking loader.
Pass1: assigns address to all external symbols
Pass2: it performs actual loading, relocation and linking.
2.what is meant by direct linking loader?
A direct-linking loader is a relocatable loader.
3.Advantages of direct linking loader?
o It has advantage of allowing programmer multiple procedure segments and multiple
data segments.
o Complete freedom in referencing data or instructions contained in other segments,
provides flexible intersegment referencing.
4. Give the functions of the linking loader.
The linking loader performs the process of linking and relocation. It includes the
operation of automatic library search and the linked programs are directly oaded into the
memory.
SEC CSE/V SS LAB MANUAL
68
15. Give the difference between linking loader and linkageeditors.
Linking loader Linkage editor
The relocation and linking is performed each time the program is loaded It produces a linked
version of a program and which is written in a file for later execution Here the loading can be
accomplished in a single pass Two passes are required
Possible Question:
1.To implement the direct linking loader without the pass values?
2.How to implement the loader with pass values?
SEC CSE/V SS LAB MANUAL
69
DESCRIPTION
Describes the functions of loader that brings the object program into memory for execution
for loading the variable with in direct linking of two pass.
OBJECTIVE
The main objective is to write a program to implement pass two of a direct linking loader
HOW IT IS BEING ACHIEVED?
It is achieved by extracting character from the expression using built in functions in C, such
as csaddr,execaddr,cslth
SYNTAXES & KEYWORDS
execaddr-specifies the address of the execution
csaddr-specifies the count address
HOW TO EXECUTE THE PROGRAM
To compile:
To run:
REQUIREMENTS FOR EXECUTION
S.No. Facilities
required
Quantity
1 System 1
2 OS Turboc
EXPECTED OUTPUT AND ITS FORM
This program takes a source program as input, and it is expected to produce a
loader function of direct-linking loader as output.
EX.NO:10
DATE:
IMPLEMENT PASS TWO OF A DIRECT-LINKING LOADER.
SEC CSE/V SS LAB MANUAL
70
LIMITATIONS
Allocate, relocate, link, and load all the subroutines each time in order to
execute a program.
Furthermore, even though loader program smaller than assembler, it
absorb considerable amount of memory.
APPLICATIONS
The list of symbols not defined in the current segment but used in the current
segment are stored in a data structure called USE table.
The lists of symbols defined in the current segment and referred by the other
segments are stored in a data structure called DEFINITION table.
Used in both list and definition table.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int search(char sym[10]);
char progaddr[10];
int data[100],mem[100],q,d=0,value,j,k;
int csaddr,execaddr,cslth;
char type[10],addr[10],i,obj[10],mflag[10],op;
void main()
{
FILE *in,*address;
in=fopen("OBJECT.TXT","r");
address=fopen("ADDRESS.TXT","r");
clrscr();
fscanf(address,"%s",progaddr);
fclose(address);
csaddr=atoi(progaddr);
execaddr=atoi(progaddr);
fscanf(in,"%s\t%s\t%s\t%s",type,addr,obj,mflag);
while(!feof(in))
{
cslth=atoi(obj);
while(strcmp(type,"E")!=0)
{
SEC CSE/V SS LAB MANUAL
71
fscanf(in,"%s\t%s\t%s\t%s\n",type,addr,obj,mflag);
if(strcmp(type,"T")==0)
{
mem[i]=atoi(addr)+csaddr;
data[i]=atoi(obj);
i++;
}
else if(strcmp(type,"M")==0)
{
op=mflag[0];
for(j=1;j<strlen(mflag);j++)
mflag[j-1]=mflag[j];
mflag[j-1]='\0';
value=search(mflag);
q=d+atoi(addr)/3;
if(op=='+')
data[q]=data[q]+value;
else
data[q]=data[q]-value;
}
}
d=cslth/3;
execaddr=csaddr+atoi(addr);
csaddr=csaddr+cslth;
fscanf(in,"%s\t%s\t%s\t%s\n",type,addr,obj,mflag);
}
fclose(in);
q=(execaddr-(atoi(progaddr)))/3;
printf("\PROGRAM LOADED IN MEMORY IS READY FOR EXECUTION\n");
printf("EXECUTION STARTING ADDRESS:%d\n",execaddr);
printf("ADDRESS\tDATA\n");
for(j=0;j<i;j++)
printf("%d\t%d\n",mem[j],data[j]);
gotoxy(1,q+4);
getch();
}
int search(char sym[10])
{
FILE *fp;
char s1[10],s2[10],s3[10],s4[10];
fp=fopen("ESTAB.TXT","r");
while(!feof(fp))
{
fscanf(fp,"%s\t%s\t%s\t%s\n",s1,s2,s3,s4);
SEC CSE/V SS LAB MANUAL
72
if(strcmp(s2,sym)==0)
{
fclose(fp);
return(atoi(s3));
}
}
fclose(fp);
return 0;
}
INPUT FILE:
OBJECT.TXT:
H PROGA 9 _
D A1 6 _
D A2 9 _
T 0000 000006 _
T 0003 010006 _
T 0006 000006 _
M 0006 06 _B1
M 0006 06 +B2
E 0000 _ _
H PROGB 9 _
D B1 6 _
D B2 9 _
T 0000 000006 _
T 0003 010006 _
T 0006 000006 _
M 0006 06 _A1
M 0006 06 +A2
E 0000 _
OUTPUT:
PROGRAM LOADED IN MEMORY IS READY FOR EXECUTION
EXECUTION STARTING ADDRESS:2509
ADDRESS DATA
2500 6
2503 10006
2506 6
2509 6
2512 10006
2515 6
SEC CSE/V SS LAB MANUAL
73
RESULT
The program has been executed in C was implemented and the above output is obtained
VIVA QUESTION:
1.Define dynamic linking.
If the subroutine is loaded and linked to the program during its first call(run time),
then it is called as dynamic loading or dynamic linking.
2. Write the advantage of dynamic linking.
. It has the ability to load the routine only when they are needed.
. The dynamic linking avoids the loading of entire library for each execution.
3. What is meant by static executable and dynamic executable?
In static executable, all external symbols are bound and ready to run. In dynamic
executables some symbols are bound at run time.
4. What is shared and private data?
The data divided among processing element is called shared data. If the data is not
shared among processing elements then it is called private data.
POSSIBLE QUESTION:
1.How to get the ADDR function in direct linking loader?
2.To implement the direct linking loader without the pass values?
SEC CSE/V SS LAB MANUAL
74
DESCRIPTION
Using of simple text editor with insert and delete the character ,word and sentence
OBJECTIVE:
The main objective is to write a program to implement a simple text editor with features
like insertion /deletion of a character,word and sentence.
HOW IT IS BEING ACHIEVED?
It is achieved by extracting character from the expression using built in functions in C, such
as create(), append(), view(),delete();
SYNTAXES & KEYWORDS
Append()-To overwrite the character
View()-To view the character
Delete()- To delete the character
HOW TO EXECUTE THE PROGRAM
To compile and run the program.
REQUIREMENTS FOR EXECUTION
S.No. Facilities
required
Quantity
1 System 1
2 OS Turboc
EXPECTED OUTPUT AND ITS FORM
This program takes a source program as input, and it is expected to produce the
insert/delete the sentence,word,character as output.
EX.NO:11
DATE:
IMPLEMENT A SIMPLE TEXT EDITOR WITH FEATURES
LIKE INSERTION / DELETION OF A CHARACTER, WORD,
AND SENTENCE.
SEC CSE/V SS LAB MANUAL
75
LIMITATIONS
Easy to implement the mentioned character but not at the same time.
APPLICATIONS :
Easy to use in various application
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<process.h>
int i,j,ec,fg,ec2,c1;
FILE *fp1,*fp2,*fp3;
char fn[10],e,ch,c,n;
void create();
void append();
void view();
void delete();
void main()
{
clrscr();
do
{
printf("\n MENU\n");
printf("\n1.CREATE \n2.VIEW \n3.DELETE \n4.EXIT\n");
printf("Enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
view();
break;
case 3:
delete();
break;
default:
exit(0);
}
}
SEC CSE/V SS LAB MANUAL
76
while(1);
}
void create()
{
fp1=fopen("exam.c","w");
printf("\n Enter the data and press z to stop\n");
while(1)
{
c=getchar();
if(c!='z')
{
fputc(c,fp1);
}
else
{
fclose(fp1);
printf("Enter the new file name:\n");
scanf("%s",&fn);
fp1=fopen("exam.c","r");
fp2=fopen(fn,"w");
while(!feof(fp1))
{
c=getc(fp1);
putc(c,fp2);
}
fclose(fp2);
break;
}
}
}
void view()
{
printf("Enter the filename:\n");
scanf("%s",&fn);
fp1=fopen(fn,"r");
if(fp1==NULL)
{
printf("File not found");
goto l1;
}
while(!feof(fp1))
{
c=getc(fp1);
printf("%c",c);
SEC CSE/V SS LAB MANUAL
77
}
l1:fclose(fp1);
getch();
}
void delete()
{
printf("Enter the file name");
scanf("%s",&fn);
fp1=fopen(fn,"r");
if(fp1==NULL)
{
printf("file not found");
goto end3;
}
if (remove(fn)==0)
{
printf("File deleted");
goto end2;
}
else
printf("ERROR\n");
end2:getch();
end3:fclose(fp1);
}
OUTPUT:
MENU
1.CREATE
2.VIEW
3.DELETE
4.EXIT
Enter your choice1
Enter the data and press z to stop
GOPI
SRI
z
Enter the new file name:
sathis
MENU
SEC CSE/V SS LAB MANUAL
78
1.CREATE
2.VIEW
3.DELETE
4.EXIT
Enter your choice2
Enter the filename:
sathis
GOPI
SRI
MENU
1.CREATE
2.VIEW
3.DELETE
4.EXIT
Enter your choice3
Enter the file namesathis
File deleted
MENU
1.CREATE
2.VIEW
3.DELETE
4.EXIT
Enter your choice4
RESULT
The program has been executed in C was implemented and the above output is obtained
VIVA QUESTION:
1. Define interactive editor?
An interactive editor is a computer program that allows a user to create
and revise a target document. The term document includes objects such as computer
programs, text, equations, tables, diagrams, line art, and photographs any thing that onemight
find on a printed page.
2. What are the tasks performed in the editing process?
4 tasks
. Select the part of the target document to be viewed and manipulated.
. Determine how to format this view on-line and how to display it.
. Specify and execute operations that modify the target document.
. Update the view appropriately.
SEC CSE/V SS LAB MANUAL
79
3. What are the three categories of editors devices?
. Text device/ String devices
. Button device/Choice devices
. Locator device
4. What is the function performed in editing phase?
In the actual editing phase, the target document is created or altered with
a set of operations such as insert, delete, replace, move and copy.
5. Define Locator device?
Locator devices are two-dimensional analog-to-digital converters that
position a cursor symbol on the screen by observing the users movement of the
device. The most common such devices for editing applications are the mouse and the data
tablet.
POSSIBLE QUESTION:
1.How to view the character ?
2.How to inset a word and sentence?
SEC CSE/V SS LAB MANUAL
80
DESCRIPTION
Using of simple table with maintain the hashing function.
OBJECTIVE:
The main objective is to write a program to implement a simple table with suitable
hashing
HOW IT IS BEING ACHIEVED?
It is achieved by extracting character from the expression using built in functions in C, such
as isalpha(c),size,addr etc
SYNTAXES & KEYWORDS
isalpha-To check the character
size-size of the file
addr-address of the file
HOW TO EXECUTE THE PROGRAM
To compile and run program.
REQUIREMENTS FOR EXECUTION
S.No. Facilities
required
Quantity
1 System 1
2 OS Turboc
EX.NO:12
DATE:
IMPLEMENT A SYMBOL TABLE WITH SUITABLE HASHING.
SEC CSE/V SS LAB MANUAL
81
EXPECTED OUTPUT AND ITS FORM
This program takes a source program as input, and it is expected to produce the
as output with hashing function.
LIMITATIONS
Easy to implement the mentioned character but not at the same time.
APPLICATIONS :
Easy to use in various application
ALGORITHM:
1. Start the program.
2. Create symbol table ,Enter the choice of your operations.
3. If more data is to be entered in to the same table hash function is used.
4. Get the data type,name,element size and the their address of the element.
5. Store the elements is a array format.
6. End the program
PROGRAM:
# include <stdio.h>
# include <conio.h>
# include <ctype.h>
# include <dos.h>
# include <iostream.h>
# include <fstream.h>
void main()
{
int i,j,k,n,add=0,size;
char c,in[20],temp[20],val[20],var[20];
FILE*fp;
clrscr();
fp=fopen("input.c","r");
c=getc(fp);
printf('\n THE INPUT FILE IS:")
while(!feof(fp))
{
printf("%c",c);
c=getc(fp);
}
SEC CSE/V SS LAB MANUAL
82
fclose(fp);
printf("\n**************symbol table**************");
printf("\n DAta type\tName\tNo.of Elements\tSize\tAddress");
printf("************************************");
while(!feof(fp))
{
if(isalpha(c))
{
i=0;
do
{
temp[i==]=c
c=getc(fp);
}while(isalpha(c));
temp[i]='\0';
if(strcmp("int",temp)==0)
{
strcpy(val," ");
strcpy(in," ");
strcpy(var," ");
while(c!=';')
{
strcpy(val," ");
c=getc(fp);
i=0;
do
{
var[i++]=c;
c=getc(fp);
}while(isalpha(c));
var[i]='\0';
if(c=='[')
{
i=0;
c=getc(fp);
strcpy(in," ")
do
{
in[i++]=c;
c=getc(fp);
k=atoi(in);
size=size*k;
}
else
{
strcpy(in,'1');
size=2;
}
if(c=='=')
c=getc(fp);
SEC CSE/V SS LAB MANUAL
83
i=0;
do
{
val[i++]=c;
c=getc(fp);
}while(c!=','&&c!=';')
val[i]='\0';
}
printf("\n%s\t\t%s\t%s\t\t%s\t%dBytes\t%d",temp,var,val,in,size,add);
add=add+size;
}
}
}
else
c=getc(fp);
}
getch();
}
SAMPLE OUTPUT:
INPUT FILE:
20
15
OUTPUT FILE:
NAME SAMP
ADDR SDR
SIZE 58
RESULT
The program has been executed in C was implemented and the above output is obtained
VIVA QUESTION:
1. Define interactive debugging systems.
An interactive debugging system provides programmers with facilities
that aid in the testing and debugging of programs.
. Debugging functions and capabilities
. Relationship with other parts of the system
SEC CSE/V SS LAB MANUAL
84
. User interface criteria.
2. What is meant by static executable and dynamic executable?
In static executable, all external symbols are bound and ready to run. In
dynamic executables some symbols are bound at run time.
3. What is shared and private data?
The data divided among processing element is called shared data. If the
data is not shared among processing elements then it is called private data.
POSSIBLE QUESTION:
1.How to implement the symbol table using hash function for modify and search?
2. How to implement the symbol table using hash function for delete?
SEC CSE/V SS LAB MANUAL
85
EXPERIMENTS BEYOND THE SYLLABUS
TOKEN SEPERATION
AIM:
To implement the C program to perform the functions of a token seperation.
ALGORITHM:
1. Declare the variables.
2. Enter the source program as an input.
3. Declare and initialize the keywords like int, if, then, else, printf.
4. Declare and initialize the operators like +,-,*,/
5. Declare and initialize the punctuation as (),{},[].
call the function tokenseparation to recognize the tokens present in the source
program.
6. Compare the input token(keyword ) with the keywords declared
print the token as a keyword if the keyword is matched with the keywords declared
7. Compare the input token(operator) with operators declared
if it is matched, print that token as an operator.
8. Compare the input token(punctuation) with the punctuation declared.
9. Print the token as punctuation if any input is an punctuation.
10. Compare the given token with identifier then print that token as an identifier
call the function printtokens () to print the token.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int iskey(char *str)
SEC CSE/V SS LAB MANUAL
86
{
FILE *fp1;
char str1[20];
fp1=fopen("KEY.C","r");
do
{
fscanf(fp1,"%s",str1);
if(!strcmp(str1,str))
{
fclose(fp1);
return 1;
}
}
while (!feof(fp1));
fclose(fp1);
return 0;
}
int isoper(char d)
{
if (d=='<'||d=='{'||d=='>'||d=='*'||d=='=')
return 1;
return 0;
}
int issymbol(char c)
{
if(c=='{'||c=='}'||c=='('||c==')'||c==':'||c=='#'||c==','||c==';')
return 1;
return 0;
}
main()
{
FILE *fp1,*fp2;
int no1,no2,no3,no4,no5,i,flag,choice;
char key[15][15],ident[20][15],cons[20][10],oper[20],symbol[20];
char str1[20],chr,fname;
printf ("\nEnter the file name:");
scanf("%s",fname);
fp1=fopen("INPUT.C","r");
fp2=fopen("OUTPUT.TXT","w");
no1=no2=no3=no4=no5=0;
while(!feof(fp1))
{
SEC CSE/V SS LAB MANUAL
87
i=0;
fscanf(fp1,"%c",&chr);
if(chr>='a'&&chr<='z')
{
str1[i+1]=chr;
while(chr>='a'&&chr<='z')
{
fscanf(fp1,"%c",&chr);
if(chr>='a'&&chr<='z')
str1[i++]=chr;
}
str1[i]='\0';
if(iskey(str1))
{
flag=0;
for(i=0;i<no1;i++)
{
if(!strcmp(str1,key[i]))
flag=1;
}
if(flag!=1)
strcpy(key[no1++],str1);
}
else if (strcmp(str1,"define")==0)
{
fscanf(fp1,"%s",cons[no2++]);
}
else
{
flag=0;
for(i=0;i<no3;i++)
{
if(!strcmp(str1,ident[i]))
flag=1;
}
if(flag!=1)
strcpy(ident[no3++],str1);
}
}
if(isoper(chr))
{
SEC CSE/V SS LAB MANUAL
88
flag=0;
for(i=0;i<no4;i++)
{
if(chr==oper[i])
flag=1;
}
if(flag!=1)
oper[no4++]=chr;
}
else if (issymbol(chr))
{
flag=0;
for(i=0;i<no5;i++)
{
if(chr==symbol[i])
flag=1;
}
if (flag!=1)
{
symbol[no5++]=chr;
}
}
}
fclose(fp1);
fprintf(fp2,"\nkeywords");
for(i=0;i<no1;i++)
{
fprintf(fp2,"\n%s",key[i]);
}
fprintf(fp2,"\nconstants");
for(i=0;i<no2;i++)
{
fprintf(fp2,"\n%s",cons[i]);
}
fprintf(fp2,"\n\nidentifiers");
for(i=0;i<no3;i++)
{
fprintf(fp2,"%s",ident[i]);
}
fprintf(fp2,"\n\noperators");
for(i=0;i<no4;i++)
{
fprintf(fp2,"\nspecial charecters");
}
SEC CSE/V SS LAB MANUAL
89
for(i=0;i<no5;i++)
{
fprintf(fp2,"\n%c",symbol[i]);
}
fclose(fp2);
do
{
printf("\nmenu\n1.keywords\n2.constants\n3.identifiers");
printf("\n 4.operators\n5.special characters\n6.exit");
printf("\n enter your choice:");
switch(choice)
{
case 1:
printf("\n Keywords\n");
for(i=0;i<no1;i++)
{
printf("\n%s",key[i]);
}
break;
case 2:
printf("\nConstants\n");
for(i=0;i<no2;i++)
{
printf("\n%s",cons[i]);
}
break;
case 3:
printf("\nIdentifiers\n");
for(i=0;i<no3;i++)
{
printf("\n%s",ident[i]);
}
break;
case 4:
printf("\nOperators\n");
for(i=0;i<no4;i++)
{
printf("\n%c",oper[i]);
}
break;
case 5:
printf("\nspecial characters\n");
for(i=0;i<no5;i++)
{
SEC CSE/V SS LAB MANUAL
90
printf("\n%c",symbol[i]);
}
break;
case 6:
break;
}
}
while (choice!=6);
return 0;
}
INPUT FILES:
INPUT.C
#include<stdio.h>
#define pi 3.14
void main()
{
float radius,area;
printf("\nEnter the radious:");
scanf("%f",&radius);
area=pi*radius;
printf("The area is:%f",area);
}
OUTPUT:
Identifiers
Include stdio.h main radious area printf scanf pi
Constants
Pi
Operators
< > = *
Special charecters
# , ; () {}
Key words
Void float
RESULT:
Thus the above program executed and verified successfully.
SEC CSE/V SS LAB MANUAL
91
FILE MANIPULATION
Aim:
To write a C Program to create an employee database & to calculate net and gross salary
using files.
Algorithm:
1. Enter name salary and deduction for employees in a text file.
2. In a C program create a structure for employees.
3. The members are name salary & deduction..
4. Open the files in read mode and get the values of salary and deduction
5. Calculate the net & gross salary
6. Close the file.
1. Open a new file in write mode and write the values of name, salary, deduction into it.
2. Print the database from the file and close it.
PROGRAM
# include <stdio.h>
# include <conio.h>
void main()
{
char c ;
FILE *fptr1 ;
clrscr() ;
printf("Enter the text to be stored in the file.\n") ;
printf("Use ^Z or F6 at the end of the text and press
ENTER: \n\n") ;
fptr1 = fopen("COURSES.DAT","w") ;
while((c = getc(stdin)) != EOF)
fputc(c, fptr1) ;
SEC CSE/V SS LAB MANUAL
92
fclose(fptr1) ;
printf("\nThe content of the file is : \n\n") ;
fptr1 = fopen("COURSES.DAT", "r") ;
do
{
c = fgetc(fptr1) ;
putchar(c) ;
} while(c != EOF) ;
fclose(fptr1) ;
getch() ;
}
RESULT:
Thus the above program executed and verified successfully.
SEC CSE/V SS LAB MANUAL
93
LENGTH OF MACRO
Aim:
To create a program in c implementing files to find the macro length of a file.
Algorithm:
1.Create and open a file in read mode.
2. Type words between macro and mend.
3.Save the file and open it.
4.Read its content.
5. Increment the counter variable till the string comparison is not equal to zero.
6.Decrement the counter variable once is equal to zero.
7.Print the counter variable which is number of lines.
8.End.
Program
#include<stdio.h>
#define LENGTH 3
#define WIDTH 2
int main()
{
int r,c;
for(r=1; r<=LENGTH; r++)
{
for(c=1; c<=WIDTH; c++)
printf("%d%d",c,r);
printf("\n");
}
getch();
return 0;
}
OUTPUT:
Length-153
WIDTH-89
RESULT:
Thus the above program executed and verified successfully.