Memory mode in C language, near far huge keyword

Source: Internet
Author: User
Tags compact function definition

This article is reproduced from 6044576

The compilation mode is how to put the program code and data in memory, how to allocate the stack, and confirm the memory size and how to access them, when the specified memory mode (compilation mode), the language compiler will compile the organization program according to the pre-selected memory mode, C language provides 6 kinds of compilation mode, These 6 modes are: Micro-mode (Tiny), small mode (Small), medium mode (Medium), compact mode, large mode (Large) and Jumbo Mode (Huge). Users can choose according to their own program size and needs.

The so-called small program refers to the program only one program segment, the size of not more than 64KB, the default code (function) pointer is near (short-range pointer). The so-called Big program refers to the program has a number of program segments, each program segment is not more than 64KB, but the total program volume can exceed 64KB, the default code pointer is far (remote pointer). Small data means that data has only one data segment, and the default data pointer is near. Big data means that data has more than one data segment, and the default data pointer is far.

C-language compilation mode-micro mode (Tiny)
In the micro-mode program data and code are placed in the same paragraph, that is, they do not exceed 64KB. In micro mode, the segment addresses of the code snippet, stack segment, and data segment are the same, that is, cs=ds=ss=es. In this section, the code is loaded first, with the lowest address, followed by static variables and global variables. Then there is the heap, and finally the stack. Both the stack and the heap are dynamic, the heap grows from the low address to the high address, the stack grows from the high address to the low address, and if the two are equal, the space is exhausted. In micro-mode, the data pointer is near, the General applet can be compiled with this compilation mode. You can also use the Exe2bin conversion program in DOS. EXE program into a. COM program.

Code snippets, data segments, and stack segments are all in the same paragraph, when addressing them, all at the same address offset reference point, the segment with this feature is also known as belonging to the same set of segments (DGROUP), the stack is growing upward, that is, each stack once, the stack pointer sp minus 2, that is, the direction of the address reduction, It starts with the initial value pointing to the bottom of the stack, which is 0xFFFF (64KB). The heap is growing downward, that is, changing the direction of the increased address. It can be seen from the graph that the heap and stack addresses grow in opposite directions, and when the two do not meet, there is a free space. The general program is this state, when the stack address is large, the two may be re-merged to cover some of the heap space.

C-language compilation mode-small mode (Small)
In small mode, the code in the program is placed within the 64KB code snippet, and the data is placed in a 64KB data segment. In small mode, the stack segment, the additional data segment and the data segment all point to the same address, they are three, namely, ds=ss=es, the pointer is near, the general program is compiled in small mode. Data segments, stack segments, and additional fields are the same group, that is, their offset addresses are at the same address as the reference point. In addition to the heap that share a segment with the data/stack, there is a far heap.

C-language compilation mode-medium mode (Medium)
In medium mode, all data is placed within 64KB data segments, so near is used within the data segment, the code can be larger than 64KB (1MB allowed), and thus can be used in different code snippets (far remote pointers). Code modules from different source files are placed within different code segments. Strictly speaking, the functions within the same source file are also placed in different code segments. This compilation mode is suitable for large programs with large code volumes and small data volumes. There is a far heap.

C Language Compilation mode-compact mode
In compact mode, when the amount of data exceeds 64KB, it can be placed in multiple data segments, and the pointer within the data segment is (FAR). The code amount does not exceed 64KB, within a segment, so the pointer within the code snippet is short-range (near). In this mode, however, the static data still cannot exceed 64KB, and the heap is accessed using the far pointer. The code, the static data, the stack, and the heap each have their own segments. Heaps of only far heaps, not nearly heaps.

C-language compilation mode-large mode (Large)
In large mode, both the code and the data are in the far pointer and can reach 1MB. static data, stack, heap with compact mode, code in the same mode. Static data is still the same as in compact mode and cannot exceed 64KB.

C Language Compilation Mode-Mega Mode (Huge)
In the mega mode, the code snippet and data segment are all using the far pointer, the code is distributed in different code sections, the data is also distributed in different data segments, they come from different source programs, and there is only one large stack. and the static data size allows for more than 64KB.

Compact mode, large mode, giant mode data area size are allowed to exceed 64KB, that is, data can be used to refer to different data segments of the data access, they are called Big Data storage mode. But a little different: compact mode and large mode according to C, its static data, such as arrays, structures or other types of data is defined as static type, its data volume can not exceed 64KB, and only the giant mode is allowed to exceed 64KB. 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 64KB size, and the stack growth does not affect the heap space.

No matter which compilation mode is used, the code and the amount of data generated by the C source program cannot exceed 64KB, and for more than one source program, it can be decomposed into two or more programs according to how much code or data is compiled separately. Large code program to choose large Code compilation mode (medium mode, large mode and giant mode), large data volume program should choose Big Data compilation mode (compact mode, large mode and giant mode), so that the compilation of the generated. obj file will be brought to the connector information, the code and data are arranged in different segments. The resulting. exe file will tell DOS when it loads how the program should load the code snippet and data segment and initialize the register. In this way, you can determine the size of the data area in different compilation modes, that is, greater than 64KB, or not more than 64KB.

In TC, the memory mode has close relation with far, near, huge and other key words. In tiny, small mode, all function definitions, global variable definitions, and pointer variable definitions, if not displayed with the far, near, huge and other keywords, are used by default to the near keyword; in medium mode, the function definition uses the FAR keyword by default. The variable definition uses the NEAR keyword by default, and in compact mode the function definition pattern uses the next keyword, and the variable definition uses the FAR keyword by default; function definitions and variable definitions in large mode use the FAR keyword The function definition in huge mode uses the FAR keyword, and the variable definition uses the HUGE keyword by default.

What is the true meaning of the near, far, and huge keywords? These three keywords can only be used to modify functions, global variables, and pointer variables, and for non-pointer-type local variables, these keywords have no practical meaning. When these keywords are used to decorate a function, the meaning of huge is the same as far, which indicates that the function is called by far, that is, the call requires a 32bits call address consisting of a segment value and a segment offset, a jump using far call, and a pre-press stack to save the current CS:IP. Near modifier function, used to indicate that the function is called by the close call method, the call requires only a nearly 16bits of the address, that is, the current CS of the intra-segment offset.

When these three keywords are used to decorate the pointer, the near pointer is essentially an unsigned integer of 16bits, which gives the offset address of the pointed variable within the current data segment, that is to say, when using the near-type pointer addressing, it is actually the following addressing operation: [DS: pointer variable value]. For the far-type pointer variable, you can address any place in the 1MB addressing space, the nature of the far pointer is a 32bits integer, the high 16bits is the segment value, the low 16bits is the intra-segment offset, and in Turbo C when using the far pointer, The high 16bits is placed in the ES register before addressing the following: [ES: Pointer variable low 16bits value]. For pointer variables of type hug, the difference between a far pointer variable is that, when +/++/-/--such as a pointer variable in the range, the range pointer variable holds the segment value unchanged (that is, the height is 16bits), and only the offset within the segment is added and reduced, so that the phenomenon of wrapping occurs in the segment. And the huge-type pointer, in the addition and subtraction operation will automatically change the value of the segment, will not appear in the paragraph wrap. So the feeling is that the huge pointer can address a larger memory space than the far pointer.

For local variables, because it is created on the stack, the near and far keywords will not make any sense, since the variables created on the stack are addressed in one way, using the SP and BP maintenance function stacks, using bp+/-an offset to address function parameter variables and local variables. Such addressing is fixed and unique, and the near and far are not useful, and near and near will have no practical meaning.

It is also easy to understand the meaning of global variables that use near, far, and huge adornments. The global variable of the near type is assigned to the current data segment, which requires only a 16bits offset, while the far global variable needs to give the segment value and offset when addressing it. An array of type huge can use more than 64K of memory space.

Cross-, near, and huge-type pointer variables convert from small-sized pointers to large-sized pointers for automatic type conversions, with the current DS forming a 32bits pointer. From large-sized pointers to small-sized pointers that require forced-type conversions, the result of conversion is to keep only a low 16bits, but this conversion has no practical meaning or is not very useful, and is extremely easy to introduce memory access errors, so you should strictly avoid the use.

It is important to note that the use of the near, far, and huge three keywords also requires close coordination of the memory mode. However, it is not said that the near, far, and huge three keywords cannot be used in tiny mode. The following pointers can also be defined in tiny mode:
char far *pbuf = 0xa0000000;
And I can guarantee that this pointer will work absolutely correctly, as well as the modification of functions and global variables. But how to work correctly, how is the most reasonable way, please think for yourself. The basic principle I also speak very clearly, will not be more to talk about.

Turbo C, I think the most confusing is the memory mode, I also spent a lot of time and energy, through the analysis of the Turbo C assembly code out of the above conclusions. Many friends are very confused about this, so this part of the focus on the next, and share with you. If there is an incorrect point, please enlighten me. The TCC compiles the assembly code by: Tcc-c-mt-s filename.c,-c indicates that compile only,-mx is used to specify the memory mode,-s indicates that the assembly code is generated, and if you are interested in using this method to analyze the compilation Code of the TCC compilation results, Thereby a deeper understanding of the relationship between C and the assembly.

When we are writing, making, and providing our own library files, we also need to be aware of the matching of memory patterns, otherwise there will be problems in linking. A simpler approach is to provide users with a full set of memory-mode library files, which is the ANSI C library of the Turbo C practice, as mentioned earlier. If you do not want to provide multiple memory-mode library files, you can explicitly type-declare each function, global variable, and pointer variable in your program to precisely define the type of each variable.

Memory mode in C language, near far huge keyword

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.