Whether you are writing a main module or a module to be called from another module, you can have both near and far code segments. This section explains how to use near and far code segments and how to use the directives and predefined equates that relate to code segments.
Near Code Segments
The small memory model is often the best choice for assembly programs that are not linked to modules in other languages, especially if you do not need more than 64K of code. This memory model defaults to near (two-byte) addresses for code and data, which makes the program run faster and use less memory.
When you use .MODEL and simplified segment directives, the .CODE directive in your program instructs the assembler to start a code segment. The next segment directive closes the previous segment; the END directive at the end of your program closes remaining segments. The example at the beginning of Section 2.2, “Using Simplified Segment Directives,” shows how to do this.
You can use the predefined symbol @CodeSize to determine whether code pointers default to NEAR or FAR.
Far Code Segments
When you need more than 64K of code, use the medium, large, or huge memory model to create far segments.
The medium, large, and huge memory models use far code addresses by default. In the larger memory models, the assembler creates a different code segment for each module. If you use multiple code segments in the small, compact, or tiny model, the linker combines the .CODE segments for all modules into one segment.
Summary: The assembler assigns names to code segments.
For far code segments, the assembler names each code segment MODNAME_TEXT, in which MODNAME is the name of the module. With near code, the assembler names every code segment _TEXT, causing the linker to concatenate these segments into one. You can override the default name by providing an argument after .CODE. (See Appendix E, “Default Segment Names,” for a complete list of segment names generated by MASM.)
With far code, a single module can contain multiple code segments. The .CODE directive takes an optional text argument that names the segment. For instance, the example below creates two distinct code segments, FIRST_TEXT and SECOND_TEXT.
.CODE FIRST
.
. ; First set of instructions here
.
.CODE SECOND
.
. ; Second set of instructions here
.
Whenever the processor executes a far call or jump, it loads CS with the new segment address. No special action is necessary other than making sure that you use far calls and jumps. See Section 3.1.2, “Near and Far Addresses.”
NOTE:
The ASSUME directive is never necessary when you change code segments. In MASM 6.0, the assembler always assumes that the CS register contains the address of the current code segment or group. See Section 2.3.3 for more information about ASSUME used with segment registers.