Title: Translate logical address to page number and index
Tags: java;virtual-memory
Question: I am supposed to convert the logicalAddress to pageNumber and index, but i don't know how to do this.
The logicalAddress is a five digit int (the logical address is given as decimal).
Any hints?
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;
Comment: It's a simulation of Paging, virtual memory.

Comment: Assuming this is about x64 (Intel), there's Intel CPU manual for you.


Here is another answer: You need to start with the page table size in bits. From that you need to break the address into two field (page table index and byte offset):
unsigned long long MASK = PAGESIZE-1 ;
The the page table index is then
unsigned long long index = (address &amp; ~MASK) / PAGESIZE ;
and the offset is
unsigned long long offset = address &amp; mask ;
Alternatively:
unsigned long long index = address / PAGESIZE ;
unsigned long long offset = address % PAGESIZE ;

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:10/09/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><virtual-memory>', 'Title': 'Translate logical address to page number and index', 'ViewCount': 1450, 'id': 26282585, 'user_AboutMe': '', 'user_AccountId': 4166095.0, 'user_DisplayName': 'user3415034', 'user_Reputation': 15.0, 'user_Views': 29.0, 'user_id': 3415034.0}}