Implicit Behaviors of Storage Model Pseudoinstructions
<span>.MODEL</span> pseudoinstructions not only define the storage model of the program but also automatically execute several important low-level settings. These implicit actions greatly simplify the writing of assembly language programs.
Core Implicit Actions
-
**Automatic Creation of Segment Group (DGROUP)**:
- Combines
<span>_DATA</span>,<span>CONST</span>,<span>_BSS</span>, and<span>STACK</span>into a segment group named DGROUP - In the SMALL model, all data segments share the same physical segment
**Automatic Setting of Segment Register Assumptions (ASSUME)**:
ASSUME CS:code_segment_name, DS:DGROUP, SS:DGROUP
Naming Conventions:
- The default name for the code segment in the SMALL model is
<span>_TEXT</span> - The name of the code segment in medium/large models varies according to the model
Code Examples and Analysis
Example 1: Implicit Behavior in the SMALL Model
; Program Name: IMPLICIT.ASM
; Function: Demonstrate implicit actions of .MODEL
.MODEL SMALL ; Implicit: DGROUP GROUP _DATA, CONST, _BSS, STACK
; Implicit: ASSUME CS:_TEXT, DS:DGROUP, SS:DGROUP
.STACK 100H ; Belongs to DGROUP
.DATA ; Belongs to DGROUP (actual segment name _DATA)
MSG DB 'DGROUP Demonstration', '$'
.CODE ; Segment name _TEXT (SMALL model)
MAIN PROC
; No need to explicitly set ASSUME
MOV AX, @DATA ; @DATA = address of DGROUP
MOV DS, AX ; Initialize DS to point to DGROUP
MOV AH, 09H
LEA DX, MSG ; Access data in DGROUP
INT 21H
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
Key Point Explanation:
<span>@DATA</span>is defined by the assembler as the address of DGROUP- All data segments (_DATA, .DATA?, .CONST) share the same segment register
- The code segment defaults to NEAR calls
Changes in Segment Names Across Different Models
Example 2: Segment Names in the MEDIUM Model
.MODEL MEDIUM
.STACK 200H ; Still belongs to DGROUP
.DATA ; _DATA
BUFFER DB 100 DUP(?)
.CODE ; Segment name varies with the model
MAIN PROC FAR ; Requires FAR calls
MOV AX, @DATA
MOV DS, AX
; Cross-segment subroutine call
CALL OTHER_SEGMENT
RET
MAIN ENDP
.CODE OTHER ; Additional code segment
OTHER_SEGMENT PROC FAR
RET
OTHER_SEGMENT ENDP
END MAIN
Explicit ASSUME Usage Scenarios
Although .MODEL provides default ASSUME settings, explicit declarations are still necessary in certain cases:
Example 3: Situations Requiring Explicit ASSUME
.MODEL SMALL
EXTRN FARDATA:BYTE ; External far data
.DATA
LOCAL_VAR DB ?
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX ; DS points to DGROUP
; Situation 1: Accessing far data requires ES
ASSUME ES:NOTHING
MOV AX, SEG FARDATA
MOV ES, AX
ASSUME ES:SEG FARDATA
MOV AL, ES:FARDATA
; Situation 2: Switching DS usage
ASSUME DS:NOTHING
MOV AX, CS ; Make DS point to code segment
MOV DS, AX
ASSUME DS:CODE
; Restore DS
MOV AX, @DATA
MOV DS, AX
ASSUME DS:DGROUP
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
Advanced Applications: Managing Multiple Data Segments
Example 4: Explicit Control of Multiple Data Segments
.MODEL COMPACT
FAR_DATA SEGMENT PARA 'FAR_DATA'
BIG_ARRAY DB 65536 DUP(?)
FAR_DATA ENDS
.DATA
SMALL_VAR DW ?
.CODE
MAIN PROC
; Default DS settings
MOV AX, @DATA
MOV DS, AX
ASSUME DS:DGROUP
; Access far data segment
MOV AX, SEG FAR_DATA
MOV ES, AX
ASSUME ES:SEG FAR_DATA
; Initialize large array
MOV CX, 32768
LEA DI, ES:BIG_ARRAY
MOV AL, 0
REP STOSW
; Switch DS to far data segment
PUSH DS
MOV AX, ES
MOV DS, AX
ASSUME DS:SEG FAR_DATA
; Directly access far data
MOV BIG_ARRAY[0], 1
; Restore DS
POP DS
ASSUME DS:DGROUP
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
Practical Development Recommendations
-
Understand Default Behaviors:
- Be clear about the implicit actions of the .MODEL pseudoinstruction under specific models
- Use
<span>/Sg</span>option to view the segment definitions generated by the assembler
Segment Register Management:
- In the small model, rely on default settings
- Large programs require explicit management of additional segment registers like ES/FS/GS
Debugging Tips:
- Check the actual values of segment registers in the debugger
- Pay attention to the matching of ASSUME declarations with actual settings
Mixed Programming:
- Maintain consistent segment conventions when interacting with high-level languages
- Be aware of segment settings during function calls
Performance Optimization Considerations
-
Advantages of the Small Model:
- Reduces segment register load instructions
- All transfers are of NEAR type
Data Access Optimization:
- Frequently accessed data should be placed within DGROUP
- Large data should use a separate segment and be accessed via ES
Code Organization:
- Keep critical path code within the same code segment
- Separate cold code into other segments
By deeply understanding the implicit actions of the .MODEL pseudoinstruction, one can write concise and efficient assembly programs. Especially in modern development environments, these automated processes can significantly enhance development efficiency.