Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string>
- #include<cmath>
- using namespace std;
- int BinToInt(string binaryString)
- {
- int value=0;
- int indexcounter = 0;
- for(int i=binaryString.length()-1;i>=0;i--){
- if(binaryString[i]=='1'){
- value += pow(2,indexcounter);
- }
- indexcounter++;
- }
- return value;
- }
- class segment{
- public:
- //declare a string variable for segment name
- //declare integer variables, one fore base, another for bound
- string segmentname;
- int base,bound;
- segment()
- {
- //initialize segment name, base & bound with some value
- base=0;
- bound=0;
- }
- segment(string name, int Base, int Bound)
- {
- // initialize segment name with name, base with Base & bound with Bound
- name=segmentname;
- Base=base;
- Bound=bound;
- }
- };
- int main()
- {
- //take number of segments segNum as input
- int segNum
- cin>>segNum
- //create an array of segment objects of size segNum
- segment segments[segNum];
- //take input segment name , base & bound for each segment & store it in the segment objects array
- string num;
- int base1,bound1;
- //take input a string variable VirtualAddress
- for(int i=0;i<segNum;i++){
- cin>>num>>base1>>bound1;
- }
- //calculate how many leftmost bits you need from segNum
- int segBit=(int)ceil(log2(segNum));
- string virtualAddress;
- cin>>virtualAddress;
- //calculate offset portion from virtualAddress
- string bn = virtualAddress.substr(0,segBit);
- int result = BinToInt(bn);
- //take the corresponding segment from segment array
- //print in which segment the virtual address falls into
- //now check if the instruction will be executed properly or not
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement