Advertisement
Foyaj128

Online3

Nov 25th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include<cmath>
  4. using namespace std;
  5.  
  6. int BinToInt(string binaryString)
  7. {
  8.         int value=0;
  9.         int indexcounter = 0;
  10.         for(int i=binaryString.length()-1;i>=0;i--){
  11.         if(binaryString[i]=='1'){
  12.         value += pow(2,indexcounter);
  13.         }
  14.         indexcounter++;
  15.         }
  16.         return value;
  17. }
  18.  
  19. class segment{
  20. public:
  21.     //declare a string variable for segment name
  22.     //declare integer variables, one fore base, another for bound
  23.     string segmentname;
  24.     int base,bound;
  25.     segment()
  26.     {
  27.         //initialize segment name, base & bound with some value
  28.         base=0;
  29.         bound=0;
  30.     }
  31.     segment(string name, int Base, int Bound)
  32.     {
  33.            // initialize segment name with name, base with Base & bound with Bound
  34.            name=segmentname;
  35.            Base=base;
  36.            Bound=bound;
  37.     }
  38. };
  39.  
  40. int main()
  41. {
  42.         //take number of segments segNum as input
  43.     int segNum
  44.     cin>>segNum
  45.         //create an array of segment objects of size segNum
  46.     segment segments[segNum];
  47.        //take input segment name , base & bound for each segment & store it in the segment objects array
  48.     string num;
  49.     int base1,bound1;
  50.         //take input a string variable VirtualAddress
  51.     for(int i=0;i<segNum;i++){
  52.     cin>>num>>base1>>bound1;
  53.     }
  54.         //calculate how many leftmost bits you need from segNum
  55.         int segBit=(int)ceil(log2(segNum));
  56.  
  57.         string virtualAddress;
  58.         cin>>virtualAddress;
  59.         //calculate offset portion from virtualAddress
  60.        string bn =  virtualAddress.substr(0,segBit);
  61.        int result = BinToInt(bn);
  62.         //take the corresponding segment from segment array
  63.         //print in which segment the virtual address falls into
  64.  
  65.         //now check if the instruction will be executed properly or not
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement