Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

memory management Assume that a system has a 32-bit virtual address with a 4-KB

ID: 3654119 • Letter: M

Question

memory management Assume that a system has a 32-bit virtual address with a 4-KB page size. Write a program in C++ language that is passed a virtual address (in decimal) on the command line and have it output the page number and offset for the given address. As an example, your program would run as follows: ./a.out 19986 Your program would output: The address 19986 contains: page number = 4 offset = 3602 Writing this program will require using the appropriate data types to store 32 bits. We encourage you to use unsigned data types as well.

Explanation / Answer


#include<iostream>
#include<stdlib.h>
/* define PAGES_SIZE as a constant of 4KB */
#define PAGE_SIZE 4096
using namespace std;
int main(int argc,char **argv){
        /* error if address not given */
        if ( argc < 2 ){
                cout << "please enter the Address ";
                return -1;
        }
        // get address from command line argument
        unsigned int address = atoi(argv[1]);
        // get page number from address
        unsigned int page_number = address / PAGE_SIZE;
        // get offset from address
        unsigned int offset = address % PAGE_SIZE;
        // print the solution
        cout << "the address " << address << " contains : Page Number = " << page_number << " And offset = " << offset << " ";
}