I am currently working on a project to translate logical addresses to physical addresses. The question asks:
This project consist of writing a program that translates logical to physical addresses for a virtual address space of 2^16 = 65,536 bytes. Your program will read from a file containing logical addresses and, using a TLB as well as a page table, will translate each logical address to its corresponding physical address and output the value of the byte stored at the translated physical address. Make sure your program uses fast operations like left/right shift operators.
Specifics: Your program will read a file containing several 32-bit integer numbers that represent logical addresses. However, you need only be concerned with 16-bit addresses, so you must mask the rightmost 16 bits of each logical address. These 16 bits are divided into (1) an 8-bit page number and (2) 8-bit page offset
I've done some research and I've developed the following code, however, I know I'm not shifting the page and offet the correct amount of bits. I was given some sample I/O and currently my code is shooting out the wrong answers. Any help would be appreciated.
EDIT: SAMPLE I/O
Virtual address: 16916 Physical address: 20 Virtual address: 62493 Physical address: 285 Virtual address: 30198 Physical address: 758
#define MASK_BITS(x) ((x) & (unsigned int)0x0000FFFF)
unsigned long long int MapAddress(unsigned long long int address){
unsigned long long int page = MASK_BITS(address);
unsigned long long int offset = MASK_BITS(address);
printf("The mapped address %llu contains:\n", address);
page = page >> 13;
printf("Page Number : %llu\n", page);
offset = offset & 0x1FFF;
printf("Offset = %llu\n\n", offset);
return 0;
}
void myFunction()
{
FILE *fp = fopen("/PATH/TO/addresses.txt", "r");
char buffer[32];
unsigned long long int x;
int cnt = 1;
while(!feof(fp)){
fscanf(fp, "%s", buffer);
x = atol(buffer);
MapAddress(x);
cnt++;
}
fclose(fp);
return;
}