import java.util.*;
class addrxlation
{
public
static void main(String args[])
{
Scanner
read = new Scanner(System.in);
String
GDT[][]={{"0000000000000000","00000000000000000000000000001111"},{"0000000000000001","00000000000000000000000011111111"},
{"0000000000000010","00000000000000000000111111111111"}};
String
PD[][]={{"00000000000000000000000000000000","00000000000000000001000000000000"},{"00000000000000000000000000000001","00000000000000000010000000000000"},
String
PT[][]={{"00000000000000000001000000000000","00000000000000000100000000000000"},{"00000000000000000010000000000000","00000000000000001000000000000000"},
{"00000000000000000011000000000000","00000000000000001100000000000000"}};
String
Page[][]={{"00000000000000000100000000010000","00000101000000010000001111100000"},{"00000000000000001000000000000000","00000101000000100000000000000000"},
{"00000000000000001100000000000000","00000000000000110000000000000000"}};
System.out.println("enter
the virtual addreaa:");
String
virtual=read.next();
System.out.println("is
paging enabled?(true/false):");
boolean
paging =Boolean.parseBoolean(read.next());
String
CR3=new String();
if(paging)
{
System.out.println("Enter
the content of cr3(base addr):");
CR3=read.next();
}
String
base= new String();
String
linear= new String();
String
temp= new String();
boolean
found=false;
int i;
String segment=virtual.substring(0,16);
System.out.println("Segment
\t::"+segment);
String offset=virtual.substring(16,48);
System.out.println("Offset
\t:"+offset);
for(i=0;i<GDT.length;i++)
{
if(GDT[i][0].equals(segment))
{
found=true;
base=GDT[i][1];
System.out.println("base
address \t:"+base);
linear=add(offset,base);
System.out.println("linear
addreass \t:"+linear);
}
}
if(!found)
{ System.out.println("Segment
does not exist");
return;
}
if(paging)
{
String
directory=linear.substring(0,10);
System.out.println("Directory
\t:"+directory);
String
table = linear.substring(10,20);
System.out.println("Table
\t\t:"+table);
String
page=linear.substring(20,32);
System.out.println("Page
\t\t:"+page);
found=false;
directory=add(CR3,directory);
for(i=0;i<PD.length;i++)
{
if(PD[i][0].equals(directory))
{
found=true;
temp=PD[i][1];
} }
if(!found)
{
System.out.println("Page
Directory Entry does not exist");
return;
}
found=false;
table=add(table,temp);
for(i=0;i<PT.length;i++)
{
if(PT[i][0].equals(table))
{
found =true;
temp=PT[i][1];
}}
if(!found)
{
System.out.println("Page tqable entry does
not exit.");
return;
}
found=false;
page=add(page,temp);
for(i=0;i< Page.length;i++)
{
if(Page[i][0].equals(page))
{
found=true;
temp=Page[i][1];
}}
if(!found)
{
System.out.println("page
does not exist.");
return;
}
System.out.println("Physical
Address:"+temp);
}}
static String add(String a,String b)
{
String
result= new String();
String
result1= new String();
int
carry=0;
int c,d;
for(int i=(a.length());i<=31;i++)
a="0"+a;
for(int i=(b.length());i<=31;i++)
b="0"+b;
for(int i=31;i>=0;i--)
{
c=a.charAt(i);
d=b.charAt(i);
if(c=='o'
&& d=='0')
{
if(carry==1)
{
result=result+"1";
carry=0;
}
else
{
result=result+"0";
carry=0;
}
}
else
if((c=='1' && d=='0') || (c=='0' && d=='1'))
{
if(carry==1)
{
result=result+"1";
carry=1;
}
else
{
result=result+"1";
carry=0;
}
}
else
if((c=='1' && d=='1') || (c=='1' && d=='1'))
{
if(carry==1)
{
result=result+"1";
carry=1;
}
else
{
result=result+"0";
carry=1;
}
}
}
int j;
for(j=31;j>=0;j--)
result1=result1+result.charAt(j);
return result1;
}
}
/*output:-
C:\Java\jdk1.6.0_17\bin>javac addrxlation.java
C:\Java\jdk1.6.0_17\bin>java addrxlation
enter the virtual addreaa:
000000000000000000000000000000000000000000000000000000001
is paging enabled?(true/false):
true
Enter the content of cr3(base addr):
000000000000000000000000000000000001
Segment
::0000000000000000
Offset
:00000000000000000000000000000000
base address
:00000000000000000000000000001111
linear address:
directory:0000000000
table:000000000
page:000000010000
physical address:00000101000000010000001111100000
/*output 2:-
C:\Java\jdk1.6.0_17\bin>javac addrxlation.java
C:\Java\jdk1.6.0_17\bin>java addrxlation
enter the virtual addreaa:
000000000000000000000000000000000000000000000000000000001
is paging enabled?(true/false):
true
Enter the content of cr3(base addr):
000000000000000000000000000000000001
Segment
::0000000000000000
Offset
:00000000000000000000000000000000
base address
:00000000000000000000000000001111
/*output3:-
C:\Java\jdk1.6.0_17\bin>javac addrxlation.java
C:\Java\jdk1.6.0_17\bin>java addrxlation
enter the virtual addreaa:
000000000000000000000000000000000000000000000000000000001
is paging enabled?(true/false):
true
Enter the content of cr3(base addr):
000000000000000000000000000000000001
Segment
::0000000000000000
Offset
:00000000000000000000000000000000
base address
:00000000000000000000000000001111