affiliate marketing

Saturday, 17 December 2011

Relocation loader using C


IMPLEMENTATION OF RELOCATION LOADER
AIM
To implement relocation loader using C.
ALGORITHM
1.Enter new starting location to which object has ti be relocated.
2.Read the content of the input file as strings one at a time in an array input.
3.Transfer the string read in array input into another array output until it is incremented.
4.Move consecutive next 3 strings into array “output”.
5.Cover current location bit associated with each text record to binary form.
6.Make necessary changes in corresponding words of object code and store the updated object code into array “output”.
7.Move object code for which corresponding  relocation bit is not set directly to the array “output” from array “input” without any change.
8.Repeat step 2 to 8 until end record is encountered.
9.If the object code is in character form ,convert into internal hexadecimal representation.
10.Move object code to specified location in memory
11.Write starting LOCCTR value of block of object code and the corresponding internal hexadecimal representative to the output files.



PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void convert(char h[12]);
char bitmask[12];
char bit[12]={0};
void main()
{
                char add[6],length[10],input[10],binary[12],relocbit,ch,pn[5];
                int start,inp,len,i,address,opcode,addr,actualadd,tlen;
                FILE *fp1,*fp2;
                clrscr();
                printf("\n\n Enter the actual starting address:");
                scanf("%x",&start);
                fp1=fopen("RLIN.DAT","r");
                fp2=fopen("RLOUT.DAT","w");
                fscanf(fp1,"%s",input);
                fprintf(fp2,"----------------\n");
                fprintf(fp2,"ADDRESS\tCONTENT\n");
                fprintf(fp2,"-----------------\n");
                while(strcmp(input,"E")!=0)
                {
                                if(strcmp(input,"H")==0)
                                {
                                                fscanf(fp1,"%s",pn);
                                                fscanf(fp1,"%x",add);
                                                fscanf(fp1,"%x",length);
                                                fscanf(fp1,"%s",input);
                                }
                                if(strcmp(input,"T")==0)
                                {
                                                fscanf(fp1,"%x",&address);
                                                fscanf(fp1,"%x",tlen);
                                                fscanf(fp1,"%s",bitmask);
                                                address+=start;
                                                convert(bitmask);
                                                len=strlen(bit);
                                                if(len>=11)
                                                                len=10;
                                                for(i=0;i<len;i++)
                                                {
                                                                fscanf(fp1,"%x",&opcode);
                                                                fscanf(fp1,"%x",&addr);
                                                                relocbit=bit[i];
                                                                if(relocbit==0)
                                                                                actualadd=addr;
                                                                else
                                                                                actualadd=addr+start;
                                                                fprintf(fp2,"\n %x\t\t%x%x\n",address,opcode,actualadd);
                                                                address+=3;
                                                }
                                                fscanf(fp1,"%s",input);
                                }
                }
                fprintf(fp2,"----------------\n");
                fcloseall();
                printf("\n\n The contents of output file(RLOUT.DAT):\n\n");
                fp2=fopen("RLOUT.DAT","r");
                ch=fgetc(fp2);
                while(ch!=EOF)
                {
                                printf("%c",ch);
                                ch=fgetc(fp2);
                }
                fclose(fp2);
                getch();
}
void convert(char h[12])
{
                int i,l;
                strcpy(bit,"");
                l=strlen(h);
                for(i=0;i<l;i++)
                {
                                switch(h[i])
                                {
                                                case '0':
                                                                strcat(bit,"0");
                                                                break;
                                                case '1':
                                                                strcat(bit,"1");
                                                                break;
                                                case '2':
                                                                strcat(bit,"10");
                                                                break;
                                                case '3':
                                                                strcat(bit,"11");
                                                                break;
                                                case '4':
                                                                strcat(bit,"100");
                                                                break;
                                                case '5':
                                                                strcat(bit,"101");
                                                                break;
                                                case '6':
                                                                strcat(bit,"110");
                                                                break;
                                                case '7':
                                                                strcat(bit,"111");
                                                                break;
                                                case '8':
                                                                strcat(bit,"1000");
                                                                break;
                                                case '9':
                                                                strcat(bit,"1001");
                                                                break;
                                                case 'A':
                                                                strcat(bit,"1010");
                                                                break;
                                                case 'B':
                                                                strcat(bit,"1011");
                                                                break;
                                                case 'C':
                                                                strcat(bit,"1100");
                                                                break;
                                                case 'D':
                                                                strcat(bit,"1101");
                                                                break;
                                                case 'E':
                                                                strcat(bit,"1110");
                                                                break;
                                                case 'F':
                                                                strcat(bit,"1111");
                                                                break;

                                }
                }
}
INPUT FILES:
RLIN.DAT
H COPY 001000 00107A
T 001000 1E FFC 14 0033 48 1039 10 0036 28 0030 30 0015 48 1061 3C 0003 20 002A 1C 0039 30 002D
T 002500 15 E00 1D 0036 48 1061 18 0033 4C 1000 80 1000 60 1003
E 000000

OUTPUT:


RESULT
Thus absolute loader is successfully implemented using C

No comments:

Post a Comment