Ss Lab Manual-cs2304
description
Transcript of Ss Lab Manual-cs2304
IMPLEMENTATION OF SYMBOL TABLE EX. NO : 1 DATE:
Aim: To write a C program to implement Symbol Table for performing insert, display, delete, search and modify.
Algorithm:
Step 1:Start the program for performing insert, display, delete, search and modify option in symbol
table.
Step 2: Define the structure of the Symbol Table.
Step 3: Enter the choice for performing the operations in the symbol Table.
Step 4: If the entered choice is 1, search the symbol table for the symbol to be inserted.
If the symbol is already present, it displays “Duplicate Symbol”. Else, insert the symbol and the
corresponding address in the symbol table.
Step 5: If the entered choice is 2, the symbols present in the symbol table are displayed.
Step 6: If the entered choice is 3, the symbol to be deleted is searched in the symbol table. If it is not
found in
the symbol table it displays “Label Not found”. Else, the symbol is deleted.
Step 7: If the entered choice is 5, the symbol to be modified is searched in the symbol table. The label or
address or both can be modified.
- 1 -
Source Code:
# include <stdio.h># include <conio.h># include <alloc.h># include <string.h># define null 0
int size=0;
void insert(); //Function declarations// void del(); int search(char lab[]); void modify(); void display();
struct symbtab{ char label[10]; int addr; struct symtab *next; };
struct symbtab *first,*last;
void main() //Main function Start//{ int op; int y; char la[10]; clrscr();do{ printf("\nSYMBOL TABLE IMPLEMENTATION\n"); printf("1. INSERT\n"); printf("2. DISPLAY\n"); printf("3. DELETE\n"); printf("4. SEARCH\n"); printf("5. MODIFY\n"); printf("6. END\n"); printf("Enter your option : "); scanf("%d",&op);
switch(op){case 1:insert();display();break;case 2:
- 2 -
display();break;case 3:del();display();break;case 4:printf("Enter the label to be searched : ");scanf("%s",la);y=search(la);if(y==1){printf("The label is already in the symbol Table");}else{printf("The label is not found in the symbol table");}break;case 5:modify();display();break;case 6:break;}}while(op<6);getch();} //Main function end//
void insert() //Function definitions// {int n;char l[10];printf("Enter the label : ");scanf("%s",l);n=search(l);if(n==1){printf("The label already exists. Duplicate cant be inserted\n");}else{struct symbtab *p;p=malloc(sizeof(struct symbtab));strcpy(p->label,l);printf("Enter the address : ");scanf("%d",&p->addr);p->next=null;if(size==0){first=p;last=p;}
- 3 -
else{last->next=p;last=p;}size++;}}
void display(){int i;struct symbtab *p;p=first;printf("LABEL\tADDRESS\n");for(i=0;i<size;i++){printf("%s\t%d\n",p->label,p->addr);p=p->next;}}int search(char lab[]){int i,flag=0;struct symbtab *p;p=first;for(i=0;i<size;i++){if(strcmp(p->label,lab)==0){flag=1;}p=p->next;}return flag;}
void modify( ){char l[10],nl[10];int add, choice, i, s;struct symbtab *p;p=first;printf("What do you want to modify?\n");printf("1. Only the label\n");printf("2. Only the address of a particular label\n");printf("3. Both the label and address\n");printf("Enter your choice : ");scanf("%d",&choice);switch(choice){case 1:printf("Enter the old label\n");scanf("%s",l);printf("Enter the new label\n");
- 4 -
scanf("%s",nl);s=search(l);if(s==0){printf("NO such label");}else{for(i=0;i<size;i++){if(strcmp(p->label,l)==0){strcpy(p->label,nl);}p=p->next;}}break;case 2:printf("Enter the label whose address is to modified\n");scanf("%s",l);printf("Enter the new address\n");scanf("%d",&add);s=search(l);if(s==0){printf("NO such label");}else{for(i=0;i<size;i++){if(strcmp(p->label,l)==0){p->addr=add;}p=p->next;}}break;case 3:printf("Enter the old label : ");scanf("%s",l);printf("Enter the new label : ");scanf("%s",nl);printf("Enter the new address : ");scanf("%d",&add);s=search(l);if(s==0){printf("NO such label");}else{for(i=0;i<size;i++){
- 5 -
if(strcmp(p->label,l)==0){strcpy(p->label,nl);p->addr=add;}p=p->next;}}break;}}
void del( ){int a;char l[10];struct symbtab *p,*q;p=first;printf("Enter the label to be deleted\n");scanf("%s",l);a=search(l);if(a==0){printf("Label not found\n");}else{if(strcmp(first->label,l)==0){first=first->next;}else if(strcmp(last->label,l)==0){q=p->next;while(strcmp(q->label,l)!=0){p=p->next;q=q->next;}p->next=null;last=p;}else{q=p->next;while(strcmp(q->label,l)!=0){p=p->next;q=q->next;}p->next=q->next;}size--;}}
- 6 -
Sample Output:
SYMBOL TABLE IMPLEMENTATION
1. INSERT 2. DISPLAY 3. DELETE 4. SEARCH 5. MODIFY 6. END Enter your option: 1
Enter the label :A
Enter the address :5
The variable should be inserted.
SYMBOL TABLE IMPLEMENTATION 1. INSERT 2. DISPLAY 3. DELETE 4. SEARCH 5. MODIFY
Enter your option :1
Enter the label :B
Enter the address :10
The variable should be inserted.
Enter your option: 2
Lable address
A 5
B 10
SYMBOL TABLE IMPLEMENTATION
1. INSERT 2. DISPLAY 3. DELETE 4. SEARCH 5. MODIFY
Enter your option: 1
Enter the label: B
Enter the address: 10
- 7 -
The label already exists. Duplicate cant be inserted
SYMBOL TABLE IMPLEMENTATION 1. INSERT 2. DISPLAY 3. DELETE 4. SEARCH 5. MODIFY
Enter your option: 5
What do you want to modify1. Only the label2. Only the address of a particular label3. Both the label and address
Enter your choice: 1
Enter old label:B
Enter new label:G
Enter your option: 2
Lable address
A 5
B 10
G 10
RESULT:
- 8 -
IMPLEMENTATION OF PASS ONE OF TWO PASS ASSEMBLER
Ex. No : 2 Date:
Aim:
To write a C program to implement PASS ONE of a two pass assembler.
Algorithm:Step 1:Open the files fp1 and fp4 in read mode and fp2 and fp3 in write mode
Step 2:Read the source program
Step 3:If the opcode read in the source program is START, the variable location counter is
initialized with the operand value.
Step 4: Else the location counter is initialized to 0.
Step 5: The source program is read line by line until the reach of opcode END.
Step 6: Check whether the opcode read is present in the operation code table.
Step 7: If the opcode is present, then the location counter is incremented by 3.
Step 8: If the opcode read is WORD, the location counter is incremented by3.
Step 9: If the opcode read is RESW, the operand value is multiplied by 3 and then the location
counter is incremented.
Step 10: If the opcode read is RESB, the location counter value is incremented by operand
value.
Step 11: If the opcode read is BYTE, the location counter is auto incremented.
Step 12: The length of the source program is found using the location counter value.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char opcode[10],mne[3],operand[10],label[10],code[10];
int loc,start,len;
FILE *fp1,*fp2,*fp3,*fp4;
clrscr();
fp1=fopen("input.dat","r");
- 9 -
fp2=fopen("sym.dat","w");
fp3=fopen("out.dat","w");
fp4=fopen("optab.dat","r");
fscanf(fp1,"%s%s%s",label,opcode,operand);
if(strcmp(opcode,"START")==0)
{
start=atoi(operand);
loc=start;
fprintf(fp3,"\t%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp1,"%s%s%s",label,opcode,operand);
}
else
loc=0;
while(strcmp(opcode,"END")!=0)
{
fprintf(fp3,"%d\t",loc);
if(strcmp(label,"**")!=0)
fprintf(fp2,"%s\t%d\n",label,loc);
fscanf(fp4,"%s%s",code,mne);
while(strcmp(code,"END")!=0)
{
if(strcmp(opcode,code)==0)
{
loc+=3;
break;
}
fscanf(fp4,"%s%s",code,mne);
}
if(strcmp(opcode,"WORD")==0)
- 10 -
loc+=3;
else if(strcmp(opcode,"RESW")==0)
loc+=(3*(atoi(operand)));
else if(strcmp(opcode,"RESB")==0)
loc+=(atoi(operand));
else if(strcmp(opcode,"BYTE")==0)
++loc;
fprintf(fp3,"%s\t%s\t%s\n”,label,opcode,operand);
fscanf(fp1,"%s%s%s",label,opcode,operand);
}
fprintf(fp3,"%d\t%s\t%s\t%s\n",loc,label,opcode,operand);
len=loc-start;
printf("Length of the program is %d",len);
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
getch();
}
- 11 -
INPUT FILE:
Input.dat
** START2000
** LDA FIVE
** STA ALPHA
** LDCH CHARZ
** STCH C1
ALPHA RESW 1
FIVE WORD 5
CHARZ BYTE C'Z'
C1 RESB 1
** END **
Optab.dat
START *
LDA 03
STA 0F
LDCH 53
STCH 57
END *
- 12 -
OUTPUT FILE:
Length of the program is 20.
Sym.dat
ALPHA 2012
FIVE 2015
CHARZ 2018
C1 2019
Out.dat
** START2000
2000 ** LDA FIVE
2003 ** STA ALPHA
2006 ** LDCH CHARZ
2009 ** STCH C1
2012 ALPHA RESW 1
2015 FIVE WORD 5
2018 CHARZ BYTE C’Z’
2019 C1 RESB 1
2020 ** END **
RESULT:
- 13 -
IMPLEMENTATION PROGRAM FOR PASS 2 OF TWO PASS ASSEMBLER
Ex. No : 3 Date:
Aim: To implement of pass two of two pass assembler.
Algorithm:
Step 1:Start the programStep 2:Initialize all the variablesStep 3:Open a file
Step 4:Read the content of the fileStep 5:If opcode is BYTE then write address, label, opcode, operand into a file
Else if opcode is WORD then write address, label, opcode, operand into a file Else if opcode is “RESB” or “RESW” write address, label, opcode, operand into a file
if it is not match anything then
write address, label, opcode, operand into a fileStep 6:Finally terminate the of pass two of pass two assembler.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE *fp1,*fp2,*fp3,*fp4;
char label[10],opcode[10],operand[10],symbol[10],code[10];
char mnemonic[5],character,add[10],objectcode[10];
int flag,flag1,locctr,location,loc;
clrscr();
fp1=fopen("out.dat","r");
fp2=fopen("p2out.dat","w");
fp3=fopen("optab.dat","r");
- 14 -
fp4=fopen("symtab.dat","r");
if(fp1==NULL)
{
printf("The file cannot be open");
}
if(fp2==NULL)
{
printf("The file cannot be open");
}
if(fp3==NULL)
{
printf("The file cannot be open");
}
if(fp4==NULL)
{
printf("The file cannot be open");
}
fscanf(fp1,"%s%s%s",label,opcode,operand);
if(strcmp(opcode,"START")==0)
{
fprintf(fp2,"%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp1,"%d%s%s%s",&locctr,label,opcode,operand);
}
while(strcmp(opcode,"END")!=0)
{
flag=0;
fscanf(fp3,"%s%s",code,mnemonic);
while(strcmp(code,"END")!=0)
{
- 15 -
if((strcmp(opcode,code)==0)&&(strcmp(mnemonic,"*"))!=0)
{
flag=1;
break;
}
fscanf(fp3,"%s%s",code,mnemonic);
}
if(flag==1)
{
flag1=0;
rewind(fp4);
while(!feof(fp4))
{
fscanf(fp4,"%s%d",symbol,&loc);
if(strcmp(symbol,operand)==0)
{flag=1;
break;
}}
if(flag1==1)
{
itoa(loc,add,10);
strcpy(objectcode,strcat(mnemonic,add));
}
}
else if(strcmp(opcode,"BYTE")==0 || strcmp(opcode,"WORD")==0)
{
if((operand[0]=='C')||(operand[0]=='X'))
{
character=operand[2];
- 16 -
itoa(character,add,16);
strcpy(objectcode,add);
}
else
{
itoa(atoi(operand),add,10);
strcpy(objectcode,add);
}
}
strcpy(objectcode,"\0");
fprintf(fp2,"%s\t%s\t%s\t%s\n",label,opcode,operand,locctr,objectcode);
fscanf(fp1,"%d%s%s%s",&locctr,label,opcode,operand);
}
fprintf(fp2,"%s\t%s\t%s\n",label,opcode,operand,locctr);
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
getch();
}
- 17 -
INPUT FILE:
Out.dat
** START2000
2000 ** LDA FIVE
2003 ** STA ALPHA
2006 ** LDCH CHARZ
2009 ** STCH C1
2012 ALPHA RESW 1
2015 FIVE WORD 5
2018 CHARZ BYTE C’Z’
2019 C1 RESB 1
2020 ** END **
Optab.dat
START *
LDA 03
STA 0F
LDCH 53
STCH 57
END *
Symtab.dat
ALPHA 2012
FIVE 2015
CHARZ 2018
C1 2019
- 18 -
OUTPUT FILE:
P2out.dat
** START2000
** LDA FIVE 2000 032015
** STA ALPHA 2003 0F2012
** LDCH CHARZ 2006 532018
** STCH C1 2009 572019
ALPHA RESW 1 2012
FIVE WORD 5 2015 5
CHARZ BYTE C'Z' 2018 5a
C1 RESB 1 2019
** END ** 2020
RESULT:
- 19 -
IMPLEMENTATION OF SINGLE PASS ASSEMBLER Ex. No : 4 Date:
Aim: To implement of Single pass an assembler.
Algorithm:
Step 1: Start the program
Step 2.Initialize all the variables
Step 3.Open a files for read and write.
Step 4: Read the content from the input file.
Step 5. Write the object code with address to the output file.
Step 6: Finally terminate the of pass two of pass two assembler.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 10
struct input
{
char label[10],opcode[10],operand[10],mnemonic[5];
int loc;
};
struct input table[MAX];
struct symtab
{
char sym[10];
int f,val,ref;
};
- 20 -
struct symtab symtbl[MAX];
void main()
{
int f1,i=1,j=1,flag,locctr,x;
char add[10],code[10],mnemcode[5];
FILE *fp1,*fp2,*fp3;
clrscr();
fp1=fopen("input.dat","r");
fp2=fopen("optab.dat","r");
fp3=fopen("spout.dat","w");
fscanf(fp1,"%s%s%s",table[i].label,table[i].opcode,table[i].operand);
if(strcmp(table[i].opcode,"START")==0)
{
locctr=atoi(table[i].operand);
i++;
fscanf(fp1,"%s%s%s",table[i].label,table[i].opcode,table[i].operand);
}
else locctr=0;
while(strcmp(table[i].opcode,"END")!=0)
{
if(strcmp(table[i].label,"**")!=0)
{
for(x=1;x<j;x++)
{
f1=0;
if((strcmp(symtbl[x].sym,table[i].label)==0) && (symtbl[x].f==1))
{
symtbl[x].val=locctr;
- 21 -
symtbl[x].f=0;
table[symtbl[x].ref].loc=locctr;
f1=1;
break;
}
}
if(f1==0)
{
strcpy(symtbl[j].sym,table[i].label);
symtbl[j].val=locctr;
symtbl[j].f=0;
j++;
}
}
fscanf(fp2,"%s%s",code,mnemcode);
while(strcmp(code,"END")!=0)
{
if(strcmp(table[i].opcode,code)==0)
{
strcpy(table[i].mnemonic,mnemcode);
locctr+=3;
for(x=1;x<=j;x++)
{
flag=0;
if(strcmp(table[i].operand,symtbl[x].sym)==0)
{
flag=1;
if(symtbl[x].f==0)
table[i].loc=symtbl[x].val;
- 22 -
break;
}
}
if(flag!=1)
{
strcpy(symtbl[j].sym,table[i].operand);
symtbl[j].f=1;
symtbl[j].ref=i;
j++;
}
}
fscanf(fp2,"%s%s",code,mnemcode);
}
rewind(fp2);
if(strcmp(table[i].opcode,"WORD")==0)
{
locctr+=3;
strcpy(table[i].mnemonic,'\0');
table[i].loc=atoi(table[i].operand);
}
else if(strcmp(table[i].opcode,"RESW")==0)
{
locctr+=(3*(atoi(table[i].operand)));
strcpy(table[i].mnemonic,'\0');
table[i].loc=atoi('\0');
}
else if(strcmp(table[i].opcode,"RESB")==0)
{
locctr+=(atoi(table[i].operand));
- 23 -
strcpy(table[i].mnemonic,'\0');
table[i].loc=atoi('\0');
}
else if(strcmp(table[i].opcode,"BYTE")==0)
{
++locctr;
if((table[i].operand[0]=='C')||(table[i].operand[0]=='X'))
table[i].loc=(int)table[i].operand[2];
else
table[i].loc=locctr;
}
i++;
fscanf(fp1,"%s%s%s",table[i].label,table[i].opcode,table[i].operand);
}
for(x=1;x<=i;x++)
fprintf(fp3,"%s\t%s\t%s\t%s\n",table[x].label,table[x].opcode,table[x].operand,strcat(table[x].mnemonic,itoa(table[x].loc,add,10)));
for(x=1;x<j;x++);
printf("%s\t%d\n",symtbl[x].sym,symtbl[x].val);
getch();
}
- 24 -
INPUT FILE:
Input.dat
** START6000
** JSUB CLOOP
** JSUB RLOOP
ALPHA WORD 23
BETA RESW 3
GAMMA BYTE C'Z'
DELTA RESB 4
CLOOP LDA ALPHA
RLOOP STA BETA
** LDCH GAMMA
** STCH DELTA
** END **
Optab.dat
START *
JSUB 48
LDA 14
STA 03
LDCH 53
STCH 57
END *
- 25 -
OUTPUT:
Spout. dat
** START6000 0
** JSUB CLOOP 486023
** JSUB RLOOP 486026
ALPHA WORD 23 23
BETA RESW 3 0
GAMMA BYTE C'Z' 90
DELTA RESB 4 0
CLOOP LDA ALPHA 146006
RLOOP STA BETA 036009
** LDCH GAMMA 536018
** STCH DELTA 576019
** END ** 0
RESULT:
- 26 -
IMPLEMENTATION OF MACROPROCESSOREx. No : 5 Date:
Aim : To perform the Simple Macro processor using c program .
Algorithm:Step 1:Start the macro processor program
Step 2:Include the necessary header files and variable
Step 3:Open the three files
Step 4:Read the variable until the opcode is not is equal to zero
Step 5:Then check if the opcode is equal to Macro if Macro
Then
Copy macro name=label
Step 6:Get the variable label ,opcode ,operand
In these if condition perform the while loop until opcode is not equal to MEND
Step 7:Copy the variable
Step 8:else if opcode is equal to macro name
Step 9:Perform the for loop from 0 to length
Step 10:else if it is not match
write label, opcode, operand in to a file
Step 11:Finally terminate the program
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char n1,n,c1,i;
char fn[10][10],ilab[20],iopd[20],m[20][3],oper[20],opd[20];
FILE *fp1,*fp2,*p[5];
clrscr();
n=0;
- 27 -
fp1=fopen("macin.txt","r");
while(!feof(fp1))
{
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
if(strcmp(iopd,"macro")==0)
n++;
}
printf("No of macros=%d\n",n);
n1=n;
printf("Enter the text filename\n");
for(i=0;i<n;i++)
{
scanf("%s",fn[i]);
p[i]=fopen(fn[i],"w");
}
n=0;
rewind(fp1);
while(!feof(fp1))
{
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
if(strcmp(iopd,"macro")==0)
{
strcpy(m[n],oper);
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
while(strcmp(iopd,"mend")!=0)
{
fprintf(p[n],"%s %s %s\n",ilab,iopd,oper);
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
}
- 28 -
fclose(p[n]);
n++;
}}
for(i=0;i<n1;i++)
p[i]=fopen(fn[i],"r");
fp2=fopen("outm.txt","w");
rewind(fp1);
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
while(!feof(fp1))
{
if(strcmp(iopd,"call")==0)
{ for(i=0;i<n1;i++)
{ if(strcmp(m[i],oper)==0)
{
rewind(p[i]);
fscanf(p[i],"%s%s%s",ilab,iopd,oper);
while(!feof(p[i]))
{
fprintf(fp2,"%s %s %s\n",ilab,iopd,oper);
c1=1;
fscanf(p[i],"%s%s%s",ilab,iopd,oper);
}break;
} } }
if(c1!=1)
fprintf(fp2,"%s %s %s\n",ilab,iopd,oper);
c1=0;
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
}fprintf(fp2,"%s %s %s\n",ilab,iopd,oper);
}
- 29 -
INPUT FILE:
Macin.txt
** macro m1
** move a,b
** mend ---
** macro m2
** lda b
** mend ---
** start 1000
** lda a
** call m1
** call m2
** add a,b
- 30 -
OUTPUT FILE:
No of macros=2
Enter the text filename
macrom1.txt
macrom2.txt
Macrom1.txt
** move a,b
Macrom2.txt
** lda b
Outm.txt
** macro m1
** move a,b
** mend ---
** macro m2
** lda b
** mend ---
** start 1000
** lda a
** move a,b
** lda b
** add a,b
RESULT:
- 31 -
IMPLEMENTATION OF RELOCATING LOADEREx. No : 6 Date:
Aim: To perform the Relocation Loader using c program.
Algorithm:Step 1:Start the programStep 2:Include the necessary header file and variableStep 3:Open the two file for read and write.Step 4:Read the contentStep 5:Using while loop perform the loop until character is not equal to EStep 6:If the character is HStep 7:Get the variable add, length, and inputStep 8:Else if the character is TStep 9:Get the variable address and bitmask And perform the for loop for starting zero to up to lenGet the opcode ,addr and assign relocbit to bitmaskIf relocabit is zeroThenAssign actualadd to addr;elseAdd the addr and star value. Finally terminate the program.
SOURCE CODE
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct object_code
{
int locctr;
char add[10];
}obcode[300];
void main()
{
char input[100][16],output[100][16],binary[20],address[20],stloc[10];
int len,bitmask,loc,tlen=0,tloc,textloc,i=0,location,j,k,count=0,start,n,num=0,inc=0;
FILE *fp1,*fp2;
clrscr();
fp1=fopen("linput.dat","r");
- 32 -
fp2=fopen("reloadout.dat","w");
printf("Enter the location where the program has to be loaded:");
scanf("%s",stloc);
start=atoi(stloc);
location=start;
tloc=start;
fscanf(fp1,"%s",input[i]);
while(strcmp(input[i],"T")!=0)
{
strcpy(output[i],input[i]);
i++;
fscanf(fp1,"%s",input[i]);
strcpy(output[i],input[i]);
}
itoa(start,output[2],10);
while(strcmp(input[i],"E")!=0)
{
strcpy(output[i],input[i]);
if(strcmp(input[i],"T")==0)
{
for(j=0;j<3;j++)
{
i++;
fscanf(fp1,"%s",input[i]);
strcpy(output[i],input[i]);
}
bitmask=atoi(output[i]);
itoa(bitmask,binary,2);
strcpy(output[i],NULL);
- 33 -
textloc=atoi(output[i-2]);
textloc=textloc+start;
itoa(textloc,output[i-2],10);
for(n=0;n<(textloc-(tloc+tlen));n++)
{
strcpy(obcode[inc].add,"xx");
obcode[inc++].locctr=location++;
}
tlen=atoi(output[i-1]);
tloc=textloc;
k=0;
}
else
{
if(binary[k]==1)
{
num=0;
len=strlen(output[i]);
strcpy(address,NULL);
for(j=2;j<len;j++)
{
address[num]=output[i][j];
output[i][j]='\0';
num++;
}
loc=atoi(address);
loc=loc+start;
itoa(loc,address,10);
strcat(output[i],address);
- 34 -
}
k++;
len=strlen(output[i]);
num=0;
for(n=0;n<len;n++)
{
obcode[inc].add[num++]=output[i][n];
if(num>1)
{
obcode[inc++].locctr=location++;
num=0;
}
}
}
i++;
fscanf(fp1,"%s",input[i]);
}
strcpy(output[i],input[i]);
i++;
fscanf(fp1,"%s",input[i]);
loc=atoi(input[i]);
loc=loc+start;
strcpy(output[i],itoa(loc,address,10));
count=0;
i=0;
n=0;
fprintf(fp2,"%d\t",obcode[n].locctr);
for(n=0;n<inc;n++)
{
- 35 -
fprintf(fp2,"%s",obcode[n].add);
i++;
if(i>3)
{
fprintf(fp2,"\t");
i=0;
count++;
}
if(count>3)
{
fprintf(fp2,"\n%d\t",obcode[n+1].locctr);
count=0;
}
}
getch();
}
- 36 -
INPUT FILE
Linput.dat
H COPY 000000 00107A
T 000001 1E FFC 140033 481039 O00036 280030 300015 481061 3C0003 00002A 0C0039 00002D
T 00001E 15 E00 0C0036 481061 080033 4C0000 454F46 000003 000000
T 000039 1E FFC 040030 000030 E0105D 30103F D8105D 280030 301057 483039 2C105E 381039
T 001057 0A 800 100036 4C000F F1 001000
T 000061 19 E00 040030 E01079 301064 508039 DC1079 2C0036 381064 4C0000 05
E 000000
OUTPUT FILE
Loadout.dat
3000 14303348 40391030 36283030 30001548
3016 10613110 03200030 21103920 0033xx41
3032 50364810 61383033 41500045 41961030
3048 03200000 xx143030 43003014 10133010
3064 44241064 21303030 40575460 39212064
3080 381045xx xxxxxxxx xxxxxxxx xxxxxx10
3096 30365230 00154000 301000xx xx343030
3112 14107930 40645030 39152079 22003638
3128 10644300 0025
RESULT:
- 37 -
IMPLEMENTATION OF ABSOLUTE LOADER
Ex. No : 7 Date:
Aim:
To implement Absolute loader using c.
Algorithm:
Step 1: Start the program.
Step 2: Assign the required variable
Step 3:Open the files for read and write
Step 4:Read the content
Step 5:Using while loop perform the loop until character is not equal to E
Step 6:Then compare the character is equal to H
If H then
read start from input file
Like that get length, and input
Else if the character is T Then
perform the frprintf in fp1 for input file
Else if it is not H or T Then
perform the fprintf in fp2 for output file for ,
Step 7:Finally terminate the program
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct object_code
{
int locctr;
char byte[5];
};
struct object_code code[200];
void main()
- 38 -
{
FILE *fp1,*fp2;
char input[15];
int i,len,n=0,count=0,inc=0,textloc,tlen,tloc=0,num=0,loc;
clrscr();
fp1=fopen("lout.dat","r");
fp2=fopen("loadout.dat","w");
rewind(fp1);
rewind(fp2);
fscanf(fp1,"%s",input);
if(strcmp(input,"H")==0)
{
for(i=0;i<4;i++)
{
if(i==1)
fscanf(fp1,"%x",&loc);
else
fscanf(fp1,"%s",input);
}
}
tloc=loc;
while(strcmp(input,"E")!=0)
{
if(strcmp(input,"T")==0)
{
fscanf(fp1,"%x",&textloc);
for(i=0;i<(textloc-(tloc+tlen));i++)
{
strcpy(code[inc].byte,"xx");
- 39 -
code[inc++].locctr=loc++;
}
fscanf(fp1,"%x",&tlen);
tloc=textloc;
}
else
{
len=strlen(input);
for(i=0;i<len;i++)
{
code[inc].byte[num++]=input[i];
if(num>1)
{
code[inc].locctr=loc;
loc++;
inc++;
num=0;
}
}
}
fscanf(fp1,"%s",input);
}
n=0;
i=0;
count=0;
fprintf(fp2,"%x\t",code[i].locctr);
for(i=0;i<inc;i++)
{
fprintf(fp2,"%s",code[i].byte);
- 40 -
n++;
if(n>3)
{
fprintf(fp2,"\t");
n=0;
count++;
}
if(count>3)
{
fprintf(fp2,"\n%x\t",code[i+1].locctr);
count=0;
}
}
getch();
}
- 41 -
INPUT FILE:
Lout.dat
H COPY 002000 00107a
T 002000 1e 142033 483039 102036 282030 302015
483061 3c2003 00202a 0c2039 00202d
T 00201e 15 2c2036 483061 182033 4c0000 454f46
200003 100000
T 002039 1e 242030 302030 e0305d 30303f d8305d
282030 303057 53a039 2c305e 38303f
T 002057 a 102036 4c0000 f1 201000
T 002071 19 342030 e03079 303064 4fa039 dc3079
2c2036 383064 4c0000 15
E 002000
OUTPUT:
Loadout.dat
2000 14203348 30391020 36282030 30201548
2010 30613c20 0300202a oc203900 202d2c20
2020 36483061 1820334c 0000454f 46200003
2030 100000xx xxxxxxxx xx242030 302030e0
2040 305d3030 3fd8305d 28203030 305753a0
2050 392c305e 38303f10 20364c00 00f12010
2060 00xxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
2070 xx342030 e0307930 30644fa0 39dc3079
2080 2c203638 30644c00 0015
RESULT:
- 42 -
IMPLEMENTATION OF PASS1 OF LINKING LOADER Ex. No : 8 Date:
AIM :To implement pass one of linking loader using c.
Algorithm:
Step 1:Enter the location where the program has to be loadedStep 2:Assign the address got from the user as the first control section addressStep 3:Read the header record of the control Section
From the details of the header read store the control section length in variable Enter the control Section name with its address into the external symbol table
Step 4:For each symbol in the subsequent ‘D’ records the symbol must be entered into the symbol table along with its address, added along with the corresponding control section addredd until an end record is reached.Step 5:Assign the starting address of next control section as the address of the current control section plus the length of the control sectionStep 6:Repeat the process from step 3 to 5 until there are no more input
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10
struct estab
{
char csect[10];
char sym_name[10];
long int add;
int length;
}table[MAX];
void main()
{
FILE *fp1,*fp2;
char input[10];
long int i,count=0,start,length,loc;
clrscr();
- 43 -
fp1=fopen("linklin.dat","r");
fp2=fopen("linklout.dat","w");
printf("Enter the location where the program has to be loaded:");
scanf("%lx",&start);
fprintf(fp2,"CSECT\tSYMNAME\tADDRESS\tLENGTH\n");
rewind(fp1);
while(!feof(fp1))
{
fscanf(fp1,"%s",input);
if(strcmp(input,"H")==0)
{
fscanf(fp1,"%s",input);
strcpy(table[count].csect,input);
strcpy(table[count].sym_name,"\0");
fscanf(fp1,"%s",input);
table[count].add=atoi(input)+start;
fscanf(fp1,"%s",input);
length=atoi(input);
table[count++].length=atoi(input);
fscanf(fp1,"%s",input);
}
if(strcmp(input,"D")==0)
{
fscanf(fp1,"%s%lx",input,&loc);
while((strcmp(input,"R")!=0))
{
strcpy(table[count].csect,"\0");
strcpy(table[count].sym_name,input);
table[count].add=loc+start;
- 44 -
table[count++].length=0;
fscanf(fp1,"%s%lx",input,&loc);
}
while(strcmp(input,"T")!=0)
fscanf(fp1,"%s",input);
}
if(strcmp(input,"T")==0)
while(strcmp(input,"E")!=0)
fscanf(fp1,"%s",input);
fscanf(fp1,"%s",input);
start=start+length;
}
for(i=0;i<count;i++)
fprintf(fp2,"%s\t%s\t%lx\t%d\n",table[i].csect,table[i].sym_name,table[i].add,table[i].length);
getch();
}
- 45 -
INPUT FILE
Linklin.dat
H PROGA 000000 000070
D LISTA 000040 ENDA 000054
R LISTB ENDB LISTC ENDC
T 000020 10 03201D 77100004 150014
T 000054 16 100014 15100006 00002F 1000014 FFFFC0
M 000024 05 +LISTB
M 000054 06 +LISTC
M 000058 06 +ENDC
M 000064 06 +LISTB
E 000000
H PROGB 000000 000088
D LISTB 000060 ENDB 000070
R LISTA ENDA LISTC ENDC
T 000036 11 03100000 772027 05100000
T 000070 18 100000 05100006 05100020 05100030 100000
M 000037 05 +LISTA
M 000044 05 +ENDA
M 000070 06 +ENDA
M 000074 06 +ENDC
M 000078 06 +ENDC
M 000082 06 +ENDA
E 000000
H PROGC 000000 000057
D LISTC 000030 ENDC 000042
R LISTA ENDA LISTB ENDB
- 46 -
T 000018 12 03100000 77100004 05100000
T 000042 15 100030 100008 100011 100000 100000
M 000019 05 +LISTA
M 000023 05 +LISTB
M 000027 05 +ENDA
M 000048 06 +LISTA
M 000051 06 +ENDA
M 000054 06 +LISTB
E 000000
OUTPUT FILE
linklout.dat
CSECT SYMNAME ADDRESS LENGTH
PROGA 4000 70
LISTA 4040 0
ENDA 4054 0
PROGB 4046 88
LISTB 40a6 0
ENDB 40b6 0
PROGC 409e 57
LISTC 40ce 0
ENDC 40e0 0
RESULT:
- 47 -
PASS2 OF LINKING LOADER Ex. No : 9 Date:
Aim:To implement pass one of linking loader using c.
Algorithm:Step 1:Assign the control section address in a variable, CSADR.Step 2:Read the Header recordStep 3:From the information available in the header record read, store the control section length in a variable Step 4:Do the following process until an ‘end’ record is reached
i. If the subsequent records read is a text record ‘T’, and if the object code is in character form convert it into machine representation, and move the object code from the record to the memory location control sections address plus the specified address in the text record.
ii. If the sequent records read is modification record ‘M’ then search for the modifying symbol name in the external symbol table created by pass 1 if it is found the add, or subtract the corresponding symbol address found with the value starting at the location (CSADD plus the specified address in the modification record).
Step 5:Add the control section length to the current control section address to fing the address of the next control section, and repeat the entire process until there are no more input.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<process.h>
struct estab
{
char sy[20];
char addr[10];
}
e[]={{"LISTA","0040"},{"ENDA","0054"},{"LISTB","00C3"},{"ENDB","00D3"},{"LISTC","0112"},{"ENDC","0124"}};
struct obj
{
- 48 -
char r[10];
char se[20];
char th[20];
char fo[20];
char fi[20];
}o;
void main()
{
int n=0,j=0,i;
FILE *pf,*st;
clrscr();
pf=fopen("objpgm.txt","r");
st=fopen("result.txt","w+");
fscanf(pf,"%s%s%s%s%s",o.r,o.se,o.th,o.fo,o.fi);
for(i=0;i<=2;i++)
{
while(strcmp(o.r,"E")!=0)
{
if(strcmp(o.r,"R")==0)
{
for(j=0;j<6;j++)
{
if(strcmp(o.se,e[j].sy)==0)
strcat(o.se,e[j].addr);
if(strcmp(o.th,e[j].sy)==0)
strcat(o.th,e[j].addr);
if(strcmp(o.fo,e[j].sy)==0)
strcat(o.fo,e[j].addr);
if(strcmp(o.fi,e[j].sy)==0)
- 49 -
strcat(o.fi,e[j].addr);
}}
if(strcmp(o.r,"M")==0)
{
for(j=0;j<6;j++)
{
if(strcmp(o.fi,e[j].sy)==0)
{
strcpy(o.fi,e[j].addr);
}}}
n++;
fprintf(st,"\n%s\t%s\t%s\t%s\t%s\t\n",o.r,o.se,o.th,o.fo,o.fi);
fscanf(pf,"\n%s%s%s%s%s",o.r,o.se,o.th,o.fo,o.fi);
}
fprintf(st,"\n%s\t%s\t%s\t%s\t%s\t\n",o.r,o.se,o.th,o.fo,o.fi);
fprintf(st,"\n\n");
fscanf(pf,"%s%s%s%s%s",o.r,o.se,o.th,o.fo,o.fi);
}
fclose(pf);
fclose(st);
printf("\nPASS 2 DIRECT LINKING LOADER RESULT IS WRITTEN IN RESULT.TXT FILE");
getch();
}
- 50 -
INPUT FILE
Objpgm.txt
H PROGA 0000 0063 -
D LISTA 0040 ENDA 0054
R LISTB ENDB LISTC ENDC
M 000024 06 + LISTB
M 000054 05 + LISTC
M 000057 05 + ENDB
M 00005A06 + ENDC
E - - - -
H PROGB 0000 007F -
D LISTB 00C3 ENDB 00D3
R LISTA ENDA LISTC ENDC
M 000024 06 + LISTA
M 000054 06 + LISTC
M 000057 05 + ENDA
M 00005A05 + ENDC
E - - - -
H PROGC 0000 0051 -
D LISTC 0112 ENDC 0124
R LISTB ENDB LISTA ENDA
M 000024 05 + LISTB
M 000054 06 + LISTA
M 000057 05 + ENDB
M 00005A06 + ENDA
E - - - -
- 51 -
OUTPUT FILE:
PASS 2 DIRECT LINKING LOADER RESULT IS WRITTEN IN RESULT.TXT FILE
Result.txt
H PROGA 0000 0063 -
D LISTA 0040 ENDA 0054
R LISTB00C3 ENDB00D3 LISTC0112 ENDC0124
M 000024 06 + 00C3
M 000054 05 + 0112
M 000057 05 + 00D3
M 00005A06 + 0124
E - - - -
H PROGB 0000 007F -
D LISTB 00C3 ENDB 00D3
- 52 -
R LISTA0040 ENDA0054 LISTC0112 ENDC0124
M 000024 06 + 0040
M 000054 06 + 0112
M 000057 05 + 0054
M 00005A05 + 0124
E - - - -
H PROGC 0000 0051 -
D LISTC 0112 ENDC 0124
R LISTB00C3 ENDB00D3 LISTA0040 ENDA0054
M 000024 05 + 00C3
M 000054 06 + 0040
M 000057 05 + 00D3
M 00005A06 + 0054
E - - - -
RESULT:
- 53 -
SIMPLE TEXT EDITOR
Ex. No : 10 Date:
Aim:
To create a simple text editor using C.
Algorithm:
Step 1:Provide blank screen for the user to type the document
Step 2:Instruct the user to enter the text using the editor
Step 3:Display the entered character on the screen
Step 4:Characters displayed on the screen are stored in a character array
Step 5:Specify if the content has to be saved or not.
Step 6:If SAVE option is selected
a) Get the data file name from the user
b) Open the file in write mode
c) Move the content of the character array to the file and close the file
Step 7:If OPEN option is selected
a) Open the data file in the read mode
b) Read one character at a time from the file and move it to the character array
c) Repeat the above steps utile the end of file is reached
d) Display the characters from the character array on the screen
Step 8:Finally close the text editor and exit
- 54 -
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct list
{
char text[100];
}l[100];
int length,i,len=1,x=0,n;
char string[100],r[100],d[100],s[100];
void insert();
void search();
void replace();
void del();
void display();
void main()
{
int ch;
clrscr();
do
{
printf("\n1.Insert\n2.Search\n3.Replace\n4.Delete\n5.Display\n6.Exit\n");
printf("Enter Your Choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();break;
- 55 -
case 2:
search();break;
case 3:
replace();break;
case 4:
del();break;
case 5:
display();break;
case 6:
exit(1);break;
}}
while(ch<6);
}
void insert()
{
printf("\nEnter the string :");
scanf("%s",l[len].text);
len=len+1;
}
void search()
{
printf("\n Enter the string to be searched :");
scanf("%s",s);
for(i=1;i<=len;i++)
{
x=strcmp(l[i].text,s);
if(x==0)
{
printf("\n String Found!\n");
- 56 -
for(i=0;i<len;i++)
{
printf("%s",l[i].text);
}
printf("\n");
break;
}
}
if(x!=0)
{
printf("\n String Not Found!");
}
}
void replace()
{
printf("\n Enter the string to be replaced :");
scanf("%s",s);
for(i=0;i<=len;i++)
{
x=strcmp(l[i].text,s);
if(x==0)
{
printf("\n Enter the new string :");
scanf("%s",r);
strcpy(l[i].text,r);
printf("\n String replaced successfully!");
break;
} }
- 57 -
if(x!=0)
{
printf("\n String Not Found to Replace.");
} }
void del()
{
printf("\n Enter the String to be deleted :");
scanf("%s",d);
for(i=1;i<=len;i++)
{
x=strcmp(l[i].text,d);
if(x==0)
{
strcpy(l[i].text,"");
printf("\n String is deleted successfully!");
break;
} }
if(x!=0)
printf("\n String Not Found.");
}
void display()
{
printf("\nFinal String :\n");
for(i=0;i<len;i++)
{
printf("%s",l[i].text);
} printf("\n");
getch();
}
- 58 -
OUTPUT:
1.Insert
2.Search
3.Replace
4.Delete
5.Display
6.Exit
Enter Your Choice :1
Enter the string :SYSTEM
1.Insert
2.Search
3.Replace
4.Delete
5.Display
6.Exit
Enter Your Choice :1
Enter the string :SOFTWARE
1.Insert
2.Search
3.Replace
4.Delete
5.Display
6.Exit
Enter Your Choice :5
Final String :
SYSTEMSOFTWARE
- 59 -
1.Insert
2.Search
3.Replace
4.Delete
5.Display
6.Exit
Enter Your Choice :2
Enter the string to be searched :SYSTEM
String Found!
1.Insert
2.Search
3.Replace
4.Delete
5.Display
6.Exit
Enter Your Choice :2
Enter the string to be searched :LAB
String Not Found!
1.Insert
2.Search
3.Replace
4.Delete
5.Display
6.Exit
- 60 -
Enter Your Choice :3
Enter the string to be replaced :
SOFTWARE
Enter the new string :SOFT
String replaced successfully!
1.Insert
2.Search
3.Replace
4.Delete
5.Display
6.Exit
Enter Your Choice :5
Final String :
SYSTEMSOFT
1.Insert
2.Search
3.Replace
4.Delete
5.Display
6.Exit
Enter Your Choice :4
Enter the String to be deleted :SOFT
String is deleted successfully!
- 61 -
1.Insert
2.Search
3.Replace
4.Delete
5.Display
6.Exit
Enter Your Choice :5
Final String :
SYSTEM
1.Insert
2.Search
3.Replace
4.Delete
5.Display
6.Exit
Enter Your Choice :6
RESULT:
- 62 -