Вопрос

Assume that a system has a 64-bit virtual address with an 8-kb page size. Write a C- 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: ./a.out 8219986 The program would output: The address 8219986 contains: Page number: 1003 Offset = 3410 All computation must be done in binary.

#include <stdio.h>
int main(int argc,char *argv[])
{
    unsigned long long int MappedAdress = atoll(argv[1]);
    unsigned long long int page = MappedAdress;
    unsigned long long int offset = MappedAdress;
    //print long long address
    printf("The MappedAdress %llu contains:\n",MappedAdress);
    //shift to right 13 bit to retrive pages, then print
    page = page >> 13;
    printf("Page Number : %llu\n",page);
    //since it's 64-bit opr. Add offset AND 1FFF , 
    offset = offset & 0x1FFF;
    //print the offset
    printf("Offset = %llu\n\n",offset);
    return 0;
}

Can anyone explain to me what just happened here ? and what to shall I do to write such a program but in Java as long as I have no background in C language?

Это было полезно?

Решение

Can anyone explain to me what just happened here ?

Roughly speaking, you converted an address 64 bit virtual address to a virtual page number and an offset, using the right-shift operator (>>) and the bitwise AND operator (&).

And what to shall I do to write such a program but in Java as long as I have no background in C language?

What you should do is to read your Java textbook or lecture notes to find out about the >> and & operators (for integer operands), and what they do. Then write the equivalent code in Java.

HINTS:

  1. The >> and bitwise & operators mean pretty much the same thing in C and Java.
  2. You should probably also read the lecture notes where your lecturer explained about virtual addresses and how they work.

Другие советы

LEt's go back to the problem statement: given a 64-bit number that represents a byte address on a computer that has 8K size pages, return the page number and offset for that address.

That simply means that you take number / 8K to get the page address and the remainder is tie offset. Of course, that can often be computed faster by using a right shift to get the page number and a mask to get the offset (but do handle leading "1" bit in the 64-bit number).

You don't need the C program to figure out how to break a 64 bit number into the two desired pieces.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top