==============================================================
  Experiment 01: Page Table - Virtual Address Translation
==============================================================
  [Back to Index]
--------------------------------------------------------------

AIM:
  To implement a page table that translates a given virtual
  address into a physical address using page number and offset.

--------------------------------------------------------------

THEORY:
  In a paging system, the virtual address space is divided
  into fixed-size pages, and physical memory is divided into
  frames of the same size.

  A page table maps each virtual page number to a physical
  frame number. Given a virtual address:

    Page Number = Virtual Address / Page Size
    Offset      = Virtual Address % Page Size

  The physical address is formed by combining the frame
  number (from the page table) with the offset.

  This program uses identity mapping (page i maps to frame i)
  for simplicity.

--------------------------------------------------------------

ALGORITHM:
  1. Read virtual space size (MB), page size (KB), and
     virtual address from command-line arguments.
  2. Convert sizes to bytes.
  3. Validate the virtual address.
  4. Calculate total number of pages.
  5. Create a page table with identity mapping.
  6. Compute page number and offset from virtual address.
  7. Look up the page table for the frame number.
  8. Display the physical address as <frame, offset>.

--------------------------------------------------------------

SOURCE CODE: page_table.c

--------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    int vspace_mb, page_kb;
    unsigned int virtual_address;
    unsigned int virtual_space_bytes;
    unsigned int page_size_bytes;
    unsigned int num_pages;
    unsigned int page_number, offset;
    unsigned int *page_table;

    if (argc != 4)
    {
        printf("Usage: %s <VirtualSpace_MB> <PageSize_KB> <VirtualAddress>\n", argv[0]);
        return 1;
    }

    vspace_mb = atoi(argv[1]);
    page_kb = atoi(argv[2]);
    virtual_address = atoi(argv[3]);

    virtual_space_bytes = vspace_mb * 1024 * 1024;
    page_size_bytes = page_kb * 1024;

    if (virtual_address >= virtual_space_bytes)
    {
        printf("Invalid Virtual Address\n");
        return 1;
    }

    num_pages = virtual_space_bytes / page_size_bytes;

    page_table = (unsigned int *)malloc(num_pages * sizeof(unsigned int));

    for (unsigned int i = 0; i < num_pages; i++)
        page_table[i] = i;   // Identity mapping

    page_number = virtual_address / page_size_bytes;
    offset = virtual_address % page_size_bytes;

    if (page_number >= num_pages)
    {
        printf("Page Table Miss!\n");
    }
    else
    {
        printf("Physical Address = <%u, %u>\n",
               page_table[page_number], offset);
    }

    free(page_table);
    return 0;
}
--------------------------------------------------------------

COMPILE: gcc -o page_table page_table.c
RUN:     ./page_table 16 4 8196

EXAMPLE OUTPUT:
  Physical Address = <2, 4>

==============================================================
  [Back to Index]  |  [Next: Exp 02]
==============================================================