Sei sulla pagina 1di 64

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;
};
struct table t[20];
int i,j,n;
void create();
void modify();
int search(char variable[],int n);
1
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);
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();

2
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++;
printf("\n Enter the variable and the value : ");
scanf("%s%d",t[n].var,&t[n].value);
}
}
void modify()
{
char variable[10];
int result=0;

3
printf("\n Enter the variable to be searched for : ");
scanf("%s",variable);
result=search(variable,n);
if(result==0)
printf("\n The variable doest not belong to the table.");
else
{
printf("\n The current value of the variable %s is %d.\n Enter the new
variable and its value",t[result].var,t[result].value);
scanf("%s%d",t[result].var,&t[result].value);
}

}
int search(char variable[],int n)
{
int flag;
for(i=1;i<=n;i++)
{
if(strcmp(t[i].var,variable)==0)
{
flag=1;
break;
}
}
if(flag==1)
return i;
else
return 0;
}
void display()
{

4
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

Enter the number of entries: 3


Enter the variable and the value:
NUM1 1
NUM2 2
NUM3 3
SYMBOL TABLE CREATION
1. CREATE
2. INSERT
3. MODIFY
4. SEARCH
5. DISPLAY
6. EXIT
Enter your choice : 5
VARIABLE VALUE
NUM1 1

5
NUM2 2
NUM3 3

SYMBOL TABLE CREATION


1. CREATE
2. INSERT
3. MODIFY
4. SEARCH
5. DISPLAY
6. EXIT
Enter your choice : 2
Enter the variable and the value : NUM4 10

SYMBOL TABLE CREATION


1. CREATE
2. INSERT
3. MODIFY
4. SEARCH
5. DISPLAY
6. EXIT
Enter your choice : 5
VARIABLE VALUE
NUM1 1
NUM2 2
NUM3 3
NUM4 10

SYMBOL TABLE CREATION


1. CREATE
2. INSERT
3. MODIFY

6
4. SEARCH
5. DISPLAY
6. EXIT
Enter your choice : 3
Enter the variable to be searched for : NUM3
The current value of the variable NUM3 is 3.
Enter the new variable and its value NUM7 30

SYMBOL TABLE CREATION


1. CREATE
2. INSERT
3. MODIFY
4. SEARCH
5. DISPLAY
6. EXIT
Enter your choice : 5

VARIABLE VALUE
NUM1 1
NUM2 2
NUM7 30
NUM4 10
SYMBOL TABLE CREATION
1. CREATE
2. INSERT
3. MODIFY
4. SEARCH
5. DISPLAY
6. EXIT
Enter your choice : 4

7
Enter the variable to be searched for : NUM2
Variable=NUM2 Value=2 Location=2

SYMBOL TABLE CREATION


1. CREATE
2. INSERT
3. MODIFY
4. SEARCH
5. DISPLAY
6. EXIT
Enter your choice : 6

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

8
Aim:
To implement a simple text editor with features like insertion / deletion of a character, word, and
sentence.
Algorithm:
1. Design a Menu using switch case
2. Get the choice
3. If choice is “1” then, type the text to be inserted in the editor window
4. If choice is “2” then enter the text to be modified,,,, then type the text which one
is inserted
5. If the choice is “3” then change the color of the text and display the text
6. If the choice is “4” then change the background color of the text and display it.
7. If the choice is “5” then display the text in the editor window
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()

9
{
int ch;
clrscr();
for(;;)
{
printf("\n\tTEXT EDITOR");
printf("\n\t1.Insert\n\t2.Modify\n\t3.ChangeTextcolor\n\t4.ChangeTextBackground\
n\t5.Display\n\t6.Exit");
printf("\n Enter the option:");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: modify();
break;
case 3: changecolor();
break;
case 4: bkground();
break;
case 5: display();
break;
case 6: exit(1);
}
}
}
void insert()
{
printf("\n Enter the no of words in the text:");
scanf("%d",&n);
printf("\n Enter the string:");

10
for(i=0;i<n;i++)
scanf("%s",l[i].text);
}
void modify()
{
printf("Enter the string to be Modified:");
scanf("%s",s);
printf("Enter the string to be inserted:");
scanf("%s",s1);
for(i=0;i<n;i++)
{
x=strcmp(l[i].text,s);
if(x==0)
strcpy(l[i].text,s1);
}
}
void display()
{
printf("The String is: ");
for(i=0;i<n;i++)
cprintf("%s\t",l[i].text);
getch();
}
void changecolor()
{
printf("The String is: ");
textcolor(rand()%100);
for(i=0;i<n;i++)
cprintf("%s\t",l[i].text);
}
void bkground()

11
{
printf("The String is: ");
textbackground(rand()%100);
for(i=0;i<n;i++)
cprintf("%s\t",l[i].text);
getch();
}

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

12
1.Insert
2.Modify
3.ChangeTextcolor
4.ChangeTextBackground
5.Display
6.Exit
Enter the option:5
The String is: welcome to ccet
TEXT EDITOR
1.Insert
2.Modify
3.ChangeTextcolor
4.ChangeTextBackground
5.Display
6.Exit
Enter the option:3
The String is: welcome to ccet
TEXT EDITOR
1.Insert
2.Modify
3.ChangeTextcolor
4.ChangeTextBackground
5.Display
6.Exit
Enter the option:4
The String is: welcome to ccet

Result
Thus a Text editor was implemented in C
3. Implementation of Symbol table using Hashing Technique

13
Aim:
To write a C program to implement symbol table using open addressing hashing technique.
Algorithm:
1. Create a symbol table with size n.
2. Create a menu
1. Insert
2. Delete
3. Display
4. Search
5. Exit
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
4. If the choice is 2, then perform deletion from the symbol table.
(i) Get the address to be deleted as input
(ii) Apply hash function h to the address and find out the hash key value.
(iii) Delete the particular item located in the symbol tables’s hash key value.
5. If the choice is 3, then display the list of items (LABEL, ADDRESS) stored in the symbol table.
6. If the choice is 4, find out where the item is located.
(i) Get the search item address as input.
(ii) Apply hash function h to the input address and get the hash key value.
(iii) If the symbol table entry of that hash key value that is matched with the input, then
display ‘the item is found’.
(iv) Otherwise display ‘item is not found’
7. If the choice is 5, then exit from the program.

Program:

14
#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()
{
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)

15
printf("\n Hash Table is Full");
else
{
count++;
insert();
}
break;
case 2:
count--;
delete();
break;
case 3:
display();
break;
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)
{

16
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);
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);

17
}
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++)
{
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

18
4.SEARCH
5.EXIT
Enter the choice:1

Enter the Label: BEGIN


Enter the addr(except 0):1234

MENU
1.INSERT
2.DELETE
3.DISPLAY
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

19
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: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

20
NUMBER LABEL ADDRESS
----------------------------------------------------------------
4 BEGIN 1234
5 NUM1 5434
6 LOOP 9876

MENU
1.INSERT
2.DELETE
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

21
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter the choice:4
Enter the address to search:1234
Entries are found

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.

4. Implementation of Pass 1 of a Two Pass Assembler

22
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. Other wise 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.
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:

23
#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)
{
fprintf(fp3,"%d\t",locctr);
if(strcmp(label,"**")!=0)
fprintf(fp2,"%s\t%d\n",label,locctr);
rewind(fp4);
fscanf(fp4,"%s",mnemonic);

24
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);
fclose(fp2);
fclose(fp3);
fclose(fp4);
getch();
}

INPUT

25
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
LDX 04
LDCH 50
STA 0C
STB 78
STX 10
STCH 54
TIX 2C
J 3C
JSUB 48

26
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
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.
5. IMPLEMENTATION OF PASS 2 OF A TWO PASS ASSEMBLER

27
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.
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");

28
fp2=fopen("twoout.dat","w");
fp3=fopen("optab.dat","r");
fp4=fopen("symbtab.dat","r");
fscanf(fp1,"%s%s%s",label,opcode,operand);
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)

29
{
flag1=1;
break;
}
}
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);
}

30
fprintf(fp2,"\t%s\t%s\t%s\n",label,opcode,operand);
fclose(fp1);
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

31
SUBR 94
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

32
2015 NUM2 RESW 1
2018 CHAR1 BYTE C’A’ 41
2019 CHAR2 RESB 1
** END BEGIN

Result:
Thus the program to implement a PASS 2 of a two pass assembler, using C language was
executed successfully.

6. IMPLEMENTATION OF A SINGLE PASS ASSEMBLER


Aim:
33
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.
1. If there is a symbol in the label field.
i. Check the symbol table to see if has already been stored and if it is marked as undefined
entry. If so then update the symbol table with the proper address and mark it as defined entry.
ii. Otherwise the symbol is entered into the symbol table 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 and convert the constants in the
operand field to the object code.
b) If the opcode is BYTE, increment the LOCTR by one and convert the constants in the
operand field to the object code.
c) If the opcode is RESW, increment the LOCCTR by integer equivalent of the operand value
*3 and convert the constants in the operand field to the object code.
d) If the opcode is RESB, increment the LOCCTR by the integer equivalent of the operand
value and convert the constants in the operand field to the object code.
3. If there is a symbol in the operand field.
i. Check the symbol table to see if has already been stored. If so, then assemble the object code
by combining the machine code equivalent of the instruction with the symbol address.
ii. Otherwise the symbol is entered into the symbol table and it is marked as undefined entry.
4. If there is no symbol in the operand field, then operand address is assigned as zero, and it is
assembled with the machine code equivalent of the instruction.

34
5. Write the input line along with the object code onto output file.
Step 5: Close all the opened files and exit.

Program:
#include <stdio.h>
#include <conio.h>
#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)

35
{
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(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++;
}
}

36
rewind(fp2);
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,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)
{

37
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));
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)));

38
for(x=1;x<j;x++)
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

39
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

40
STABLE.DAT
BEGIN 2000
LOOP1 2023
LOOP2 2029
NUM1 2006
NUM2 2009
CHAR1 2018
CHAR2 2019

Result:
Thus the program to implement a PASS 2 of a two pass assembler, using C language was
executed successfully.
7. IMPLEMENTATION OF AN ABSOLUTE LOADER
Aim:
To implement an absolute loader, using C language.

41
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
{
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;

42
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)
{
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

43
{
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);
n++;
if(n>3)
{
fprintf(fp2,"\t");
n=0;
count++;
}
if(count>3)

44
{
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
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 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

45
RESULT
Thus the program to implement an absolute loader, using C language was executed
successfully.

8. IMPLEMENTATION OF RELOCATING LOADER


Aim:
To implement a C program for relocation loader.

46
Algorithm:
1. Enter the new starting location to which the object code has to be relocated.
2. Read the content of the input file as strings one at a time in an arrry ”input”.
3. Transfer the string read in array “input” into another array “output”, until “T” is encountered.
4. Move the consecutive next three strings into array “output”.
5. Convert the current string read which is the relocation bit associated with each text record to
binary form.
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”.
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.
8. Repeat step 2 to 8 until an end record is encountered.
9. If object code is in character form convert form it to internal hexadecimal representation.
10. Move object codes to specified locations in memory.
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];
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;

47
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);
if(len>=11)
len=10;
for(i=0;i<len;i++)
{
fscanf(fp1,"%x",&opcode);
fscanf(fp1,"%x",&addr);

48
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])
{
case '0':
strcat(bit,"0");
break;
case '1':
strcat(bit,"1");
break;
case '2':

49
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;
case 'A':
strcat(bit,"1010");
break;
case 'B':
strcat(bit,"1011");
break;
case 'C':
strcat(bit,"1100");

50
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

OUTPUT
RLOUT.DAT
ADDRESS CONTENT
5000 145033
5003 485039
5006 105036
5009 285030
500C 305015

51
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:

52
To implement a pass 1 of a direct linking loader using C program.

Algorithm:
1. Enter the location where the program has to be loaded.
2. Assign the address got from the user as the first control section address.
3. Read the header record of the control section
(i)From the details of the header read store the control section length in a variable
(ii)Enter the control section name with its address into the external symbol table.
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 address until an end
record is reached.
5. Assign the starting address of next control section as the address of the current control section
plus the length of the control section
6. Repeat the process from step 3 to step 5 until there are no more input.

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;

53
clrscr();
f1=fopen("lkin.dat","r");
f2=fopen("lkout.dat","w");
printf("Enter the location where the pgm has to be loaded:");
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);

54
}
while(strcmp(ip,"T")!=0)
fscanf(f1,"%s",ip);
}
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:

55
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.

10. IMPLEMENTATION OF PASS 2 OF A DIRECT LINKING LOADER


Aim:
To implement a pass2 of a direct linking loader using C.

56
Algorithm:
1. Assign the control section address in a variable, CSADR.
2. Read the header record.
(i)From the information available in the header record, store the control section
length in a variable.
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
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
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;

57
void main()
{
FILE *f1,*f2;
int csaddr,maddr,cslth,addr;
clrscr();
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)

58
{
es.addr=es.addr+s;
printf("%d\t %d\n",maddr,es.addr);
break;
}
}
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

59
PROGB 4063
LISTB 4093

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.

11. IMPLEMENTATION OF A MACRO PROCESSOR


Aim:
To implement a macro processor, using C language.

60
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.
2. Check if the opcode field contains Macro Name. If so,
i. Read the corresponding macro definition from the DEFTAB.
ii. Write the macro definition onto the expanded source file.
3. Otherwise, write the line onto the expanded source file.
Step 3: Close all the opened files and exit.

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;

61
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)
{
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);

62
}
break;
}
}
if(flag==0)
fprintf(fp2,"%s %s %s\n",ilab,iopd,oper);
}
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
}
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 **

63
** M2 **
** M1 **
** END **

OUTPUT
MACOUT.DAT

** START 1000
** LDA N1
** MUL N2
** STA N5
** LDA N1
** SUB N2
** STA N4
** LDA N1
** ADD N2
** STA N3
** END **

Result:
Thus the program for the implementation of a macro processor was executed successfully
and the result is verified.

64

Potrebbero piacerti anche