How to work out physical address corresponding to logical address?

Question Detail: 

I am looking to calculate the physical address corresponding to a logical address in a paging memory management scheme. I just want to make sure I am getting the calculation right, as I fear I could be wrong somewhere.

So, the data I have is as follows:

  • The logical address: 717

  • Logical memory size: 1024 bytes (4 pages)

  • Page Table (Page number - Frame number):

    (0 - 5), (1 - 2), (2 - 7), (3 - 0)

  • Physical memory: 16 frames

So, with 1024 bytes in the logical memory, and 4 pages, then each page is 256 bytes.

Therefore, the size of the physical memory must be 4,096, right? (256*16).

Then, to calculate the logical address offset:

1024 mod 717 = 307

Is that how we calculate the offset?

And, we can assume that 717 is in page 2 (1024 / 717 = 2.8)?

So, according to the page table, the corresponding frame number is 3.

And so to get the physical address, we multiply the frame number and page size?

2 * 256 = 768

Then, do we add the offset, like so:

768 + 307 = 1,075

Thank you for taking the time to read. If I don't quite have this correct, would you be able to advise on the correct protocol to calculating this?

Asked By : Ciaran Gallagher
Best Answer from StackOverflow

Question Source : http://cs.stackexchange.com/questions/7743

Answered By : Paul A. Clayton

You are correct in your reasoning that the pages are 256 bytes and that the physical memory capacity is 4096 bytes.

However, there are errors after that.

The offset is the distance (in bytes) relative to the start of the page. I.e., logical_address mod page_size. The bits for this portion of the logical address are not translated (given power of two page size).

The logical (virtual) page number is number of (virtual) page counting from zero. I.e., logical_address / page_size.

As you noted, the physical page is determined by the translation table, indexed using the logical (virtual) address.

Once the physical page number had been found, the physical address of the start of that page is found by multiplying the physical page number by the page size. The offset is then added to determine the precise physical address. I.e., (physical_page_number * page_size) + offset.

So a logical address of, e.g., 508, with 256 byte pages would have an offset of 508 mod 256 = 252. The logical/virtual page number would be 508 / 256 = 1. With the given translation table, logical page 1 translates to the physical page number 2. The physical address would then be physical_page_number * page_size + offset = 2 * 256 + 252 = 764.

No comments

Powered by Blogger.