Where does the instruction of an executable go to?

Please do not mark as Duplicate : I know that executable files such us .exe are a set of instructions in machine language (binary) but what I don't know if those instruction are targeted to the system (then to the kernel) and then to the CPU or they're read from the memory directly by the CPU ? I'm a bit confused

asked Mar 15, 2016 at 10:00 AnotherOne AnotherOne 913 2 2 gold badges 8 8 silver badges 21 21 bronze badges

3 Answers 3

Arranging for executable files to run is done by the process loader, usually a part of, or executed by, the UI shell.

The exe file contains header metadata and executable code.

The loader reads the executable file header, allocates an initial working set of kernel and other resources that a process will need to run and creates a thread to run code at the process entry point. If that code has not been read into memory by the loader, it will happen now in an immediate page pault loads it.

The process then exists, and a thread is running it.

Summary: the image file, (.exe in Wondows), contains both metadata for interpretation by the OS loader AND executable code. The executable code instructions are read into memory by the loader and the CPU fetches and executes those instructions from memory.

answered Mar 15, 2016 at 10:20 Martin James Martin James 24.7k 4 4 gold badges 37 37 silver badges 60 60 bronze badges

even though that I don't know what a loader is (I think it's a part of the OS) , and I don't know what a thread is , I can clearly see that the OS sets the table for the CPU to run the instruction by preparing the memory , am I right ?

Commented Mar 15, 2016 at 10:31 @MekacherAnis essentially, yes:) Commented Mar 15, 2016 at 14:39

Your question starts with a mistake.

I know that executable files such us .exe are a set of instructions in machine language (binary)

Usually, an executable file is set of instructions how a program or library is to be loaded into memory. (There are some executables that are loaded as it into memory but those are normally used for operating system and embedded systems.)

A program or library will have data that is:

A linker usually organizes the parts of a program in program sections that correspond to those groups.

The program load reads the instructions in the executable file and creates these sections. For read only data, the loader creates the appropriate pages, loads the data into them, and marks the pages a read write. Some systems actually map the memory page directly to the executable and uses the executable for paging those sections.

For a read write, initialized to zero section, the executable does not need to contain data. I just needs to tell the loader to create the pages and set everything to zero.

The executable will also define the dynamic libraries that need to be loaded and addresses fix ups (addresses that need to be fixed, depending upon where things are loaded).

If the executable defines a program (rather than a library) it will indicate the starting address of the program so that it can execute.