-1

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;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
Husso
  • 262
  • 6
  • 17
  • The problem statement says that page and offset are each 8 bits. Why are you shifting by 13 bits? – Barmar Apr 12 '16 at 21:12
  • Not related to your masking problem, but: [why while(!feof(fp)) is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – Barmar Apr 12 '16 at 21:13
  • Can you provide some example input and what you expect as output (by hand)? – jojonas Apr 12 '16 at 21:18
  • 1
    @jojonas I added some sample I/O. – Husso Apr 12 '16 at 21:26
  • @Barmar I wasn't sure how many I should be shifting to be quite honest. As for the while(!feof(fp)) it is for the addresses I'm reading from a file. – Husso Apr 12 '16 at 21:27
  • I know what it's for. It's still wrong, as explained in that question. – Barmar Apr 12 '16 at 21:28

1 Answers1

0

The instructions say that the address is split into 8-bit page and offset, but your code is using 13 bits instead of 8 bits. You need to shift by 8 bites to get the page number, and mask the lower 8 bits to get the offset.

unsigned long long int MapAddress(unsigned long long int address){
    address = MASK_BITS(address);

    printf("The mapped address %llu contains:\n", address);

    unsigned long long int page = address >> 8;
    printf("Page Number : %llu\n", page);

    unsigned long long int offset = address & 0xFF;

    printf("Offset = %llu\n\n", offset);
    return 0;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I edited the code to what you suggested, but, unfortunately, it still isn't providing the correct results. I've added some sample I/O to the question to help make things more clear. – Husso Apr 12 '16 at 21:32
  • The sample output you showed doesn't have the page number and offset. How do you know this code is wrong? – Barmar Apr 12 '16 at 21:39