Solved

code segment

Posted on 2003-11-10
10
498 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:Kdo
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
 

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:Kdo
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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:
Kdo 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-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.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now