Concept of Modular Programming Design
Modular programming design refers to the method of breaking down a large program into multiple independent modules with clear tasks, which are written and debugged separately before being linked together to form a complete program. This approach is particularly important in assembly language development as it effectively manages complexity.
Advantages of Modular Programming Design:
- Individual modules are easy to write, debug, and modify
- Allows multiple programmers to work in parallel, speeding up development
- Reusable verified modules
- Improves program readability
- Modifications can be localized, reducing the scope of impact
Steps in Modular Programming Design
- Requirements Analysis: Clearly define the functions the program needs to accomplish
- Module Division: Break down tasks into clearly defined modules and define interfaces
- Module Development: Write each module in the appropriate language and perform initial debugging
- Integration Testing: Connect the modules to form a complete program and debug
- Documentation Organization: Write comprehensive development documentation
Implementation of Modular Design in Assembly Language
In assembly language, modularity is mainly achieved through the following methods:
- **Using Procedures**: Implemented through CALL/RET instructions
- **Using Macros**: Code expansion method
- **Using Include Files**: Manage common definitions
- **Segmentation**: Manage code and data segmentation
Example 1: Simple Procedure Call
; Main Module MAIN.ASM
EXTRN DISPLAY_MSG:NEAR ; Declare external procedure
DATA SEGMENT
MSG1 DB 'Main module calling submodule...', '$'
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX
; Call the procedure of the main module
LEA DX, MSG1
CALL DISPLAY_MSG
; Call the procedure of the submodule
CALL SUB_MODULE_PROC
MOV AH, 4CH
INT 21H
CODE ENDS
END START
; Submodule SUB.ASM
PUBLIC SUB_MODULE_PROC ; Declare public procedure
EXTRN DISPLAY_MSG:NEAR ; Declare external procedure
DATA SEGMENT
MSG2 DB 'This is submodule procedure!', '$'
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
SUB_MODULE_PROC PROC FAR
PUSH AX
PUSH DX
PUSH DS
MOV AX, DATA
MOV DS, AX
LEA DX, MSG2
CALL DISPLAY_MSG
POP DS
POP DX
POP AX
RET
SUB_MODULE_PROC ENDP
CODE ENDS
END
; Public Module LIB.ASM
PUBLIC DISPLAY_MSG ; Declare public procedure
CODE SEGMENT
ASSUME CS:CODE
DISPLAY_MSG PROC NEAR
MOV AH, 09H
INT 21H
RET
DISPLAY_MSG ENDP
CODE ENDS
END
Example 2: Modularization Using Macros
; Macro Definition Module MACROS.ASM
PRINT_STRING MACRO MSG
LEA DX, MSG
MOV AH, 09H
INT 21H
ENDM
READ_INPUT MACRO
MOV AH, 01H
INT 21H
ENDM
; Main Program MAIN.ASM
INCLUDE MACROS.ASM
DATA SEGMENT
PROMPT DB 'Enter a character: $'
RESULT DB 0DH, 0AH, 'You entered: $'
CHAR DB ?, '$'
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX
PRINT_STRING PROMPT ; Use macro
READ_INPUT ; Use macro
MOV CHAR, AL
PRINT_STRING RESULT
PRINT_STRING CHAR
MOV AH, 4CH
INT 21H
CODE ENDS
END START
Module Linking Methods
In a DOS environment, the following steps can be used to link multiple modules:
-
Assemble each module separately:
MASM MAIN.ASM; MASM SUB.ASM; MASM LIB.ASM; -
Link all object files:
LINK MAIN.OBJ+SUB.OBJ+LIB.OBJ; -
Generate the executable file
Considerations for Modular Programming
- Clear Interface Definitions: Clearly define the calling conventions and parameter passing methods between modules
- Register Preservation: Submodules should preserve registers that may be modified
- Stack Balance: Ensure the stack pointer is correct before and after calls
- Naming Conventions: Avoid name conflicts and use meaningful label names
- Comprehensive Documentation: Each module should have detailed functional descriptions and interface documentation
Modular programming design is an effective method for managing complex assembly projects, and reasonable module division can significantly improve development efficiency and code quality.