Created
November 9, 2014 02:26
-
-
Save internetsadboy/e997e444e45638c50381 to your computer and use it in GitHub Desktop.
Translate a 32-bit virtual (hexadecimal) address and produce its corresponding page number and page offset for a system with 8-KB pages.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdlib.h> | |
| #include <stdio.h> | |
| #define PAGE_SIZE 8192 | |
| /* | |
| * main | |
| * | |
| * Translate a 32-bit virtual (hexadecimal) address and produce | |
| * its corresponding page number and page offset for a system | |
| * with 8-KB pages. | |
| * | |
| * @param {Integer} argc | |
| * @param {Character} argv | |
| * @return {Integer} 1 | 0 | |
| * | |
| * Example 7.28 p. 314 | |
| * | |
| * ./a.out 0x4E12 (19986) | |
| * page number: 0x4 (4) | |
| * page offset: 0xe12 (3602) | |
| */ | |
| int main (int argc, char *argv[]) | |
| { | |
| unsigned logical_address, page_number, page_offset; | |
| // invalid num arguments handler | |
| if (argc != 2) | |
| { | |
| printf("translate_address.c accepts 1 command-line argument\n"); | |
| exit(1); | |
| } | |
| // convert hexadecimal logical address to its integer equivalent | |
| sscanf(argv[1], "%x", &logical_address); | |
| // compute the page number | |
| page_number = logical_address / (PAGE_SIZE / 2); | |
| // compute the page offset | |
| page_offset = logical_address - page_number * (PAGE_SIZE / 2); | |
| // log page number hex | |
| printf("page number: 0x%x\n", page_number); | |
| // log page offset hex | |
| printf("page offset: 0x%x\n", page_offset); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment