Problem Statement: Complete the Programming Problems 8.33 at the end of Chapter
ID: 3712335 • Letter: P
Question
Problem Statement: Complete the Programming Problems 8.33 at the end of Chapter 8 in the textbook. Steps Write a C program as required in 8.33 Test three input addresses: two addresses must be consecutive number; the third address must not be consecutive to the previous two addresses. Take a screenshot of the program output message after you test the program. Notes Appropriate error checking of the command line is always a good practice for programmers. It is strongly encouraged because it also reduces grading error. (You can follow the examples in the textbook and the slides) Design your program in a clear and modular fashionExplanation / Answer
The formula to calculate Page Number = Address/page size
So, to determine the Page Number shift the virtual address right by 12 bits(which means you are dividing the address by 2 12 times which becomes address/2^12 = address/4096.
Finding Page Number and Offset(just the maths):
page number = 19986 / 4k = 19986 / 4096 = 4
page offset = 19986 mod 4k = 19986 mod 4096 = 3602
Here is my C code:
#include <stdio.h>
int main(int argc, char *argv[]) {
unsigned long pageNumber;
unsigned long offset;
unsigned long address;
address= atoll(argv[1]);
pageNumber = address >> 12; // to speed up the calculation
offset = address & 0xfff; // masking out rightmost 12 bits
printf("The address %lu contains: ", address);
printf("page number = %lu ",pageNumber);
printf("offset = %lu ", offset);
return 0;
}
Please give a thumbs up if the answer was helpful.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.