Sie sind auf Seite 1von 6

6. IMPLEMENTATION OF ABSOLUTE LOADER Aim To write a C program to implement an absolute loader. Algorithm: 1. Start the program 2.

Assign the required variable 3. Open the files fp1=fopen("input2.dat","r"); fp2=fopen("out2.dat","w"); 4. Read the content. Using while loop perform the loop until character is not equal to E. 5. Then compare whether the character is equal to H 6. If H then get the starting address, length and input 7. Else if the character is T then store the string as the three address in the output file with input[0],inuput[1] for address input[2],inuput[3] for address+1 input[4],inuput[5] for address+2 8. Else if it is not H or T then perform the following fprintf in fp2 for output file for , input[0],inuput[1] for address input[2],inuput[3] for address+1 input[4],inuput[5] for address+2 9. Increment the address value by 3. 10. Read the next input string and repeat from step 4. 11. Finally terminate the program Source Code: # include <stdio.h> # include <conio.h> # include <string.h> void main() { char input[10]; int start,length,address; FILE *fp1,*fp2; clrscr(); fp1=fopen("input2.dat","r"); fp2=fopen("out2.dat","w"); fscanf(fp1,"%s",input); while(strcmp(input,"E")!=0) { if(strcmp(input,"H")==0) {

fscanf(fp1,"%d",&start); fscanf(fp1,"%d",&length); fscanf(fp1,"%s",input); } else if(strcmp(input,"T")==0) { fscanf(fp1,"%d",&address); fscanf(fp1,"%s",input); fprintf(fp2,"%d\t%c%c\n",address,input[0],input[1]); fprintf(fp2,"%d\t%c%c\n",(address+1),input[2],input[3]); fprintf(fp2,"%d\t%c%c\n",(address+2),input[4],input[5]); address+=3; fscanf(fp1,"%s",input); } else { fprintf(fp2,"%d\t%c%c\n",address,input[0],input[1]); fprintf(fp2,"%d\t%c%c\n",(address+1),input[2],input[3]); fprintf(fp2,"%d\t%c%c\n",(address+2),input[4],input[5]); address+=3; fscanf(fp1,"%s",input); } } fclose(fp1); fclose(fp2); printf("FINISHED"); getch(); } Input & Output: Input File: INPUT2.DAT H 1000 232 T 1000 142033 483039 102036 T 2000 298300 230000 282030 302015 E Output File: OUT2.DAT 1000 1001 14 20

1002 1003 1004 1005 1006 1007 1008 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011

33 48 30 39 10 20 36 29 83 00 23 00 00 28 20 30 30 20 15

7. IMPLEMENTATION OF RELOCATING LOADER Aim To write a C program to implement a relocating loader. Algorithm: 1. Start the program 2. Include the necessary header file and variable 3. Open the following two file fp1= relinput.dat fp2= reloutput.dat 4. Read the content. Using while loop perform the loop until character is not equal to E. 5. If the character is H then get the variable add, length, and input 6. Else if the character is T then get the variable address and bitmask and perform the for loop starting from zero and up to len. 7. Get the opcode ,addr and assign relocbit to bitmask 8. If relocabit is zero then actualadd=addr 9. Else add the addr and starting value 10. Finally terminate the program Source Code: # include <stdio.h> # include <conio.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; clrscr(); printf("Enter the actual starting address : "); scanf("%d",&start); fp1=fopen("relinp.dat","r"); fp2=fopen("reloutput.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%d\n",address,opcode,actualadd); address+=3; } fscanf(fp1,"%s",input); } } fclose(fp1); fclose(fp2); printf("FINISHED"); getch(); } Input & Output: Input File: Enter the actual starting address: 5000 RELINPUT.DAT H 1000 200 T 1000 11001 14 1033 48 1039 90 1776 92 1765 57 1765 T 2011 11110 23 1838 43 1979 89 1060 66 1849 99 1477 E 1000 Output File: RELOUTPUT.DAT

6000 6003 6006 6009 6012 7011 7014 7017 7020 7023

146033 486039 901776 921765 576765 236838 436979 896060 666849 991477

Das könnte Ihnen auch gefallen