I had a lab today that I could not complete because I cannot understand the basic process to going from a virtual address to a physical address. My understanding so far is that virtual memory is kept in a page table, it consists of pages(which contain the address and an indication if it's present or not), and that to get the physical address you need to offset by 12 bits and to make the 4 higher order bits an index(?) That's really where I get confused.
This is my code currently, it's not cohesive, but it generally shows what I understand so far. I would really appreciate any help with my understanding of this process, it seems strangely straight forward and simple, I don't know what isn't clicking for me.
`
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
struct Map{
unsigned int frame_Num : 3;
unsigned int valid : 1;
};
struct Map PageTable[16];
void createPageTable(){
PageTable[0].frame_Num = 0x2;
PageTable[0].valid = 1;
PageTable[1].frame_Num = 0x1;
PageTable[1].valid = 1;
PageTable[2].frame_Num = 0x6;
PageTable[2].valid = 1;
PageTable[3].frame_Num = 0x0;
PageTable[3].valid = 0;
PageTable[4].frame_Num = 0x4;
PageTable[4].valid = 1;
PageTable[5].frame_Num = 0x3;
PageTable[5].valid = 1;
PageTable[6].frame_Num = 0x0;
PageTable[6].valid = 0;
PageTable[7].frame_Num = 0x0;
PageTable[7].valid = 0;
PageTable[8].frame_Num = 0x0;
PageTable[8].valid = 0;
PageTable[9].frame_Num = 0x5;
PageTable[9].valid = 1;
PageTable[10].frame_Num = 0x0;
PageTable[10].valid = 0;
PageTable[11].frame_Num = 0x7;
PageTable[11].valid = 1;
PageTable[12].frame_Num = 0x0;
PageTable[12].valid = 0;
PageTable[13].frame_Num = 0x0;
PageTable[13].valid = 0;
PageTable[14].frame_Num = 0x0;
PageTable[14].valid = 0;
PageTable[15].frame_Num = 0x0;
PageTable[15].valid = 0;
}
int translateToPhysicalAddress(int virtualAddress){
//int offset = 12;
//int physicalAddress = offset<<virtualAddress;
//printf("0x%x\n", physicalAddress);
}
int main(){
createPageTable();
//translateToPhysicalAddress(5);
return 0;
}
`
I had people keep explaining it to me with the same wording as I described before, Ive tried googling things, going over my prof slides, etc. I was out for a week which does not help, but I just don't understand the basic process.