macro
Macros Overview
Macros are short blocks of code defined by the programmer, substituted in when invoked.
They resemble procedures visually, but differ significantly in how they operate.
Invoking a macro does not affect the stack or the esp (stack pointer).
During assembly, the assembler replaces macro calls with the macro's content.
Each macro call performs code substitution, not a jump to another code section.
Macro calls are removed in the assembler's preprocessing and substituted before assembly, leading to larger code size when numerous macros are used.
Writing a Macro
Define a macro before use, typically at the beginning of the program.
Directives:
MACRO: Indicates the start of a macro.
ENDM: Indicates the end of a macro.
Input parameters are optional and follow identifier rules, assigned values during invocation.
Syntax:
macroName macro … endmExample:
newLine macro call crlf endm
Passing Parameters into a Macro
Example of a macro with a parameter:
displayChar macro characterIt modifies register al in eax without preserving it unless pushed.
For register preservation:
Example:
updatedDisplayChar macro character push eax mov al, character call writeChar pop eax endm
For multiple parameters, use a framework:
Example:
displaySum macro int1, int2 mov eax, int1 add eax, int2 call writeInt endm
Preserve eax for the sum function using push/pop.
Calling a Macro
Call macros simply by their name:
newLine.For parameters:
updatedDisplayChar 'a'.Ensure that parameters cannot overwrite preserved registers in the macro after popping.
Writing a Loop in a Macro
Incorrectly writing a loop can cause duplication errors of labels, as shown:
Incorrect Example:
LoopExample macro mov ecx, 5 mov al, 'a' L1: call writeChar Loop L1 endm
Solution: Use local labels to prevent duplication.
Corrected Example:
LoopExample macro Local L1 mov ecx, 5 mov al, 'a' L1: call writeChar Loop L1 endm
Differences Between Macros and Procedures
A macro is a named block of assembly statements, while procedures involve jumps.
Macros perform code substitution during preprocessing, expanding code size significantly when used many times.
Defining Macros
Macro definitions must precede usage in programs.
Directives: MACRO initiates and ENDM concludes definitions.
Parameters are optional, must follow identifier rules.
Syntax:
macroname MACRO [parameter-1, parameter-2,...] statement-list ENDM
Macro Example
Simple macro example defined and invoked:
mNewLine MACRO ; define the macro call Crlf ENDM
The assembler substitutes "mNewLine" with "call Crlf" during invocation.
There is no CALL to invoke a macro explicitly.
Working with registers requires push/pop for preservation.
Invoking Macros
When a macro is invoked, arguments match declared parameters, replaced during expansion.
The macro generates assembly source code; arguments are treated as simple text by the preprocessor.
Specific Macro Examples
mPutChar Macro:
mPutchar MACRO char push eax mov al, char call WriteChar pop eax ENDMWrites a character to standard output, noted for saving and restoring eax.
mWriteStr Macro:
mWriteStr MACRO buffer push edx mov edx,OFFSET buffer call WriteString pop edx ENDMFacilitates displaying a string by passing the string name.
mReadStr Macro:
mReadStr MACRO varName push ecx push edx mov edx,OFFSET varName mov ecx,(SIZEOF varName) - 1 call ReadString pop edx pop ecx ENDMWraps around ReadString calls for easy variable handling.
Handling Invalid Arguments
If an argument is invalid or missing, the error is detected during assembly of the expanded code.
Example of Invalid Argument:
mPutchar 1234hwould raise an error.
Summary of Key Concepts
Macros facilitate re-use of code without duplicating it directly in a program.
Defined using MACRO and ENDM, accepting parameters defined as text strings.
Ideal for when small blocks of code need to be invoked repeatedly.