Are the Heap, the Stack and the Data segment on the same assembly program?
As far as i know, an assembly program is devided to two- 1)the code 2)the data. Now, when we code, say on cc++, the code is loaded to the memory and then the CPU starts execute the code, one instruction by one, as an assembly program. My questions are: 1. where is the c code stored? I mean, when I'm running a program in Visual Studio, is the code loaded to one of them- Heap,Stack..? 2. So the memory is virtually divided to Stack,Heap and Date segment, but when the CPU executes the program, as an assembly program, are they all 1 assembly program with the same data area or they are formed in to,lets say 2 or 3 assebmly program thats jumps from one to another?
** Let me add this question, maybe it will clarify my intention: When I'm launching a C program, the code (machine instructions) is loaded to the memory. So, this is one assembly program. But how does the memory division take place? I mean, how des the different memory sections such as Stack, Data segment, etc. modify the assembly program?
The segments or sections are contiguous divisions in object and executable files, much like chapters in a book. The stack and bss sections do not exist in the file but are created at runtime.
The point of the sections is mostly to divide the program into areas that the operating system can protect in different ways. In order to arrange for that, the sections must start on page boundaries and be contiguous in memory.
The basic ("important") sections are...
text or code - the OS will write-protect this section, and since it's immutable, it can also share it between multiple processes or threads running the same executable
data - the OS will map this r/w and won't directly1 share it
bss - this section consists of zero-initialized data.
stack - typically separated from the program, it generally grows downward from higher addresses
The last two sections aren't in the executable file because they don't need any intiialization.
If you are asking about how they are implemented, well, the assembler and linker create a table-of-contents and write out the sections in binary like chapters in a book. The OS then reads them separately and puts them in different sections of the address space.
The specifics and the terminology are different between Unix-like systems and Windows, but the principles are the same.
1. Yeah yeah, copy-on-write allows for sharing data too, kind of.
From wikipedia:
The PC architecture supports a few basic read-write memory regions in a program namely: Stack, Data and Code. The heap is another region of address space available to a program, from which memory can be dynamically allocated or freed by the operating system in response to system calls such as malloc and free.
I recommend you to read the full article
There is also this question on SO: How are the different segments like heap, stack, text related to the physical memory?
Also, these articles may be worth reading. Especially the last one:
To answer your questions:
1. where is the c code stored?
In the code segment.
2. So the memory is virtually divided to Stack, Heap and Date segment,
In real mode, yes. In protected mode… depends. To simplify extremely: program memory is mapped to physical memory. Each program lives in its own address space.
I recommend these articles if you want to know more:
3. but when the CPU executes the program, as an assembly program, are they all 1 assembly program with the same data area or they are formed in to,lets say 2 or 3 assebmly program thats jumps from one to another?
No. There are no jumps. Processor registers are pointing towards the next instruction to be executed. Others are pointing towards the stack, etc.
链接地址: http://www.djcxy.com/p/2330.html上一篇: Java“双Brace初始化”的效率?
下一篇: 堆,堆栈和数据段在同一个汇编程序中吗?