Professional Documents
Culture Documents
Appendix B: System Software Lab Programs
Appendix B: System Software Lab Programs
APPENDIX B
SYSTEM SOFTWARE LAB PROGRAMS
1. IMPLEMENTATION OF SYMBOL TABLE WITH FEATURES LIKE
CREATE, INSERT, MODIFY, SEARCH AND DISPLAY
Aim:
To implement a symbol table with functions to create, insert, modify, search, and
display, using C language.
Algorithm:
Step 1: Design a menu through which we can create a symbol table and perform operations as
insert, modify, search and display.
Step 2: Create a Symbol table with fields as ‘variable’ and ‘value’ using create() option.
Step 3:Entries may be added to the table while it’s created itself.
Step 4:Append new contents to the symbol table with the constraints that there is no
duplication of entries, using insert () option.
Step 5:Modify existing content of the table using modify () option.
Step 6:Use display () option to display the contents of the table.
Program:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
struct table
{
char var[10];
int value;
164
System Software System Software Lab Programs
};
struct table t[20];
int i,j,n;
void create();
void modify();
int search(char variable[],int n);
void insert();
void display();
void main()
{
int ch,result=0;
char v[10];
do
{
clrscr();
printf("\n\t\t SYMBOL TABLE CREATION\n");
printf("\n\t1. CREATE\n\t2. INSERT\n\t3. MODIFY\n\t4. SEARCH\n\t5.
DISPLAY\n\t6. EXIT ");
printf("\n Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2: insert();
break;
case 3: modify();
break;
case 4: printf("\n Enter the variable to be searched for : ");
scanf("%s",v);
165
System Software System Software Lab Programs
result=search(v,n);
if(result==0)
printf("\n The variable doest not belong to the table.");
else
printf("\n Variable=%s\tValue=%d\tLocation=%d",
t[result].var,t[result].value, result);
getch();
break;
case 5: display();
break;
case 6: exit(1);
}
}while(ch!=6);
getch();
}
void create()
{
printf("\nEnter the number of entries: ");
scanf("%d",&n);
printf("\nEnter the variable and the value:\n");
for(i=1;i<=n;i++)
scanf("%s%d",t[i].var,&t[i].value);
}
void insert()
{
if(i>=20)
printf("\n Cannot insert. Table is full.\n");
else
{
n++;
166
System Software System Software Lab Programs
}
int search(char variable[],int n)
{
int flag;
for(i=1;i<=n;i++)
{
if(strcmp(t[i].var,variable)==0)
{
flag=1;
break;
167
System Software System Software Lab Programs
}
}
if(flag==1)
return i;
else
return 0;
}
void display()
{
printf("\nVARIABLE\tVALUE\n");
for(i=1;i<=n;i++)
printf("\n%s\t\t%d",t[i].var,t[i].value);
getch();
}
OUTPUT:
SYMBOL TABLE CREATION
1. CREATE
2. INSERT
3. MODIFY
4. SEARCH
5. DISPLAY
6. EXIT
Enter your choice: 1
VARIABLE VALUE
NUM1 1
NUM2 2
NUM3 3
NUM4 10
VARIABLE VALUE
NUM1 1
NUM2 2
170
System Software System Software Lab Programs
NUM7 30
NUM4 10
SYMBOL TABLE CREATION
1. CREATE
2. INSERT
3. MODIFY
4. SEARCH
5. DISPLAY
6. EXIT
Enter your choice : 4
171
System Software System Software Lab Programs
Result:
Thus the program to implement a symbol table with functions to create, insert, modify,
search, and display, using C language was executed successfully.
2. TEXT EDITOR
Aim:
To implement a simple text editor with features like insertion / deletion of a character, word,
and sentence.
Algorithm:
Step 1: Design a Menu using switch case
Step 2: Get the choice
Step 3: If choice is “1” then, type the text to be inserted in the editor window
Step 4: If choice is “2” then enter the text to be modified, then type the text which one
172
System Software System Software Lab Programs
is inserted
Step 5: If the choice is “3” then change the color of the text and display the text
Step 6: If the choice is “4” then change the background color of the text and display it.
Step 7: If the choice is “5” then display the text in the editor window
Step 8: If the choice is “6” then exit from the program
Program:
#include<stdio.h>
#include<string.h>
#include<conio.h>
struct list
{
char text[100];
}l[100];
int i,n,x=0;
char s[100],s1[100];
void insert();
void modify();
void display();
void changecolor();
void bkground();
void main()
{
int ch;
clrscr();
for(;;)
{
printf("\n\tTEXT EDITOR");
printf("\n\t1.Insert\n\t2.Modify\n\t3.ChangeTextcolor\n\t4.ChangeTextBackg
round\n\t5.Display\n\t6.Exit");
173
System Software System Software Lab Programs
Output
TEXT EDITOR
1.Insert
2.Modify
3.ChangeTextcolor
4.ChangeTextBackground
5.Display
6.Exit
Enter the option:1
Enter the no of words in the text:3
Enter the string: welcome to chettinad
TEXT EDITOR
1.Insert
2.Modify
3.ChangeTextcolor
4.ChangeTextBackground
5.Display
6.Exit
Enter the option:2
Enter the string to be Modified: chettinad
Enter the string to be inserted: ccet
TEXT EDITOR
1.Insert
2.Modify
3.ChangeTextcolor
4.ChangeTextBackground
5.Display
6.Exit
176
System Software System Software Lab Programs
177
System Software System Software Lab Programs
Result
Thus a Text editor was implemented in C.
Aim:
To write a C program to implement symbol table using open addressing hashing technique.
Algorithm:
Step 1: Create a symbol table with size n.
Step 2: Create a menu
1. Insert
2. Delete
3. Display
4. Search
5. Exit
Step 3: If the choice is 1, then perform insertion into the symbol table.
(i) Get the Label and address to insert as input.
(ii) Apply hash function h(x)=x mod n to the address and find out the hash value.
(iii) Insert the new label and address into the hash value of the symbol table
Step 4: If the choice is 2, then perform deletion from the symbol table.
178
System Software System Software Lab Programs
Program:
#include <stdio.h>
#include <conio.h>
int i,n,ch,b,index;
char a[10];
static int count=0;
struct symbol
{
char label[10];
int addr;
}table[15];
void insert();
void delete();
void display();
void search();
int h();
void main()
179
System Software System Software Lab Programs
{
clrscr();
printf("\n\n Enter the Symbol Table Size\n");
scanf("%d",&n);
for(i=0;i<n;i++)
table[i].addr=0;
while(1)
{
printf("\n\t\tMENU\n1.INSERT\n2.DELETE\n3.DISPLAY\n4.SEARCH\n5.EXIT"
);
printf("\n Enter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(count==n)
printf("\n Hash Table is Full");
else
{
count++;
insert();
}
break;
case 2:
count--;
delete();
break;
case 3:
display();
break;
180
System Software System Software Lab Programs
case 4:
search();
break;
case 5:
exit(0);
}
}
}
void insert()
{
printf("\nEnter the Label:");
scanf("%s",a);
printf("\nEnter the addr(except 0):");
scanf("%d",&b);
index=h(b);
while(table[index].addr!=0)
{
if(index==n-1)
index=0;
else
index++;
}
strcat(table[index].label,a);
table[index].addr=b;
}
void delete()
{
printf("\n Enter the address to delete:");
scanf("%d",&b);
index=h(b);
181
System Software System Software Lab Programs
while(table[index].addr!=b)
{
if(index==n-1)
index=0;
else
index++;
}
table[index].addr=0;
}
void display()
{
printf("\n\t\t SYMBOL TABLE");
printf("\n NUMBER\tLABEL\tADDRESS");
printf("\n ----------------------");
for(i=0;i<n;i++)
{
if(table[i].addr!=0)
printf("\n%d\t%s\t%d",i,table[i].label,table[i].addr);
}
getch();
}
int h(int temp)
{
return temp%n;
}
void search()
{
printf("\n Enter the address to search:");
scanf("%d",&b);
for(i=0;i<n;i++)
182
System Software System Software Lab Programs
{
if(table[i].addr==b)
{
printf("\n Entries are found");
break;
}
}
if(i>=n)
printf("\n Entries are not found");
getch();
}
Output:
Enter the Symbol Table Size
10
MENU
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter the choice:1
MENU
1.INSERT
2.DELETE
3.DISPLAY
183
System Software System Software Lab Programs
4.SEARCH
5.EXIT
Enter the choice:3
SYMBOL TABLE
NUMBER LABEL ADDRESS
----------------------------------------------------------------
4 BEGIN 1234
MENU
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter the choice: 1
Enter the Label: LOOP
Enter the addr(except 0):9876
MENU
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter the choice:3
SYMBOL TABLE
NUMBER LABEL ADDRESS
-----------------------------------------------------------------
184
System Software System Software Lab Programs
4 BEGIN 1234
6 LOOP 9876
MENU
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter the choice:1
Enter the Label: NUM1
Enter the addr(except 0):5434
MENU
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter the choice:3
SYMBOL TABLE
NUMBER LABEL ADDRESS
----------------------------------------------------------------
4 BEGIN 1234
5 NUM1 5434
6 LOOP 9876
MENU
1.INSERT
2.DELETE
185
System Software System Software Lab Programs
3.DISPLAY
4.SEARCH
5.EXIT
Enter the choice:2
Enter the address to delete: 5434
MENU
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter the choice:3
SYMBOL TABLE
NUMBER LABEL ADDRESS
-----------------------------------------------------------------
4 BEGIN 1234
6 LOOP 9876
MENU
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter the choice:4
Enter the address to search:1234
Entries are found
186
System Software System Software Lab Programs
MENU
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter the choice:4
Enter the address to search:7765
Entries are not found
Result:
Thus the program for implementation of symbol table using open addressing hashing
technique was successfully executed and the result is verified.
187
System Software System Software Lab Programs
Aim:
To implement a PASS 1 of a two pass assembler, using C language.
Algorithm:
Step 1:Read the input line.
Step 2:Check to see if the opcode field in the input line is “START”.
(i) Find if there is any operand field after START; initialize the LOCCTR to the operand
value.
(ii) Otherwise, if there is no value in the operand field the LOCCTR is set to zero.
Step 3:Write the line to the intermediate file.
Step 4: Repeat the following for the other lines in the program until the opcode field contains
END directive.
1. 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 the symbol is entered into the SYMTAB, along with the memory address
in which it is stored.
2. If there is an opcode in the opcode field
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 LOCCTR by three.
b) If the opcode is BYTE, increment the LOCTR by one.
c) If the opcode is RESW, increment the LOCCTR by integer equivalent of the
operand value *3.
d) If the opcode is RESB, increment the LOCCTR by the integer equivalent of the
operand value.
3. Write each and every line processed to the intermediate file along with their
location counters.
188
System Software System Software Lab Programs
Step 5:Calculate the length of the program by subtracting the starting address of the program
from the final value of the LOCCTR
Step 6:Close all the opened files and exit.
Program:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char opcode[10],mnemonic[10],operand[10],label[10],code[10];
int locctr,start,length;
FILE *fp1,*fp2,*fp3,*fp4;
clrscr();
fp1=fopen("input.dat","r");
fp2=fopen("symbtab.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);
locctr=start;
fprintf(fp3,"%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp1,"%s%s%s",label,opcode,operand);
}
else
locctr=0;
while(strcmp(opcode,"END")!=0)
189
System Software System Software Lab Programs
{
fprintf(fp3,"%d\t",locctr);
if(strcmp(label,"**")!=0)
fprintf(fp2,"%s\t%d\n",label,locctr);
rewind(fp4);
fscanf(fp4,"%s",mnemonic);
while(strcmp(mnemonic,"END")!=0)
{
if(strcmp(opcode,mnemonic)==0)
{
locctr+=3;
break;
}
fscanf(fp4,"%s",mnemonic);
}
if(strcmp(opcode,"WORD")==0)
locctr+=3;
else if(strcmp(opcode,"RESW")==0)
locctr+=(3*(atoi(operand)));
else if(strcmp(opcode,"RESB")==0)
locctr+=(atoi(operand));
else if(strcmp(opcode,"BYTE")==0)
++locctr;
fprintf(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",locctr,label,opcode,operand);
length=locctr-start;
printf("\n The length of the program is %d",length);
fclose(fp1);
190
System Software System Software Lab Programs
fclose(fp2);
fclose(fp3);
fclose(fp4);
getch();
}
INPUT
INPUT.DAT
MAIN START 2000
BEGIN LDA NUM1
** STA NUM2
** LDCH CHAR1
** STCH CHAR2
NUM1 WORD 5
NUM2 RESW 1
CHAR1 BYTE C’A’
CHAR2 RESB 1
** END BEGIN
OPTAB.DAT
ADD 18
ADDR 90
SUB 1C
SUBR 94
MUL 20
MULR 98
DIV 24
DIVR 9C
LDA 00
LDB 68
191
System Software System Software Lab Programs
LDX 04
LDCH 50
STA 0C
STB 78
STX 10
STCH 54
TIX 2C
J 3C
JSUB 48
RSUB 4C
JEQ 30
JLT 38
JGT 34
START *
END *
OUTPUT
The length of the program is 20
OUT.DAT
MAIN START 2000
2000 BEGIN LDA NUM1
2003 ** STA NUM2
2006 ** LDCH CHAR1
2009 ** STCH CHAR2
2012 NUM1 WORD 5
2015 NUM2 RESW 1
2018 CHAR1 BYTE C’A’
2019 CHAR2 RESB 1
2020 ** END BEGIN
192
System Software System Software Lab Programs
SYMBTAB.DAT
BEGIN 2000
NUM1 2012
NUM2 2015
CHAR1 2018
CHAR2 2019
Result:
Thus the program to implement a PASS 1 of a two pass assembler, using C language was
executed successfully.
Aim:
To implement a PASS 2 of a two pass assembler, using C language.
Algorithm:
Step 1:Read the first line from the intermediate file.
193
System Software System Software Lab Programs
Step 2:Check to see if the opcode field in the input line is “START”, if so then write the line onto
the final output file.
Step 3:Repeat the following for the other lines in the intermediate file until the opcode field
contains END directive.
1. If there is a symbol in the operand field, then the object code is assembled by
combining the machine code equivalent of the instruction with the symbol address.
2. If there is no symbol in the operand field, then the operand address is assigned as
zero and it is assembled with the machine code equivalent of the instruction.
3. If the opcode field is BYTE or WORD or RESB, then convert the constants in the
operand filed to the object code.
4. Write the input line along with the object code onto the final output file.
Step 4:Close all the opened files and exit.
Program:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char symbol[20],opcode[20],mnemonic[20],operand[20], label[20],code[20],
character, add[20],objectcode[20];
int locctr,flag,flag1,loc;
FILE *fp1,*fp2,*fp3,*fp4;
clrscr();
fp1=fopen("out.dat","r");
fp2=fopen("twoout.dat","w");
fp3=fopen("optab.dat","r");
fp4=fopen("symbtab.dat","r");
fscanf(fp1,"%s%s%s",label,opcode,operand);
194
System Software System Software Lab Programs
if(strcmp(opcode,"START")==0)
{
fprintf(fp2,"\t%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;
rewind(fp3);
fscanf(fp3,"%s%s",mnemonic,code);
while(strcmp(mnemonic,"END")!=0)
{
if((strcmp(opcode,mnemonic)==0)&&(strcmp(code,"*")!=0))
{
flag=1;
break;
}
fscanf(fp3,"%s%s",mnemonic,code);
}
if(flag==1)
{
flag1=0;
rewind(fp4);
while(!feof(fp4))
{
fscanf(fp4,"%s%d",symbol,&loc);
if(strcmp(symbol,operand)==0)
{
flag1=1;
break;
195
System Software System Software Lab Programs
}
}
if(flag1==1)
{
itoa(loc,add,10);
strcpy(objectcode,strcat(code,add));
}
}
else if(strcmp(opcode,"BYTE")==0||strcmp(opcode,"WORD")==0)
{
if(operand[0]=='C'||operand[0]=='1X')
{
character=operand[2];
itoa(character,add,16);
strcpy(objectcode,add);
}
else
{
itoa(atoi(operand),add,10);
strcpy(objectcode,add);
}
}
else
strcpy(objectcode,"\0");
fprintf(fp2,"%d\t%s\t%s\t%s\t%s\n",locctr,label,opcode,operand,
objectcode);
fscanf(fp1,"%d%s%s%s",&locctr,label,opcode,operand);
}
fprintf(fp2,"\t%s\t%s\t%s\n",label,opcode,operand);
fclose(fp1);
196
System Software System Software Lab Programs
fclose(fp2);
fclose(fp3);
fclose(fp4);
getch();
}
INPUT
OUT.DAT
MAIN START 2000
2000 BEGIN LDA NUM1
2003 ** STA NUM2
2006 ** LDCH CHAR1
2009 ** STCH CHAR2
2012 NUM1 WORD 5
2015 NUM2 RESW 1
2018 CHAR1 BYTE C’A’
2019 CHAR2 RESB 1
2020 ** END BEGIN
SYMBTAB.DAT
BEGIN 2000
NUM1 2012
NUM2 2015
CHAR1 2018
CHAR2 2019
OPTAB.DAT
ADD 18
ADDR 90
SUB 1C
SUBR 94
197
System Software System Software Lab Programs
MUL 20
MULR 98
DIV 24
DIVR 9C
LDA 00
LDB 68
LDX 04
LDCH 50
STA 0C
STB 78
STX 10
STCH 54
TIX 2C
J 3C
JSUB 48
RSUB 4C
JEQ 30
JLT 38
JGT 34
START *
END *
OUTPUT
TWOOUT.DAT
MAIN START 2000
2000 BEGIN LDA NUM1 002012
2003 ** STA NUM2 0C2015
2006 ** LDCH CHAR1 502018
2009 ** STCH CHAR2 542019
2012 NUM1 WORD 5 5
198
System Software System Software Lab Programs
Result:
Thus the program to implement a PASS 2 of a two pass assembler, using C
language was executed successfully.
Aim:
To implement a single pass (Load and Go) assembler, using C language.
Algorithm:
Step 1:Read the input line.
Step 2:Check to see if the opcode field in the input line is “START”.
1. Find if there is any operand field after START; initialize the LOCCTR to the operand
value.
2. Otherwise if there is no value in the operand field the LOCCTR is set to zero.
Step 3:Write the line onto the output file.
Step 4:Repeat the following for the other lines in the input file until the opcode field contains
END directive.
199
System Software System Software Lab Programs
Program:
#include <stdio.h>
#include <conio.h>
200
System Software System Software Lab Programs
#include <string.h>
#include <stdlib.h>
#define MAX 20
struct input
{
char opcode[10],mnemonic[10],operand[10],label[10];
int loc;
}table[MAX];
struct symtab
{
char sym[10];
int f,val,ref;
}symtb1[MAX];
void main()
{
int f1,i=1,j=1,flag,locctr,x;
char add[10],code[10],mnemcode[10];
FILE *fp1,*fp2,*fp3,*fp4;
clrscr();
fp1=fopen("z:\input.dat","r");
fp2=fopen("z:\optab.dat","r");
fp3=fopen("spout.dat","w");
fp4=fopen("stable.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);
}
201
System Software System Software Lab Programs
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(symtb1[x].sym,table[i].label)==0) &&
(symtb1[x].f==1))
{
symtb1[x].val=locctr;
symtb1[x].f=0;
table[symtb1[x].ref].loc=locctr;
f1=1;
break;
}
}
if(f1==0)
{
strcpy(symtb1[j].sym,table[i].label);
symtb1[j].val=locctr;
symtb1[j].f=0;
j++;
}
}
rewind(fp2);
fscanf(fp2,"%s%s",code,mnemcode);
while(strcmp(code,"END")!=0)
202
System Software System Software Lab Programs
{
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,symtb1[x].sym)==0)
{
flag=1;
if(symtb1[x].f==0)
table[i].loc=symtb1[x].val;
break;
}
}
if(flag!=1)
{
strcpy(symtb1[j].sym,table[i].operand);
symtb1[j].f=1;
symtb1[j].ref=i;
j++;
}
}
fscanf(fp2,"%s%s",code,mnemcode);
}
if(strcmp(table[i].opcode,"WORD")==0)
{
locctr+=3;
strcpy(table[i].mnemonic,'\0');
203
System Software System Software Lab Programs
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));
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].l
oc,add,10)));
for(x=1;x<j;x++)
204
System Software System Software Lab Programs
fprintf(fp4,"%s\t%d\n",symtb1[x].sym,symtb1[x].val);
getch();
}
INPUT
INPUT.DAT
MAIN START 2000
BEGIN JSUB LOOP1
** JSUB LOOP2
NUM1 WORD 5
NUM2 RESW 3
CHAR1 BYTE C'A'
CHAR2 RESB 4
LOOP1 LDA NUM1
** STA NUM2
LOOP2 LDCH CHAR1
** STCH CHAR2
** END BEGIN
OPTAB.DAT
ADD 18
ADDR 90
SUB 1C
SUBR 94
MUL 20
MULR 98
DIV 24
DIVR 9C
LDA 00
LDB 68
205
System Software System Software Lab Programs
LDX 04
LDCH 50
STA 0C
STB 78
STX 10
STCH 54
TIX 2C
J 3C
JSUB 48
RSUB 4C
JEQ 30
JLT 38
JGT 34
START *
END *
OUTPUT
SPOUT.DAT
MAIN START 2000 0
BEGIN JSUB LOOP1 482023
** JSUB LOOP2 482029
NUM1 WORD 5 5
NUM2 RESW 3 0
CHAR1 BYTE C'A' 65
CHAR2 RESB 4 0
LOOP1 LDA NUM1 002006
** STA NUM2 0C2009
LOOP2 LDCH CHAR1 502018
** STCH CHAR2 542019
** END BEGIN 0
206
System Software System Software Lab Programs
STABLE.DAT
BEGIN 2000
LOOP1 2023
LOOP2 2029
NUM1 2006
NUM2 2009
CHAR1 2018
CHAR2 2019
207
System Software System Software Lab Programs
Result:
Thus the program to implement a PASS 2 of a two pass assembler, using C language was
executed successfully.
Algorithm:
Step 1:Read the Header record.
Step 2:Verify the Program name, length and starting address.
Step 3:Read first Text record.
Step 4:Repeat the following process until an end record is encountered
1. Set LOC to starting address of the text record
2. If object code is in character form, then convert into internal hexadecimal
representation.
3. Moves object code to the specified location (LOC) in memory.
4. Increment LOC by three.
5. Read next record from the input file.
Step 5:Jump to address specified in End record.
Step 6:Close all the opened files and exit.
Program:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
struct object_code
{
208
System Software System Software Lab Programs
int locctr;
char byte[5];
};
struct object_code code[500];
void main()
{
char input[15];
int i,len,n=0,count=0,inc=0,textloc,tlen=0,tloc=0,num=0,loc;
FILE *fp1,*fp2;
clrscr();
fp1=fopen("loadin.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)
{
209
System Software System Software Lab Programs
fscanf(fp1,"%x",&textloc);
for(i=0;i<textloc-(tloc+tlen);i++)
{
strcpy(code[inc].byte,"xx");
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);
210
System Software System Software Lab Programs
for(i=0;i<inc;i++)
{
fprintf(fp2,"%s",code[i].byte);
n++;
if(n>3)
{
fprintf(fp2,"\t");
n=0;
count++;
}
if(count>3)
{
fprintf(fp2,"\n%x\t",code[i+1].locctr);
count=0;
}
}
fclose(fp1);
fclose(fp2);
getch();
}
INPUT
IP.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 0A 102036 4C0000 F1 201000
211
System Software System Software Lab Programs
OUTPUT
LOADOUT.DAT
2000 14203348 30391020 36282030 30201548
2010 30613C20 0300202A 0C203900 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:
Thus the program to implement an absolute loader, using C language was executed
successfully.
Aim:
To implement a C program for relocation loader.
Algorithm:
Step 1: Enter the new starting location to which the object code has to be relocated.
Step 2: Read the content of the input file as strings one at a time in an arrry ”input”.
Step 3: Transfer the string read in array “input” into another array “output”, until “T” is
encountered.
Step 4: Move the consecutive next three strings into array “output”.
Step 5: Convert the current string read which is the relocation bit associated with each text
record to binary form.
Step 6: Make the necessary changes in the corresponding words of object code by adding
the new starting address with the address part of the object code for which the corresponding
relocation bit to set, and store the updated object code into the array “output”.
Step 7: Move the object code for which the corresponding relocation bit is not set directly to
the array “output” from the array “input” without any change.
Step 8: Repeat step 2 to 8 until an end record is encountered.
Step 9: If object code is in character form convert form it to internal hexadecimal
representation.
Step 10: Move object codes to specified locations in memory.
Step 11: Write the starting location counter value of a block of object code, and the
corresponding internal hexadecimal representations to the output file.
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void convert(char h[12]);
char bitmask[12];
213
System Software System Software Lab Programs
char bit[12]={0};
void main()
{
char add[6],length[10],input[10],binary[12],relocbit,ch,pn[5];
int start,inp,len,i,address,opcode,addr,actualadd,tlen;
FILE *fp1,*fp2;
clrscr();
printf("\n\n Enter the actual starting address : ");
scanf("%x",&start);
fp1=fopen("rlin.dat","r");
fp2=fopen("rlout.dat","w");
fscanf(fp1,"%s",input);
fprintf(fp2,"\nADDRESS\tCONTENT");
while(strcmp(input,"E")!=0)
{
if(strcmp(input,"H")==0)
{
fscanf(fp1,"%s",pn);
fscanf(fp1,"%x",add);
fscanf(fp1,"%x",length);
fscanf(fp1,"%s",input);
}
if(strcmp(input,"T")==0)
{
fscanf(fp1,"%x",&address);
fscanf(fp1,"%x",&tlen);
fscanf(fp1,"%s",bitmask);
address+=start;
convert(bitmask);
len=strlen(bit);
214
System Software System Software Lab Programs
if(len>=11)
len=10;
for(i=0;i<len;i++)
{
fscanf(fp1,"%x",&opcode);
fscanf(fp1,"%x",&addr);
relocbit=bit[i];
if(relocbit=='0')
actualadd=addr;
else
actualadd=addr+start;
fprintf(fp2,"\n%X\t\t%X%X",address,opcode,actualadd);
address+=3;
}
fscanf(fp1,"%s",input);
}
}
fclose(fp1);
fclose(fp2);
getch();
}
void convert(char h[])
{
int i,l;
strcpy(bit,"");
l=strlen(h);
for(i=0;i<l;i++)
{
switch(h[i])
{
215
System Software System Software Lab Programs
case '0':
strcat(bit,"0");
break;
case '1':
strcat(bit,"1");
break;
case '2':
strcat(bit,"10");
break;
case '3':
strcat(bit,"11");
break;
case '4':
strcat(bit,"100");
break;
case '5':
strcat(bit,"101");
break;
case '6':
strcat(bit,"110");
break;
case '7':
strcat(bit,"111");
break;
case '8':
strcat(bit,"1000");
break;
case '9':
strcat(bit,"1001");
break;
216
System Software System Software Lab Programs
case 'A':
strcat(bit,"1010");
break;
case 'B':
strcat(bit,"1011");
break;
case 'C':
strcat(bit,"1100");
break;
case 'D':
strcat(bit,"1101");
break;
case 'E':
strcat(bit,"1110");
break;
case 'F':
strcat(bit,"1111");
break;
}
}
}
INPUT
Enter the location where the program has to be loaded: 5000
RLIN.DAT
H COPY 000000 00107A
T 000000 1E FFC 14 0033 48 0039 10 0036 28 0030 30 0015 48 0061 3C 0003 20 002A
1C 0039 30 002D
T 001500 15 E00 1D 0036 48 1061 18 0033 4C 1000 80 1000 60 1003
E 000000
217
System Software System Software Lab Programs
OUTPUT
RLOUT.DAT
ADDRESS CONTENT
5000 145033
5003 485039
5006 105036
5009 285030
500C 305015
500F 485061
5012 3C5003
5015 20502A
5018 1C5039
501B 30502D
6500 1D5036
6503 486061
6506 185033
6509 4C1000
650C 801000
650F 601003
Result
Thus the program for implementing relocating loader was executed successfully and
the result is verified.
9. IMPLEMENTATION OF PASS 1 OF A DIRECT LINKING LOADER
Aim:
To implement a pass 1 of a direct linking loader using C program.
Algorithm:
Step 1: Enter the location where the program has to be loaded.
Step 2: Assign the address got from the user as the first control section address.
218
System Software System Software Lab Programs
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10
struct estab
{
char csect[10],symname[10];
long int add;
int length;
}table[MAX];
void main()
{
FILE *f1,*f2;
char ip[10];
long int i,count=0,start,length,loc;
clrscr();
f1=fopen("lkin.dat","r");
f2=fopen("lkout.dat","w");
printf("Enter the location where the pgm has to be loaded:");
219
System Software System Software Lab Programs
scanf("%lx",&start);
fprintf(f2,"CSECT TSYMNAME TADDRESS TLENGTH\n");
rewind(f1);
while(!feof(f1))
{
fscanf(f1,"%s",ip);
if(strcmp(ip,"H")==0)
{
fscanf(f1,"%s",ip);
strcpy(table[count].csect,ip);
strcpy(table[count].symname,"\0");
fscanf(f1,"%s",ip);
table[count].add=atoi(ip)+start;
fscanf(f1,"%s",ip);
table[count++].length=atoi(ip);
fscanf(f1,"%s",ip);
}
if(strcmp(ip,"D")==0)
{
fscanf(f1,"%s%lx",ip,&loc);
while(strcmp(ip,"R")!=0)
{
strcpy(table[count].csect,"\0");
strcpy(table[count].symname,ip);
table[count].add=loc+start;
table[count++].length=0;
fscanf(f1,"%s%lx",ip,&loc);
}
while(strcmp(ip,"T")!=0)
fscanf(f1,"%s",ip);
220
System Software System Software Lab Programs
}
if(strcmp(ip,"T")==0)
while(strcmp(ip,"E")!=0)
fscanf(f1,"%s",ip);
fscanf(f1,"%s",ip);
start=start+length;
}
for(i=0;i<count;i++)
fprintf(f2,"%s\t%s\t%lx\t
%d\n",table[i].csect,table[i].symname,table[i].add,
table[i].length);
getch();
}
INPUT
Enter the location where the pgm has to be loaded: 4000
LKIN.DAT
H PROGA 000000 000070
D LISTA 000040 ENDA 000054
R LISTB ENDB LISTC ENDC
T 000020 10 03201D 77100004 150014
M 000024 05 +LISTB
M 000054 06 +LISTC
M 000058 06 +ENDC
M 000064 06 +LISTB
E 000000
OUTPUT:
221
System Software System Software Lab Programs
LKOUT.DAT
CSECT SYMBOL ADDRESS LENGTH
PROGA 4000 70
LISTA 4040 0
ENDA 4054 0
Result:
Thus the program for pass 1 of a direct linking loader was successfully implemented
and the result is verified.
Aim:
To implement a pass2 of a direct linking loader using C.
Algorithm:
Step 1: Assign the control section address in a variable, CSADR.
Step 2: Read the header record. From the information available in the header record, store the
control section length in a variable.
Step 3: 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
222
System Software System Software Lab Programs
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 subsequent records read is modification record ‘M’ then search for the
modifying symbol name in the external symbol table created by pass1 if it is found then
add, or subtract the corresponding symbol address found with the value starting at the
location
Step 4: Add the control section length to the current control sections address to find the
address of the next control section, and repeat the entire process until there are no more
input.
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct obj
{
char rec[20],sym[10],addr[20];
}ob;
struct estab
{
char sym[20];
int addr;
}es;
void main()
{
FILE *f1,*f2;
int csaddr,maddr,cslth,addr;
clrscr();
223
System Software System Software Lab Programs
f1=fopen("OBJ.TXT","r");
f2=fopen("ESTAB.TXT","r");
fscanf(f2,"%s%d",es.sym,&es.addr);
csaddr=es.addr;
printf("Memoryaddr Object code \n");
while(!feof(f1))
{
fscanf(f1,"%s%s%s",ob.rec,ob.sym,ob.addr);
cslth=atoi(ob.addr);
while(strcmp(ob.rec,"E")!=0)
{
int s;
fscanf(f1,"%s%s%s",ob.rec,ob.sym,ob.addr);
if(strcmp(ob.rec,"T")==0)
{
addr=csaddr+atoi(ob.sym);
s=atoi(ob.addr);
printf("%d\t %s\n",addr,ob.addr);
}
if(strcmp(ob.rec,"M")==0)
{
maddr=csaddr+atoi(ob.sym);
while(!feof(f2))
{
fscanf(f2,"%s%d",es.sym,&es.addr);
if(strcmp(ob.addr,es.sym)==0)
{
es.addr=es.addr+s;
printf("%d\t %d\n",maddr,es.addr);
break;
224
System Software System Software Lab Programs
}
}
rewind(f2);
}
}
csaddr+ =cslth;
}
fcloseall();
getch();
}
INPUT
OBJ.TXT
H PROGA 0063
D LISTA 0040
T 0054 0000
M 0059 LISTB
E 0003 NULL
H PROGB 0030
D LISTB 0030
T 0050 0000
M 0078 LISTA
E 0003 NULL
ESTAB.TXT
PROGA 4000
LISTA 4040
PROGB 4063
LISTB 4093
225
System Software System Software Lab Programs
OUTPUT
Memoryaddr Object code
4054 0000
4059 4093
4113 0000
4141 4040
Result:
Thus the program for pass 2 of a direct linking loader was successfully
implemented and the result is verified.
Aim:
To implement a macro processor, using C language.
Algorithm:
Step 1: Get the line from the input file.
Step 2: Repeat the following until the opcode field contains END directive.
1. Check if the opcode field contains MACRO directive. If so,
i. Enter the macro name into the NAMTAB.
ii. Read the next line.
iii. Repeat the following until the opcode field contains MEND directive.
a. Enter the line into DEFTAB.
b. Read the next line.
iv. Mark the pointers in the NAMTAB to the beginning and end of the macro
definition in DEFTAB.
226
System Software System Software Lab Programs
Program:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
int n,i,flag;
char ilab[20],iopd[20],oper[20],NAMTAB[20][20];
FILE *fp1,*fp2,*DEFTAB;
clrscr();
fp1=fopen("macin.dat","r");
fp2=fopen("macout.dat","w");
n=0;
rewind(fp1);
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
while(!feof(fp1))
{
if(strcmp(iopd,"MACRO")==0)
{
strcpy(NAMTAB[n],ilab);
DEFTAB=fopen(NAMTAB[n],"w");
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
while(strcmp(iopd,"MEND")!=0)
227
System Software System Software Lab Programs
{
fprintf(DEFTAB,"%s %s %s\n",ilab,iopd,oper);
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
}
fclose(DEFTAB);
n++;
}
else
{
flag=0;
for(i=0;i<n;i++)
{
if(strcmp(iopd,NAMTAB[i])==0)
{
flag=1;
DEFTAB=fopen(NAMTAB[i],"r");
fscanf(DEFTAB,"%s%s%s",ilab,iopd,oper);
while(!feof(DEFTAB))
{
fprintf(fp2,"%s %s %s\n",ilab,iopd,oper);
fscanf(DEFTAB,"%s%s%s",ilab,iopd,oper);
}
break;
}
}
if(flag==0)
fprintf(fp2,"%s %s %s\n",ilab,iopd,oper);
}
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
}
228
System Software System Software Lab Programs
getch();
}
INPUT
MACIN.DAT
M1 MACRO **
** LDA N1
** ADD N2
** STA N3
** MEND **
M2 MACRO **
** LDA N1
** SUB N2
** STA N4
** MEND **
M3 MACRO **
** LDA N1
** MUL N2
** STA N5
** MEND **
** START 1000
** M3 **
** M2 **
** M1 **
** END **
OUTPUT
MACOUT.DAT
** START 1000
229
System Software System Software Lab Programs
** LDA N1
** MUL N2
** STA N5
** LDA N1
** SUB N2
** STA N4
** LDA N1
** ADD N2
** STA N3
** END **
230
System Software System Software Lab Programs
Result:
Thus the program for the implementation of a macro processor was executed
successfully and the result is verified.
231