0% found this document useful (0 votes)
30 views

Sslab - 1

The document describes a relocation loader program that takes an object file as input, applies relocation to the object code addresses based on the provided load address, and outputs the relocated addresses. It reads records from the object file, checks for relocation records marked with 'M', stores the relocation addresses and operation in a structure. It then reads the records, extracts object code from instruction records marked with 'T', applies the necessary relocation by comparing addresses in the code to those in the relocation structure, and prints the relocated addresses.

Uploaded by

sanin mohammed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Sslab - 1

The document describes a relocation loader program that takes an object file as input, applies relocation to the object code addresses based on the provided load address, and outputs the relocated addresses. It reads records from the object file, checks for relocation records marked with 'M', stores the relocation addresses and operation in a structure. It then reads the records, extracts object code from instruction records marked with 'T', applies the necessary relocation by comparing addresses in the code to those in the relocation structure, and prints the relocated addresses.

Uploaded by

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

//Name: Abheda K P

//Roll no: B21CSB04


//Pass 1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
FILE *f1,*f2,*f3,*f4;
f1=fopen("input.txt","r");
f2=fopen("symtab.txt","w");
f4=fopen("intermediate.txt","w");
int sa,lc;
char
la[20],mne[20],op[20],otp[20],o[20];
printf("SOURCE FILE\n");
fscanf(f1,"%s%s%X",la,mne,&lc)
; if(strcmp(mne,"START")==0)
{
sa=lc;
printf("\t%s\t%s\t%X\n",la,mne,sa)
;}
fscanf(f1,"%s%s%s",la,mne,op);
while(strcmp(mne,"END")!=0)
{
printf("\t%s\t%s\t%s\n",la,mne,op);
fscanf(f1,"%s%s%s",la,mne,op);
}
if(strcmp(mne,"END")==0)
{
printf("\t%s\t%s\t%X",la,mne,sa);
}
fclose(f1);
f1=fopen("input.txt","r");
printf("\nOPTAB\n");
f3=fopen("optab.txt","r");
fscanf(f3,"%s%s",otp,o);
while(!feof(f3))
{
printf("%s\t%s\t\n",otp,o);
fscanf(f3,"%s%s",otp,o);
}
printf("\nINTERMEDIATE
FILE\n");
fscanf(f1,"%s%s%X",la,mne,&lc);
if(strcmp(mne,"START")==0)
{
sa=lc;
fprintf(f4,"%X\t%s\t%s\t%X\n",lc,la,mne,sa)
; printf("%X\t%s\t%s\t%X\n",lc,la,mne,sa);
fscanf(f1,"%s%s%s",la,mne,op);
}
else
{
lc=0;
}
while(strcmp(mne,"END")!=0)
{
fprintf(f4,"%X\t%s\t%s\t%s\n",lc,la,mne,op)
; printf("%X\t%s\t%s\t%s\n",lc,la,mne,op);
if(strcmp(la,"-")!=0)
fprintf(f2,"%s\t%X\n",la,lc);
// printf("%s\t%X\n",la,lc);
f3=fopen("optab.txt","r");
fscanf(f3,"%s%s",otp,o);
while(!feof(f3))
{
if(strcmp(otp,mne)==0)
{
lc+=3;
break;
}
fscanf(f3,"%s%s",otp,o);
}
if(strcmp(mne,"WORD")==0)
lc+=3;
else if(strcmp(mne,"RESW")==0)
lc+=3*atoi(op);
else if(strcmp(mne,"RESB")==0)
lc+=atoi(op);
else if(strcmp(mne,"BYTE")==0)
{
if(op[0]=='X')
lc+=1;
else
lc+=strlen(op)-3;
}
fclose(f3);
//fprintf(f4,"%X\t%s\t%s\t%s\n",lc,la,mne,op);
fscanf(f1,"%s%s%s",la,mne,op);
}
int pgm;
if(strcmp(mne,"END")==0)
{
fprintf(f4,"%X\t%s\t%s\t%X\n",lc,la,mne,sa)
; pgm=lc-sa;
printf("%X\t%s\t%s\t%X",lc,la,mne,sa);
}
printf("\nprogram length=%X\n",pgm);
printf("\nSYMTAB\n");
fscanf(f4,"%X%s%s%s",&lc,la,mne,op);
while(strcmp(mne,"END")!=0)
{
while(strcmp(la,"-")!=0&&strcmp(mne,"START")!=0)
{
printf("%s\t%X\t\n",la,lc);
}
fscanf(f4,"%X%s%s%s",&lc,la,mne,op);
}
fclose(f1);
fclose(f2);
}
OUTPUT
r5b@user-260-p022il:~/Desktop/ss r5b04$ gcc
pass1.c r5b@user-260-p022il:~/Desktop/ss r5b04$
./a.out SOURCE FILE
PGM1 START 1000
- LDA ALPHA
- MUL BETA
- STA GAMMA
ALPHA WORD 2
BETA WORD 4
GAMMA RESW 1
- END 1000
OPTAB
LDA 00
MUL 20
STA 0C
INTERMEDIATE FILE
1000 PGM1 START 1000
1000 - LDA ALPHA
1003 - MUL BETA
1006 - STA GAMMA
1009 ALPHA WORD 2
100C BETA WORD 4
100F GAMMA RESW 1
1012 - END 1000
program length=12
INPUT FILES:
input.txt

optab.txt

OUTPUT FILES:
symtab.txt

intermediate.txt
//Name: Abheda K P
//Roll no: B21CSB04
//Pass 2
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
FILE *fin,*fsym,*fopt,*flen;
fin=fopen("intermediate.txt","r");
fsym=fopen("symtab.txt","r");
fopt=fopen("optab.txt","r");
flen=fopen("length.txt","r");
char
la[20],mne[20],op[20],otp[20],d[20],sym[20],opval[3]; int
sa,len,oplen,symval;
fscanf(fin,"%X%s%s%X",&sa,la,mne,&sa);
if(strcmp(mne,"START")==0)
{
printf("%X\t%s\t%s\t%X\n",sa,la,mne,sa);
}
fscanf(fin,"%X%s%s%s",&sa,la,mne,op);
int addr=sa;
while(strcmp(mne,"END")!=0)
{
fscanf(fopt,"%s%s",otp,opval);
while(!feof(fopt))
{
if(strcmp(mne,otp)==0)
{
fscanf(fsym,"%s%X",sym,&symval);
while(!feof(fsym))
{
if(strcmp(op,sym)==0)
{
printf("%X\t%s\t%s\t%s\t%s%X\n",sa,la,mne,op,opval,symval); }
fscanf(fsym,"%s%X",sym,&symval);
}
}
fscanf(fopt,"%s%s",otp,opval);
}
fseek(fopt,SEEK_SET,0);
fseek(fsym,SEEK_SET,0);
if(strcmp(mne,"WORD")==0)
printf("%X\t%s\t%s\t%s\t00000%s\n",sa,la,mne,op,op);
if(strcmp(mne,"BYTE")==0)
{
for(int i=0;i<strlen(op);i++)
{
printf("%X",op[i]);
}
printf("\n");
}
if(strcmp(mne,"RESW")==0&&strcmp(mne,"RESB")==0
) printf("%X\t%s\t%s\t%s\n",sa,la,mne,op);
fscanf(fin,"%X%s%s%s",&sa,la,mne,op);
}
printf("%X\t%s\t%s\t%s\n",sa,la,mne,op);
fclose(fin);
fclose(fopt);
fclose(fsym);
fin=fopen("intermediate.txt","r");
fsym=fopen("symtab.txt","r");
fopt=fopen("optab.txt","r");
printf("OBJECT PROGRAM\n");
fscanf(fin,"%X%s%s%X",&sa,la,mne,&sa);
if(strcmp(mne,"START")==0)
{
fscanf(flen,"%X%X",&len,&oplen);
}
printf("H^%s^00%X0000%X\nT^00%X^%X^",la,sa,len,sa,oplen)
; fscanf(fin,"%X%s%s%s",&sa,la,mne,op);
addr=sa;
while(strcmp(mne,"END")!=0)
{
fscanf(fopt,"%s%s",otp,opval);
while(!feof(fopt))
{
if(strcmp(mne,otp)==0)
{
fscanf(fsym,"%s%X",sym,&symval);
while(!feof(fsym))
{
if(strcmp(op,sym)==0)
{
printf("%s%X^",opval,symval);
}
fscanf(fsym,"%s%X",sym,&symval);
}
}
fscanf(fopt,"%s%s",otp,opval);
}
fseek(fopt,SEEK_SET,0);
fseek(fsym,SEEK_SET,0);
if(strcmp(mne,"WORD")==0)
printf("00000%s^",op);
if(strcmp(mne,"BYTE")==0)
{
for(int i=0;i<strlen(op);i++)
{
printf("%X",op[i]);
}
printf("\n");
}
fscanf(fin,"%X%s%s%s",&sa,la,mne,op);
}
printf("\nE^00%X\n",addr);
fclose(fin);
fclose(fopt);
fclose(fsym);
fclose(flen);
}
INPUT FILES:
Input.txt
Optab.txt

Intermediate.txt

length.txt

OUTPUT
r5b@user-260-p022il:~/Desktop/ss r5b04$ gcc
pass2.c r5b@user-260-p022il:~/Desktop/ss r5b04$
./a.out
1000 PGM1 START 1000
1000 - LDA ALPHA 001009
1003 - MUL BETA 20100C
1006 - STA GAMMA 0C100F 1009 ALPHA
WORD 2 000002 100C BETA WORD 4 000004
1012 - END 1000
OBJECT PROGRAM
H^PGM1^001000000012
T^001000^1E^001009^20100C^0C100F^000002^000004
^ E^001000
//Name: Abheda K P
//Roll no: B21CSB04
//Absolute Loader
#include <stdio.h>
#include <string.h>
char input[10], label[10], ch1, ch2;
int addr, w = 0, start, ptaddr, l, length = 0, end, count = 0, k, taddr, address, i =
0; FILE *fp1, *fp2;
void check();
int main()
{
fp1 = fopen("inputt.txt", "r");
fp2 = fopen("output.txt", "w");
fscanf(fp1, "%s", input);
printf("\n\nABSOLUTE LOADER\n");
fprintf(fp2, "\n-------------------------------------------------------\n");
fprintf(fp2, "MEMORY ADDRESS\t\t\tCONTENTS");
fprintf(fp2, "\n-------------------------------------------------------\n");
while (strcmp(input, "E") != 0)
{
if (strcmp(input, "H") == 0)
{
fscanf(fp1, "%s %x %x %s", label, &start, &end, input);
address = start;
}
else if (strcmp(input, "T") == 0)
{
l = length;
ptaddr = addr;
fscanf(fp1, "%x %x %s", &taddr, &length, input);
addr = taddr;
if (w == 0)
{
ptaddr = address;
w = 1;
}
for (k = 0; k < (taddr - (ptaddr + l)); k++)
{
address = address + 1;
fprintf(fp2, "xx");
count++;
if (count == 4)
{
fprintf(fp2, " ");
i++;
if (i == 4)
{
fprintf(fp2, "\n\n%x\t\t", address);
i = 0;
}
count = 0;
}
}
if (taddr == start)
fprintf(fp2, "\n\n%x\t\t", taddr);
fprintf(fp2, "%c%c", input[0], input[1]);
check();
fprintf(fp2, "%c%c", input[2], input[3]);
check();
fprintf(fp2, "%c%c", input[4], input[5]);
check();
fscanf(fp1, "%s", input);
}
else
{
fprintf(fp2, "%c%c", input[0], input[1]);
check();
fprintf(fp2, "%c%c", input[2], input[3]);
check();
fprintf(fp2, "%c%c", input[4], input[5]);
check();
fscanf(fp1, "%s", input);
}
}
fprintf(fp2, "\n-------------------------------------------------------\n");
fclose(fp1);
fclose(fp2);
printf("\n\n The contents of output file:\n");
fp2 = fopen("output.txt", "r");
ch2 = fgetc(fp2);
while (ch2 != EOF)
{
printf("%c", ch2);
ch2 = fgetc(fp2);
}
fclose(fp2);
}
void check()
{
count++;
address++;
taddr = taddr + 1;
if (count == 4)
{
fprintf(fp2, " ");
i++;
if (i == 4)
{
fprintf(fp2, "\n\n%x\t\t", taddr);
i = 0;
}
count = 0;
}
}
INPUT FILES
inputt.txt
OUTPUT
r5b@user-260-p022il:~/Desktop/ss r5b04$ ./a.out
OUPUT
-------------------------------------------------------
MEMORY ADDRESS CONTENTS
-------------------------------------------------------
1000 14103348 20390010 36281030 30101548
1010 20613C10 0300102A 0C103900 102D0C10
1020 36482061 0810334C 0000454F 46000003
1030 000000xx xxxxxxxx xxxxxxxx xxxxxxxx
1040 xxxxxxxx xxxxxx04 10300010 30E0205D
1050 30203FD8 205D2810 30302057 5490392C
1060 205E3820 3Fxxxxxx xxxxxxxx xxxxxxxx
1070 xxxxxxxx xxxxxx10 10364C00 00000000
1080 00100004 1030E020 79302064 509039DC
1090 20792C10 36
-------------------------------------------------------
//Name: Abheda K P
//Roll no: B21CSB04
//Relocation Loader
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE *obj;
struct mod{
char op[10];
int reloc[10];
int flag[10];
}r;
char prog_name[10], record[100], locn[6], instr[2], objcode[9], cnt[2], relocn[6]; int i, j,
k = 0, flag = 0, rec_len, start, ind, new_loc, load_addr, count = 0, mod_count = 0,
minstr;
int rel_addr;
void main(){
obj = fopen("OBJ.txt", "r");
printf("Enter load address:- ");
scanf("%X", &load_addr);
for(i = 5, k = 0; k < 4, i <= 8; k++, i++){
locn[k] = record[i];
}
while(record[0] != 'E'){
if(record[0] == 'M'){
for(i = 2, j = 0; i < 8, j < 6; i++, j++){
relocn[j] = record[i];
}
sscanf(relocn, "%X", &rel_addr);
rel_addr += load_addr;
r.reloc[mod_count] = rel_addr;
r.op[mod_count] = record[11];
r.flag[mod_count] = 0;
mod_count ++;
}
fscanf(obj, "%s", record);
}
rewind(obj);
fscanf(obj, "%s", record);
printf("Location Object Code\n");
while(record[0] != 'E'){
if(record[0] == 'T'){
for (j = 4, k = 0; j < 8, k < 4; k++, j++){
locn[k] = record[j];
}
sscanf(locn, "%X", &start);
new_loc = start ;
new_loc += load_addr ;
for(i=9, k=0; i<11, k<2; i++, k++){
cnt[k] = record[i];
}
sscanf(cnt, "%x", &count);
count = count / 3;
ind = 12;
for(i = 0; i < 10; i++){
while(count > 0){
objcode[0] = '\0';
for(j = 0, k = ind; j < 6, k < ind + 6; j++, k+ +){
objcode[j] = record[k];
}
ind += 6 ;
if(record[ind] == '^' || record[ind] == '\0'){ ind++ ;
objcode[j] = '\0';
}
else{
while(record[ind] != '^'){
if(record[ind] == '\0'){
break;
}
objcode[j] = record[ind] ;
ind++;
j++;
}
ind++;
}
objcode[j] == '\0';
sscanf(objcode, "%x", &minstr) ; for(i =
0; i < mod_count; i++){
if(r.reloc[i] > new_loc && r.reloc[i] <
new_loc+4 && r.flag[i] != 1){
if(r.op[i] == '+'){
minstr += load_addr; }
else{
minstr -= load_addr;
}
r.flag[i] = 1 ;
break;
}
}
printf("%X\t%X\n",new_loc,minstr) ;
new_loc += strlen(objcode) / 2; count--;
}
}
}
fscanf(obj, "%s", record);
}
}
INPUT
OBJECT.txt:
H^COPY^000000^000030
T^000000^1C^17202D^69202D^4B101036^032026^290000^332007^4B10105D^3F3FEC^0
32010
T^00001C^13^0F2016^010003^0F200D^4B10105D^3E2003^454F46
M^000007^05+COPY
M^000014^05+COPY
M^000027^05+COPY
E^000000
OUTPUT:
Enter load address:- 3000
Location Object Code
3000 17202D
3003 69202D
3006 4B104036
300A 32026
300D 290000
3010 332007
3013 4B10405D
3017 3F3FEC
301A 32010
301C F2016
301F 10003
3022 F200D
3025 4B10405D
3029 3E2003
302C 454F46

You might also like