Memory Segments - Part 3 [ Code Segment]

The code is displayed above. What will you do with it? The first thing is compiling it and then linking the code. Let use assume that the above code is placed in the MyProgram.cpp file. When you compile, the compiler will translates this human readable language into something what called object code. If there are 20 such files, the job of the compiler is generate twenty different object files and prompt any errors that does not comply to the C++ syntax. Then the linker will go through all 20-object files to form an executable that is the .exe file. 

What happens when you double click the exe name? The exe is considered as process by the operating system and once the process is committed, the M/c code (exe) is loaded into the memory and processor will access these machine instructions to perform specified action. This memory is called Code Segment. In out example, the translated code goes as exe, and when the exe is executed, the instructions are loaded into the memory called code segment.

 

Let us take the first statement in the Program entry:

            //MemSeg04: Declare a loval variable and declare a pointer and store the address of local variable

            int m = 12;

 

The above code will be translated and packed in the exe as a machine-readable language. In the VS2005 IDE I kept a break point on the above shown statement and once the breakpoint is hit, using the context menu I asked for show Assembly code. The Assembly revealed is shown below:

 

Pic01.JPG

 

The address marked in Red box shows it stores the assembly command MOV (It may be mapped to number and then in terms of zeros and ones that is low and high voltage. We no need to go that much deep into the electronics of it). The Address in the Red box is actually allocated in the Code Segment. A pointer can store this address also.