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

In C++ l. Assume that a system has a 32-bit virtual address with a 4-KB page siz

ID: 3779988 • Letter: I

Question

In C++ l. Assume that a system has a 32-bit virtual address with a 4-KB page size. Write a program 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:
yourprogram 19986
Your program would output:
The address 19986 contains:
Page number = 4
Offset = 3602
Writing this program will require using the appropriate data type to store 32 bits. Use unsigned
data type types as well.

Explanation / Answer

#include<iostream>
#include<stdlib.h>
#define SIZE_OF_PAGE 4096
using namespace std;

int main(int argc, char **argv){
if(argc<2){
cout<<"Please enter the address in command line: ";
return -1;
}
unsigned int addr = atoi(argv[1]);
unsigned int page_number = addr / SIZE_OF_PAGE;
unsigned int Page_offset = addr%SIZE_OF_PAGE;
cout<<"The Address "<<addr<<" contains: Page number = "<<page_number<<" Offset = "<<Page_offset<<endl;

}

==================================================

akshay@akshay-Inspiron-3537:~/Chegg$ g++ sys.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out 19986
The Address 19986 contains:
Page number = 4
Offset = 3602