views:

1460

answers:

2

Lets say I have a normal page table:

Page Table (Page size = 4k)

      Page #:  0  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15          
Page Frame #:  3  x  1  x  0  x  2  x  5  x   7   4   6   x   x   x

How can I convert an arbitrary logical address like 51996 into a physical memory address?


If I take log base 2 (4096), I get 12. I think this is how many bits I'm suppose to use for the offset of my address.

I'm just not sure. 51996 / 4096 = 12.69. So does this mean it lay on page#12 with a certain offset?

How do I then turn that into the physical address of "51996"?

A: 

If I understand your question correctly (I probably don't), you want to know how to find the physical address from the virtual address using the page table structures. In that case, pretend you are the processor. Use the 10 most significant bits of the address to find the page table in the page directory (the top level page table). The next 10 bits are the index into the page table (the lower level page table). Use the address in that page table entry to find the physical page address. The last ten bits are the byte address into the page.

By the way, you would probably find a lot more people who would understand this type of question at an OS oriented site such as OSDev. I couldn't really go into much detail in this answer because I haven't done this type of stuff in years.

Zifre
+1  A: 

To determine the page of a given memory address, take the first P bits (of the N bit) number.

P = lg2(numberOfPages)
In your example, P=lg2(16)=4

So the first 4 bits of a given memory address will tell us the page. That means the rest should be the offset from the start of that page.

Your example address, 51996, is 1100101100011100 in binary. I.e. [1100:101100011100].

1100 (12 in decimal) is the page number
101100011100 (2844 in decimal) is the offset

Now we need to find where page 12 is in memory.
Looking at your frame table, it appears that page 12 is resident in the 6th frame. In a system where all memory is pageable (i.e. no memory mapped IO) the 6th page frame will be at (entriesPerPage*pageNum)-1

In this case, 4000*6-1 = 23999 (The "-1" is needed since memory is 0-indexed.)

Now all we have to do is add the offset and we have the physical memory address:

23999 + 2844=26843 = 0x68DB

Done!

Hope this (edit) was helpful XD

ParoXoN