将逻辑地址转换为页码和索引

时间:2014-10-09 15:41:43

标签: java virtual-memory

我应该将logicalAddress转换为pageNumber和index,但我不知道如何做到这一点。 logicalAddress是一个五位int(逻辑地址以十进制形式给出)。 任何提示?

public byte read(int logicalAddress){
    //called by a process to read memory from its logical address
    //this.pageNumber = pageNumber;
    byte data=0;

    //THIS IS THE PART I NEED HELP WITH *******

    //calculate pageNumber and index from the logical address
    int pageNumber = (logicalAddress/NbrOfPages);
    int index = (logicalAddress-(pageNumber*PageSize));

    //*****************************************

    //check if we get a pageFault
    if(pageTable[pageNumber]==-1){
        //call method to solve page fault
        pageFault(pageNumber);
        //the following two should be used in step 2 and 3 of the lab
        //pageFaultFIFO(pageNumber);
        //pageFaultLRU(pageNumber);
    }
    //read data from RAM
    int frame=pageTable[pageNumber];
    int physicalAddress=frame*PageSize+index;
    data=RAM[physicalAddress];
    //print result
    System.out.print("Virtual address: "+logicalAddress);
    System.out.print(" Physical address: "+physicalAddress);
    System.out.println(" Value: "+data);
    return data;
}

1 个答案:

答案 0 :(得分:0)

您需要从页面表大小开始。从那里你需要将地址分成两个字段(页表索引和字节偏移):

 unsigned long long MASK = PAGESIZE-1 ; 

然后是页表索引

 unsigned long long index = (address & ~MASK) / PAGESIZE ;

,偏移量为

 unsigned long long offset = address & mask ;

可替换地:

unsigned long long index = address / PAGESIZE ;
unsigned long long offset = address % PAGESIZE ;