SS Lab Manual

61
1 SYSTEM SOFTWARE CS2308 LAB MANUAL COMPUTER SCIENCE & ENGINEERING KARPAGA VINAYAGA COLLEGE OF ENGG AND TECHNOLOGY MADURANTAKAM STAFF REFERENCE S.PRABHU. MTech LECTURER Prepared by : S.PRABHU Lecturer KVCET

Transcript of SS Lab Manual

1

SYSTEM SOFTWARECS2308

LAB MANUAL

COMPUTER SCIENCE & ENGINEERING KARPAGA VINAYAGA COLLEGE OF ENGG AND TECHNOLOGY MADURANTAKAM

STAFF REFERENCE

S.PRABHU. MTech LECTURER

Prepared by : S.PRABHU Lecturer KVCET

2 INDEXSn 1 Program Title Pag e Sign

Symbol Table

2

One Pass Assembler

3

Two Pass Assembler

4

Single Pass Assembler

5

Single Pass Macro Processor Absolute Loader

6

7

Relocating Loader

8

One Pass Direct-Linking Loader

9

Two Pass Direct-Linking Loader

10

Simple Text Editor

11

Two Pass Macro Processor

12

Symbol Table With Hashing QUESTIONS IT2308 SYSTEM SOFTWARE LAB

Prepared by : S.PRABHU Lecturer KVCET

3

1.

Implement a symbol table with functions to create, insert, modify, search, and display.

2.

Implement pass one of a two pass assembler.

3.

Implement pass two of a two pass assembler.

4.

Implement a single pass assembler.

5.

Implement a two pass macro processor

6.

Implement a single pass macro processor.

7.

Implement an absolute loader.

8.

Implement a relocating loader.

9.

Implement pass one of a direct-linking loader.

10.

Implement pass two of a direct-linking loader.

11.

Implement a simple text editor with features like insertion / deletion of a character, word, and sentence.

12.

Implement a symbol table with suitable hashing

IMPLEMENTATION OF SYMBOL TABLEEX. NO : 1 DATE:

Prepared by : S.PRABHU Lecturer KVCET

4

Aim:To write a C program to implement Symbol Table for performing insert, display, delete, search and modify.

Algorithm:Step 1:Start the program for performing insert, display, delete, search and modify option in symbol table. Step 2: Define the structure of the Symbol Table. Step 3: Enter the choice for performing the operations in the symbol Table. Step 4: If the entered choice is 1, search the symbol table for the symbol to be inserted. If the symbol is already present, it displays Duplicate Symbol. Else, insert the symbol and the corresponding address in the symbol table. Step 5: If the entered choice is 2, the symbols present in the symbol table are displayed. Step 6: If the entered choice is 3, the symbol to be deleted is searched in the symbol table. If it is not found in the symbol table it displays Label Not found. Else, the symbol is deleted. Step 7: If the entered choice is 5, the symbol to be modified is searched in the symbol table. The label or address or both can be modified.

Source Code:# include # include # include # include # define null 0 int size=0; void insert(); void del(); int search(char lab[]); void modify(); void display(); struct symbtab { char label[10]; int addr;

//Function declarations//

Prepared by : S.PRABHU Lecturer KVCET

5 struct symtab *next; }; struct symbtab *first,*last; void main() //Main function Start// { int op; int y; char la[10]; clrscr(); do { printf("\nSYMBOL TABLE IMPLEMENTATION\n"); printf("1. INSERT\n"); printf("2. DISPLAY\n"); printf("3. DELETE\n"); printf("4. SEARCH\n"); printf("5. MODIFY\n"); printf("6. END\n"); printf("Enter your option : "); scanf("%d",&op); switch(op) { case 1: insert(); display(); break; case 2: display(); break; case 3: del(); display(); break; case 4: printf("Enter the label to be searched : "); scanf("%s",la); y=search(la); if(y==1) { printf("The label is already in the symbol Table"); } else { printf("The label is not found in the symbol table"); } break; case 5: modify(); display(); break; case 6: break; } } while(oplabel,l); printf("Enter the address : "); scanf("%d",&p->addr); p->next=null; if(size==0) { first=p; last=p; } else { last->next=p; last=p; } size++; } } void display() { int i; struct symbtab *p; p=first; printf("LABEL\tADDRESS\n"); for(i=0;ilabel,p->addr); p=p->next; } } int search(char lab[]) { int i,flag=0; struct symbtab *p;

Prepared by : S.PRABHU Lecturer KVCET

7 p=first; for(i=0;ilabel,lab)==0) { flag=1; } p=p->next; } return flag; } void modify( ) { char l[10],nl[10]; int add, choice, i, s; struct symbtab *p; p=first; printf("What do you want to modify?\n"); printf("1. Only the label\n"); printf("2. Only the address of a particular label\n"); printf("3. Both the label and address\n"); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("Enter the old label\n"); scanf("%s",l); printf("Enter the new label\n"); scanf("%s",nl); s=search(l); if(s==0) { printf("NO such label"); } else { for(i=0;ilabel,l)==0) { strcpy(p->label,nl); } p=p->next; } } break; case 2: printf("Enter the label whose address is to modified\n"); scanf("%s",l); printf("Enter the new address\n");

Prepared by : S.PRABHU Lecturer KVCET

8 scanf("%d",&add); s=search(l); if(s==0) { printf("NO such label"); } else { for(i=0;ilabel,l)==0) { p->addr=add; } p=p->next; } } break; case 3: printf("Enter the old label : "); scanf("%s",l); printf("Enter the new label : "); scanf("%s",nl); printf("Enter the new address : "); scanf("%d",&add); s=search(l); if(s==0) { printf("NO such label"); } else { for(i=0;ilabel,l)==0) { strcpy(p->label,nl); p->addr=add; } p=p->next; } } break; } } void del( ) { int a; char l[10]; struct symbtab *p,*q; p=first;

Prepared by : S.PRABHU Lecturer KVCET

9 printf("Enter the label to be deleted\n"); scanf("%s",l); a=search(l); if(a==0) { printf("Label not found\n"); } else { if(strcmp(first->label,l)==0) { first=first->next; } else if(strcmp(last->label,l)==0) { q=p->next; while(strcmp(q->label,l)!=0) { p=p->next; q=q->next; } p->next=null; last=p; } else { q=p->next; while(strcmp(q->label,l)!=0) { p=p->next; q=q->next; } p->next=q->next; } size--; } }

Sample Output:SYMBOL TABLE IMPLEMENTATION 1. INSERT 2. DISPLAY 3. DELETE 4. SEARCH Prepared by : S.PRABHU Lecturer KVCET

10 5. MODIFY 6. END Enter your option: 1 Enter the label :A Enter the address :5 The variable should be inserted. SYMBOL TABLE IMPLEMENTATION 1. INSERT 2. DISPLAY 3. DELETE 4. SEARCH 5. MODIFY Enter your option :1 Enter the label :B Enter the address :10 The variable should be inserted. Enter your option: 2 Lable A B address 5 10

SYMBOL TABLE IMPLEMENTATION 1. INSERT 2. DISPLAY 3. DELETE 4. SEARCH 5. MODIFY Enter your option: 1 Enter the label: B Enter the address: 10 The label already exists. Duplicate cant be inserted SYMBOL TABLE IMPLEMENTATION 1. INSERT 2. DISPLAY Prepared by : S.PRABHU Lecturer KVCET

11 3. DELETE 4. SEARCH 5. MODIFY Enter your option: 5 What do you want to modify 1. Only the label 2. Only the address of a particular label 3. Both the label and address Enter your choice: 1 Enter old label:B Enter new label:G Enter your option: 2 Lable A B G address 5 10 10

RESULT: Thus the program is executed successfully and the result is obtained.

IMPLEMENTATION OF PASS ONE OF TWO PASS ASSEMBLEREx. No : 2 Date:

Prepared by : S.PRABHU Lecturer KVCET

12

Aim:To write a C program to implement PASS ONE of a two pass assembler.

Algorithm:Step 1: Open the files fp1 and fp4 in read mode and fp2 and fp3 in write mode Step 2: Read the source program Step 3: If the opcode read in the source program is START, the variable location counter is initialized with the operand value. Step 4: Else the location counter is initialized to 0. Step 5: The source program is read line by line until the reach of opcode END. Step 6: Check whether the opcode read is present in the operation code table. Step 7: If the opcode is present, then the location counter is incremented by 3. Step 8: If the opcode read is WORD, the location counter is incremented by3. Step 9: If the opcode read is RESW, the operand value is multiplied by 3 and then the location counter is incremented. Step 10: If the opcode read is RESB, the location counter value is incremented by operand value. Step 11: If the opcode read is BYTE, the location counter is auto incremented. Step 12: The length of the source program is found using the location counter value.

Source Code:#include #include #include #include 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("SYMTAB.DAT","w"); fp3=fopen("OUTPUT.DAT","w"); fp4=fopen("OPTAB.DAT","r");

Prepared by : S.PRABHU Lecturer KVCET

13 fscanf(fp1,"%s%s%s",label,opcode,operand); if(strcmp(opcode,"START")==0) { start=atoi(operand); locctr=start; fprintf(fp3,"\t%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;

Prepared by : S.PRABHU Lecturer KVCET

14 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 Length of the program is %d",length); fclose(fp1); fclose(fp2); fclose(fp3); fclose(fp4); getch(); }

INPUT : INPUT.DATMAIN 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.DATADD ADDR SUB SUBR MUL MULR DIV DIVR LDA LDB LDX LDCH STA STB STX STCH 18 90 1C 94 20 98 24 9C 00 68 04 50 0C 78 10 54 Prepared by : S.PRABHU Lecturer KVCET

15 TIX 2C J 2C JSUB 48 RSUB 4C JEQ 30 JLT 38 JGT 34 START * END *

OUTPUT: OUTPUT.DATMAIN 2000 2003 2006 2009 2012 2015 2018 2019 2020 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

SYMTAB.DATBEGIN 2000 NUM1 2012 NUM2 2015 CHAR1 CHAR2

2018 2019

RESULT: Thus the program is executed successfully and the result is obtained.

IMPLEMENTATION PROGRAM FOR PASS 2 OF TWO PASS ASSEMBLEREx. No : 3 Date:

Prepared by : S.PRABHU Lecturer KVCET

16

Aim:To implement of pass two of two pass assembler.

Algorithm:Step 1:Start the program Step 2:Initialize all the variables Step 3:Open a file Step 4:Read the content of the file Step 5:If opcode is BYTE then write address, label, opcode, operand into a file Else if opcode is WORD then write address, label, opcode, operand into a file Else if opcode is RESB or RESW write address, label, opcode, operand into a file if it is not match anything then write address, label, opcode, operand into a file Step 6:Finally terminate the of pass two of pass two assembler.

SOURCE CODE:

#include #include #include #include void main() { char symbol[20],opcode[20],mnemonic[20],code[20],operand[20],character,add[20], objectcode[20],label[20]; int locctr,flag,flag1,loc; FILE *fp1,*fp2,*fp3,*fp4; clrscr(); fp1=fopen("OUT1.DAT","r"); fp2=fopen("TWOOUT1.DAT","w"); fp3=fopen("OPTAB1.DAT","r"); fp4=fopen("SYMTAB1.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);

Prepared by : S.PRABHU Lecturer KVCET

17

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);Prepared by : S.PRABHU Lecturer KVCET

18

} } 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); printf("\n Output"); fclose(fp1); fclose(fp2); fclose(fp3); fclose(fp4); getch(); } INPUT OUTPUT: OUT1.DAT MAIN START 2000 BEGIN 2003 ** 2006 ** 2009 ** 2012 NUM1 2015 NUM2 2018 CHAR1 2019 CHAR2 2020 ** END SYMTAB1.DAT BEGIN 2000 NUM1 2012 NUM2 2015 CHAR1 2018 CHAR2 2019 2000 LDA STA LDCH STCH WORD 5 RESW BYTE RESB BEGIN

NUM1 NUM2 CHAR1 CHAR2 1 C'A' 1

OPTAB1.DAT ADD 18 ADDR 90 SUB 1C SUBR 94 MUL 20Prepared by : S.PRABHU Lecturer KVCET

19

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 2C JSUB 48 RSUB 4C JEQ 30 JLT 38 JGT 34 START END *

*

OUTOBJ.DAT MAIN 2000 2003 2006 2009 2012 2015 2018 2019 ** START 2000 BEGIN LDA NUM1 002012 ** STA NUM2 0C2015 ** LDCH CHAR1 502018 ** STCH CHAR2 542019 NUM1 WORD 5 5 NUM2 RESW 1 CHAR1 BYTE C'A' CHAR2 RESB 1 END BEGIN

41

RESULT: Thus the program is executed successfully and the result is obtained.

IMPLEMENTATION OF SINGLE PASS ASSEMBLEREx. No : 4 Date:

Prepared by : S.PRABHU Lecturer KVCET

20

Aim:To implement of Single pass an assembler.

Algorithm:Step 1: Start the program Step 2.Initialize all the variables Step 3.Open a files for read and write. Step 4: Read the content from the input file. Step 5. Write the object code with address to the output file. Step 6: Finally terminate the of pass two of pass two assembler.

SOURCE CODE:#include #include #include #include #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 symtbl[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("INPUTEX4.DAT","r"); fp2=fopen("OPTABEX4.DAT","r"); fp3=fopen("SPOUTEX4.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); Prepared by : S.PRABHU Lecturer KVCET

21 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