Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When we run an executable,the machine code is called one by one in the CPU,but how does the OS feed each instruction to CPU?

What's the internals?

share|improve this question

migrated from Jul 1 '11 at 6:37

This question came from our site for professional and enthusiast programmers.

Might as well hit up Wikipedia for this one buddy. . . – surfasb Jul 1 '11 at 8:08

The loader loads binaries from disk into memory, relocating jump and load operations as required. From there, the CPU just loads the code from memory and executes it.

share|improve this answer
+1 for being slightly better than M. Hewgill´s answer. – JdeBP Jul 1 '11 at 11:35

The OS sets the Instruction Pointer (IP) register of the CPU to the beginning of the code to execute (which is in RAM). The CPU starts executing instructions in sequence.

share|improve this answer
how's that actually carried out? – new_perl Jul 1 '11 at 4:24
At the simplest level, the IP register is set with something like a JMP instruction. I suggest you find and study some introductory materials on CPU architecture. – Greg Hewgill Jul 1 '11 at 4:30
It's a close-run thing. But M. Vazquez-Abram's answer is slightly better in that it makes it clear that the CPU "feeds" itself. This answer still leaves open the implication that the operating system is some sort of intermediary that "feeds" machine code to a processor, which of course it isn't (since the operating system is itself machine code). – JdeBP Jul 1 '11 at 11:37

The OS sets the Instruction Pointer of the processor to the memory address of where the instruction for the program is and the processor does the work from there. I could get more technical but I don't know how much information you want.

share|improve this answer

The OS does not feed instructions to the CPU, the CPU pulls instructions from memory on its own based in the PC (Program Counter) or IP (Instruction Pointer) or whatever other term you want to use. The OS itself is just a bunch of instructions being fed into the processor. The processor does not know the OS from any other program. The OS reads the machine code portion of an executable (executables used by well known OSes like windows and linux have more information in the file other than just the binary or executable portion) and puts it in ram somewhere. And essentially the OS branches to the entry point instruction in the executable, by changing the PC. Many different ways to do this, branching, changing the return value on an interrupt, processor specific methods, etc.

Petzold has a book called "Code", pretty good, give it a read.

share|improve this answer
Just to add: Intel calls the register pointing to the instruction it is executing the "IP" or Instruction Pointer. It's EIP in 32-bit processors running in 32-bit mode, and RIP in 64-bit processors running in 64-bit mode (the E stands for Extended, still don't know what the R stands for) – LawrenceC Mar 3 '12 at 15:21

You must log in to answer this question.