Section definition and concept in Win32 assembly

Source: Internet
Author: User

Win32 Assembly SourceProgramThe source program structure that contains all segments is as follows:

 
. 386.Model flat,Stdcalloption Casemap:None<Some include statements>.Stack[Size of the stack segment].Data<Some initialized variable definitions>.Data? <Some variable definitions that have not been initialized>.Const<Some constant Definitions>.Code<Code> <Start number> <Other statements>End start label

 

. Stack,. Data,. Data ?,. Const and. Code are segmented pseudo commands.

In Win32, only code and data are allowed.

. Data,. Data? And. co. NST are data segments.

. Code is a code segment.

. Stack segment definition is often ignored

Different from Dos assembly, the Win32 assembly does not have to consider the stack. The system will allocate a extended, sufficiently large segment to the program as the stack segment.

 

Didn't we say that segments are not used in Win32 environments?

Yes, these "segments" are not actually the meaningful segments in the DOS assembly, but the memory "segments ".

The end of the previous segment is the start of the next segment. All segments, including the address space used by the system, constitute the entire addressable 4 GB space.

The memory management in the Win32 environment uses the paging mechanism of the 80386 processor. Each page (4 kb) can specify attributes freely. Therefore, the last 4 kb may be code, the attribute is executable but not writable. the next 4 kb may be readable or writable but unexecutable data. What about the following? It may be readable, non-writable, or non-executable data.

In Win32 compilation source programs, the concept of "segmentation" is actually to classify different types of data or code and then put it on the memory pages of different attributes (that is, different "segments ")., Which does not involve using different segment selectors.

Although it is defined using the same. Code and. Data statements as DoS assembly, the meaning is completely different!

 

Data Segment

==============

. Data,. Data? And. Const define data segments, which correspond to different data definitions. They are also placed in different sections in the final executable files.

Data Definitions in programs can be classified into three categories:

    1. Readable and writable Defined variables. The data must be defined in. data Segment ,. the data segment is the initialized data segment, and the defined data is readable and writable. When the program is loaded, these values are already in the memory ,. the data segment is stored in the _ data section of the executable file.
    2. Readable and writable undefined variables. These variables are generally used as buffers or after the program is executed. The data can be defined in. Data or. Data? But usually put it in. Data? . Although the definition can be used normally in both sections, it is defined in. Data? The. exe file size is not increased.
    3. For example, if you want to use a 100 KB buffer, you can define it in the data segment:

      Szbuffer dB 100*1024 DUP (?)

      If put in. in the data segment, the compiler considers that the data must be valid when the program is loaded, so it retains all 100 kb of content when generating executable files, even if they are all zero! If the size of other parts of the program is 50 kb. the size of the EXE file is 150 kb. If the buffer is defined as 1 MB. the EXE file is increased to 1 050 kb .. Data? The content compiler considers that the program will be used only after execution. Therefore, only the size information is retained when an executable file is generated, and disk space is not wasted for it. In the same case as above, even if the buffer is defined as 1 MB, the executable file is only 50 kb! In short,. Data? Segment is the uninitialized data segment, where the data is also readable and writable, but does not occupy space in the executable file,. Data? Segments are stored in the _ BSS section in the executable file.

    4. Constant. For example, the string information to be displayed is valid when the program is loaded, but it does not need to be modified throughout the execution process. The data can be stored in. in the const segment ,. the const segment is a constant segment, which is readable and cannot be written. For convenience, constants are often defined together in the. Data Segment in small programs, rather than a. Const segment. If the program accidentally writes the write command for the data in the. Const segment, it will cause a protection error.
    5. If you are not afraid of poor program readability, you can also mix the items defined in the. Const segment into the. Code segment, because the. Code segment can also be read.

Code segment

================

The. Code segment is a code segment. All commands must be written in the code segment. In the executable file, the code segment is placed in the _ text section.

Data segments in the Win32 environment are not executable. Only code segments have executable attributes. For applications that work in privileged level 3,. Code segments cannot be written.

Of course, things always have two sides. In Windows 95, programs running at the privilege level 0 have the right to read and write all segments, including code segments. In addition, programs running under priority 3 do not necessarily cannot write code segments. The attributes of code segments are determined by the attribute bit in the PE Header of the executable file. if you change the code snippet attribute bit to writable, you can modify your own code snippet in the program.

 

From <Win32 Assembly tutorial> author: Luo yunbin

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.