0% found this document useful (0 votes)
51 views3 pages

Absolute & Relocation Print

The document describes a C program that performs relocation of addresses in object code. It opens an input file containing object code with addresses, reads the starting address, then reads each address and relocates it by adding the starting address if the relocation bit is 0. The relocated addresses are written to an output file. It loops through the input file until reaching the end tag, then closes both files and prints "FINISHED".

Uploaded by

Ajnamol N R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views3 pages

Absolute & Relocation Print

The document describes a C program that performs relocation of addresses in object code. It opens an input file containing object code with addresses, reads the starting address, then reads each address and relocates it by adding the starting address if the relocation bit is 0. The relocated addresses are written to an output file. It loops through the input file until reaching the end tag, then closes both files and prints "FINISHED".

Uploaded by

Ajnamol N R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Program(absolute)

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
FILE *fp;
int i,j,staddr1;
char line[50],name1[50],staddr[200];
fp=fopen("ab.txt","r");
fscanf(fp,"%s",line);
for(i=2,j=0;line[i]!='^';i++,j++)
{
name1[j]=line[i];
}
name1[j]='\0';
printf("Name from obj.%s\n",name1);
while(!feof(fp))
{
fscanf(fp,"%s",line);
while(line[0]=='T')
{
for(i=2,j=0;line[i]!='^';i++,j++)
{
staddr[j]=line[i];
}
staddr[j]='\0';
staddr1=atoi(staddr);
i=12;
while(line[i]!='?')
{
if(line[i]!='^')
{
printf("00%d\t%c%c\n",staddr1,line[i],line[i+1]);
staddr1++;
i=i+2;
}
else
i++;
}
fscanf(fp,"%s",line);
}
if(line[0]=='E')
{
fclose(fp);
}
}
}

ab.txt:
H^COPY^001000^0035
T^001000^0C^001003^001122?
T^002000^03^010B0A?
E^001000
Output:
001000 00
001001 10
001002 03
001003 00
001004 11
001005 22
002000 01
002001 0B
002002 0A

Program(relocation)
# include <stdio.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;
printf("Enter the actual starting address : ");
scanf("%d",&start);
fp1=fopen("relinput.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");
}
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:
Enter the actual starting address : 0
FINISHED

reloutput.dat:
1000 141033
1003 481039
1006 901776
1009 921765
1012 571765
2011 231838
2014 431979
2017 891060
2020 661849
2023 991477

You might also like