Solved

code segment

Posted on 2003-11-10
10
501 Views
Last Modified: 2010-04-15
Can anybody tell me what the code segment? Please give me some example.

THIS IS NOT AN ASSIGNMENT.

0
Comment
Question by:john2789
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 9719657

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
0
 

Author Comment

by:john2789
ID: 9719675
Still, I don't get it. Could you explain it more detail?
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9719710
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.
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

Author Comment

by:john2789
ID: 9719713
could you show me what  the code segment looks like on some programming?
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 9719747

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
0
 

Author Comment

by:john2789
ID: 9719762
so, code segment is

printf(  );? If we have 3 printf ( ) statements, are they also code segments?
0
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 50 total points
ID: 9719787

All three will end up in the code segment (there's only one code segment per program).  Along with all of the if, while, for, switch, and return statements.  Plus all of the assignment statements.  They all reduce to executable code (instructions) so they all get placed in the code segment.

Kent
0
 
LVL 22

Expert Comment

by:grg99
ID: 9721912
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.

0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9722098
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.
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9722124
"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.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question