Anda di halaman 1dari 48

1

EX: NO: 1 PROGRAM TO SYMBOL TABLE AIM: To write a C program to create a symbol table and do the insertion, modification, search and display operations. ALGORITHM: 1. Start the program. 2. Create a menu through which we can create a symbol table and perform operations as insert, modify, search and display. 3. Create a symbol table with fields as variable and value using !create" option. #ntries may be added to the table while it is created itself. $. %ppend new contents to the symbol table with the constraint that there is no duplication of entries, using !insert" option. &. 'odify e(isting content of the table using modify option. ). *se display option to display the contents of the table.

PROGRAM: +include,stdio.h+include,conio.h+include,stdlib.h+include,string.hstruct table . char var/1012 int value2 32 struct table tbl/2012 int i,4,n2 void create562 void modify562 int serach5char variable/1,int n62 void insert562 void display562 void main56 . int ch,result702 char v/1012 clrscr562 do . printf58 1.Create 9n862 printf58 2.:nsert 9n862 printf58 3.'odify 9n862 printf58 $.Search 9n862 printf58 &.;isplay 9n862 printf58 ).#(it 9n862 printf58#nter the Choice<862 scanf58=d8,>ch62 switch5ch6 . case 1<create562 brea?2 case 2< insert562 brea?2 case 3<modify562 brea?2 case $<printf58#nter the variable to be searched for<862 scanf58=s8,>v62

result7search5v,n62 if5result7706 printf58The variable does not belong to the table862 else printf58The location of the variable is =d 9n The value of =s is =d 8, result,tbl/result1.var,tbl/result1.value62 brea?2 case &<display562 brea?2 case )<e(it5162 3 3 while5ch@7)62 getch562 3 void create56 . printf58#nter the number of entries<862 scanf58=d8,>n62 printf58#nter the variable and the value<9n862 for5i712i,7n2iAA6 . scanf58=s=d8,tbl/i1.var,>tbl/i1.value62 chec?< if5tbl/i1.var/01-7B0B>>tbl/i1.var/01,7BCB6 . printf58The variable should start with an alphabet 9n #nter correct variable name<"62 scanf58=s=d8,tbl/i1.var,>tbl/i1.value62 goto chec?2 3 chec?1< for547124,i24AA6 . if5strcmp5tbl/i1.var,tbl/41.var67706 . printf58The variable already e(ists.#nter another variable 9n862 scanf58=s=d8,tbl/i1.var,>tbl/i1.value62 goto chec?12 3 3 3 printf58The table after creation is<9n862 display562 3

void insert56 . if5i-7206 printf58cannot insert. table is full9n862 else . nAA2 printf58#nter the variable and the value<9n862 scanf58=d=s8,tbl/n1.var,>tbl/n1.value62 chec?< if5tbl/i1.var/01-7B0B>>tbl/i1.var/01,7BCB6 . printf58The variable should start with an alphabet 9n #nter the correct variable name<"62 scanf58=s=d8,tbl/i1.var,>tbl/i1.value62 goto chec?2 3 chec?1< for547124,n24AA6 . if5strcmp5tbl/41.var,tbl/i1.var67706 . printf58The variable already e(ists. #nter the another variable9n862 scanf58=s=d8,tbl/i1.var,>tbl/i1.value62 goto chec?12 3 3 printf58The table after insertion is<9n862 display562 3 3 void modify56 . char variable/1012 int result702 printf58#nter the variable to be modified<862 scanf58=s8,>variable62 result7search5variable,n62 if5result7706 printf58=s does not belong to the table8,variable62 else . printf58The current value of the variable =s is =d 9n enter the new variable and its value",tbl/result1.var,tbl/result1.value62

&

scanf58=s=d8,tbl/result1.var,>tbl/result1.value62 chec?< if5tbl/i1.var/01-7B0B>>tbl/i1.var/01,7BCB6 . printf58The variable should start with an alphabet 9n #nter the correct variable name<"62 scanf58=s=d8,tbl/i1.var,>tbl/i1.value62 goto chec?2 3 3 printf58The table after modification<9n862 display562 3 int search5char variable/1,int n6 . int flag2 for5i712i,7n2iAA6 if5strcmp5tbl/i1.var,variable67706 . flag712 brea?2 3 if5flag7716 return i2 else return 02 3 void display56 . printf58D%E:%FG#9tD%G*#9n862 for5i712i,7n2iAA6 printf58=s9t9t=d9n8,tbl/i1.var,tbl/i1.value62 3

OUTPUT: 1.Create 2.:nsert 3.'odify $.Search &.;isplay ).#(it #nter the Choice<1 #nter the number of entries<2 #nter the variable and the value< ; $3 T) The table after creation is< D%E:%FG# D%G*# ; $3 T ) 1.Create 2.:nsert 3.'odify $.Search &.;isplay ).#(it #nter the Choice<2 #nter the variable and the value< H &$ The table after insertion is< D%E:%FG# D%G*# ; $3 T ) 0 1.Create 2.:nsert 3.'odify $.Search &.;isplay ).#(it #nter the Choice<#nter the variable and the value< The variable already e(ists. #nter the another variable The table after insertion is< D%E:%FG# D%G*# ; $3 T ) 0 H &$ 1.Create

2.:nsert 3.'odify $.Search &.;isplay ).#(it #nter the Choice<3 #nter the variable to be modified<; ) The current value of the variable ; is $3 enter the new variable and its valueI CJ The variable should start with an alphabet #nter the correct variable name<The table after modification< D%E:%FG# D%G*# I CJ T ) 0 H &$ 1.Create 2.:nsert 3.'odify $.Search &.;isplay ).#(it #nter the Choice<$ #nter the variable to be searched for<T The location of the variable is 2 The value of T is ) 1.Create 2.:nsert 3.'odify $.Search &.;isplay ).#(it #nter the Choice<& D%E:%FG# D%G*# I CJ T ) 0 H &$ 1.Create 2.:nsert 3.'odify $.Search &.;isplay ).#(it #nter the Choice<) RESULT: Thus the symbol table operations were performed.

EX: NO: 2 IMPLEMENTATION OF A PASS 1 OF A TWO PASS ASSEMBLER AIM: To implement a pass1 of a two pass assembler using C . ALGORITHM: 1. Eead the input line. 2. Chec? to see if the opcode in the input line is !ST%ET". 5i6Iind if there is any operand field after ST%ET, if so that indicates the location from which the program has to be loaded in the memory, so initialiLe the GMCCTE to the operand value. 5ii6Mtherwise if there is no value in the operand field the GMCCTE is set to Lero and the program starts loading from the starting address Lero. 3. Nrite the line to the intermediate file. $. Oow start the following processing for the other lines in the program, if it is not a comment line, until the opcode is !#O;" 5a6:f there is a symbol in the label field. 5i6Chec? the symbol table to see if has already been stored over there. :f so then it is a duplicate symbol, the error message should be displayed. 5ii6Mtherwise the symbol is entered into the SP'T%F, along with the memory address in which it is stored. 5b6:f there is an opcode in the line read. 5i6Search the MQT%F to see if the opcode is present, if so increment the location counter by 3. 5ii6:f the opcode is !NME;", increment the location counter by 3 5iii6 :f the opcode is !FPT#", increment the location counter by 1. 5iv6 :f the opcode is !E#SN", increment the location counter by the integer eRuivalent of the operand value S3.

5v6:f the opcode is !E#SF", increment the location counter by the integer eRuivalent of the operand value. 5c6Nrite each and every line processed to the intermediate file along with their location counters. &. Subtract the starting address of the program from the final value of the GMCCTE, to calculate the length of the program. ). Close all the opened files and e(it.

10 PROGRAM:

+include,stdio.h+include,conio.h+include,string.hstruct optab . char mnem/101,mcode/2012 32 struct optab op/)17..8G;%8,8&C83,.8G;T8,80$83,.8ST%8,80&83, .8%;;8,8$C83,.8T:T8,80J83,.8UGT8,80&8332 void main56 . char opcode/101,operand/101,label/101,flag2 int i,locctr,curaddr2 I:G# Sfp1,Sfp2,Sfp32 clrscr562 fp17fopen581in.dat8,8r862 fp27fopen58inter.dat8,8w862 fp37fopen58symbol.dat8,8w862 fscanf5fp1,8=s9t=s9n8,opcode,operand62 if5strcmp5opcode,8ST%ET867706 . locctr7atoi5operand62 fscanf5fp1,8=s9t=s9t=s9n8,label,opcode,operand62 while5strcmp5opcode,8#O;86@706 . flag7BfB2 curaddr7locctr2 if5strcmp5label,8O*GG86@706 fprintf5fp3,8=s9t=d9n8,label,curaddr62 for5i702i,7&2iAA6 . if5strcmp5opcode,op/11.mnem67706 flag7BtB2 3 if5flag77BtB6 locctrA732 else if5strcmp5opcode,8NME;867706 locctrA732 else if5strcmp5opcode,8E#SN867706 locctrA73Satoi5operand62 else if5strcmp5opcode,8E#SF867706 locctrA7atoi5operand62 else if5strcmp5opcode,8FPT#867706

11

locctrA7strlen5operand62 else printf58=d9t=s9t=s9t=s9t invalid statement9n8,curaddr,label,opcode,operand62 fprintf5fp2,8=d9t=s9t=s9n8,curaddr,opcode,operand62 fscanf5fp1,8=s=s=s8,label,opcode,operand62 3 fprintf5fp2,89tO*GG9t#O;862 getch562 3 3

OUTPUT:

12

1IN.DAT: ST%ET O*GG E#Q#%T O*GG O*GG O*GG O*GG V#EM %GQW% F#T% H%''% ;#GT% O*GG 2000 G;T G;% %;; ST% T:T UGT FPT# E#SF E#SN E#SN NME; #O; V#EM %GQW% F#T% H%''% ;#GT% E#Q#%T 0 1 1 1 2

INTER.DAT: 2000 2003 2003 2003 2003 2003 2003 200$ 200& 200J 2011 G;T G;% %;; ST% T:T UGT FPT# E#SF E#SN E#SN NME; O*GG V#EM %GQW% F#T% H%''% ;#GT% E#Q#%T 0 1 1 1 2 #O;

SYMBOL.DAT: E#Q#%T V#EM %GQW% F#T% H%''% ;#GT% RESULT: Thus the Qass1 of a assembler were implemented and e(ecuted. EX: NO: 3 2003 2003 200$ 200& 200J 2011

13

IMPLEMENTATION OF A PASS2 OF A TWO PASS ASSEMBLER AIM: To Nrite a C program for pass2 of a two pass assembler. ALGORITHM: 1. Eead the first line from the intermediate file 2. Chec? to see if the opcode from the first line read is !ST%ET". 5i6 :f so then write the label, opcode and operand field values of the corresponding statement directly to the final output file. 3. Star the following processing, for the other lines in the intermediate file if it is not a comment line until an !#O;" statement is reached 5i6 Start writing the location counter, opcode and operand fields of the corresponding statement to the output file, along with the ob4ect code. The ob4ect code is found by assembling each statement opcodes machine eRuivalent with the label address. 5ii6 :f there is no symbolXlabel in the operand field, then the operand address is assigned as Lero, and it is assembled with the ob4ect code of the instruction. 5iii6 :f the opcode is FPT#,NME;,E#SN,etc convert the constants to the ob4ect code. $. Close the opened files and e(it.

PROGRAM:

1$

+include,stdio.h+include,conio.h+include,string.hstruct optab . char mnem/101,mcode/2012 32 struct optab op/)17..8G;%8,8&C83,.8G;T8,80$83,.8ST%8,80&83, .8%;;8,8$C83,.8T:T8,80J83,.8UGT8,80&8332 void main56 . char opcode/101,operand/101,label/101,ob4/101,addr/101,addr1/101, symbol/1012 int i2 I:G# Sfp1,Sfp2,Sfp32 clrscr562 fp17fopen5822in.dat8,8r862 fp27fopen5822sym.dat8,8r862 fp37fopen5822out.dat8,8w862 fscanf5fp1,8=s9t=s9t=s9t=s9n8,addr,label,opcode,operand62 while5@feof5fp166 . strcpy5ob4,8 862 for5i702i,7&2iAA6 . if5strcmp5opcode,op/i1.mnem67706 strcpy5ob4,op/i1.mcode62 3 rewind5fp262 while5@feof5fp266 . fscanf5fp2,8=s=s9n8,symbol,addr162 if5strcmp5symbol,operand67706 strcat5ob4,addr162 3 if55strcmp5opcode,8FPT#867706YY5strcmp5opcode,8NME;8677066 strcat5ob4,operand62 fprintf5fp3,8=s9t=s9t=s9t=s9t=s9n8,addr,label,opcode,operand,ob462 printf58=s9t=s9t=s9t=s9t=s9n8,addr,label,opcode,operand,ob462 fscanf5fp1,8=s9t=s9t=s9t=s9n8,addr,label,opcode,operand62 3 getch562 3 OUTPUT:

1&

22IN.DAT: 2000 G;T V#EM 2003 G;% %GQW% 2003 %;; F#T% 2003 ST% H%''% 2003 T:T ;#GT% 2003 UGT E#Q#%T 2003 FPT# 0 200$ E#SF 1 200& E#SN 1 200J E#SN 1 2011 NME; 2 O*GG #O; 22SYM.DAT: E#Q#%T V#EM %GQW% F#T% H%''% ;#GT% 2003 2003 200$ 200& 200J 2011

22OUT.DAT: 2000 G;% F#T% 2003 UGT 0 200& E#SN RESULT: Thus the pass2 of a assembler were implemented and e(ecuted. G;T %GQW% 2003 T:T E#Q#%T 200$ E#SN 1 V#EM 2003 ST% ;#GT% 2003 E#SF 1 2011 2003 %;; H%''% 2003 FPT# 1 200J NME;

0&200J

EX.NO:4

1)

IMPLEMENTATION OF SINGLE PASS ASSEMBLER AIM: To implement a single pass assembler by using C language. ALGORITHM: 1. Start the program. 2. %ssembler simply generates ob4ect code as it scans the source program. 3. :f the instruction operand is a symbol that has not yet been defined, the operand address is omitted when the instruction is assembled. $. This symbol is entered into the symbol table, as an undefined symbol with S . &. Nhen the definition of the symbol is encountered, the proper address is inserted in a list associated with that symbol. ). Nhen nearly half of the program translation is over some of the forward reference problems that e(isted earlier would have been resolved, while others might have been added. K. Continue the process to the end of the program to fill all the forward references properly. J. %t the end of the program, symbol table entries still mar?ed with S are undefined symbols. C. Stop the program

PROGRAM:

1K

+include,stdio.h+include,conio.h+include,string.h+include,stdlib.h+define '%T 10 struct input . char label/101,opcode/101,operand/101,mnemonic/&12 int loc2 32 struct input table/'%T12 struct symtab . char sym/1012 int f,val,ref2 32 struct symtab symtbl/'%T12 void main56 . int f1,i71,471,flag,locctr,(2 char add/101,code/101,mnemcode/&12 I:G# Sfp1,Sfp2,Sfp32 clrscr562 fp17fopen58sin.dat8,8r862 fp27fopen58optab.dat8,8r862 fp37fopen58sout.dat8,8w862 fscanf5fp1,8=s=s=s8,table/i1.label,table/i1.opcode,table/i1.operand62 if5strcmp5table/i1.opcode,8ST%ET867706 . locctr7atoi5table/i1.operand62 iAA2 fscanf5fp1,8=s=s=s8,table/i1.label,table/i1.opcode,table/i1.operand62 3 else locctr702 while5strcmp5table/i1.opcode,8#O;86@706 . if5strcmp5table/i1.label,8SS86@706 . for5(712(,42(AA6 . f1702 if55strcmp5symtbl/(1.sym,table/i1.label67706>>5symtbl/(1.f77166 . symtbl/(1.val7locctr2 symtbl/(1.f702

1J

table/symtbl/(1.ref1.loc7locctr2 f1712 brea?2 3 3 if5f17706 . strcpy5symtbl/41.sym,table/i1.label62 symtbl/41.val7locctr2 symtbl/41.f702 4AA2 3 3 fscanf5fp2,8=s=s8,code,mnemcode62 while5strcmp5code,8#O;86@706 . if5strcmp5table/i1.opcode,code67706 . strcpy5table/i1.mnemonic,mnemcode62 locctrA732 for5(712(,742(AA6 . flag702 if5strcmp5table/i1.operand,symtbl/(1.sym67706 . flag712 if5symtbl/(1.f7706 table/i1.loc7symtbl/(1.val2 brea?2 3 3 if5flag@716 . strcpy5symtbl/41.sym,table/i1.operand62 symtbl/41.f712 symtbl/41.ref7i2 4AA2 3 3 fscanf5fp2,8=s=s8,code,mnemcode62 3 rewind5fp262 if5strcmp5table/i1.opcode,8NME;867706 . locctrA732

1C

strcpy5table/i1.mnemonic,B90B62 table/i1.loc7atoi5table/i1.operand62 3 else if5strcmp5table/i1.opcode,8E#SN867706 . locctrA753S5atoi5table/i1.operand6662 strcpy5table/i1.mnemonic,B90B62 table/i1.loc7atoi5B90B62 3 else if 5strcmp5table/i1.opcode,8FPT#867706 . AAlocctr2 if55table/i1.operand/0177BCB6YY5table/i1.operand/0177BTB66 table/i1.loc75int6table/i1.operand/212 else table/i1.loc7locctr2 3 iAA2 fscanf5fp1,8=s=s=s8,table/i1.label,table/i1.opcode,table/i1.operand62 3 for5(712(,7i2(AA6 fprintf5fp3,8=s9t=s9t=s9t =s9n8,table/(1.label,table/(1.opcode,table/(1.operand,strcat5table/(1.mnemonic ,itoa5table/(1.loc,add,106662 for5(712(,42(AA6 printf58=s9t=d9n8,symtbl/(1.sym,symtbl/(1.val62 getch562 3 OUTPUT: SIN.DAT SS ST%ET SS US*F SS US*F %GQW% NME; F#T% E#SN H%''% FPT# ;#GT% E#SF CGMMQ G;% EGMMQ ST% SS G;CW SS STCW SS #O; OPTAB.DAT )000 CGMMQ EGMMQ 23 3 CBVB $ %GQW% F#T% H%''% ;#GT% SS

20

ST%ET US*F G;% ST% G;CW STCW #O;

S $J 1$ 03 &3 &K S

SOUT.DAT SS SS SS %GQW% F#T% H%''% ;#GT% CGMMQ EGMMQ SS SS SS ST%ET US*F US*F NME; E#SN FPT# E#SF G;% ST% G;CW STCW #O; )000 CGMMQ EGMMQ 23 3 CBVB $ %GQW% F#T% H%''% ;#GT% SS 0 $J)01C $J)022 23 0 C0 0 1$)00) 03)00C &3)01J &K)01C 23

RESULT: Thus the single pass assembler was successfully implemented.

EX: NO: 5

21

IMPLEMENTATION OF A MACRO PROCESSOR AIM: To implement a C program for macro processor. ALGORITHM: 1. Het the statement from the input file. 2. Irom the line read, chec? if the opcode is the directive !'%CEM", if so then the number of macro n must be incremented. 3. Eepeat step 1 and 2 until and end of file is encountered. $. Mpen n number of files in write mode. These files will later hold the body of the n macros respectively. &. Eewind the input file pointer. ). :f the opcode is !macro" 5i6#nter the name present in the operand field into the array named m . 5ii6Nrite the line to the e(panded output file. 5iii6#nter lines in the body of each macro into the corresponding files already opened in step$. 5iv6Nrite the body of each macro to the e(panded output files also, until a !mend" is reached. K. Mtherwise if the opcode is !call" the line must be a macro invocation statement so, the macro body is retrieved from the corresponding file and written to the e(panded output file. J. Nrite all the remaining lines directly to the e(panded file.

22 PROGRAM:

+include,stdio.h+include,conio.h+include,string.h+include,stdlib.hvoid main56 . char n1,n,c1,i2 char fn/101/101,ilab/201,iop/201,m/201/31,oper/201,opd/2012 I:G# Sf1,Sf2,Sp/&12 clrscr562 n702 f17fopen58macroin.dat8,8r862 while5@feof5f166 . fscanf5f1,8=s=s=s8,ilab,iop,oper62 if5strcmp5iop,8macro867706 nAA2 3 printf58no.of macros7=d9n8,n62 n17n2 printf58#nter the te(t filename<9n862 for5i702i,n2iAA6 . scanf58=s8,fn/i162 p/i17fopen5fn/i1,8w862 3 n702 rewind5f162 while5@feof5f166 . fscanf5f1,8=s=s=s8,ilab,iop,oper62 if5strcmp5iop,8macro867706 . strcpy5m/n1,oper62 fscanf5f1,8=s=s=s8,ilab,iop,oper62 while5strcmp5iop,8mend86@706 . fprintf5p/n1,8=s=s=s9n8,ilab,iop,oper62 fscanf5f1,8=s=s=s8,ilab,iop,oper62 3 fclose5p/n162 nAA2 3

23

3 for5i702i,n12iAA6 p/i17fopen5fn/i1,8r862 f27fopen58macroout.dat8,8w862 rewind5f162 fscanf5f1,8=s=s=s8,ilab,iop,oper62 while5@feof5f166 . if5strcmp5iop,8call867706 . for5i702i,n12iAA6 . if5strcmp5m/i1,oper67706 . rewind5p/i162 fscanf5p/i1,8=s=s=s8,ilab,iop,oper62 while5@feof5p/i166 . fprintf5f2,8=s=s=s9n8,ilab,iop,oper62 c1712 fscanf5p/i1,8=s=s=s8,ilab,iop,oper62 3 brea?2 3 3 3 if5c1@716 fprintf5f2,8=s=s=s9n8,ilab,iop,oper62 c1702 fscanf5f1,8=s=s=s8,ilab,iop,oper62 3 fprintf5f2,8=s=s=s9n8,ilab,iop,oper62 getch562 3

2$

OUTPUT: no.of macros72 #nter the te(t filename< 'acroin.dat 'acroout.dat MACROIN.DAT: SS macro m1 SS move a,b SS mend ZZZ SS macro m2 SS lda b SS mend ZZZ SS start 1000 SS lda a SS call m1 SS call m2 SS add a,b MACROOUT.DAT: SS macro m1 SS move a,b SS mend ZZZ SS macro m2 SS lda b SS mend ZZZ SS start 1000 SS lda a SS move a,b SS lda b SS add a,b RESULT: Thus the macro processor was implemented successfully.

2&

EX.NO:6 IMPLEMENTATION OF ABSOLUTE LOADER AIM: To implement an %bsolute Goader by using C. ALGORITHM: 1. Eead header record and filter the starting location and other details. 2. Eead first te(t record. 3. ;o the following processing until an end record is reached 5i6:f ob4ect is in character form converts it to internal he(adecimal representation. 5ii6'ove ob4ect codes to specified locations in memory. 5iii6Nrite the starting location counter value of a bloc? of ob4ect code, and the corresponding internal he(adecimal representations to the output file. 5iv6Eead ne(t te(t record from the input file. $. Close all the opened files and e(it.

2)

PROGRAM: +include,stdio.h+include,conio.h+include,string.h+include,stdlib.hstruct ob4ect[code . int locctr2 char byte/&12 32 struct ob4ect[code code/20012 void main56 . I:G# Sf1,Sf22 char ip/1&12 int i,len,n70,count70,inc70,te(tloc,tlen,tloc70,num70,loc2 clrscr562 f17fopen58ain.dat8,8r862 f27fopen58aout.dat8,8w862 rewind5f162 rewind5f262 fscanf5f1,8=s8,ip62 if5strcmp5ip,8W867706 . for5i702i,$2iAA6 . if5i7716 fscanf5f1,8=(8,>loc62 else fscanf5f1,8=s8,ip62 3 3 tloc7loc2 while5strcmp5ip,8#86@706 . if5strcmp5ip,8T867706 . fscanf5f1,8=(8,>te(tloc62 for5i702i,5te(tlocZ5tlocAlen662iAA6 . strcpy5code/inc1.byte,8((862 code/incAA1.locctr7locAA2 brea?2 3

2K

fscanf5f1,8=(8,>tlen62 tloc7te(tloc2 3 else . len7strlen5ip62 for5i702i,len2iAA6 . code/inc1.byte/numAA17ip/i12 if5num-16 . code/inc1.locctr7loc2 locAA2 incAA2 num702 3 3 3 fscanf5f1,8=s8,ip62 3 n702 i702 count702 fprintf5f2,8=(9t8,code/i1.locctr62 for5i702i,inc2iAA6 . fprintf5f2,8=s8,code/i1.byte62 nAA2 if5n-36 . fprintf5f2,89t862 n702 countAA2 3 if5count-36 . fprintf5f2,89n=(9t8,code/iA11.locctr62 count702 3 3 getch562 3

2J

OUTPUT: AIN.DAT W CMQP 002000 0010Ka T 002000 1e 1$2033 $J303C 10203) 2J2030 30201& $J30)1 3c2003 00202a 0c203C 00202d T 00201e 1& 2c203) $J30)1 1J2033 $c0000 $&$f$) 200003 100000 T 00203C 1e 2$2030 302030 e030&d 30303f dJ30&d 2J2030 3030&K &3a03C 2c30&e 3J303f # 002000 AOUT.DAT 2000 2010 2020 2030 20$0 20&0 ((1$2033 $J30)13c 2c203)$J 00031000 303fdJ30 &e3J303f $J303C10 20030020 30)11J20 00((2$20 &d2J2030 203)2J20 2a0c203C 33$c0000 30302030 3030&K&3 3030201& 00202d(( $&$f$)20 e030&d30 a03C2c30

RESULT: Thus the absolute loader was successfully implemented and e(ecuted.

2C

EX.NO:7 IMPLEMENTATION OF RELOCATION LOADER AIM: To implement a C program for relocation loader. ALGORITHM: 1. #nter the new starting location to which the ob4ect code has to be relocated. 2. Eead the content of the input file as strings one at a time in an arrry "input". 3. Transfer the string read in array !input" into another array !output", until !T" is encountered. $. 'ove the consecutive ne(t three strings into array !output". &. Convert the current string read which is the relocation bit associated with each te(t record to binary form. ). 'a?e the necessary changes in the corresponding words of ob4ect code by adding the new starting address with the address part of the ob4ect code for which the corresponding relocation bit to set, and store the updated ob4ect code into the array !output". K. 'ove the ob4ect code for which the corresponding relocation bit is not set directly to the array !output" from the array !input" without any change. J. Eepeat step 2 to J until an end record is encountered. C. :f ob4ect code is in character form convert form it to internal he(adecimal representation. 10. 'ove ob4ect codes to specified locations in memory. 11. Nrite the starting location counter value of a bloc? of ob4ect code, and the corresponding internal he(adecimal representations to the output file.

30

PROGRAM: +include,stdio.h+include,conio.h+include,string.h+include,stdlib.hstruct ob4ect[code . int locctr2 char add/1012 3opcode/30012 void main56 . char in/1001/1)1,op/1001/1)1,bin/201,address/201,stloc/1012 int len,bitmas?,loc,tlen70,tloc,te(tloc,i70,location,4,?,count70,start,n,num70,inc7 02 I:G# Sf1,Sf22 clrscr562 f17fopen58lin.dat8,8r862 f27fopen58lout.dat8,8w862 printf58#nter the location where the program has to be loaded<862 scanf58=s8,stloc62 start7atoi5stloc62 location7start2 tloc7start2 fscanf5f1,8=s8,in/i162 while5strcmp5in/i1,8T86@706 . strcpy5op/i1,in/i162 iAA2 fscanf5f1,8=s8,in/i162 strcpy5op/i1,in/i162 3 itoa5start,op/21,1062 while5strcmp5in/i1,8#86@706 . strcpy5op/i1,in/i162 if5strcmp5in/i1,8T867706 . for547024,324AA6 . iAA2 fscanf5f1,8=s8,in/i162 strcpy5op/i1,in/i162

31

3 bitmas?7atoi5op/i162 itoa5bitmas?,bin,262 strcpy5op/i1,O*GG62 te(tloc7atoi5op/iZ2162 te(tloc7te(tlocAstart2 itoa5te(tloc,op/iZ21,1062 for5n702n,5te(tlocZ5tlocAtlen662nAA6 . strcpy5opcode/inc1.add,8((862 opcode/incAA1.locctr7locationAA2 3 tlen7atoi5op/iZ1162 tloc7te(tloc2 ?702 3 else . if5bin/?177B1B6 . num702 len7strlen5op/i162 strcpy5address,O*GG62 for547224,len24AA6 . address/num17op/i1/412 op/i1/417B90B2 numAA2 3 loc7atoi5address62 loc7locAstart2 itoa5loc,address,1062 strcat5op/i1,address62 3 ?AA2 len7strlen5op/i162 num702 for5n702n,len2nAA6 . opcode/inc1.add/numAA17op/i1/n12 if5num-16 . opcode/incAA1.locctr7locationAA2 num702 3 3 3

32

iAA2 fscanf5f1,8=s8,in/i162 3 strcpy5op/i1,in/i162 iAA2 fscanf5f1,8=s8,in/i162 loc7atoi5in/i162 loc7locAstart2 strcpy5op/i1,itoa5loc,address,10662 count702 i702 n702 fprintf5f2,8=d9t8,opcode/n1.locctr62 for5n702n,inc2nAA6 . fprintf5f2,8=s8,opcode/n1.add62 iAA2 if5i-36 . fprintf5f2,89t862 i702 countAA2 3 if5count-36 . fprintf5f2,89n=d9t8,opcode/nA11.locctr62 count702 3 3 getch562 3 OUTPUT: #nter the location where the program has to be loaded< &000 LIN.DAT W CMQP 000000 0010K3 T 000000 10 01& 1$0033 $J103C 10003) 2J0030 30001& $J10)1 311003 200030 211033 200033 T 000011 1C 0$& $1203) $J10)1 3J0033 $12000 $&$1C) 100003 200000 # 000000 LOUT.DAT &000 1$&033$J )03C10&0 3)2J&030 30001&$J &01) )0)13110 03200030 21103320 0033(($1 &032 K03)$J10 )13J&033 $1K000$& $1C)10&0 &0$J 03200000 RESULT: Thus the relocation loader were implemented and e(ecuted.

33

EX.NO: IMPLEMENTATION OF A PASS1 OF A DIRECT LIN!ING LOADER AIM: To implement a pass 1 of a direct lin?ing loader by using C program. ALGORITHM: 1. #nter the location where the program has to be loaded. 2. %ssign the address got from the user as the first control section address. 3. Eead the header record of the control section 5i6Irom the details of the header read store the control section length in a variable 5ii6#nter the control section name with its address into the e(ternal symbol table. $. Ior each symbol in the subseRuent ; records the symbol must be entered into the symbol table along with its address, added along with the corresponding control section address until an end record is reached. &. %ssign the starting address of ne(t control section as the address of the current control section plus the length of the control section ). Eepeat the process from step 3 to step & until there are no more input.

3$

PROGRAM: +include,stdio.h+include,conio.h+include,stdlib.h+define '%T 10 struct estab . char csect/101,symname/1012 long int add2 int length2 3table/'%T12 void main56 . I:G# Sf1,Sf22 char ip/1012 long int i,count70,start,length,loc2 clrscr562 f17fopen58l?in.dat8,8r862 f27fopen58l?out.dat8,8w862 printf58#nter the location where the pgm has to be loaded<862 scanf58=l(8,>start62 fprintf5f2,8CS#CT Tsymname Taddress Tlength9n862 rewind5f162 while5@feof5f166 . fscanf5f1,8=s8,ip62 if5strcmp5ip,8W867706 . fscanf5f1,8=s8,ip62 strcpy5table/count1.csect,ip62 strcpy5table/count1.symname,890862 fscanf5f1,8=s8,ip62 table/count1.add7atoi5ip6Astart2 fscanf5f1,8=s8,ip62 length7atoi5ip62 table/countAA1.length7atoi5ip62 fscanf5f1,8=s8,ip62 3 if5strcmp5ip,8;867706 . fscanf5f1,8=s=l(8,ip,>loc62 while5strcmp5ip,8E86@706 . strcpy5table/count1.csect,890862

3&

strcpy5table/count1.symname,ip62 table/count1.add7locAstart2 table/countAA1.length702 fscanf5f1,8=s=l(8,ip,>loc62 3 while5strcmp5ip,8T86@706 fscanf5f1,8=s8,ip62 3 if5strcmp5ip,8T867706 while5strcmp5ip,8#86@706 fscanf5f1,8=s8,ip62 fscanf5f1,8=s8,ip62 start7startAlength2 3 for5i702i,count2iAA6 fprintf5f2,8=s9t=s9t=l(9t =d9n8,table/i1.csect,table/i1.symname,table/i1.add,table/i1.length62 getch562 3 OUTPUT: L!IN.DAT W QEMH% 000000 0000K0 ; G:ST 0000$0 #O;% E G:STF #O;F G:STC #O;C T 000020 10 03201; KK10000$ ' 00002$ 0& AG:STF ' 0000&$ 0) AG:STC ' 0000&J 0) A#O;C ' 0000)$ 0) AG:STF # 000000 L!OUT.DAT CS#CT QEMH% Tsymname G:ST #O;% RESULT: Thus the pass 1 of a direct lin?ing loader were successfully implemented Taddress 3000 30$0 30&$ Tlength K0 0 0 0000&$ 1&001$

3)

EX: NO: " IMPLEMENTATION OF A PASS2 OF A DIRECT LIN!ING LOADER AIM: To implement a pass2 of a direct lin?ing loader using C. ALGORITHM: 1. %ssign the control section address in a variable, CS%;E. 2. Eead the header record. 5i6Irom the information available in the header record, store the control section length in a variable. 3. ;o the following process until an end record is reached. 5i6:f the subseRuent records read is a te(t record T , and if the ob4ect code is in character form convert it into machine representation ,and move the ob4ect code from the record to the memory location control sections address plus the specified address in the te(t record. 5ii6:f the subseRuent records read is modification record ' then search for the modifying symbol name in the e(ternal symbol table created by pass1 if it is found then add, or subtract the corresponding symbol address found with the value starting at the location $. %dd the control section length to the current control sections address to find the address of the ne(t control section, and repeat the entire process until there are no more input.

3K

PROGRAM: +include,stdio.h+include,conio.h+include,stdlib.h+include,string.hstruct e(ttable . char csect/101,sname/1012 int padd,plen2 3estab/2012 struct ob4code . char code/1&12 int add2 3ob4co/&0012 void main56 . I:G# Sf1,Sf2,Sf32 int i,4,(,y,n70,num70,inc70,count70,record70,pstart,e(eloc,start,te(tloc,loc,mloc/ 301,te(len,mlen/301,len,location2 long int newadd2 char Sadd1,operation,lbl/101,ip/101,label/301/101,addr/1012 clrscr562 f17fopen582lin.dat8,8r862 f27fopen582l?out.dat8,8r862 f37fopen582lin?out.dat8,8w862 rewind5f162 rewind5f262 rewind5f362 while5@feof5f266 . fscanf5f2,8=s=s=d =d8,estab/num1.csect,estab/num1.sname,>estab/num1.padd,>estab/num1.plen 62 numAA2 3 e(eloc7estab/01.padd2 loc7e(eloc2 start7loc2 while5@feof5f166 . fscanf5f1,8=s8,ip62 if5strcmp5ip,8W867706

3J

. fscanf5f1,8=s8,ip62 for5i702i,num2iAA6 if5strcmp5ip,estab/i1.csect67706 . pstart7estab/i1.padd2 brea?2 3 while5strcmp5ip,8T86@706 fscanf5f1,8=s8,ip62 3 do . if5strcmp5ip,8T867706 . fscanf5f1,8=d8,>te(tloc62 te(tloc7te(tlocApstart2 for5i702i,5te(tlocZloc62iAA6 . strcpy5ob4co/inc1.code,8((862 ob4co/incAA1.add7startAA2 3 fscanf5f1,8=d8,>te(len62 loc7te(tlocAte(len2 3 else if5strcmp5ip,8'867706 . fscanf5f1,8=d8,>mloc/record162 mloc/record17mloc/record1Apstart2 fscanf5f1,8=d8,>mlen/record162 fscanf5f1,8=s8,label/recordAA162 3 else . len7strlen5ip62 (702 for5i702i,len2iAA6 . ob4co/inc1.code/(AA17ip/i12 if5(-16 . ob4co/incAA1.add7startAA2 (702 3 3

3C

3 fscanf5f1,8=s8,ip62 3 while5strcmp5ip,8#86@7062 if5strcmp5ip,8#867706 fscanf5f1,8=s8,ip62 3 for5n702n,record2nAA6 . operation7label/n1/012 len7strlen5label/n162 for5i712i,len2iAA6 lbl/iZ117label/n1/i12 lbl/lenZ117B90B2 len702 strcpy5addr,890862 location7mloc/n1Ze(eloc2 loc7location2 count702 while5len,mlen/n16 . strcat5addr,ob4co/locationAA1.code62 countAA2 lenA722 3 for5i702i,num2iAA6 if5strcmp5lbl,estab/i1.sname67706 brea?2 switch5operation6 . case BAB<newadd7strtol5addr,>add1,106A5long int6estab/i1.padd2 brea?2 case BZB<newadd7strtol5addr,>add1,106A5long int6estab/i1.padd2 brea?2 3 ltoa5newadd,addr,1062 (702y702 while5count-06 . ob4co/loc1.code/(AA17addr/yAA12 if5(-16 . (702 locAA2 countZZ2

$0

3 3 3 count702 n702 fprintf5f3,8=d9t8,ob4co/01.add62 for5i702i,inc2iAA6 . fprintf5f3,8=s8,ob4co/i1.code62 nAA2 if5n-36 . fprintf5f3,89t862 n702 countAA2 3 if5count-36 . fprintf5f3,89n=d9t8,ob4co/iA11.add62 count702 3 3 getch562 3

$1

OUTPUT: 2LIN.DAT W ; E T ' ' ' ' # 2LOUT.DAT QEMH% SS SS 2LIN!OUT.DAT 2000 (((((((( (((((((( 201) (((((((( 03201;1KK 2032 (((((((( (((((((( (((((((( 10213$1& (((((((( (((((((( 001$ (((( (((((((( SS G:ST% #O;% 2000 20$0 20&$ K0 0 0 QEMHF G:STF G:ST% 000020 0000$$ 0000K0 0000K$ 0000)$ 000000 000000 0000)0 #O;% 11 0& 0) 0) 0) 0000J0 #O;F G:STC 03100000 AG:ST% A#O;% A#O;% AG:STF 0000K0 #O;C KK202K

01&00000

RESULT:

Thus the pass2 of a direct lin?ing loader was implemented successfully.

$2

EX: NO: 1# IMPLEMENTATION OF A SIMPLE EDITOR AIM: To write a C program to implement a simple editor ALGORITHM: 1. Qrovide the blan? screen for the user to type the document 2. :nstruct the user to enter the te(t using the editor. 3. ;isplay the entered character on the screen. $. Characters displayed on the screen are stored in a character array. &. Specify if the content has to be saved or not. ). :f S%D# option is selected 5i6Het the data file name from the user 5ii6Mpen the file in write mode 5iii6'ove the content of the character array to the file and close the file. K. :f MQ#O option is selected 5i6Mpen the data file in the read mode 5ii6Eead one character at a time from the file and move it to the character array 5iii6Eepeat the above steps until the end of file is reached 5iv6;isplay the characters from the character array on the screen. J. Iinally close the te(t editor and e(it.

$3

PROGRAM: +include,stdio.h+include,conio.h+include,ctype.h+include,dos.h+include,iostream.h+include,fstream.hchar filename/1&12 char buff/100012 int c(,cy,count2 void c[pos56 . c(7where(562 cy7wherey562 goto(y53&,162 cout,,89nType your te(t and then press escape ?ey9n82 goto(y5100,10062 cprintf58=2d=2d8,cy,c(62 goto(y5c(,cy62 3 void main56 . char ch,c2 ofstream outfile2 ifstream infile2 clrscr562 c[pos562 c(7where(562 cy7wherey562 while5c@72K6 . c7getch562 switch5c6 . case J0<goto(y5c(,cyA162 brea?2 case KK<goto(y5c(A1,cy62 brea?2 case K2<goto(y5c(,cyZ162 brea?2 case K&<goto(y5c(Z1,cy62 brea?2

$$

case 32<printf58 862 buff/countAA17B B2 brea?2 case 13<goto(y51,cyA162 buff/countAA17B9nB2 brea?2 default<if55c-7)& >> c,71226YY 5c-7$J >> c,7&K66 cprintf58=c8,c62 brea?2 3 buff/countAA17c2 c[pos562 3 cprintf589n9n ;o * want to Save\5yXn6<862 scanf58=c8,>c62 if55c77ByB6YY5c77BPB66 . cprintf589n9n#nter the filename with e(tension in J characters only<862 scanf58=s8,filename62 outfile.open5filename,ios<<out62 outfile,,buff2 outfile.close562 cout,,89n;o * want to open5yXn6<9n 82 ch7getch562 cout,,ch2 . if55ch77ByB6YY5ch77BPB66 . cprintf589n#nter the filename to open<862 scanf58=s8,>filename62 infile.open5filename,ios<<in62 infile.get5buff,count,BSB62 goto(y5C0,162 printf58=s8,buff62 getch562 infile.close562 3 3 3 3

$&

OUTPUT: simple te(t editor program Type your te(t and then press escape ?ey 213 ;o * want to Save\5yXn6<y #nter the filename with e(tension in J characters only<t.t(t ;o * want to open5yXn6< y #nter the filename to open<t.t(t simple te(t editor program

RESULT: Thus the editing operations were performed.

$)

EX: NO: 11 IMPLEMENTATION OF A MACRO SUBSTITUTION AIM: To write a C program to perform a macro substitution operations such as addition, subtraction, multiplication and division. ALGORITHM: 1. Start the program. 2. :nclude the necessary header files. 3. :nitialiLe and assign the values to the variables. $. :mplement the functions and print the result. &. Stop the program.

$K

PROGRAM: +include,stdio.h+include,conio.hvoid main56 . int c,d,e,f2 int add562 int sub562 int mul562 int div562 c7add562 d7sub562 e7mul562 f7div562 printf589n The value of addition is =d8,c62 printf589n The value of subtraction is =d8,d62 printf589n The value of multiplication is =d8,e62 printf589n The value of division is =d8,f62 getch562 3 int add56 . int a,b,c2 printf589n#nter the value of a<862 scanf58=d8,>a62 printf589n#nter the value of b<862 scanf58=d8,>b62 c7aAb2 return c2 3 int sub56 . int a,b,d2 d7aZb2 return d2 3 int mul56 . int a,b,e2 e7aSb2 return e2 3 int div56 . int a,b,f2 f7aXb2

$J

return f2 OUTPUT:

#nter the value of a<10 #nter the value of b<2 The value of addition is 12 The value of subtraction is J The value of multiplication is 20 The value of division is &

RESULT: Thus the macro substitution operations were performed and e(ecuted.

Anda mungkin juga menyukai