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

Assume that a system has a 32-bit virtual address with a 4-K.B page size. Write

ID: 3572480 • Letter: A

Question

Assume that a system has a 32-bit virtual address with a 4-K.B 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: your program 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 <stdio.h>

int main(int argc,char **argv)
{
//declare addr which is 32 bit data type
unsigned int addr ;
//declare page and offset
int page ,offset;
if( argc < 2)
{
printf("Enter address as an argument ");
return -1;
}
//assign address read from command line and convert it to number
addr = atoi(argv[1]);
//divide address by 4096 as 1 page is 4k byte
page = addr/4096 ;
//offset is obtained by getting reminder after diving by 4k
offset = addr % 4096;
printf("The address %d contains: ",addr);
printf("Page number = %d ",page);
printf("Page offset = %d ",offset);
return 0;
}

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

/a.out 19986                                                                                                                                                           

The address 19986 contains:                                                                                                                                                     

Page number = 4                                                                                                                                                                 

Page offset = 3602