C language compiling Mode

Source: Internet
Author: User

The compile mode refers to how to place program code and data in the memory, how to allocate stacks, and how to determine the memory size occupied and how to access them. After the memory mode (compile mode) is specified, the compiled program will compile the program in the memory mode selected in advance. The C language provides six compilation modes, which are: the micro mode (tiny ), small, medium, compact, large, and huge ). Shows the relationship between them. You can select your own program size and needs.

 

A small program has only one program segment and the size cannot exceed 64 KB. The default code (function) pointer is near (short-range pointer ). The so-called large program means that the program only has multiple program segments, each of which cannot exceed 64 KB, but the total program volume can exceed 64 KB. The default code pointer is far (Remote pointer ). Small data means that there is only one data segment in the index data. The default data pointer is near. Big Data means that the index has multiple data segments, and the default data pointer is far.

C language compilation mode-micro mode (tiny)

In micro-mode, the data and code in the program are all placed in the same segment, that is, they do not exceed 64 KB. In micro-mode, the segment addresses of the code segment, stack segment, and data segment are the same, that is, cs = DS = Ss = es. In the micro mode, the data pointers are all near. Generally, small programs can compile in this mode. You can also use the exe2bin Conversion Program in DOS to convert the. exe program into a. com program. In micro mode, the memory allocation mode is shown in.

 

It can be seen that the code segment, data segment, and stack segment are in the same segment. when addressing them, they are all at the same address offset reference point, the segment with this characteristic is also called the same group (dgroup). It can be seen that the stack grows up, that is, every time the stack is pressed, the stack pointer SP decreases by 2, that is, it moves to the direction of address reduction, and its initial value points to the bottom of the stack, that is, 0 xFFFF (64kb ). The heap grows down, that is, it changes to the direction of adding an address. From the figure, we can see that the heap and stack address grow in the opposite direction. When the two do not meet each other, free space occurs. Generally, the program is in this State. When a large number of stack addresses are occupied, the two may merge to overwrite part of the heap space.

C language compilation mode-small mode (small)
In small mode, the code in the program is placed in the 64 kB code segment, and the data is placed in the 64 KB data segment. In small mode, the stack segment, additional data segment, and data segment all point to the same address. The three of them are one, that is, DS = Ss = es, and the pointers are all near, generally, programs are compiled in small mode. Shows the memory allocation in small mode. We can see that the data segment, stack segment, and additional segment are in the same segment group, that is, their offset addresses all use the same segment as the reference point.

 

C language compiling mode-Medium)
In moderate mode, all data is stored in a 64 KB data segment. Therefore, near can be used in the data segment, and the code size can be larger than 64 KB (1 MB allowed). Therefore, different code segments can be used, use the code segment (far remote pointer ). This compilation mode applies to large programs with large amounts of code and small amounts of data. Shows the memory allocation in normal mode.

 

C language compilation mode-compact)

In compact mode, when the data volume exceeds 64 KB, it can be placed in multiple data segments. The pointer in the data segment is (FAR ). When the code size does not exceed 64 KB, the pointer in the code segment can be within a short range (near ). However, in this mode, the static data cannot exceed 64 KB, And the heap uses the far pointer for access. Shows the memory structure in the compact mode.

 

C language compilation mode-large mode (large)

In big mode, both the Code and data use the far pointer, and both can reach 1 MB. Static data is still in the same compact mode and cannot exceed 64 KB. Shows the memory structure in large mode.

 

C language compilation mode-giant mode (huge)

In the giant mode, far pointers are used for code segments and data segments. codes are distributed in different code segments and data is distributed in different data segments. They come from different source programs and have only one big stack. In addition, the static data size can exceed 64 KB. Shows the memory structure in giant mode.

 

The size of the compact mode, big mode, and giant mode data zone can exceed 64 KB. That is, you can use the far pointer to access data in different data segments, which are also called the big data storage mode. But there is a difference: the Compact mode and the large mode are based on C. When the static data, such as arrays, structures, or other types of data, is defined as static data, the data volume cannot exceed 64 KB, only the giant mode can exceed 64 KB. In the big data storage mode, the heap and stack are stored in different segments in the form of dynamic data and local variables, so that they are not limited by the size of 64 KB, stack growth does not affect stack space.

No matter which compilation mode is used, the code and data size generated by C source program compilation cannot exceed 64 KB, it can be divided into two or more programs based on code or data. Large code compilation modes (medium mode, large mode, and giant mode) are required for large code programs. Large Data Compilation modes (compact mode, large mode, and giant mode) should be used for large data programs ), compiled in this way. the OBJ file will bring the Connection Program Information and arrange the code and data in different segments. The generated. EXE file will tell the DOS program how to load the code segment and data segment, and how to initialize the register. In this way, you can determine the size of the Data zone opened up in different compilation modes, that is, the size is larger than 64 kB or no more than 64 KB.

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.