How do instructions from a program get sent to different memory levels?
The processor pulls them into caches using the program counter aka instruction pointer that refers to the next instruction to execute.
Are they pre-loaded when the chips are manufactured for the first time?
Not for general purpose programs. Only special environments burn programs into permanent storage, like ROM, which may happen for booting a computer or for an embedded system. General purpose programs rely on an operating system to associate their code & data with memory.
My question now is, do instructions required for program execution get moved to memory by the compiler the same way data gets pushed?
Recap: The compiler follows the statements & expressions of the (source) code program, translating them into machine code instructions & data; these machine code instructions instruct the processor what to do to carry out the high level language program. This includes machine code instructions to load data into CPU registers, sometimes so that the data is closer to the CPU so quicker to work with, other times because that is the only way that the CPU can operate on the data (by the machine code instruction set of the processor).
There is a difference between having machine code instructions in memory and having them in the processor.
The compiler does not, per se, command instructions to be moved into the processor. The processor is an interpreter of machine code instructions, and all on its own it will pull instructions into itself for execution. The compiler lays out sequences of instructions and the CPU automatically brings them in for execution — that is is the CPU’s purpose.
Program code (modulo modern caches) is too large to directly load into the CPU. Thus, programs are stored in main memory, like data that is too large to fit directly in the CPU. Program code is sequences of machine code instructions. Since these instructions are stored in memory, each individual instruction has a unique memory address! The processor refers to instructions by their memory address, and by their memory address has a concept of what instruction is executing now and what will/should execute next.
The mechanism for this goes to the instruction pointer register (in intel-speak) or the program counter register (in virtually all other machine code languages), but the concept is the same: there is a fundamental register inside the processor whose job is to hold the address of machine code instruction that is executing now, and whose job is also to change to indicate what instruction to execute next.
As mentioned, the processor is an interpreter of machine code instructions. It uses its internal program counter in what is called a fetch-execute pattern, where it demands of the memory system the value of the machine code instruction to execute next. That demand, a request to read an some particular memory address, may cause cache misses, or page fault (or both), which ultimately, through conspiracy of both the processor and the operating system, will cause machine code instructions to load into memory, to load in to caches, to load into the processor itself.
Each machine code instruction of the program informs the processor what machine code instruction to execute next. The program can move the program counter backwards to re-execute instructions that have already executed (forming loops) or move it forward to exit a loop or skip a then or else statement. There is a cooperation between the processor and the machine code, where the processor is told, at every step, what machine code to execute next. So, the machine code program is directing the execution of sequences of the machine code program, but it is the processor that is doing the actual execution (though under the direction of the program).
Would we say that the compiler tells the processor to load instruction to execute into the CPU? Sort of, but not quite as explicitly as commands to load data into the CPU.
The compiler creates a sequences in the machine code program used to control execution, so I would say it implicitly tells the processor what instructions to bring into the caches. Otherwise (modulo memory mapping and page faulting) the machine code instructions are already in memory (by the operating system associating the memory with the program code & data), and the processor will manage caches to keep frequently executing instructions in the higher levels of the cache.
what do CPU designers load into the cache/main memory when it comes fresh out of the foundry?
Nothing, the memory is considered blank and can be repurposed. On some systems we cannot even guarantee the value of each memory cell when the processor boots. Where the program (as a file on disc) specifies the proper initial values for memory, then the original values that were in memory, from boot or from another program’s execution, are replaced with those specified values. Other memory used by the program is zeroed out before use.
are any instructions preloaded into memory at the time of designing and manufacturing of the chip so that it executes a pre-defined set of sequences when it is powered up for the first time?
Yes, sometime some memories are hard-coded with code and data that helps the computer boot. This is not a general purpose way that programs run.
CLICK HERE to find out more related problems solutions.