Monday, October 24, 2011

SYSTEM SOFTWARE LAB-SIMULATION OF A RELOCATABLE LOADER

                         SIMULATION OF A RELOCATABLE LOADER


PROGRAM CODE:-

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MEMORYSIZE 0x100
struct memory
{
int content[MEMORYSIZE];
int startingoffset;
};


struct memory* creatememory(int startingoffset)
{
int i;
struct memory *temp = (struct memory*)malloc(sizeof(struct memory));
temp->startingoffset = startingoffset;
for(i=0;i<MEMORYSIZE;i++)
temp->content[i] = 0xFF;
return temp;
}


char* getlinefromfile(FILE *file)
{
char ch='\0';
int position=0;
char *buffer=(char*)malloc(sizeof(char)*1000);
while(ch != '\n')
{
if((ch = fgetc(file)) != EOF)
{
if(ch != '^' && ch!= '\n')
buffer[position++]=ch;
}
}
buffer[position]='\0';
if(position == 0)
return NULL;
return buffer;
}


struct memory* loadprogramintomemory(char *filename,int startaddress)
{
int i;
int programlength,textrecordlength,numberofhalfbytes,newvalue,mask = 0xFF;
FILE *fp = fopen(filename,"r");
char *line = getlinefromfile(fp),buffer[100];
char programname[7];
int modificationaddress;
struct memory *mem = NULL;
int address;
while(line != NULL)
{
mask = 0xFF;
if(line[0] == 'T')
{
for(i=1;i<7;i++)
buffer[i-1] = line[i];
buffer[6]='\0';
address = strtol(buffer,NULL,16);
for(i=7;i<9;i++)
buffer[i-7] = line[i];
buffer[i-7] = '\0';
textrecordlength = strtol(buffer,NULL,16);
for(i=9;i<strlen(line);i++)
buffer[i-9] = line[i];
buffer[i-9] ='\0';
for(i =0;i<textrecordlength*2;i+=2,address++)
{

if(buffer[i] >= 48 && buffer[i] <= 57)
mem->content[address] = (buffer[i] - 48) << 4;
else if(buffer[i] >=65 && buffer[i] <=70)
mem->content[address] = (buffer[i] - 55) << 4;
if(buffer[i+1] >=48 && buffer[i+1] <= 57)
mem->content[address] += (buffer[i+1] - 48);
else if(buffer[i+1] >=65 && buffer[i+1] <=70)
mem->content[address] += (buffer[i+1] - 55);
}
}
else if(line[0] == 'H')
{
for(i=1;i<7;i++)
programname[i-1] = line[i];
programname[6] = '\0';
for(i = 13;i<19;i++)
buffer[i-13] = line[i];
buffer[6] = '\0';
programlength = strtol(buffer,NULL,16);
if(programlength > MEMORYSIZE)
break;
mem = creatememory(startaddress);
address = 0;
}
else if(line[0] == 'E')
break;
else if(line[0] == 'M')
{
for(i=1;i<7;i++)
buffer[i-1] = line[i];
buffer[6] = '\0';
modificationaddress = strtol(buffer,NULL,16);
for(i=7;i<9;i++)
buffer[i-7] = line[i];
buffer[i-7] = '\0';
numberofhalfbytes =  strtol(buffer,NULL,16);
if(numberofhalfbytes%2 == 0)
{
newvalue = 0;
for(i=0;i<numberofhalfbytes/2;i++)
{
newvalue += mem->content[modificationaddress+i];
if(i < (numberofhalfbytes/2)-1)
{
mask = mask << 8;
newvalue = newvalue << 8;
}
}
newvalue += startaddress;
for(i=0;i<numberofhalfbytes/2;i++)
{
mem->content[modificationaddress+i] = (mask & newvalue) >> ((numberofhalfbytes/2 - i-1)*8);
mask = mask >> 8;
}
}
else
{
newvalue = mem->content[modificationaddress] & 0x0F;
newvalue = newvalue << 8;
mask = mask << 8;
for(i=1;i<=numberofhalfbytes/2;i++)
{
newvalue += mem->content[modificationaddress+i];
if(i < (numberofhalfbytes/2))
{
mask = mask << 8;
newvalue = newvalue << 8;
}
}
newvalue += startaddress;
mem->content[modificationaddress] = (mem->content[modificationaddress]&0xF0) + ((newvalue &mask) >> (numberofhalfbytes/2+1));
mask = mask >> 8;
for(i=1;i<=numberofhalfbytes/2;i++)
{
mem->content[modificationaddress+i] = (mask & newvalue) >> ((numberofhalfbytes/2 -i)* 8);
mask = mask >> 8;
}
}
}
line = getlinefromfile(fp);
}
return mem;
}


void displaymemory(struct memory *mem)
{
int i;
printf("\nMemory Map:\n");
for(i=0;i<MEMORYSIZE;i++)
{
if(i%4 == 0)
printf("\t");
if(i%0x10 == 0)
printf("\n%04X\t",mem->startingoffset+i);
printf("%02X",mem->content[i]);
}
printf("\n\n");
}


int main()
{
char filename[100];
struct memory *mem;
char start[6];
printf("\nEnter the name of the object program: ");
scanf("%s",filename);
printf("\nEnter the starting address : ");
scanf("%s",start);
mem = loadprogramintomemory(filename,strtol(start,NULL,16));
if(mem != NULL)
{
printf("\nMemory Map after loading program: \n");
displaymemory(mem);
}
else
printf("\nMemory Space not enough for loading given object program\n");
return 0;
}

SAMPLE-INPUT-FILE:-
H^copy  ^000000^000060
T^000000^1A^0C1004^001009^4B101014^5C1007^4B101021^001040^141017^481050
T^000024^09^000000^454F46^000003
M^000007^06
M^00000E^05
E^001000

No comments:

Post a Comment