0
$\begingroup$

Im designing my own CPU but I don't know how it jumps to an instruction that's no longer in ram. People have told me it puts the address in the SSD but for example, if the address were 3 in ram it would most likely not be in address 3 in the SSD. someone help me I don't understand.

$\endgroup$
1
  • 1
    $\begingroup$ Are you referring to virtual memory ? $\endgroup$
    – user16034
    Apr 21, 2023 at 6:25

2 Answers 2

3
$\begingroup$

The program counter holds an address. Period. The CPU fetches an instruction from that address. Period.

Most CPUs today have a hardware unit called an MMU (memory management unit) which sits between the CPU and the RAM and messes with the addresses. The MMU takes the top many bits from the address and looks them up in a mapping table (in RAM) and replaces them with the value in the table. E.g. 0x12345678 -> look at table entry 0x12345 -> entry says 0xfffbc -> actual address is 0xfffbc678. (0x1000 bytes is called a page and this table is called the page table)

The entry can also say the address is invalid. This causes an exception i.e. the same kind of thing that is triggered by invalid instruction, or divide by zero, etc. The CPU saves the address somewhere and it sets the program counter to the OS's exception handling code for invalid addresses.


The OS can use this to simulate extra RAM. It can promise 1GB of memory even though you only have 512MB. The other 512MB are invalid page table entries. When the program tries to access the 513'th MB, the CPU sees the invalid entry, and raises an invalid-address exception.

The OS code then picks some page it thinks the program isn't using, looks what is there, saves it on the SSD, and makes that page invalid in the page table.

Then it looks at the address that caused the invalid-address exception, looks in some data structure to see what was supposed to be there (e.g. a fresh page with zeroes, or a page that was saved to the SSD before), reads that data into the page it picked before, and puts an entry in the page table which says this page address turns into that page address. Then it goes back to the program which is none the wiser.

As long as the program can use all 1GB of addresses to access memory (because no matter which address the program uses, the OS quickly goes and makes it valid), the program doesn't know the OS and MMU are playing games behind the scenes. The program can think there's more memory than there actually is. This is popularly known as "virtual memory". It works well if the program is using less than 512MB at a time (and maybe the other 512MB are features you aren't using right now, for example). If the program really does use more than 512MB at a time, the program can run really slowly (so slowly that you might wish it would just crash so you would take the hint and go to the store and buy a RAM upgrade!).

As you can see it's an OS feature, not a CPU feature. The CPU needs to have an MMU, but the OS does the rest.

$\endgroup$
1
$\begingroup$

Virtual addresses can correspond to data in RAM or on disk and the system keeps address translation tables saying for instance:

  • address 15037 is mapped to RAM page 7000, at offset 37 (hence address 7037)

  • address 15037 is mapped to disk, track 345, sector 25, at offset 37.

(Addresses are mapped by whole pages, 1000 bytes in my example.)

When the CPU wants to access that virtual address, the system will either convert to the correct RAM address, or cause the loading of the relevant page from disk and map it to RAM before it can be accessed. This loading mechanism is transparent to the program being executed (it works by interruption).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.