Vous êtes sur la page 1sur 28

Q-1 Design and implement a Text Editor with features like creation, insertion, display,

append, file deletion etc.


Program Flow Chart

Program Code
#include<stdio.h>

int i, j, ec, fg, ec2;


char fn[20], e, c, waste;
FILE *fp1, *fp2, *fp;

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

if(c == 'F' && waste == '^'){


fclose(fp1);
printf("\nEnter a name for the File: ");
scanf("%s",fn);
fp1=fopen("temp.txt","r");
fp2=fopen(fn,"w");

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;
};

struct SymTab *first, *last;

int main(){

int option, srch_res;


char label[20];
system("clear");

printf("\nSYMBOL-TABLE using Linked List\n");


printf("\n*\t1 -> Insert\n*\t2 -> Display\n*\t3 -> Delete\n*\t4 ->
Search\n*\t5 -> Modify\n*\t6 -> Exit\n");
printf("\nChoose an option : ");

scanf("%d", &option);

while(option < 6){

switch(option){

case 1:
insert();
break;

case 2:
display();
break;

case 3:
delete();
break;

case 4:

printf("\nEnter the label to be searched : ");


scanf("%s", label);
srch_res = search(label);
fflush(stdout);
fflush(stdin);
printf("\nResult:");
if(srch_res == -1)
printf("\nThe label (%s) is not present in the
symbol table\n", label);
break;

case 5:
modify();
break;

case 6:
exit(0);

printf("\n*\t1 -> Insert\n*\t2 -> Display\n*\t3 -> Delete\n*\t4


-> Search\n*\t5 -> Modify\n*\t6 -> Exit\n");
printf("\nChoose an option : ");

scanf("%d", &option);

return 0;
}

void insert(){

int srch_res;
char label[20];

printf("\nEnter a new label : ");

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

printf("(%s, %s, %d)", ptr->label, ptr->symbol, ptr->address);


ptr = ptr->next;
}
if(size == 0){
printf("Nothing to display!!");
}
printf("\n");
}

int search(char label[]){


int i, ret = 0;
struct SymTab *ptr;
ptr = first;

for(i=0; i<size; i++){


if(strcmp(ptr->label, label) == 0){
ret = i;
printf("Found at index %d : (%s, %s, %d)\n", i, ptr->label,
ptr->symbol, ptr->address);
break;
}
ptr = ptr->next;
}
return ret;
}

void modify()
{
char label[20], new_label[20];
int add, choice, i, s;
struct SymTab *ptr;
ptr = first;

printf("\nWhat do you want to modify?\n");


printf("\n1. Only the label\n2.Only the address\n3.Both the label and
address\n");
printf("Enter your choice : ");

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

for(i=0; i<size; i++){


if(strcmp(ptr->label, label) == 0){
strcpy(ptr->label, new_label);
break;
}
ptr = ptr->next;
}
printf("After Modification:\n");
display();
}

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;

while(strcmp(ptr2->label, label) != 0){


ptr = ptr->next;
ptr2 = ptr2->next;
}
ptr->next = NULL;
last = ptr; }else{
ptr2 = ptr->next;
while(strcmp(ptr2->label, label) != 0){
ptr = ptr->next;
ptr2 = ptr2->next;
}
ptr->next = ptr2->next;
}
size--;
printf("\nAfter Deletion:\n");
display();

} }

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

FILE *f1, *f2, *f3, *f4;


int lc, sa, l, op1, o, len;
char m1[20], la[20], op[20], otp[20];

f1 = fopen("input.txt", "r");
f3 = fopen("symtab.txt", "w");

fscanf(f1, "%s %s %d", la, m1, &op1);

if(strcmp(m1, "START") == 0){


sa = op1;
lc = sa;
printf("\t%s\t%s\t%d\n", la, m1, op1);
}else
lc=0;

fscanf(f1, "%s %s", la, m1);

while(!feof(f1)){

fscanf(f1, "%s", op);


printf("\n%d\t%s\t%s\t%s\n", lc, la, m1, op);

if(strcmp(la, "-") != 0){


fprintf(f3, "\n%d\t%s\n", lc, la);
}

f2 = fopen("optab.txt", "r");
fscanf(f2, "%s %d", otp, &o);

while(!feof(f2)){

if(strcmp(m1, otp) == 0){


lc = lc + 3;
break;
}

fscanf(f2, "%s %d", otp, &o);


}

fclose(f2);

if(strcmp(m1, "WORD") == 0){


lc = lc + 3;
}else if(strcmp(m1, "RESW") == 0){
op1 = atoi(op);
lc = lc + (3*op1);
}else if(strcmp(m1, "BYTE") == 0){
if(op[0] == 'X')
lc = lc + 1 ;
else{
len = strlen(op) - 2;
lc = lc + len;
}
}
else if(strcmp(m1, "RESB") == 0){
op1 = atoi(op);
lc = lc + op1;
}

fscanf(f1, "%s%s", la, m1);


}

if(strcmp(m1, "END") == 0){


printf("Program length = %d\n", lc-sa);
}

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");
}

Q-6 Design and implement an absolute loader.


Program Code
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
main()
{
FILE *fp;
int i,addr1,l,j,staddr1;
char name[10],line[50],name1[10],addr[10],rec[10],ch,staddr[10];
printf("enter program name:" );
scanf("%s",name);
fp=fopen("abssrc.txt","r");
fscanf(fp,"%s",line);
for(i=2,j=0;i<8,j<6;i++,j++)
name1[j]=line[i];
name1[j]='\0';
printf("name from obj. %s\n",name1);
if(strcmp(name,name1)==0)
{
do
{
fscanf(fp,"%s",line);
if(line[0]=='T')
{
for(i=2,j=0;i<8,j<6;i++,j++)
staddr[j]=line[i];

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

Submitted To:Prof. Poonam Saini

Submitted By:Raghav Kukreja


12103024

Vous aimerez peut-être aussi