Monday, October 24, 2011

SYSTEM SOFTWARE LAB-SIMULATION OF ABSOLUTE LOADER

                                       
                                      SIMULATION OF ABSOLUTE 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 i;
int programlength,textrecordlength;
FILE *fp = fopen(filename,"r");
char *line = getlinefromfile(fp),buffer[100];
char programname[7];
struct memory *mem = NULL;
int address;
while(line != NULL)
{
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 - mem->startingoffset] = (buffer[i] - 48) << 4;
else if(buffer[i] >=65 && buffer[i] <=70)
mem->content[address - mem->startingoffset] = (buffer[i] - 55) << 4;
if(buffer[i+1] >=48 && buffer[i+1] <= 57)
mem->content[address - mem->startingoffset] += (buffer[i+1] - 48);
else if(buffer[i+1] >=65 && buffer[i+1] <=70)
mem->content[address - mem->startingoffset] += (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=7;i<13;i++)
buffer[i-7] = line[i];
buffer[6] = '\0';
address = strtol(buffer,NULL,16);
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(address);
}
else if(line[0] == 'E')
break;
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;
printf("\nEnter the name of the object program: ");
scanf("%s",filename);
mem = loadprogramintomemory(filename);
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;
}

INPUT FILE:-
H^copy  ^001000^000060
T^001000^18^0C1004^001009^141014^5C1007^181021^001040^141017^481050
T^001024^09^000000^454F46^000003
E^001000

No comments:

Post a Comment