You are on page 1of 51

Ex No: 1

CREATION OF SYMBOL TABLE

AIM:

To write a C program to understand the working function of assembler in first pass


creating symbol table where the tables are entered in the first pass along with the
corresponding addresses.

ALGORITHM:

STEP 1: Start the program execution.


STEP 2: Create a structure for opcode table and assign the values.
STEP 3: Create a structure for symbol table and assign the values.
STEP 4: Create a structure for intermediate code table and assign the values.
STEP 5: Write the opcode in separate file and machine code in another separate file.
STEP 6: Open the opcode file and compare it with the given machine code and then
generate opcode for corresponding source code.
STEP 7: Check the forward reference in intermediate code and print the corresponding
jump statement address.
STEP 8: Compare machine code with the opcode.If any jump statement with backward
reference is present, then print backward reference address.
STEP 9: For symbol table, print the symbol and address of the symbol.
STEP 10: Stop the program execution.
PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct table
{
char var[10];
int value;
};
struct table tbl[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];
clrscr();
do
{
printf("Enter ur choice:\n1.Create\n2.Insert\n3.Modify\n4.Search\n5.Display\n6.Exit");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
insert();
break;
case 3:
modify();
break;
case 4:
printf("Enter the variabe to be searched\n");
scanf("%s",&v);
result=search(v,n);
if(result==0)
printf("The variable does not belong to the table\n");
else
printf("The location of variable is %d. The value of %s is %d",
result,tbl[result].var,tbl[result].value);
break;
case 5:
display();
break;
case 6:
exit(1);
}
}
while(ch!=6);
getch();
}
void create()
{
printf("Enter the number of entries\n");
scanf("%d",&n);
printf("Enter the variable and the value:\n");
for(i=1;i<=n;i++)
{
scanf("%s%d",tbl[i].var,&tbl[i].value);
check:
if(tbl[i].var[0]>='0' && tbl[i].var[0]<='9')
{
printf("The variable should start with an alphabet\nEnter the correct variable name\n");
scanf("%s%d",tbl[i].var,&tbl[i].value);
goto check;
}
check1:
for(j=1;j<1;j++)
{
if(strcmp(tbl[i].var,tbl[j].var)==0)
{
printf("The variable already exists.\nEnter another variable\n");
scanf("%s%d",tbl[i].var,&tbl[i].value);
goto check1;
}
}
}
printf("The table after creation is\n");
display();
}
void insert()
{
if(i>=20)
printf("Cannotinsert. Table is full");
else
{
n++;
printf("Enter the variable and value\n");
scanf("%s%d",tbl[n].var,&tbl[n].value);
check:
if(tbl[i].var[0]>='0' && tbl[i].var[0]<='9')
{
printf("The variable should start with alphabet\nEnter the correct variable name\n");
scanf("%s%d",tbl[i].var,&tbl[i].value);
goto check;
}
check1:
for(j=1;j<n;j++)
{
if(strcmp(tbl[j].var,tbl[i].var)==0)
{
printf("The variable already exist\nEnter another variable\n");
scanf("%s%d",tbl[i].var,&tbl[i].value);
goto check1;
}
}
printf("The table after insertion is\n");
display();
}
}
void modify()
{
char variable[10];
int result=0;
printf("Enter the variable to be modified\n");
scanf("%s",&variable);
result=search(variable,n);
if(result==0)
printf("%sdoes not belong to the table",variable);
else
{
printf("The current value of the variable%s is %d, Enter the new variable and its
value",tbl[result].var,tbl[result].value);
scanf("%s%d",tbl[result].var,&tbl[result].value);
check:
if(tbl[i].var[0]>='0' && tbl[i].var[0] <= '9')
{
printf("The variable should start with alphabet\n Enter the correct variable name\n");
scanf("%s%d",tbl[i].var,&tbl[i].value);
goto check;
}
}
printf("The table after modification is\n");
display();
}
int search(char variable[],int n)
{
int flag;
for(i=1;i<=n;i++)
{
if(strcmp(tbl[i].var,variable)==0)
{
flag=1;
break;
}
}
if(flag==1)
return i;
else
return 0;

}
void display()
{
printf("Variable\t value\n");
for(i=1;i<=n;i++)
printf("%s\t\t%d\n",tbl[i].var,tbl[i].value);
}
OUTPUT:
Enter ur choice:
1.Create
2.Insert
3.Modify
4.Search
5.Display
6.Exit
1
Enter the number of entries
2
Enter the variable and the value:
A 26
B 42
The table after creation is
Variable value
A 26
B 42

Enter ur choice:
1.Create
2.Insert
3.Modify
4.Search
5.Display
6.Exit
2
Enter the variable and value
D 10
The table after insertion is
Variable value
A 26
B 42
D 10

Enter ur choice:
1.Create
2.Insert
3.Modify
4.Search
5.Display
6.Exit
3
Enter the variable to be modified
D
The current value of the variableD is 10, Enter the new variable and its value
C
20
The table after modification is
Variable value
A 26
B 42
C 20

Enter ur choice:
1.Create
2.Insert
3.Modify
4.Search
5.Display
6.Exit

4
Enter the variabe to be searched
A
The location of variable is 1. The value of A is 26

Enter ur choice:
1.Create
2.Insert
3.Modify
4.Search
5.Display
6.Exit
5
Variable value
A 26
B 42
C 20
Enter ur choice:
1.Create
2.Insert
3.Modify
4.Search
5.Display
6.Exit
6

RESULT:

Thus the program has been done and the output has been verified.
Ex No: 2

PASS ONE OF TWO PASS ASSEMBLER

AIM:

To write a C program to implement pass one of two pass assembler.

ALGORITHM:

STEP 1: Start the program execution.


STEP 2: Create a symbol table.
STEP 3: Check for specified table in the symbol table.
STEP 4: If symbol is not found, show any error message or add it to SYMTAB.
STEP 5: For word increment the value by 3.
STEP 6: For RESW, operand value is incremented by 3 into locctr.
STEP 7: For BYTE, increment according to instructions.
STEP 8: Store the address value as program address.
STEP 9: Stop the program execution.
PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct sourcefile
{
char label[10],instr[10],operand[10];
}s;
struct pseudocd
{
char s[10];
}ps;
struct source_result
{
int address;
char label[10],instr[10],oper[10];
}res;
int find(char str[10])
{
FILE *pse;
pse=fopen("pseudo.txt","r");
while(!feof(pse))
{
fscanf(pse,"%s",ps.s);
if(strcmp(str,ps.s)==0)
{
fclose(pse);
return 1;
}
}
fclose(pse);
return 0;
}
void main()
{
FILE *ss,*ss1;
int addr,detect;
clrscr();
ss=fopen("s.txt","r+");
ss1=fopen("res.txt","w+");
while(!feof(ss))
{
fscanf(ss,"%s%s%s",s.label,s.instr,s.operand);
detect=find(s.instr);
if(detect==1)
{
if(strcmp(s.instr,"START")==0)
addr=atoi(s.operand);
else if(strcmp(s.instr,"WORD")==0)
addr=res.address+3;
else if(strcmp(s.instr,"RESW")==0)
addr=res.address+(3*atoi(s.operand));
else if(strcmp(s.instr,"RESB")==0)
addr=res.address+atoi(s.operand);
else if(strcmp(s.instr,"BYTE")==0)
addr=res.address+strlen(s.operand)-3;
else
addr=res.address+3;
strcpy(res.label,s.label);
strcpy(res.instr,s.instr);
strcpy(res.oper,s.operand);
fprintf(ss1,"%d\t%s\t%s\t%s\n",res.address,res.label,res.instr,res.oper);
res.address=addr;
}
else
{
printf("Error Encounted");
fclose(ss);
fclose(ss1);
getch();
}
}
printf("\nPass 1 Completed");
fclose(ss);
fclose(ss1);
getch();
}
OUTPUT:

Input:

pseudo.txt

START
READ
WORD
BYTE
RESW
RESB
END

s.txt

NULL START 1000


A RESB 100
LOOP READ A
B BYTE C"LOOP"
C WORD 100
G RESW 5
NULL END NULL

Output:
Pass 1 Completed
res.txt

0 NULL START 1000


1000 A RESB 100
1100 LOOP READ A
1103 B BYTE C"LOOP"
1107 C WORD 100
1110 G RESW 5
1125 NULL END NULL

RESULT:

Thus the program has been done and the output has been verified.
Ex No: 3

PASS TWO OF TWO PASS ASSEMBLER

AIM:

To write a C program to implement pass two of two pass assembler.

ALGORITHM:

STEP 1: Start the program execution.


STEP 2: Assemble the instructions and generate object program.
STEP 3: Search SYMTAB for operand value, provided symbol is found in the operand
field.
STEP 4: Opcodes in ‘BYTE’ or ‘WORD’ is converted from constant to object code.
STEP 5: Write object program and assembly listing.
STEP 6: Stop the program execution.
PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct opc
{
int len;
char mnemonic[10],code[3];
}opcode;
struct opd
{
int address;
char code[10];
}op;
struct source_result
{
int address;
char label[10],instr[10],operand[10];
}res;
struct res
{
int a;
char c[10];
}s;
void main()
{
FILE *r,*o,*result,*symb;
int i,j,found=0,l;
char s1[10];
clrscr();
r=fopen("re.txt","r");
o=fopen("opcode.txt","r");
result=fopen("output.txt","w");
symb=fopen("symbol.txt","r");
while(!feof(r))
{
fscanf(r,"%d%s%s%s",&res.address,&res.label,&res.instr,&res.operand);
rewind(o);
rewind(symb);
found = 0;
while(!feof(o))
{
fscanf(o,"%d%s%s",&opcode.len,&opcode.mnemonic,&opcode.code);
if(strcmp(res.instr,opcode.mnemonic)==0)
{
op.address=res.address;
strcpy(op.code,opcode.code);
fprintf(result,"%d\t%s",op.address,op.code);
found=1;
break;
}
}
if(found==0)
continue;
while(!feof(symb))
{
fscanf(symb,"%d%s",&s.a,&s.c);
if(strcmp(res.operand,s.c)==0)
{
fprintf(result,"%d\n",s.a);
break;
}
else if(strcmp(res.operand,"NULL")==0)
{
fprintf(result,"0000");
break;
}
else if(res.operand[0]=='#')
{
strcpy(s1,res.operand);
l=strlen(s1)-1;
for(i=4;i>l;i--)
fprintf(result,"0");
for(j=1;j<=l;j++)
fprintf(result,"%c",s1[j]);
fprintf(result,"\n");
break;
}
}
}
printf("Pass 2 completed!");
fcloseall();
getch();
}
OUTPUT:

opcode.txt

1 READ 1F
1 LDA 00

re.txt

0 NULL START 1000


1000 A RESB 100
1100 LOOP READ A
1103 B BYTE C"INDIA"
1107 C WORD INDIA
1110 G RESW 5
1125 CLOOP LDA #5
1128 NULL END NULL

symbol.txt

1000 A
1100 LOOP
1107 C
1110 G
1125 CLOOP

Output
Pass 2 completed!
output.txt

1100 1F1000
1125 000005

RESULT:

Thus the program has been done and the output has been verified.
Ex No: 4

IMPLEMENTATION OF SINGLE PASS ASSEMBLER

AIM:

To write a C program to implement single pass assembler.

ALGORITHM:

STEP 1: Start the program execution.


STEP 2: Assembler simply generate object code as it scans the source code.
STEP 3: If the instruction operand is a symbol text, has not yet been defined, the
operands address is omitted.
STEP 4: When nearly half the program translation is over, some of the forward reference
problem are existed.
STEP 5: Combine the process to the end of the program to fill forward reference
property.
STEP 6: At the end of the program, the symbol table entries with ‘x’ are undefined.
STEP 7: Stop the program execution.
PROGRAM :

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
FILE *f1,*f2;
char ch,str[30],str1[10],str2[30],cstr[15];
int i,j,num,q,r;
clrscr();
printf("Enter your assembly instructions\n");
f1=fopen("asin","w");
while(1)
{
ch=getchar();
if(ch=='*')
break;
fputc(ch,f1);
}
fclose(f1);
f1=fopen("asin","r");
f2=fopen("asout","w");
while(1)
{
fgets(str,25,f1);
strncpy(str1,str,3);
str1[3]='\0';
j=0;
for(i=3;i<strlen(str);i++)
{
str2[j]=str[i];
j++;
}
str2[j]='\0';
if((strcmp(str1,"lda"))==0)
{
fputs("3a\t",f2);
fputs(str2,f2);
}
else if((strcmp(str1,"mov"))==0)
{
fputs("47\n",f2);
}
else if((strcmp(str1,"add"))==0)
{
fputs("80\n",f2);
}
else if((strcmp(str1,"sub"))==0)
{
fputs("90\n",f2);
}
else if((strcmp(str1,"hlt"))==0)
{
fputs("76\n",f2);
break;
}
else if((strcmp(str1,"sta"))==0)
{
fputs("32\t",f2);
num=atoi(str2)
q=num/100;
r=num%100;
if(r==0)
fputs("00\t",f2);
else
fputs(itoa(r,cstr,10),f2);
fputs("\t",f2);
fputs(itoa(q,cstr,10),f2);
fputs("\n",f2);
}
else
{
fputs("error\n",f2);
}
}
fclose(f1);
fclose(f2);
f2=fopen("asout","r");
printf("\nTHE OBJECT CODE CONTENTS\n");
ch=fgetc(f2);
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(f2);
}
fclose(f2);
getch();
}
OUTPUT:

Input
Enter your assembly instructions
lda 5000
sub z
sta 9988
hlt
*

Output
THE OBJECT CODE CONTENTS
3a 00 50
90
32 88 99
76

RESULT:

Thus the program has been done and the output has been verified.
Ex No: 5

IMPLEMENTATION OF MACROPROCESSOR

AIM:

To write a C program to implement MACROPROCESSOR.

ALGORITHM:

STEP 1: Start the program execution.


STEP 2: Macro instructions are included in a separate file.
STEP 3: The instructions with ‘macro’,’mend’,’call’ on them should not be printed in the
output.
STEP 4: Print all other instructions such as start,load,store,add,sub etc with their values.
STEP 5: Stop the program execution.
PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char n1,n,c1,i;
char fn[10][10],ilab[20],iopd[20],m[20][3],oper[20],opd[20];
FILE *fp1,*fp2,*p[5];
clrscr();
n=0;
fp1=fopen("z:\macin.txt","r");
while(!feof(fp1))
{
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
if(strcmp(iopd,"macro")==0)
n++;
}
printf("no.of macros=%d\n",n);
n1=n;
printf("enter the text filename \n");
for(i=0;i<n;i++)
{
scanf("%s",fn[i]);
p[i]=fopen(fn[i],"w");
}
n=0;
rewind(fp1);
while(!feof(fp1))
{
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
if(strcmp(iopd,"macro")==0)
{
strcpy(m[n],oper);
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
while(strcmp(iopd,"mend")!=0)
{
fprintf(p[n],"%s%s%s\n",ilab,iopd,oper);
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
}
fclose(p[n]);
n++;
}
}
for(i=0;i<n1;i++)
p[i]=fopen(fn[i],"r");
fp2=fopen("z:\outm.txt","w");
rewind(fp1);
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
while(!feof(fp1))
{
if(strcmp(iopd,"call")==0)
{
for(i=0;i<n1;i++)
{
if(strcmp(m[i],oper)==0)
{
rewind(p[i]);
fscanf(p[i],"%s%s%s",ilab,iopd,oper);
while(!feof(p[i]))
{
fprintf(fp2,"%s%s%s",ilab,iopd,oper);
c1=1;
fscanf(p[i],"%s%s%s",ilab,iopd,oper);
}
break;
}
}
}
if(c1!=1)
fprintf(fp2,"%s%s%s\n",ilab,iopd,oper);
c1=0;
fscanf(fp1,"%s%s%s",ilab,iopd,oper);
}
fprintf(fp2,"%s%s%s\n",ilab,iopd,oper);
}
OUTPUT:

Input:
macin.txt
** macro m1
** move a,b
** mend ---
** macro m2
** lda b
** mend ---
** start 1000
** lda a
** call m1
** call m2
** add a,b

Output:
No. of Macros =2
Enter the Text file Names
Mac1.dat
Mac2.dat

outm.txt
** macro m1
** move a,b
** mend---
** macro m2
** lda b
** mend ---
** start 1000
** lda a
** move a,b callm1
** lda b callm2
** add a,b

mac1.dat
** move a,b

mac2.dat
** lda b

RESULT:

Thus the program has been done and the output has been verified.
Ex No: 6

IMPLEMENTATION OF ABSOLUTE LOADER

AIM:

To write a C program to implement absolute loader.

ALGORITHM:

STEP 1: Start the program execution.


STEP 2: Get the header record.
STEP 3: Get the text record.
STEP 4: Get the end record.
STEP 5: Display the program name.
STEP 6: Display the starting address.
STEP 7: Display the program length.
STEP 8: Display the text address.
STEP 9: Stop the program execution.
PROGRAM:

#include <stdio.h>
#include <conio.h>
void main()
{
FILE *myfile;
int i=0,f=0,f1=0,j,k;
char ch,fn[30],stadd[15],textrec[80];
clrscr();
myfile=fopen("in.txt","r");
if(myfile == NULL)
{
puts("Cannot open the file");
exit(0);
}
while((!feof(myfile)) && f1==0)
{
while(f1==0)

{
ch = fgetc(myfile);
if((ch=='H' || ch=='h' ) && f==0)
f=1;
if(ch=='-')
{
f1=1;
break;
}
if(ch != 'H' && f==1 && f1==0)
{
fn[i]=ch;
i++;
fn[i]='\0';
}
}
if(f1==1)
for(j=0;j<=12;j++)
stadd[j]=(ch=fgetc(myfile));
stadd[j]='\0';
while(!feof(myfile))
{
ch=fgetc(myfile);
if(ch=='T')
for(k=1;k<=80;k++)
textrec[k]=(ch=fgetc(myfile));
textrec[k]='\0';
}

}
printf("Program name is %s \n",fn);
printf("\n Starting Address ");
for(i=0;i<=5;i++)
printf("%c",stadd[i]);

printf("\n Length of the Program ");


for(i=7;i<=12;i++)
printf("%c",stadd[i]);

printf("\n \n Text Address %s \n",textrec);


for(i=1;i<=80;i++)
{
if((i%7)!=0)
printf("%c",textrec[i]);
else
printf("\n");
}
getch();
}
OUTPUT:

Input
IN.TXT
HCOPY-001000-00107A
T001000-141033-482039-001036-281030-301015-482061-00102D
001000

Output
Program name is COPY
Starting address   1000
Length of the Program 00107A
Text Address
001000
141033
482039
001036
281030
301015
482061
00102D
001000

RESULT:

Thus the program has been done and the output has been verified.
Ex No: 7

IMPLEMENTATION OF RELOCATION LOADER

AIM:

To write a C program to implement relocation loader.

ALGORITHM:

STEP 1: Start the program execution.


STEP 2: Enter the starting address location for relocating the object code.
STEP 3: Transfer the input array into output array.
STEP 4: Convert the current string array into binary form,where current string is
relocating bit.
STEP 5: Relocating bit is subjected to required changes before transferring input to
output and also move object code
STEP 6: Stop the program execution.
PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct object_code
{
int locctr;
char add[10];
}obcode[300];
void main()
{
char input[100][16],output[100][16],binary[20],address[20],stloc[10];
int len,bitmask,loc,tlen=0,tloc,textloc,i=0,location,j,k,count=0,start,n,num=0,inc=0;
FILE *fp1,*fp2;
clrscr();
fp1=fopen("z:\ssinput.dat","r");
fp2=fopen("z:\ssoutput.dat","w");
printf("Enter the location where the program has to be loaded:\n");
scanf("%s",stloc);
start=atoi(stloc);
location=start;
tloc=start;
fscanf(fp1,"%s",input[i]);
while(strcmp(input[i],"T")!=0)
{
strcpy(output[i],input[i]);
i++;
fscanf(fp1,"%s",input[i]);
strcpy(output[i],input[i]);
}
itoa(start,output[2],10);
while(strcmp(input[i],"E")!=0)
{
strcpy(output[i],input[i]);

if(strcmp(input[i],"T")==0)
{
for(j=0;j<3;j++)
{
i++;
fscanf(fp1,"%s",input[i]);
strcpy(output[i],input[i]);
}
bitmask=atoi(output[i]);
itoa(bitmask,binary,2);
strcpy(output[i],"NULL");
textloc=atoi(output[i-2]);
textloc=textloc+start;
itoa(textloc,output[i-2],10);
for(n=0;n<(textloc-(tloc+tlen));n++)
{
strcpy(obcode[inc].add,"xx");
obcode[inc++].locctr=location++;
}
tlen=atoi(output[i-1]);
tloc=textloc;
k=0;
}
else
{
if(binary[k]=='1')
{
num=0;
len=strlen(output[i]);
strcpy(address,NULL);
for(j=2;j<len;j++)
{
address[num]=output[i][j];
output[i][j]='\0';
num++;
}
loc=atoi(address);
loc=loc+start;
itoa(loc,address,10);
strcat(output[i],address);
}
k++;
len=strlen(output[i]);
num=0;
for(n=0;n<len;n++)
{
obcode[inc].add[num++]=output[i][n];
if(num>1)
{
obcode[inc++].locctr=location++;
num=0;
}
}
}
i++;
fscanf(fp1,"%s",input[i]);
}
strcpy(output[i],input[i]);
i++;
fscanf(fp1,"%s",input[i]);
loc=atoi(input[i]);
loc=loc+start;
strcpy(output[i],itoa(loc,address,10));
count=0;
i=0;
n=0;
fprintf(fp2,"%d\t",obcode[n].locctr);
for(n=0;n<inc;n++)
{
fprintf(fp2,"%s",obcode[n].add);
i++;
if(i>3)
{
fprintf(fp2,"\t");
i=0;
count++;
}
if(count>3)
{
fprintf(fp2,"\n%d\t",obcode[n+1].locctr);
count=0;
}
}
getch();
}
OUTPUT:

Input:

ssinput.dat

H COPY 000000 001073


T 000000 10 015 140035 431039 100036 280030 300065 481061 311003
200030 211033 200033
T 000011 19 045 140036 481060 380033 412000 454196 100005 20000
T 000031 15 135 140030 430030 141013 301044 241064 210030 301057
546275 212064 381045
T 000058 05 056 100036 520000 151000 301000
T 000065 19 080 340030 141079 301064 503039 152079 220030 3810064
432000 25
E 000000

Output:

Enter the location where the program has to be loaded:


3000

ssoutput.dat

3000 14303543 40391030 36283030 30006548


3016 10613110 03200030 21103320 0033xx14
3032 30364810 60383033 41500045 41961030
3048 052000xx 14303043 00301410 13301044
3064 24106421 30303040 57549275 21206438
3080 1045xxxx xxxxxxxx xxxxxxxx xxxx1030
3096 36523000 15400030 1000xxxx 34303014
3112 10793040 64503039 15207922 00303810
3128 06432000 25

RESULT

Thus the above program has been done and output is verified.
Ex No: 8

IMPLEMENTATION OF PASS ONE OF DIRECT LINKING LOADER

AIM:

To write a C program to implement pass one of direct linking loader.

ALGORITHM:

STEP 1: Start the program execution.


STEP 2: Enter the location when the program has to be loaded.
STEP 3: Assign the address got from the user as the first control section address.
STEP 4: Read the loader record of control section.
STEP 5: Enter the symbol into the symbol table with address and also with control
section address
STEP 6: Address of the control section and control section length together is the starting
address of next control section.
STEP 7: Stop the program execution.
PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10
struct estab
{
char csect[10];
char sym_name[10];
long int add;
int length;
}
table[MAX];
void main()
{
FILE *fp1,*fp2;
char input[10];
long int i,count=0,start,length,loc;
clrscr();
fp1=fopen("link1in.c","r");
fp2=fopen("file1.c","w");
printf("Enter the location where the program has to be loaded");
scanf("%lx",&start);
fprintf(fp2,"CSECT\t\tSYMNAME\t\tADDRESS\tLENGTH\n");
rewind(fp1);
while(!feof(fp1))
{
fscanf(fp1,"%s",input);
if(strcmp(input,"H")==0)
{
fscanf(fp1,"%s",input);
strcpy(table[count].csect,input);
strcpy(table[count].sym_name,"\0");
fscanf(fp1,"%s",input);
table[count].add=atoi(input)+start;
fscanf(fp1,"%s",input);
length=atoi(input);
table[count++].length=atoi(input);
fscanf(fp1,"%s",input);
}
if(strcmp(input,"D")==0)
{
fscanf(fp1,"%s%lx",input,&loc);
while((strcmp(input,"R")!=0))
{
strcpy(table[count].csect,"\0");
strcpy(table[count].sym_name,input);
table[count].add=loc+start;
table[count++].length=0;
fscanf(fp1,"%s%lx",input,&loc);
}
while(strcmp(input,"T")!=0)
fscanf(fp1,"%s",input);
}
if(strcmp(input,"T")==0)
while(strcmp(input,"E")!=0)
fscanf(fp1,"%s",input);
fscanf(fp1,"%s",input);
start=start+length;
}
for(i=0;i<count;i++)
fprintf(fp2,"%s\t\t%s\t\t%lx\t\t
%d\n",table[i].csect,table[i].sym_name,table[i].add,table[i].length);
getch();
}
OUTPUT:

Input File:
link1in.c
H PROGA 000000 000070
D LISTA 000040 ENDA 000054
R LISTB ENDB LISTC ENDC
T 000020 10 03201D 77100004 150014
T 000054 16 100014 15100006 00002F 100014 FFFFC0
M 000024 05+LISTB
M 000054 06+LISTC
M 000058 06+ENDC
M 000064 06+LISTB
E 000000

H PROGB 000000 000088


D LISTB 000060 ENDB 000070
R LISTA ENDA LISTC ENDC
T 000036 11 031000000 772027 05100000
T 000070 18 100000 05100000 05100020 05100030 100000
M 000037 05+LISTA
M 000044 05+ENDA
M 000070 06+ENDA
M 000074 06+ENDC
M 000078 06+ENDC
M 000082 06+ENDA
E 000000

H PROGC 000000 000057


D LISTC 00003 ENDC 00042
R LISTA ENDA LISTB ENDB
T 000018 12 031000000 77100004 05100000
T 000042 15 100030 100008 1000011 100000 100000
M 000019 05+LISTA
M 000023 05+LISTB
M 000027 05+ENDA
M 000048 06+LISTA
M 000051 06+ENDA
M 000054 06+LISTB
E 000000
Enter the location where the program has to be loaded
3000
Output file
file1.c

CSECT SYMNAME ADDRESS LENGTH


PROGA 3000 70
LISTA 3040 0
ENDA 3054 0
PROGB 3046 88
LISTB 30a6 0
ENDB 30b6 0
PROGC 309e 57
LISTC 30a1 0
ENDC 30e0 0

RESULT:

Thus the program has been done and the output has been verified.
Ex No: 9

IMPLEMENTATION OF PASS TWO OF DIRECT LINKING LOADER

AIM:

To write a C program to implement pass two of direct linking loader.

ALGORITHM:

STEP 1: Start the program execution.


STEP 2: Find the control section length from the header record.
STEP 3: In the text record, character form is converted to machine representation.
STEP 4: Move the object code from record to memory location.
STEP 5: In the modification record, search for symbol to be modified from external table.
Found symbol address is added or subtracted with corresponding symbol address
and value at starting location.
STEP 6: Stop the program execution.
PROGRAM:
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct ext_table
{
char csect[10];
char sname[10];
int padd;
int plen;
}
estab[20];
struct object_code
{
char code[15];
int add;
}
obcode[500];
void main()
{
FILE *fp1,*fp2,*fp3;
int
i,j,n=0,num=0,inc=0,count=0,record=0,pstart,exeloc,start,textloc,loc,mloc[30],textlen,ml
en[30],length,location,x,y;
long int newadd;
char *add1,operation,lbl[10],input[10],label[30][10],address[10];
clrscr();
fp1=fopen("link1.c","r");
fp2=fopen("link2.c","r");
fp3=fopen("link3.c","w");
rewind(fp1);
rewind(fp2);
rewind(fp3);
while(!feof(fp2))
{
fscanf(fp2,"%s%s%d
%d",estab[num].csect,estab[num].sname,&estab[num].padd,&estab[num].plen);
num++;
}
exeloc=estab[0].padd;
loc=exeloc;
start=loc;
while(!feof(fp1))
{
fscanf(fp1,"%s",input);
if(strcmp(input,"H")==0)
{
fscanf(fp1,"%s",input);
for(i=0;i<num;i++)
if(strcmp(input,estab[i].csect)==0)
{
pstart=estab[i].padd;
break;
}
while(strcmp(input,"T")!=0)
fscanf(fp1,"%s",input);
}
do
{
if(strcmp(input,"T")==0)
{
fscanf(fp1,"%d",&textloc);
textloc=textloc+pstart;
for(i=0;i<(textloc-loc);i++)
{
strcpy(obcode[inc].code,"xx");
obcode[inc++].add=start++;
}
fscanf(fp1,"%d",&textlen);
loc=textloc+textlen;
}
else if(strcmp(input,"M")==0)
{
fscanf(fp1,"%d",&mloc[record]);
mloc[record]=mloc[record]+pstart;
fscanf(fp1,"%d",&mlen[record]);
fscanf(fp1,"%s",label[record++]);
}
else
{
length=strlen(input);
x=0;
for(i=0;i<length;i++)
{
obcode[inc].code[x++]=input[i];
if(x>1)
{
obcode[inc++].add=start++;
x=0;
}
}
}
fscanf(fp1,"%s",input);
}
while(strcmp(input,"E")!=0);
if(strcmp(input,"E")==0)
fscanf(fp1,"%s",input);
}
for(n=0;n<record;n++)
{
operation=label[n][0];
length=strlen(label[n]);
for(i=1;i<length;i++)
lbl[i-1]=label[n][i];
lbl[length-1]='\0';
length=0;
strcpy(address,"\0");
location=mloc[n]-exeloc;
loc=location;
count=0;
while(length<mlen[n])
{
strcat(address,obcode[location++].code);
count++;
length+=2;
}
for(i=0;i<num;i++)
if(strcmp(lbl,estab[i].sname)==0)
break;
switch(operation)
{
case '+':newadd=strtol(address,&add1,10)+(long int)estab[i].padd;
break;
case '-':newadd=strtol(address,&add1,10)-(long int)estab[i].padd;
break;
}
ltoa(newadd,address,10);
x=0;
y=0;
while(count>0)
{
obcode[loc].code[x++]=address[y++];
if(x>1)
{
x=0;
loc++;
count--;
}
}
}
count=0;
n=0;
fprintf(fp3,"%d\t",obcode[0].add);
for(i=0;i<inc;i++)
{
fprintf(fp3,"%s",obcode[i].code);
n++;
if(n>3)
{
fprintf(fp3,"%s",obcode[i].code);
n++;
if(n>3)
fprintf(fp3,"\t");
n=0;
count++;
}
if(count>3)
{
fprintf(fp3,"\n%d\t",obcode[i+1].add);
count=0;
}
}
getch();
}
OUTPUT:

Input:
link1.c

H PROGA 000000 000070


D LISTA 000040 ENDA 000054
R LISTB ENDB LISTC ENDC
T 000020 10 03201D 77100004 150014
T 000054 16 100014 15100006 00002F 100014 FFFFC0
M 000024 05+LISTB
M 000054 06+LISTC
M 000058 06+ENDC
M 000064 06+LISTB
E 000000

H PROGB 000000 000088


D LISTB 000060 ENDB 000070
R LISTA ENDA LISTC ENDC
T 000036 11 031000000 772027 05100000
T 000070 18 100000 05100000 05100020 05100030 100000
M 000037 05+LISTA
M 000044 05+ENDA
M 000070 06+ENDA
M 000074 06+ENDC
M 000078 06+ENDC
M 000082 06+ENDA
E 000000

H PROGC 000000 000057


D LISTC 00003 ENDC 00042
R LISTA ENDA LISTB ENDB
T 000018 12 031000000 77100004 05100000
T 000042 15 100030 100008 1000011 100000 100000
M 000019 05+LISTA
M 000023 05+LISTB
M 000027 05+ENDA
M 000048 06+LISTA
M 000051 06+ENDA
M 000054 06+LISTB
E 000000
link2.c

CSECT SYMNAME ADDRESS LENGTH


PROGA 4000 70
LISTA 4040 0
ENDA 4054 0
PROGB 4046 88
LISTB 40a6 0
ENDB 40b6 0
PROGC 409e 57
LISTC 40a1 0
ENDC 40e0 0

Output:
link3.c
0 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
16 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
32 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
48 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
64 xxxxxxxx xxxxxxxx xxxxxxxx 0xxxxxx
80 0xxxxxx 0xxxxxx xxxx0320 1D771000
96 045150014 xxxxxxxx xx0xxxx 0xxxx0
112 xxxxxxxx xxxxxxxx xxxxxxxx 101415
128 10000600 22F1000 14FFFFC0 03100000
144 77202705 100000xx xxxxxxxx xxxx0xx
160 xxxx0xx xxxx0xx xxxx0xx xxxx1000
176 00051000 00051000 20051000 30100000
192 03100000 77100004 05100000 xxxxxxxx
208 xxxxxxxx xxxxxxxx 10003010 00081000
224 01100000 100000

RESULT

Thus the above program has been done and output is verified.
Ex No: 10

IMPLEMENTATION OF SIMPLE TEXT EDITOR

AIM:

To write a C program to implement simple text editor.

ALGORITHM:

STEP 1: Start the program execution.


STEP 2: List the menu.
STEP 3: Get the choice.
STEP 4: If choice is 1, create a new file.
STEP 5: If choice is 2, view the required file.
STEP 6: If choice is 3, get the file name to be edited, enter the new text.
STEP 7: If choice is 4, delete the file.
STEP 8: If choice is 5, exit.
STEP 9: Stop the program execution.
PROGRAM:
//implementation of text editor
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<fstream.h>
#include<stdlib.h>
#include<stdio.h>
char fn[10],c;
class texteditor
{
public:
void create();
void append();
void view();
void del();
};
void main()
{
int choice;
while(1)
{
clrscr();
texteditor obj;
cout<<"\n1.CREATE\n2.VIEW\n3.APPEND\n4.DELETE\n5.EXIT\nENTER YOUR
CHOICE:";
cin>>choice;
switch(choice)
{
case 1:
obj.create();
break;
case 2:
obj.view();
break;
case 3:
obj.append();
break;
case 4:
obj.del();
break;
case 5:
exit(0);
break;
default:
exit(0);
}
}
}
//Creation of file
void texteditor::create()
{
cout<<"\nENTER THE FILE NAME:";
cin>>fn;
ofstream fp1;
fp1.open(fn);
cout<<"\nENTER THE CONTENTS OF THE FILE:";
cout<<"\nSAVE AND EXIT: CTRL &S\n";
while(1)
{
c=getch();
if(c==32)
{
cout<<' ';
fp1<<c;
}
else if(c==13)
{
cout<<"\n";
fp1<<"\n";
}
else if(c==19)
{
cout<<"\nFILE SAVED AND CLOSED\n";
fp1.close();
getch();
break;
}
else
{
cout<<c;
fp1<<c;
}
}
}
//Procedure for viewing the content of file
void texteditor::view()
{
char str[30];
cout<<"\nENTER THE FILE NAME:";
cin>>fn;
ifstream fp1(fn);
while(!fp1.eof())
{
fp1.getline(str,30);
cout<<str<<"\n";
}
fp1.close();
getch();
}
//Procedure for appending data to a file
void texteditor::append()
{
cout<<"\nSAVE:CTRL &S";
cout<<"\nENTER THE FILE NAME:";
cin>>fn;
ofstream fp1(fn,ios::ate);
while(1)
{
c=getch();
if(c==13)
{
c='\n';
cout<<"\n";
fp1<<c;
}
else if(c==19)
{
break;
}
else
{
cout<<c;
fp1<<c;
}
}
fp1.close();
}
//Procedure for deleting a file
void texteditor::del()
{
cout<<"\nENTER THE FILE NAME:";
cin>>fn;
remove(fn);
cout<<"\nFILE DELETED SUCCESSFULLY\n";
getch();
}
OUTPUT:

1.CREATE
2.VIEW
3.APPEND
4.DELETE
5.EXIT
ENTER YOUR CHOICE:1

ENTER THE FILE NAME: ANAND

ENTER THE CONTENTS OF THE FILE:


SAVE AND EXIT: CTRL &S
FRIENDS

FILE SAVED AND CLOSED

ENTER YOUR CHOICE:2

ENTER THE FILE NAME: ANAND


FRIENDS

ENTER YOUR CHOICE:3

SAVE:CTRL &S
ENTER THE FILE NAME:ANAND
JEYRAJ

ENTER YOUR CHOICE:4

ENTER THE FILE NAME:ANAND

FILE DELETED SUCCESSFULLY


ENTER YOUR CHOICE:5

EXIT…

RESULT

Thus the above program has been done and output is verified.

You might also like