Thursday 9 May 2013

SYSTEM SOFTWARE LAB MANUAL



SYSTEM SOFTWARE LAB MANUAL
List of the Experiments

01IMPLEMENTATION OF SYMBOL TABLE
02IMPLEMENTATION OF PASS 1 OF A TWO PASS ASSEMBLER
03IMPLEMENTATION OF PASS 2 OF A TWO PASS ASSEMBLER
04IMPLEMENTATION OF A SINGLE PASS ASSEMBLER
05IMPLEMENTATION OF AN ABSOLUTE LOADER
06IMPLEMENTATION OF A RELOCATING LOADER
07IMPLEMENTATION OF PASS 1 OF A DIRECT LINKING LOADER
08IMPLEMENTATION OF PASS 2 OF A DIRECT LINKING LOADER
09IMPLEMENTATION OF A SINGLE PASS MACRO PROCESSOR
10IMPLEMENTATION OF A TWO PASS MACRO PROCESSOR
11IMPLEMENTATION OF A SIMPLE TEXT EDITOR

Ex. No. 1
DATE:
Implementation of symbol table

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 tb1[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("\n1. CREATE\n2. INSERT\n3. MODIFY\n4. SEARCH\n5. DISPLAY\n6. 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",tb1[result].var,tb1[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",tb1[i].var,&tb1[i].value);
check:
            if(tb1[i].var[0]>='0'&&tb1[i].var[0]<='9')
            {
printf("\nThe variable should not start with an alphabet.\nEnter the Correct variable name :");
            scanf("%s%d",tb1[i].var,&tb1[i].value);
            goto check;
            }
            for(j=1;j<i;j++)
            {
            if(strcmp(tb1[i].var,tb1[j].var)==0)
            {
            printf("\nThe variable already exists.Enter another variable :\n");
            scanf("%s%d",tb1[i].var,&tb1[i].value);
            goto check;
            }
            }
}
printf("\n The table after creation is : \n");
display();
}

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",tb1[n].var,&tb1[n].value);
check:
            if(tb1[i].var[0]>='0'&&tb1[i].var[0]<='9')
            {
printf("\nThe variable should not start with an alphabet.\nEnter the Correct variable name :");
            scanf("%s%d",tb1[i].var,&tb1[i].value);
            goto check;
            }
            for(j=1;j<n;j++)
            {
            if(strcmp(tb1[j].var,tb1[i].var)==0)
            {
            printf("\nThe variable already exists.Enter another variable :\n");
            scanf("%s%d",tb1[i].var,&tb1[i].value);
            goto check;
            }
            }
printf("\n The table after creation is : \n");
display();
}
}
void modify()
{
char variable[10];
int result=0;
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",tb1[result].var,tb1[result].value);
 scanf("%s%d",tb1[result].var,&tb1[result].value);
 check:
            if(tb1[i].var[0]>='0'&&tb1[i].var[0]<='9')
            {
            printf("\nThe variable should not start with an alphabet.\nEnter the Correct variable
                        name :");
            scanf("%s%d",tb1[i].var,&tb1[i].value);
            goto check;
            }
}
printf("\n The table after creation is : \n");
display();
}

int search(char variable[],int n)
{
int flag;
for(i=1;i<=n;i++)
if(strcmp(tb1[i].var,variable)==0)
{
 flag=1;
 break;
}
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",tb1[i].var,tb1[i].value);
getch();
}


OUTPUT:

                      
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

 The table after creation is :

VARIABLE        VALUE

NUM1            1
NUM2            2
NUM3            3


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

 Enter the variable and the value : NUM1 10

The variable already exists.Enter another variable :
1NUM 10

The variable should not start with an alphabet.
Enter the Correct variable name :NUM4 4

 The table after insertion is :

VARIABLE        VALUE

NUM1            1
NUM2            2
NUM3            3
NUM4            4

1. CREATE
2. INSERT
3. MODIFY
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 NUM3 30

 The table after modification is :

VARIABLE        VALUE

NUM1            1
NUM2            2
NUM3            30
NUM4            4

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

 Enter the variable to be searched for : NUM2

 Variable=NUM2  Value=2 Location=2

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.



Ex. No. 2
DATE:
Implementation of pass 1 of
a two pass assembler


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

1.        Find if there is any operand field after START; initialize the LOCCTR to the operand value.
2.        Other wise 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:

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

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
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.
Ex. No. 3
DATE:
Implementation of pass 2 of
a two pass assembler


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");
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)
{
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]=='X')
{
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);
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
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
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.



Ex. No. 4
DATE:
Implementation of
A SINGLE PASS ASSEMBLER


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

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;
};
struct input table[MAX];

struct symtab
{
char sym[10];
int f,val,ref;
};
struct symtab symtb1[MAX];

void main()
{
int f1,i=1,j=1,flag,locctr,x;
char add[10],code[10],mnemcode[10];
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(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)
{
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');
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)));
for(x=1;x<=j;x++)
printf("%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
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

SYMTAB

LOOP1        2023
LOOP2        2029
NUM1         2006
NUM2         2009
CHAR1       2018
CHAR2       2019






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










Result:
            Thus the program to implement a single pass (Load and Go) assembler, using C language was executed successfully.

Ex. No. 5
DATE:
Implementation of
AN ABSOLUTE LOADER


Aim:
           
To implement an absolute loader, using C language.


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;
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
{
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)
{
fprintf(fp2,"\n%x\t",code[i+1].locctr);
count=0;
}
}
fclose(fp1);
fclose(fp2);
getch();
}







INPUT

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







RESULT
      Thus the program to implement an absolute loader, using C language was
            executed successfully.
Ex. No. 6
DATE:
Implementation of
A RELOCATING LOADER

AIM:

            To implement a C program for relocation loader.

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>
#include<stdlib.h>
struct object_code
{
int locctr;
char add[10];
}opcode[300];
void main()
{
char in[100][16],op[100][16],bin[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 *f1,*f2;
clrscr();
f1=fopen("lin.dat","r");
f2=fopen("lout.dat","w");
printf("Enter the location where the program has to be loaded:");
scanf("%s",stloc);
start=atoi(stloc);
location=start;
tloc=start;
fscanf(f1,"%s",in[i]);
while(strcmp(in[i],"T")!=0)
{
strcpy(op[i],in[i]);
i++;
fscanf(f1,"%s",in[i]);
strcpy(op[i],in[i]);
}
itoa(start,op[2],10);
while(strcmp(in[i],"E")!=0)
{
strcpy(op[i],in[i]);
if(strcmp(in[i],"T")==0)
{
for(j=0;j<3;j++)
{
i++;
fscanf(f1,"%s",in[i]);
strcpy(op[i],in[i]);
}
bitmask=atoi(op[i]);
itoa(bitmask,bin,2);
strcpy(op[i],NULL);
textloc=atoi(op[i-2]);
textloc=textloc+start;
itoa(textloc,op[i-2],10);
for(n=0;n<(textloc-(tloc+tlen));n++)
{
strcpy(opcode[inc].add,"xx");
opcode[inc++].locctr=location++;
}
tlen=atoi(op[i-1]);
tloc=textloc;
k=0;
}
else
{
if(bin[k]=='1')
{
num=0;
len=strlen(op[i]);
strcpy(address,NULL);
for(j=2;j<len;j++)
{
address[num]=op[i][j];
op[i][j]='\0';
num++;
}
loc=atoi(address);
loc=loc+start;
itoa(loc,address,10);
strcat(op[i],address);
}
k++;
len=strlen(op[i]);
num=0;
for(n=0;n<len;n++)
{
opcode[inc].add[num++]=op[i][n];
if(num>1)
{
opcode[inc++].locctr=location++;
num=0;
}   }     }
i++;
fscanf(f1,"%s",in[i]);
}
strcpy(op[i],in[i]);
i++;
fscanf(f1,"%s",in[i]);
loc=atoi(in[i]);
loc=loc+start;
strcpy(op[i],itoa(loc,address,10));
count=0;
i=0;
n=0;
fprintf(f2,"%d\t",opcode[n].locctr);
for(n=0;n<inc;n++)
{
fprintf(f2,"%s",opcode[n].add);
i++;
if(i>3)
{
fprintf(f2,"\t");
i=0;
count++;
}
if(count>3)
{
fprintf(f2,"\n%d\t",opcode[n+1].locctr);
count=0;
}   }
getch();
}
















OUTPUT:

Enter the location where the program has to be loaded: 5000

LIN.DAT

H COPY 000000 001073
T 000000 10 015 140033 481039 100036 280030 300015 481061 311003
                200030 211033 200033
T 000011 19 045 412036 481061 380033 412000 454196 100003 200000
E 000000



LOUT.DAT

5000    14503348        60391050        36285030        30001548
5016    60613110        03200030        21103320        0033xx41
5032    70364810        61385033        41700045        41961050
5048    03200000





















RESULT:
                        Thus the relocation loader were implemented and executed successfully.

Ex. No. 7
DATE:
Implementation of PASS 1 OF
A DIRECT LINKING LOADER

AIM:

            To implement a pass 1 of a direct linking loader by 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;
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);
length=atoi(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);
}
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();
}

OUTPUT:

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

LKOUT.DAT

CSECT           Tsymname     Taddress       Tlength
PROGA                                  3000               70
            LISTA            3040               0
ENDA             3054               0

RESULT:
                        Thus the pass 1 of a direct linking loader were successfully implemented
Ex. No. 8
DATE:
Implementation of PASS 2 OF
A DIRECT LINKING LOADER



AIM:

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

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.
  1. 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
  1. 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();
f1=fopen("OBJ.TXT","r");
f2=fopen("ESTABLE.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;
}
}
rewind(f2);
}
}
csaddr+ =cslth;
}
fcloseall();
getch();
}

INPUT FILE:
OBJ.TXT
H         PROGA         0063
D         LISTA           0040
T          0054              0000
M         0054              LISTB
E          0003              NULL
H         PROGB         0030
D         LISTB           0030
T          0050              0000
M         0050              LISTA
E          0003              NULL

ESTAB.TXT
PROGA          4000
LISTA             4040
PROGB          4063
LISTB             4093

OUTPUT:
Memoryaddr   Object code
4054                0000
4054                4093
4113                0000
4113                4040

Result:
                   Thus the pass 2 of a direct linking loader were successfully implemented

Ex. No. 9
Date:
Implementation of A SINGLE PASS MACRO PROCESSOR


Aim:
           
To implement a single pass 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.

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;
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);
       }
       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                  **
**        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 to implement a single pass macro processor was executed
                         Successfully.

Ex. No. 10
Date:
Implementation of A TWO PASS MACRO PROCESSOR


Aim:
           
To implement a two pass 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.

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>
void main()
{
char a[10],b[10],c[10],a1[10],b1[10],c1[10];
FILE *fp1,*fp2,*fp3,*fp5,*fp4;
fp5=fopen("nametab.txt","w");
fp4=fopen("exp.txt","w");
fp1=fopen("in11.txt","r");
fp2=fopen("deftab.txt","w");
fscanf(fp1,"%s %s %s",a,b,c);
            if(strcmp(b,"MACRO")==0)
            {
            fprintf(fp5,"%s\n",a);         
            }         
            fscanf(fp1,"%s %s %s",a,b,c);
            while(strcmp(b,"MEND")!=0)
            {
                        fprintf(fp2,"%s %s %s\n",a,b,c);
                        fscanf(fp1,"%s %s %s",a,b,c);
            }
            fprintf(fp2,"%s %s %s\n",a,b,c);
            fclose(fp2);
            fscanf(fp1,"%s %s %s",a,b,c);
            while(!feof(fp1))
            {         
                        fscanf(fp1,"%s %s %s",a,b,c);
                        if(strcmp(b,"SUM")==0)
                        {
                        fp3=fopen("deftab.txt","r");
                        fscanf(fp3,"%s %s %s",a1,b1,c1);
                        //while(!feof(fp3))
                        while(strcmp(b1,"MEND")!=0)
                        {
                         fprintf(fp4,"%s %s %s\n",a1,b1,c1);
                        fscanf(fp3,"%s %s %s",a1,b1,c1);
                        }
                        }
                        fclose(fp3);  
             
            //printf("hi %s\n",b);
            } }


INPUT FILE:

SUM   MACRO         a1
-           LDA                #5
-           LDS                a1
-           ADDR            A,S
-           STA                RES
-           MEND                        -
-           SUM               6
-           SUM                8
-           SUM                15
-           SUM              14
 
OUTPUT:

nametab.txt:
SUM
exp.txt:-
-           LDA               #9
-           LDS               a1
-           ADDR                        A,S
-           STA                RES
-           LDA                #5
-           LDS                a1
-           ADDR            A,S
-           STA                RES
-           LDA                #5
-           LDS                a1
-           ADDR           A,S
-           STA                RES
-           LDA                #5
-           LDS                a1
-           ADDR                        A,S
-           STA                RES

deftab.txt:
-           LDA               #5
-           LDS               a1
-           ADDR                        A,S
-           STA                RES
-           MEND                        -

Result:
                   Thus the program to implement two pass macro processor was executed
                        successfully.

Ex.no.11
Date:
Implementation of
A SIMPLE TEXT EDITOR

Aim:
            To write a c program to implement a simple text editor with features like insertion/deletion of a character, word, and sentence.

Algorithm:

Step 1: Start

Step 2: Design a text editor menu which contains the options like INSERT,
            SEARCH and DISPLAY.

Step 3: If the option selected is INSERT, then
1.      Provide a blank screen for the user to type the text
2.      Type the required statement.

Else if the option is SEARCH
1. Search the typed text
2. If the text is found, return the corresponding text
    Then type the required text.
3. Else print string not found.

Step 4: Using the DISPLAY() function, display the text.

Step 5: Stop the execution.













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 search();
void display();
void main()
{
int ch;
clrscr();
do
{
printf("\t MENU\n");
printf("1.Insert \n 2.Search\n 3.Display\n 4.Exit");
printf("\n Enter the opinion:");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
search();
break;
case 3:
display();
break;
case 4:
exit(1);
}}
while(ch<=4);
getch();
}
void insert()
{
printf("\n Enter the no of words in the text:");
scanf("%d",&n);
printf("\n Enter the string:");
for(i=1;i<=n;i++)
{
scanf("%s",l[i].text);
}
}
void search()
{
printf("Enter the string to be searched:");
scanf("%s",s);
printf("Enter the string to be inserted:");
scanf("%s",s1);
for(i=1;i<=n;i++)
{
x=strcmp(l[i].text,s);
if(x==0)
{
strcpy(l[i].text,s1);
}
}
if(x!=0)
printf("String not found\n");
}
void display()
{
printf("The final string is:");
for(i=1;i<=n;i++)
{
printf("%s\t",l[i].text);
}
getch();
}


OUTPUT:

MENU
1. Insert
2. Search
3. Display
4. Exit
Enter the opinion: 1
Enter the no. of words in the text: 3
Enter the string: HAI HELLO BYE

MENU
1. Insert
2. Search
3. Display
4. Exit
Enter the opinion: 2
Enter the string to be searched: BYE
Enter the string to be inserted
THANKS

MENU
1. Insert
2. Search
3. Display
4. Exit
Enter the opinion: 2
Enter the string to be searched:BYE
Enter the string to be inserted:WELCOME
String not found


MENU
1. Insert
2. Search
3. Display
4. Exit
Enter the opinion: 3
The final string is: HAI HELLO THANKS








Result:
Thus, the c program to implement a simple text editor with features like insertion/deletion of a character, word, and sentence was implemented successfully.

No comments:

Post a Comment