Differences between. BSS and. data segments

Source: Internet
Author: User

A program is essentially composed of three parts: BSS segment, data segment, and text segment. This concept does not know where it came from, but it is a very important basic concept in the current computer programming. In addition, the design of the embedded system is also very important, involving the memory size allocation during the operation of the embedded system and the space occupied by storage units.

In an architecture that adopts segmented memory management (such as Intel's 80x86 system) and BSS segment (block started by symbol segment) it is usually a memory area used to store uninitialized global variables in the program. Generally, the BSS segment is cleared during initialization. The BSS segment is a static memory allocation, that is, the program clears it from the beginning.

For example, after a program such as C language is compiled, initialized global variables are saved in the. Data Segment, and uninitialized global variables are saved in the. BSS segment.
In programming ground up. the BSS is interpreted as: there is another section called. BSS. this section is like the data section, Except t that it doesn't take up space in the executable.
Both the text and data segments are in the executable file (which is usually fixed in the image file in the embedded system) and loaded by the system from the executable file; the BSS segment is not in the executable file and is initialized by the system.

Http://blog.csdn.net/bobocheng1231/archive/2008/02/23/2115289.aspx

[Example 1]

Compile two small programs with Cl as follows:

Procedure 1:

Int ar [30000];
Void main ()
{
......
}


Procedure 2:

Int ar [300000] =
{1, 2, 3, 4, 5, 6 };
Void main ()
{
......
}


Find that the. EXE file after compilation of Program 2 is much larger than that of program 1. Now I am puzzled, So I compiled it manually and used the/Fas compilation option to check their respective. ASM. I found that the AR definition in program 1. ASM is as follows:

_ BSS segment
? Ar @ 3 Paha dd 0493e0h DUP (?) ; Ar
_ BSS ends


In program 2. ASM, Ar is defined:

_ Data Segment
? Ar @ 3 Paha dd 01 H; ar
Dd 02 h
Dd 03 h
Org $ more 1199988
_ DATA ends


The difference is obvious. BSS segment, while the other is located in. data segment, the difference between the two is: Global uninitialized variables exist in. the BSS segment is embodied as a placeholder; the Global initialized variables are stored in. data segment; and the automatic variables in the function are allocated space on the stack .. BSS does not occupy the. exe file space. The content is initialized (cleared) by the operating system, but. Data is occupied. The content is initialized by the program, which leads to the above situation.

[Example 2]

Compile the following Program (test. cpp ):
# Include <stdio. h>

# Deprecision Len 1002000

Int inbss [Len];
Float fa;
Int indata [Len] = {1, 2, 4, 5, 6, 7, 8, 9 };
Double DBB = 100.0;

Const int CST = 100;

Int main (void)
{
Int run [100] = {, 9 };
For (INT I = 0; I <Len; ++ I)
Printf ("% d", inbss [I]);
Return 0;
}

Command: CL/FA test. cpp
Press enter (/FA: Generate assembly code)
The resulting assembly code (test. ASM ):
Title Test. cpp
. 386 P
Include listing. inc
If @ version GT 510
. Model flat
Else
_ Text Segment para use32 public 'code'
_ Text ends
_ Data Segment DWORD use32 public 'data'
_ DATA ends
Const segment DWORD use32 public 'const'
Const ends
_ BSS segment DWORD use32 public 'bss'
_ BSS ends
_ TLS segment DWORD use32 public 'tls'
_ TLS ends
Flat group _ data, const, _ BSS
Assume Cs: flat, DS: flat, SS: Flat
Endif
Public? Inbss @ 3 Paha; inbss
Public? Fa @ 3ma; fa
Public? Indata @ 3 Paha; indata
Public? DBB @ 3na; DBB
_ BSS segment
? Inbss @ 3 Paha dd 0f4a10h DUP (?) ; Inbss
? Fa @ 3ma dd 01 h dup (?) ; Fa
_ BSS ends
_ Data Segment
? Indata @ 3 Paha dd 01 H; indata
Dd 02 h
Dd 03 h
Dd 04 H
Dd 05 h
Dd 06 h
Dd 07 h
Dd 08 h
Dd 09 h
Org $ more 4007964
? DBB @ 3na DQ 040591_1_r; 100; DBB
_ DATA ends
Public _ main
Extrn _ printf: near
_ Data Segment
$ Sg537 dB '% d', 00 h
_ DATA ends
_ Text Segment
_ Run $=-400
_ I $=-404
_ Main proc near
; File test. cpp
; Line 13
Push EBP
MoV EBP, ESP
Sub ESP, 404; 00000194 H
Push EDI
; Line 14
MoV dword ptr _ run $ [EBP], 1
MoV dword ptr _ run $ [EBP + 4], 2
MoV dword ptr _ run $ [EBP + 8], 3
MoV dword ptr _ run $ [EBP + 12], 4
MoV dword ptr _ run $ [EBP + 16], 5
MoV dword ptr _ run $ [EBP + 20], 6
MoV dword ptr _ run $ [EBP + 24], 7
MoV dword ptr _ run $ [EBP + 28], 8
MoV dword ptr _ run $ [EBP + 32], 9
MoV ECx, 91; 0000005bh
XOR eax, eax
Lea EDI, dword ptr _ run $ [EBP + 36]
Rep stosd
; Line 15
MoV dword ptr _ I $ [EBP], 0
JMP short $ l534
$ L535:
MoV eax, dword ptr _ I $ [EBP]
Add eax, 1
MoV dword ptr _ I $ [EBP], eax
$ L534:
Cmp dword ptr _ I $ [EBP], 1002000; 000f4a10h
Jge short $ l536
; Line 16
MoV ECx, dword ptr _ I $ [EBP]
MoV edX, dword ptr? Inbss @ 3 Paha [ECx * 4]
Push edX
Push offset flat: $ sg537
Call _ printf
Add ESP, 8
JMP short $ l535
$ L536:
; Line 17
XOR eax, eax
; Line 18
Pop EDI
MoV ESP, EBP
Pop EBP
RET 0
_ Main endp
_ Text ends
End
----------------------------------------
The Assembly file shows that the inbss and indata arrays are in different segments (inbss are in the BSS segment, while indata is in the data segment)
If you remove the indata array in test. cpp and check the size of the generated EXE file, you can find that the size of the EXE file is much smaller after indata is removed. If the inbss array is removed, the size of the EXE file is almost the same as that when it is not removed.

Note:
The BSS segment (data not manually initialized) does not allocate space for the data segment, but records the size of the space required for the data.
Data (data that has been manually initialized) segments allocate space for the data and store the data in the target file.

The data segment contains the initialized global variables and their values. The size of the BSS segment is obtained from the executable file, and then the linker obtains the size of the memory block, followed by the data segment. When this memory area enters the address space of the program, all are cleared. The entire section that contains data segments and BSS segments is usually called a data zone.

Http://www.w3china.org/blog/more.asp? Name = foxwolves & id = 29997

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.