0
$\begingroup$

When a CPU needs to get more instructions from storage and copies them to RAM, how does the CPU jump to previous instructions that are no longer in the RAM? Would it have to copy the old instructions back into RAM?
I feel like that would take a very long time to do.

FOR EXAMPLE: this is storage xsxx xxax.
Pretend the "x" is a random instruction that doesn't matter. The RAM can hold 4 instructions but the program is 8 instructions long. The first four instructions are in RAM the CPU runs all of them and copies the last 4 into RAM from storage (SSD, ROM, or HDD). Now for example "a" is an instruction the makes the CPU jump back to the "s" which is in the old 4 instructions which is not in RAM anymore.
How would it do this?

$\endgroup$

2 Answers 2

3
$\begingroup$

You have many memory pages - thousands 20 years ago, possibly millions today.

When the next instruction is not in RAM, it is loaded for example from disk. Some other page of memory will be thrown out. However it is very, very unlikely that the page with the previous instruction is thrown out, because the OS will throw out pages that have not been used in a while, and the page with the previous instruction has been used nanoseconds ago. So the whole scenario is very, very unlikely to happen.

If that page had been thrown out, it would just be reloaded from disk. The OS has no problems handling multiple page faults in one instruction.

$\endgroup$
0
$\begingroup$

There seems to be a faulty premise in your question. Normally, old instructions remain in memory. They are not removed from memory.

If the virtual memory subsystem pages/swaps them out, it will page/swap them in when needed, just as with any other memory that is accessed.

$\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.