Description
5.1 Procedure (Functions)
This section deals with how modular, reusable code blocks are defined, invoked, and managed within assembly programs.
Defining and Calling Procedures
-
Procedure Declaration and Termination:
-
PROCDirective $\rightarrow$ Procedure Entry Point Definition (e.g.,MyProc PROC NEAR/FAR) -
ENDPDirective 1$\rightarrow$ Procedure Termination Indicator2 -
CALLInstruction $\rightarrow$ Subroutine Call and Return Address Push -
RETInstruction 3$\rightarrow$ Return from Procedure and Stack Pointer Adjustment4
-
-
Segment and Scope Directives:
-
NEARDirective $\rightarrow$ Intra-Segment Procedure Call (Calling within the same code segment; only the Instruction Pointer (IP) is pushed onto the stack). -
FARDirective $\rightarrow$ Inter-Segment Procedure Call (Calling across different code segments; both the Code Segment (CS) and Instruction Pointer (IP) are pushed onto the stack). -
External Procedure Declaration: Using the
EXTRNdirective to reference a procedure defined in a different source file during Modular Programming.
-
Parameter Passing Methods
This is how data is exchanged between the calling program and the procedure.
-
-
Register Passing:
-
General-Purpose Register Exchange (Data passed through registers like
AX,BX,CX,DX).5 Fastest method.
-
-
Stack Passing:
-
Stack-Based Parameter Transfer (Parameters are pushed onto the stack before the
CALLand popped or managed by the procedure).6 -
Stack Frame Management: Techniques involving the Base Pointer (
BP) register to access parameters and local variables relative to the stack frame (e.g., accessing the $N^{th}$ parameter using offsets like $[BP+N]$).
-
-
* **Caller Cleanup:** The calling program is responsible for removing the parameters from the stack after the `RET` instruction returns.
* **Callee Cleanup:** The called procedure is responsible for removing parameters by using an optional operand with the `RET` instruction (e.g., `RET N`).
-
Global Variable Passing:
-
Shared Data Segment Access (Data exchanged via variables defined in the main data segment, accessible by both the caller and the callee).
-
Programming Concepts
-
Stack Preservation: Saving and restoring the state of used registers (e.g., using
PUSHandPOPinstructions) at the beginning and end of a procedure to ensure non-side-effect execution.7 -
Reentrant Procedures: Procedures that can be safely interrupted in the middle of execution and called again (“re-entered”) before the first call has concluded, often requiring local variable storage on the stack.
-
Recursive Procedure Implementation: A procedure that calls itself, relying heavily on stack frame allocation for preserving return addresses and local data for each nested call.
5.2 Macro (Code Substitution)
This section covers the concept of macros, which are essentially text substitution mechanisms handled by the assembler during the assembly phase.
Defining and Expanding Macros
-
Macro Definition Block:
-
MACRODirective 8$\rightarrow$ Macro Header Definition (e.g.,MyMacro MACRO param1, param2)9 -
ENDMDirective $\rightarrow$ End of Macro Definition -
Macro Expansion Process: The assembler’s action of replacing the macro name with the entire block of code defined between
MACROandENDMbefore generating machine code.
-
-
Differences from Procedures:
-
In-Line Code Substitution (Macros insert code at every call site, increasing code size but eliminating procedure call overhead).
-
Assembly-Time Processing (Macros are handled by the assembler; procedures are run-time structures).
-
No Stack Usage Overhead (Since no
CALL/RETinstructions are used).
-
Macros with Parameters
-
Positional Parameter Substitution: Arguments passed during the macro call are substituted based on their position in the macro definition (e.g.,
param1,param2). -
Local Variable Creation: Using the
LOCALdirective inside a macro to ensure that any labels or variables defined within the macro body are unique for each expansion, preventing Multiple Definition Errors. -
Conditional Assembly within Macros: Using directives like
%IF,%ELSE, and%ENDIFinside a macro to allow for flexible code generation based on conditions evaluated during the assembly process.
Advanced Macro Usage
-
Nested Macro Definitions: Defining one macro within the body of another, allowing for complex, layered code generation templates.
-
Repeat/Loop Directives: Using directives like
%REPT(Repeat) or%IRP(Indefinite Repeat) within a macro to generate repetitive code structures efficiently. -
String Manipulation Directives: Advanced directives for manipulating text and strings passed as macro parameters.





Reviews
There are no reviews yet.