Java Program for Segment Descriptor

import java.io.*;
import java.util.*;

class descriptor
{
 static String descriptor=new String();

 static String selector=new String();

 static String offset=new String(); 

 static String base=new String(); 

 static String limit=new String(); 

 static int segment; 

 static boolean add=false;
 

public static void main(String args[])

{

  Scanner sc=new Scanner(System.in);

  char des[],sel[],off[];

  char desframe[],selframe[];

  String desbin="";

  String selbin="";

  String offsetbin="";


 System.out.println("Enter the descriptor in 16bit hex: ");

 descriptor=sc.nextLine();  

 des=descriptor.toCharArray(); 

  

  for(int i=0;i < des.length;i++)

  {

   String temp=des[i]+"";

   String t=hexToBin(temp);

   desbin+=t;

  }

  

 System.out.println("Descriptor Frame: [ "+desbin+" ]\n");

  

  desframe=desbin.toCharArray();   

  String dpl=desbin.substring(45,47);  

  

  base = desbin.substring(16,40) + desbin.substring(56,64) +"";

  System.out.println("Base Add. : [ "+base+" ]");

  

  limit = desbin.substring(0,16)+desbin.substring(48,52)+"";

  System.out.println("Limit Add. : [ "+limit+" ]");


  System.out.println("\nEnter the selector in 4bit hex: ");  

  selector=sc.nextLine();

  sel=selector.toCharArray();

  

  for(int i=0;i < sel.length;i++)

  {

   String temp=sel[i]+"";

   String t=hexToBin(temp);

   selbin+=t;

  }

  System.out.println("Selector Frame: [ "+selbin+" ]\n");

  selframe=selbin.toCharArray();

  String rpl=selbin.substring(0,2);

  

  for(char i:selframe)

  {

  System.out.print(" ["+i+"] ");

  }

  System.out.println("\n");

  System.out.println("Enter The Offset of 4bit Hex: ");

  offset=sc.nextLine();

  off=offset.toCharArray();

  for(int i=0;i < off.length;i++){

   String temp=off[i]+"";

   String t=hexToBin(temp);

   offsetbin+=t;

  }

  System.out.println("Offset Frame: [ "+offsetbin+" ]\n");
  
  add=checkLimitValidation(base,offsetbin,limit);

  checkforValidity(desframe[47],dpl,rpl,desframe[44],add);

  

 }


static String hexToBin(String input)

 {

  int value=Integer.parseInt(input,16);

  String binary=Integer.toBinaryString(value);

  int j=4-binary.length();

  for(int k=0;k < j;k++)

  {

   binary=0+binary;

  }    

  return binary;

 }

 

static void checkforValidity(char p,String dpl,String rpl,char s,boolean ac)

{

  if(p=='1')

  {

   if(s=='1')

   {

   System.out.println("\n---You are working in System Segment---");



    if(ac)

    {

     System.out.println("\n segment can be accessed(sufficient memory)");  

           int dl=Integer.parseInt(dpl,2);

        int pl=Integer.parseInt(rpl,2);

        System.out.println("\nRPL: "+pl+" DPL: "+dl);

        if(pl <= dl)

        {

        System.out.println("\nsegment can be accessed");

        }

        else

        {

        System.out.println("\nsegment cannot be accessed");

        }

              }

              else

              System.out.println("\nInvalid addresss"); 

    }

   

   else

   System.out.println("\nYou have not selected System Segment");

  

    }

    

    else

    System.out.println("\nFrame address is not valid");

   



}

 

 static boolean checkLimitValidation(String base_Add, String offset, String limit_Add)

 {

  boolean flag=false;

  long base=Long.parseLong(base_Add,2);

  long off=Long.parseLong(offset,2);

  long limit=Long.parseLong(limit_Add,2);  

  long physical_Add=base+limit;

  long p_limit=base+off;

    

  if(physical_Add >= p_limit)

  {

   

   flag=true;

  } 

  return flag;

 }

}

 

/*Output

1.Enter the descriptor in 16bit hex:

abcdefabcdefabcd

Descriptor Frame: [ 1010101111001101111011111010101111001101111011111010101111001101 ]



Base Add. : [ 11101111101010111100110111001101 ]

Limit Add. : [ 10101011110011011010 ]



Enter the selector in 4bit hex:

abcd

Selector Frame: [ 1010101111001101 ]



 [1]  [0]  [1]  [0]  [1]  [0]  [1]  [1]  [1]  [1]  [0]  [0]  [1]  [1]  [0]  [1]



Enter The Offset of 4bit Hex:

abcd

Offset Frame: [ 1010101111001101 ]





---You are working in System Segment---



 segment can be accessed(sufficient memory)



RPL: 2 DPL: 3


segment can be accessed

*/

Comments