SS File
SS File
Program Code
#include<stdio.h>
void createFile();
void appendToFile();
void deleteFile();
void displayFile();
int main() {
do {
system("clear");
printf("\n/////////// BASIC TEXT EDITOR \\\\\\\\\\\\\\\\\\\\\\");
printf("\n\nAvailable Options:\n");
printf("\n1.Create a new File\n2.Open an existing File\n3.Append to
an existing File\n4.Delete a File\n5.Exit from the program\n");
printf("\nSelect a option: ");
scanf("%d",&ec);
switch(ec) {
case 1:
createFile();
break;
case 2:
displayFile();
break;
case 3:
appendToFile();
break;
case 4:
deleteFile();
break;
case 5:
exit(0);
}
} while(1);
return 0;
}
void createFile() {
fp1=fopen("temp.txt","w");
printf("\nEnter the file contents. Press '.' to 'Save' and 'Close' the
file.\n\n");
waste = '\0';
while(1) {
c=getchar();
fputc(c,fp1);
while(!feof(fp1)) {
c=getc(fp1);
putc(c,fp2);
}
fclose(fp2);
break;
}
}
waste = c;
}
void displayFile() {
printf("\nEnter the filename: ");
scanf("%s",fn);
fp1=fopen(fn,"r");
if(fp1==NULL){
printf("\nFile not found!");
goto end1;
}
while(!feof(fp1)) {
c=getc(fp1);
printf("%c",c);
end1:
fclose(fp1);
printf("\n\nPress ENTER to continue...");
fflush(stdin);
waste = getchar();
waste = getchar();
while(1){
if(waste == '\n')
break;
else{
printf("\n\nPress ENTER to continue...");
fflush(stdin);
waste = getchar();
waste = getchar();
}
}
}
void deleteFile(){
printf("\n\tEnter the file name: ");
scanf("%s",fn);
fp1=fopen(fn,"r");
if(fp1==NULL){
printf("\n\tFile not found!");
goto end2;
}
fclose(fp1);
if(remove(fn)==0){
printf("\n\nFile has been deleted successfully!");
goto end2;
}else
printf("\nError!\n");
end2:
fflush(stdin);
printf("\n\nPress ENTER to continue...");
while(getchar() != '\n'){
printf("\n\nPress ENTER to continue...");
}
}
void appendToFile() {
printf("\n\tEnter the file name: ");
scanf("%s",fn);
fp1=fopen(fn,"r");
if(fp1==NULL) {
printf("\n\tFile not found!");
goto end3;
}
while(!feof(fp1)){
c=getc(fp1);
printf("%c",c);
}
fclose(fp1);
printf("\n\tType the text and press 'Ctrl+S' to append.\n");
fp1=fopen(fn,"a");
while(1){
c=getchar();
if(c==19)
goto end3;
if(c==13){
c='\n';
printf("\n\t");
fputc(c,fp1);
} else {
printf("%c",c);
fputc(c,fp1);
}
}
end3:
fclose(fp1);
fflush(stdin);
printf("\n\nPress ENTER to continue...");
while(getchar() != '\n'){
printf("\n\nPress ENTER to continue...");
}
}
Q-2 Design and implement a symbol table with functions like create, insert, update,
search, display etc.
Program Flow Chart
Program Code
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>
int size = 0;
void insert();
void display();
void delete();
int search(char label[]);
void modify();
struct SymTab{
char label[20], symbol[10];
int address;
struct SymTab *next;
};
int main(){
scanf("%d", &option);
switch(option){
case 1:
insert();
break;
case 2:
display();
break;
case 3:
delete();
break;
case 4:
case 5:
modify();
break;
case 6:
exit(0);
scanf("%d", &option);
return 0;
}
void insert(){
int srch_res;
char label[20];
scanf("%s",label);
srch_res = search(label);
if(srch_res == 1)
printf("\nThe label (%s) is already present in the symbol
table.\nDuplicate can't be inserted", label);
else{
struct SymTab *ptr;
ptr = (struct SymTab*)malloc(sizeof(struct SymTab));
strcpy(ptr->label, label);
printf("Enter the symbol for label : ");
scanf("%s", ptr->symbol);
printf("Enter the address : ");
scanf("%d", &ptr->address);
ptr->next = NULL;
if(size == 0){
first = ptr;
last = ptr;
}else{
last->next = ptr;
last = ptr;
}
size++;
printf("Inserted Successfully\n");
}
}
void display(){
int i;
struct SymTab *ptr;
ptr = first;
printf("\n");
for(i=0; i<size; i++){
void modify()
{
char label[20], new_label[20];
int add, choice, i, s;
struct SymTab *ptr;
ptr = first;
scanf("%d", &choice);
switch(choice){
case 1:
printf("\nEnter the old label : ");
scanf("%s", label);
s = search(label);
if(s == 0)
printf("Label not found\n");
else{
printf("Enter the new label : ");
scanf("%s", new_label);
break;
case 2:
printf("\nEnter the label where the address is to be
modified : ");
scanf("%s", label);
s = search(label);
if(s == 0)
printf("Label not found\n");
else{
printf("Enter the new address : ");
scanf("%d", &add);
for(i=0; i<size; i++){
if(strcmp(ptr->label, label)==0)
ptr->address = add;
ptr = ptr->next;
}
printf("After Modification:\n");
display();
}
break;
case 3:
printf("\nEnter the old label : ");
scanf("%s", label);
s = search(label);
if(s == 0)
printf("\nLabel not found\n");
else{
printf("\nEnter the new label : ");
scanf("%s", new_label);
printf("\nEnter the new address : ");
scanf("%d", &add);
for(i=0; i<size; i++){
if(strcmp(ptr->label, label)==0){
strcpy(ptr->label, new_label);
ptr->address = add;
}
ptr = ptr->next;
}
printf("\nAfter Modification:\n");
display();
}
break;
}
}
void delete(){
int a;
char label[20];
struct SymTab *ptr, *ptr2;
ptr = first;
printf("\nEnter the label to be deleted : ");
scanf("%s", label);
a = search(label);
if(a == 0)
printf("\nLabel not found\n");
else{
if(strcmp(first->label, label) == 0)
first = first->next;
else if(strcmp(last->label, label) == 0){
ptr2 = ptr->next;
} }
Q-3 Design and implement an algorithm for one pass of two-pass assembler.
Program Flow Chart
Program Code
#include<stdio.h>
#include<string.h>
int main(){
f1 = fopen("input.txt", "r");
f3 = fopen("symtab.txt", "w");
while(!feof(f1)){
f2 = fopen("optab.txt", "r");
fscanf(f2, "%s %d", otp, &o);
while(!feof(f2)){
fclose(f2);
fclose(f1);
fclose(f3);
return 0;
}
Q-4 Design and implement an algorithm for pass two of two-pass assembler.
Program Flow Chart
Program Code
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10],ad[10],label[10],opcode[10],operand[10],symbol[10],ch;
st,diff,i,address,add,len,actual_len,finaddr,prevaddr,j=0;
int
char mnemonic[15][15]={"LDA","STA","LDCH","STCH"};
char code[15][15]={"33","44","53","57"};
FILE *fp1,*fp2,*fp3,*fp4;
clrscr();
fp1=fopen("ASSMLIST.DAT","w");
fp2=fopen("SYMTAB.DAT","r");
fp3=fopen("INTERMED.DAT","r");
fp4=fopen("OBJCODE.DAT","w");
fscanf(fp3,"%s%s%s",label,opcode,operand);
while(strcmp(opcode,"END")!=0)
{
prevaddr=address;
fscanf(fp3,"%d%s%s%s",&address,label,opcode,operand);
}
finaddr=address;
fclose(fp3);
fp3=fopen("INTERMED.DAT","r");
fscanf(fp3,"%s%s%s",label,opcode,operand);
if(strcmp(opcode,"START")==0)
{
fprintf(fp1,"\t%s\t%s\t%s\n",label,opcode,operand);
fprintf(fp4,"H^%s^00%s^00%d\n",label,operand,finaddr);
fscanf(fp3,"%d%s%s%s",&address,label,opcode,operand);
st=address;
diff=prevaddr-st;
fprintf(fp4,"T^00%d^%d",address,diff);
}
while(strcmp(opcode,"END")!=0)
{
if(strcmp(opcode,"BYTE")==0)
{
fprintf(fp1,"%d\t%s\t%s\t%s\t",address,label,opcode,operand);
len=strlen(operand);
actual_len=len-3;
fprintf(fp4,"^");
for(i=2;i<(actual_len+2);i++)
{
itoa(operand[i],ad,16);
fprintf(fp1,"%s",ad);
fprintf(fp4,"%s",ad);
}
fprintf(fp1,"\n");
}
else if(strcmp(opcode,"WORD")==0)
{
len=strlen(operand);
itoa(atoi(operand),a,10);
fprintf(fp1,"%d\t%s\t%s\t%s\t00000%s\n",address,label,opcode,operand,a)
;
fprintf(fp4,"^00000%s",a);
}
else if((strcmp(opcode,"RESB")==0)||(strcmp(opcode,"RESW")==0))
fprintf(fp1,"%d\t%s\t%s\t%s\n",address,label,opcode,operand);
else
{
while(strcmp(opcode,mnemonic[j])!=0)
j++;
if(strcmp(operand,"COPY")==0)
fprintf(fp1,"%d\t%s\t%s\t%s\t%s0000\n",address,label,opcode,operand,co
de[j]);
else
{
rewind(fp2);
fscanf(fp2,"%s%d",symbol,&add);
while(strcmp(operand,symbol)!=0)
fscanf(fp2,"%s%d",symbol,&add);
fprintf(fp1,"%d\t%s\t%s\t%s\t%s%d\n",address,label,opcode,operand,code
[j],add);
fprintf(fp4,"^%s%d",code[j],add);
}
}
fscanf(fp3,"%d%s%s%s",&address,label,opcode,operand);
}
fprintf(fp1,"%d\t%s\t%s\t%s\n",address,label,opcode,operand);
fprintf(fp4,"\nE^00%d",st);
printf("\n Intermediate file is converted into object code");
fcloseall();
printf("\n\nThe contents of Intermediate file:\n\n\t");
fp3=fopen("INTERMED.DAT","r");
ch=fgetc(fp3);
while(ch!=EOF)
{
printf("%c",ch);
ch=fgetc(fp3);
}
printf("\n\nThe contents of Symbol Table :\n\n");
fp2=fopen("SYMTAB.DAT","r");
ch=fgetc(fp2);
while(ch!=EOF)
{
printf("%c",ch);
ch=fgetc(fp2);
}
printf("\n\nThe contents of Output file :\n\n");
fp1=fopen("ASSMLIST.DAT","r");
ch=fgetc(fp1);
while(ch!=EOF)
{
printf("%c",ch);
ch=fgetc(fp1);
}
printf("\n\nThe contents of Object code file :\n\n");
fp4=fopen("OBJCODE.DAT","r");
ch=fgetc(fp4);
while(ch!=EOF)
{
printf("%c",ch);
ch=fgetc(fp4);
}
fcloseall();
getch();
}
Q-5 Design and implement a simple input-output relocatable where the initial address is
1000 and the relocating address is 5000.
Program Flow Chart
Program Code
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
void main()
{
char add[6],length[10],input[10],binary[12],bitmask[12],relocbit;
int start,inp,len,i,address,opcode,addr,actualadd;
FILE *fp1,*fp2;
start=4000;
address=1000;
fp1=fopen("INPUT.DAT","r");
fp2=fopen("output.DAT","w");
fscanf(fp1,"%s",input);
while(strcmp(input,"E")!=0)
{
if(strcmp(input,"H")==0)
{
fscanf(fp1,"%s",add);
fscanf(fp1,"%s",length);
fscanf(fp1,"%s",input);
}
if(strcmp(input,"T")==0)
{
fscanf(fp1,"%d",&address);
fscanf(fp1,"%s",bitmask);
address+=start;
len=strlen(bitmask);
for(i=0;i<len;i++)
{
fscanf(fp1,"%d",&opcode);
fscanf(fp1,"%d",&addr);
relocbit=bitmask[i];
if(relocbit=='0')
actualadd=addr;
else
actualadd=addr+start;
fprintf(fp2,"%d\t%d\t%d\n",address,opcode,actualadd);
address+=3;
}
fscanf(fp1,"%s",input);
}
}
fclose(fp1);
fclose(fp2);
printf("FINISHED");
}
staddr[j]='\0';
staddr1=atoi(staddr);
i=12;
while(line[i]!='$')
{
if(line[i]!='^')
{
printf("00%d \t %c%c\n", staddr1,line[i],line[i+1]);
staddr1++;
i=i+2;
}
else i++;
}
}
else if(line[0]='E')
fclose(fp);
}while(!feof(fp));
}
getch();
}
System Software
Lab Assignments