Ss Lab Details

download Ss Lab Details

of 22

Transcript of Ss Lab Details

  • 8/12/2019 Ss Lab Details

    1/22

    1. IMPLENTATION OF A SYMBOL TABLE

    Aim:

    To write a C program to implement Symbol Table

    Algorithm:

    Start the program for performing insert, display, delete, search andmodify option insymbol table

    Define the structure of the Symbol Table

    Enter the choice for performing the operations in the symbol Table

    If the entered choice is 1, search the symbol table for the symbol to beinserted.

    If the symbol is already present, it displays "Duplicate Symbol". Else,insert the symbol and the corresponding address in the symbol table.

    If the entered choice is 2, the symbols present in the symbol table aredisplayed.

    If the entered choice is 3, the symbol to be deleted is searched in thesymbol table.

    If it is not found in the symbol table it displays "Label Not found". Else,the symbol is deleted.

    If the entered choice is 5, the symbol to be modified is searched in thesymbol table. The label or address or both can be modified.

  • 8/12/2019 Ss Lab Details

    2/22

    Source Program:

    #include#include

    #includeint line_no,j=0;void create();void insert();void modify();void search();void display();void input();struct al{char label[10];char opcode[10];char operand[10];}a[20];struct sym{char symb[10];int addr;}s[20];

    void main(){int op;char ch[5];clrscr();input();display();do{

    printf("1.Create\n2.Modify\n3.Insert\n4.Search\n5.Exit\n");printf("Enter Your Choice");scanf("%d",&op);switch(op){case 1:create();

    break;case 2:

    modify();

  • 8/12/2019 Ss Lab Details

    3/22

    printf("Symbol table Modified..");break;

    case 3:insert();

    printf("Insertion operation Performed..");break;case 4:

    search();break;case 5:exit(0);}printf("\n Do you want to continue(Y orN):");scanf("%s",ch);}while(strcmp(ch,"Y")==0);getch();}void input()

    {int i=0;

    printf("Enter the program:\n\n");printf("LABEL\tOPCODE\tOPERAND\n");do{

    scanf("%s\t%s\t%s",a[i].label,a[i].opcode,a[i].operand);i++;}while(strcmp(a[i-1].opcode,"END"));

    line_no =i;}

    void display(){int i;

    printf("The assembly Language program is \n");

    for(i=0;i

  • 8/12/2019 Ss Lab Details

    4/22

    locctr=atoi(a[i].operand);else if(strcmp(a[i-1].opcode,"START")==0);else{

    if(strcmp(a[i-1].opcode,"WORD")==0)locctr=locctr+3;else if(strcmp(a[i-1].opcode,"RESW")==0)locctr = locctr+3*atoi(a[i-1].operand);else if(strcmp(a[i-1].opcode,"BYTE")==0)locctr = locctr+strlen(a[i-1].operand);elselocctr = locctr+3;}if(strcmp(a[i].label,"$")!=0){strcpy(s[j].symb,a[i].label);s[j].addr = locctr;printf("\n%s\t%d\n",s[j].symb,s[j].addr);}j++;

    }}void modify(){

    int ln;printf("Enter the line number to be modified:");scanf("%d",&ln);scanf("%s\t%s\t%s",a[ln].label,a[ln].opcode,a[ln].operand);display();create();}void insert(){

    int i,ln;printf("Enter the line number to be inserted:");scanf("%d",&ln);for(i=line_no;i>=ln;i--){strcpy(a[i+1].label,a[i].label);strcpy(a[i+1].opcode,a[i].opcode);strcpy(a[i+1].operand,a[i].operand);}

    line_no++;

  • 8/12/2019 Ss Lab Details

    5/22

    printf("Enter the statement:\n");scanf("%s\t%s\t%s",a[ln].label,a[ln].opcode,a[ln].operand);display();create();

    }void search(){har sh[10];int i,t=0;

    printf("Enter the label to be searched");scanf("%s",sh);for(i=0;i

  • 8/12/2019 Ss Lab Details

    6/22

    2.IMPLENTATION OF PASS ONE OF A PASS TWO ASSEMBLER

    Aim:

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

    Algorithm:

    Step1:Open the files fp1 and fp4 in read mode and fp2 and fp3 in writemode

    Step2:Read the source program

    Step3:If the opcode read in the source program is START, the variablelocation counter isinitialized with the operand value.

    Else the location counter is initialized to 0.Step4:

    The source program is read line by line until the reach of opcodeEND.

    Step5:Check whether the opcode read is present in the operation code table.Step6:If the opcode is present, then the location counter is incremented by 3.Step7:If the opcode read is WORD, the location counter is incremented by3.If the opcode read is RESW, the operand value is multiplied by 3 and then the

    locationcounter is incremented.If the opcode read is RESB, the location counter value is incremented byoperand value.If the opcode read is BYTE, the location counter is auto incremented.The length of the source program is found using the location counter value.source Program:#include#include

    #include#include

  • 8/12/2019 Ss Lab Details

    7/22

    struct symb{char lbl[100];

    int addr,addr1;

    }st[100];int k=0;FILE *f1,*f2,*f3;void main(){char lbl[100],opc[100],opr[100];int addr,i,addr1;

    clrscr();j:f1=fopen("pgm.txt","W");

    printf("Enter the Assembly Code:");while(1){scanf("%s",lbl);scanf("%s",opc);scanf("%s",opr);if(stricmp(lbl,"null")==0)strcpy(lbl,"--");if(stricmp(opr,"null")==0);

    strcpy(opr,"--");fprintf(f1,"%s\t%s\t%s\n",lbl,opc,opr);if(stricmp(opc,"end")==0){ break;}}fclose(f1);fopen("pgm.txt","r");fscanf(f1,"%s\t%s\t%s\n",lbl,opc,opr);f2 = fopen("symb.txt","W");

    f3=fopen("passone.txt","W");if(stricmp(opc,"start")==0)addr=atoi(opr);else

    addr =0;fprintf(f3,"%d\t%s\t%s\t%s\n",addr,lbl,opc,opr);addr1=addr;while(!feof(f1)){fscanf(f1,"%s\t%s\t%s\n",lbl,opc,opr);

  • 8/12/2019 Ss Lab Details

    8/22

    fprintf(f3,"%d\t%s\t%s\t%s\n",addr,lbl,opc,opr);if(stricmp(lbl,"--")!=0){for(i=0;i

  • 8/12/2019 Ss Lab Details

    9/22

    #include#include#include#include

    void main(){FILE *fp1,*fp2,*fp3,*fp4;char

    label[10],opcode[10],operand[10],code[10],mnemonics[5],symbol[10],character,add[10],objectcode[10];int flag,flag1,locctr,location,loc;clrscr();fp1=fopen("pass1out.txt","r");fp2=fopen("pass2out.txt","w");fp3=fopen("optab.txt","r");fp4=fopen("symtbl.txt","r");fscanf(fp1,"%s%s%s",label,opcode,operand);if(strcmp(opcode,"START")==0){fprintf(fp2,"%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;fscanf(fp3,"%s%s",code,mnemonics);while(strcmp(code,"END")!=0){if((strcmp(opcode,code)==0)&&(strcmp(mnemonics,"*"))!=0){flag=1;

    break;

    }fscanf(fp3,"%s%s",code,mnemonics);}if(flag==1){flag1=0;rewind(fp4);while(!feof(fp4)){

    fscanf(fp4,"%s%d",symbol,&loc);if(strcmp(symbol,operand)==0)

  • 8/12/2019 Ss Lab Details

    10/22

    {flag1=1;

    break;}

    }if(flag1==1){itoa(loc,add,10);strcpy(objectcode,strcat(mnemonics,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);

    }}elsestrcpy(objectcode,"\0");fprintf(fp2,"%s\t%s\t%s\t%d\t%s\n",label,opcode,operand,locctr,objectcode);fscanf(fp1,"%d%s%s%s",&locctr,label,opcode,operand);}fprintf(fp2,"%s\t%s\t%s\t%d\n",label,opcode,operand,locctr);

    printf("OUTPUT GENERATED.....");

    fclose(fp1);fclose(fp2);fclose(fp3);fclose(fp4);getch();

    }

  • 8/12/2019 Ss Lab Details

    11/22

    SAMPLE INPUT AND OUTPUT

    pass1out.txt

    ** START 30003000 ** LDA FIVE3003 ** STA ALPHA3006 ** LDCH CHARZ3009 ** STCH C13012 ALPHA RESW 13015 FIVE WORD 53018 CHARZ BYTE C'Z'3019 C1 RESB 1

    ** END **Optab.txt

    START *LDA 05STA 0BLDCH 55STCH 68END *

    Symbol.txt

    ALPHA 3012FIVE 3015CHARZ 3018C1 3019

    pass2out.txt

    ** START 3000** LDA FIVE 3000 053015** STA ALPHA 3003 0B3012** LDCH CHARZ 3006 553018** STCH C1 3009 683019ALPHA RESW 1 3012FIVE WORD 5 3015 5CHARZ BYTE C'Z' 3018 5aC1 RESB 1 3019

    ** END ** 3020

  • 8/12/2019 Ss Lab Details

    12/22

    \\IMPLEMENTATION OF MACROPROCESSOR\\

    #include#include#include#includevoid 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("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

  • 8/12/2019 Ss Lab Details

    13/22

    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

  • 8/12/2019 Ss Lab Details

    14/22

    SAMPLE INPUT AND OUTPUT

    No.of macros=2

    Enter the text filenameNITNIT1

    /*macin.txt*/

    ** macro m1** move a,b** mend ---** macro m2** lda b** mend ---** start 2000** lda a** call m1** call m2** add a,b

    /* outm.txt*/

    **macrom1**movea,b**mend---**macrom2**ldab**mend---**start2000**ldaa

    **movea,b**ldab** add a,b

    \\IMPLEMENTATION OF ABSOLUTE LOADER\\

    #include

    #include

    #include

  • 8/12/2019 Ss Lab Details

    15/22

    #include

    struct object_code

    {

    int locctr;

    char byte[5];

    };

    struct object_code code[200];

    void main()

    {

    FILE *fp1,*fp2;

    char input[15];

    int i,len,n=0,count=0,inc=0,textloc,tlen,tloc=0,num=0,loc;

    clrscr();

    fp1=fopen("lout.txt","r");

    fp2=fopen("loadout.txt","w");

    rewind(fp1);

    rewind(fp2);

    fscanf(fp1,"%s",input);

    if(strcmp(input,"H")==0)

    {

    for(i=0;i

  • 8/12/2019 Ss Lab Details

    16/22

    code[inc++].locctr=loc++;

    fscanf(fp1,"%x",&tlen);

    tloc=textloc;

    }

    }

    else

    {

    len=strlen(input);

    for(i=0;i1)

    {

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

    {

    fprintf(fp2,"\t");

    n=0;

    count++;

    }

    if(count>3)

  • 8/12/2019 Ss Lab Details

    17/22

    {

    fprintf(fp2,"\n%x\t",code[i+1].locctr);

    count=0;

    }

    }

    getch();

    }

    INPUT:

    H COPY 002000 00107A

    T 002000 1e 142033 483039 102036 282030 302015

    483061 3C2003 00202a 0c2039 00202a

    T 00201e 15 2c2036 483061 182033 4c0000 454f46

    200000 100000

    T 002039 1e 242030 302030 e0305d 30303f d8305d

    282030 303057 53a039 2c305e 38303f

    T 002057 a 102036 4c0000 f1 201000

    T 002071 19 342030 e03079 303064 3fa039 dC3079

    2c2036 383064 4c0000 15

    E 002000

    OUTPUT:

    2000 1e142033 48303910 20362820 30302015

    2010 4830613C 20030020 2a0c2039 00202a15

    2020 2c203648 30611820 334c0000 454f4620

    2030 00001000 001e2420 30302030 e0305d30

    2040 303fd830 5d282030 30305753 a0392c30

    2050 5e38303f a1020364 c0000f12 01000193

    2060 42030e03 07930306 43fa039d C30792c2

    2070 03638306 44c00001

  • 8/12/2019 Ss Lab Details

    18/22

    //IMPLEMENTATION OF AN RELOCATION LOADER

    #include

    #include

    #include

    #include

    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,in

    c=0;

    FILE *fp1,*fp2;

    clrscr();

    fp1=fopen("linput.txt","r");fp2=fopen("loutput.txt","w");

    printf("Enter the location where the program has to be loaded:");

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

    }

    itoa(start,output[2],10);

    while(strcmp(input[i],"E")!=0)

    {

    strcpy(output[i],input[i]);

    if(strcmp(input[i],"T")==0){

  • 8/12/2019 Ss Lab Details

    19/22

    for(j=0;j

  • 8/12/2019 Ss Lab Details

    20/22

    itoa(loc,address,10);

    strcat(output[i],address);

    }

    k++;

    len=strlen(output[i]);num=0;

    for(n=0;n1){

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

    {

    fprintf(fp2,"\t");

    i=0;count++;

  • 8/12/2019 Ss Lab Details

    21/22

    }

    if(count>3)

    {

    fprintf(fp2,"\n%d\t",obcode[n+1].locctr);

    count=0;}

    }

    getch();

    }

    /*INPUT FILES*/

    /*LINPUT.txt*/

    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

    T 000031 15 135 140030 430030 141013 301044 241064 210030

    301057

    543039 212064 381045

    T 000058 05 056 100036 520000 151 301000T 000065 19 080 340030 141079 301064 503039 152079 220036

    381064

    E 000000

  • 8/12/2019 Ss Lab Details

    22/22

    /*OUTPUT FILES*/

    /*LOUTPUT.txt*/

    3000 14303348 40391030 36283030 30001548

    3016 10613110 03200030 21103320 0033xx413032 50364810 61383033 41500045 41961030

    3048 03200000 xx143030 43003014 10133010

    3064 44241064 21303030 40575460 39212064

    3080 381045xx xxxxxxxx xxxxxxxx xxxxxx10

    3096 30365230 00154000 301000xx xx343030

    3112 14107930 40645030 39152079 22003638

    3128 1064