Link to home
Start Free TrialLog in
Avatar of john2789
john2789

asked on

code segment

Can anybody tell me what the code segment? Please give me some example.

THIS IS NOT AN ASSIGNMENT.

Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image


Hi John,

When you compile and link a program, some parts can automatically be "grouped" together by the compilation and linkage process.  Typically, the separated items are placed in a memory "segment" to isolate code (the executable portion of your program) from data.

The code segment is the executable code that is produced by the compiler.

Back when PCs were smaller, slower, and simpler, they only had 640K of memory and the term "memory segment" had a somewhat different meaning than it does today.

But the principal still holds true.

Kent
Avatar of john2789
john2789

ASKER

Still, I don't get it. Could you explain it more detail?
For example, if you declare some global variables they would go into one separate section of memory other than that which is used to store the code. Some constants might be stored separately from these segments as well (sometimes following the code before getting moved to ROM etc.). The individual stacks could also have their own section. And so on. As Kent said, these things happens during the linking process of your program. You would be able to understand it better if you've seen some assembly code.
could you show me what  the code segment looks like on some programming?

If you compile and link a short program so that you write a loadmap this is pretty easy to see.  Each of the segments will be labelled on the loadmap file.

There are many reasons for separating different items by their functionality.  There are also some good reasons for NOT separating these items, but for the most part the reasons for separating them win out.

Consider the following program:

#include <stdlib.h>

int GlobalVariable = 0;

main ()
{
  printf ("The value of the global variable is %d\n", GlobalVariable);
}


Pretty simple, huh?  Well the compiler and linker can team up to separate items of similar usage.  In this case we have code (the printf() statement will be executed), global data (the integer GlobalVariable), and a constant (the string "The value of the global varibles is %d\n").

On such a small program, separating these items into their own region (or segment) is a bit wasteful, but on a very large program you would probably want all of your variables organized in one place and all of your code in another place.  One of the things that this does is make the amount of memory required for the executable code smaller since variables aren't mixed in with it.  This can help the program run faster by not voiding the cache as often.

Constants don't usually get their own "segment", but they are usually grouped together in a region that the compiler treats as "read only" memory.  This way the contents are never changed and never require page faulting back to disk should the page where they reside ever need to be "swapped out".


Kent
so, code segment is

printf(  );? If we have 3 printf ( ) statements, are they also code segments?
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The only place you can "see" the code or data segments is in the map file created by the linker.  Compile your program with the option to generate a map file.   Study the map file.  You'll see the names of the functions are assigned addresses encompassed by the code segment, often named "_TEXT".  The names of your variables wil end up in a data segment, often named "_DATA" for preset variables, or or "_BSS" for unset or value=0  variables.

When it comes time for your program to run, the operating system will create an area of memory for the code and labels it as "Executable".  It will also creates a right size segment for data and labels it as "read/write" memory.  Then the OS will jump to the starting address in the code segment.  That's about it for segments.

On the older real-mode PC's, segments had other meaning as well, there segments were a half-arsed way of accessing more than 64K of memory.

All below is about 16-bit mode.

Segment - is a part of memory that can be accessed without change of segment register value.
Because of registers are 16 bit we can address 64k of memory.
"Code Segment" - is a segment that contain code of a program
"Data Segment" - is a segment that contain data of a program

But it rather conditionally, because of code segment can contain data and on the contrary.