I like the GHC analogy with the streets and cities as to why we need paging. Also grouping bytes of the memory into pages allows the CPU to larger amounts of memory.
Suppose the following properties are given:
- virtual address is 32 bits
- page offset is 12 bits
- physical address is 30 bits
- the RAM is 1GiB
Here's a digram that I made which shows how the page number and page offset are used to address a specific cell in the memory:
There's a virtual address which is generated by the CPU and consists of Virtual page number (20 bits) and page offset (12 bits).
Also there's a pagemap used for virtual page number to physical page number mapping (additionally Dirty bit shows if the page has been changed/Resident bit shows if the page is resident in the memory) and on the right is how the memory is partitioned into pages (in blue on the diagram).
The virtual page number is passed to the pagemap using 20 address bits. Since the page number is passed in binary having 20 address bits means that the pagemap can have up to 2^20 records (since with 20 bits you can get 2^20 different numbers) This is also the reason why the page numbers are powers of 2.
So using the pagemap you can find which physical page number is mapped to the requested virtual page number, the page offset is not altered. Having the physical page number and page offset you have the physical address. Using the page number you go to specific page of the memory and using the offset you go the specific byte cell. (Also the page offset defines the page size since 12 bits for offset means that we can address 2^12 = 4096 cells (in orange on the diagram) within a page)
In green you can see an example where we request virtual page number 2 with page offset 4095. According to the page map virtual page number 2 maps to physical page 15, which gives us the physical address with physical page number 15 and offset 4095. (normally virtual/physical page numbers and page offsets would be displayed in hexadecimal, but I used decimal just to simplify)
PS:
The example data is taken from this lecture - https://www.youtube.com/watch?v=3akTtCu_F_k - it gives very good overview of virtual memory.