ASM Macro Examples (1)
ASM Macro Examples (1)
Macro in asm
In Assembly language, a macro is a sequence of instructions grouped together under a single name,
allowing you to reuse code efficiently. Macros are similar to functions but are expanded inline during
assembly, reducing function call overhead.
Defining a Macro
In x86 Assembly (NASM or MASM), a macro is defined using `MACRO` (MASM) or `%macro` (NASM).
assembly
assembly
assembly
.code
start:
print myMessage
mov ah, 4Ch
int 21h
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/29
end start
`
assembly
`section .data
message db "Hello, World!", 0xA ; String with newline
len equ $-message
section .text
global _start
%macro print 2
mov edx, %2 ; Length
mov ecx, %1 ; Message
mov ebx, 1 ; STDOUT
mov eax, 4 ; sys_write
int 0x80
%endmacro
_start:
print message, len
Execution Speed Faster (no function call overhead) Slower due to CALL/RET
Code Reusability Less efficient (code duplication) More efficient (single function definition)
Parameter Passing Substitution before assembly Uses stack or registers
Macros are useful for small, repetitive tasks, while procedures are better for large code blocks to save
memory.
Would you like more advanced examples, such as parameterized macros or macro loops?
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/29
Macros help reduce repetitive coding and improve code readability, but they can increase the program
size due to inline expansion.
2. Expansion:
When the assembler encounters a macro invocation in the source code, it replaces the macro
call with the actual code defined in the macro.
Each time a macro is used, its body is copied into the location of the call.
Let's define a simple macro in Assembly that moves a value into the `AX` register.
assembly
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/29
Using the Macro
assembly
assembly
`MOV AX, 10
MOV AX, 20
`
The program behaves as if the macro wasn’t there, and the assembler processes the code normally.
Advantages of Macros
1. Eliminates Repetitive Code:
Instead of typing the same set of instructions multiple times, a macro can be defined once
and used multiple times.
2. Improves Readability and Maintainability:
4. Parameterization:
Macros can accept parameters, making them flexible.
Disadvantages of Macros
1. Increases Code Size:
Since macros expand inline, the same code is duplicated wherever the macro is used, leading
to larger executables.
2. No Stack or Register Preservation:
Unlike procedures, macros do not automatically save or restore registers.
3. Difficult Debugging:
Since macros are expanded before assembly, debugging macro-expanded code can be
challenging.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/29
Macros vs Procedures (Functions)
Feature Macros Procedures (Functions)
Execution Speed Faster (no CALL/RET overhead) Slower (CALL/RET overhead)
Code Size Larger (inline expansion) Smaller (reusable function)
Reusability Limited (copies code each time) High (single definition)
Conclusion
Macros are useful for small, frequently used operations that require high speed, while procedures are
better for large code blocks that need to be reused efficiently. Proper use of macros can lead to
optimized assembly programs with better performance but may increase the final executable size.
Would you like an example of advanced macro techniques, like conditional macros or loop macros?
Conditional macro expansion allows macros to include or exclude parts of their code based on
conditions. This is useful for creating flexible macros that behave differently depending on the
arguments passed or the assembler settings.
Most assemblers (MASM, NASM, etc.) support conditional assembly directives such as:
This macro prints either "Positive" or "Negative" based on the input number.
assembly
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/29
.DATA
positiveMsg DB "Positive Number$"
negativeMsg DB "Negative Number$"
.CODE
START:
CHECK_SIGN -5 ; Expands to MOV DX, OFFSET negativeMsg
CHECK_SIGN 10 ; Expands to MOV DX, OFFSET positiveMsg
MOV AH, 4Ch
INT 21h
END START
`
How It Works
The macro `CHECK_SIGN` checks whether the `number` is greater than or equal to zero.
assembly
%macro DEBUG_PRINT 1
%if DEBUG_MODE == 1
mov eax, 4 ; sys_write
mov ebx, 1 ; STDOUT
mov ecx, %1 ; Message
mov edx, 20 ; Length
int 0x80
%endif
%endmacro
section .data
debugMsg db "Debugging Enabled", 0xA
section .text
global _start
_start:
DEBUG_PRINT debugMsg ; Only expands if DEBUG_MODE == 1
mov eax, 1
xor ebx, ebx
int 0x80
`
How It Works
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/29
3. Using Conditional Loops in Macros
In NASM, you can use `%rep` for looping inside macros based on conditions.
assembly
`%macro REPEAT_MESSAGE 2
%assign i 0
%rep %2
mov eax, 4
mov ebx, 1
mov ecx, %1
mov edx, 20
int 0x80
%assign i i+1
%endrep
%endmacro
section .data
message db "Repeated message", 0xA
section .text
global _start
_start:
REPEAT_MESSAGE message, 3 ; Expands to 3 print calls
mov eax, 1
xor ebx, ebx
int 0x80
`
How It Works
Conclusion
Conditional macros customize code expansion based on input values or settings, allowing for:
Would you like more advanced examples, such as macros with nested conditions or dynamic register
selection?
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 7/29
AIF and AGO
AIF (Assembled IF) and AGO (Assembled GO TO) are macro control directives used in IBM Assembly
(HLASM - High-Level Assembler). These directives control macro flow during assembly time, allowing
conditional expansion of macro code.
Syntax:
assembly
assembly
How It Works:
Syntax:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 8/29
assembly
`AGO label
`
assembly
How It Works:
Behavior:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 9/29
If `NUM >= 0`, it prints `"Number is Positive"` and skips `"Number is Negative"` using `AGO
ENDLABEL`.
Conclusion
AIF is like an `IF` statement, checking a condition at assembly time.
Would you like an example where AIF and AGO are used for looping inside macros?
A macro processor is a system software component that processes macros by expanding macro calls
before assembling the actual code. It is a crucial part of assemblers, enabling code reuse, abstraction,
and flexibility.
1. Macro Definition Handling – Stores macro definitions encountered in the source code.
2. Macro Invocation Processing – Expands macro calls by replacing them with actual instructions.
3. Conditional Macro Expansion – Handles conditional structures like `AIF`, `AGO`, `%if`, `%else`.
4. Parameter Substitution – Replaces formal parameters with actual arguments during macro
expansion.
Record macro names in a Macro Name Table (MNT) with pointers to their definitions.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 10/29
When a macro call is encountered, it is expanded using the MDT and parameter substitution.
Macro Name Table (MNT) Stores macro names and their starting addresses in MDT.
Argument Table (APT) Stores actual parameters for substitution during expansion.
assembly
`MACRO INCR X
MOV AX, X
ADD AX, 1
MOV X, AX
MEND
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 11/29
START:
INCR VAR ; Macro call
MOV BX, AX
END
`
Index Instruction
0 MOV AX, X
1 ADD AX, 1
2 MOV X, AX
Parameter Value
X VAR
assembly
`START:
MOV AX, VAR
ADD AX, 1
MOV VAR, AX
MOV BX, AX
END
`
assembly
`MACRO CHECK_SIGN X
AIF (X LT 0) NEG
PRINT "Positive"
AGO END
NEG
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 12/29
PRINT "Negative"
END
MEND
`
2. Nested Macros
assembly
`MACRO OUTER
MOV AX, BX
INNER 10
MEND
MACRO INNER Y
ADD AX, Y
MEND
`
Conclusion
A macro processor improves code reusability, flexibility, and abstraction in assembly programming. It
works by storing macro definitions and expanding calls inline using data structures like MNT, MDT,
and APT. Advanced macro processors support conditional expansion and nested macros, making
assembly language more powerful.
Would you like an implementation of a basic macro processor in Python?
A macro processor can be implemented using either a single-pass or a two-pass approach, depending
on how it handles macro definitions and expansions.
How It Works:
Advantages:
❌ Cannot Handle Forward References – If a macro call appears before its definition, it will result in an
error.
❌ Complex Nested Macro Handling – Difficult to process macros that call other macros.
Example of One-Pass Macro Processing
Source Code
assembly
`MACRO ADDNUM X, Y
MOV AX, X
ADD AX, Y
MEND
START:
ADDNUM 5, 10 ; Macro call is expanded immediately
END
`
assembly
`START:
MOV AX, 5
ADD AX, 10
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 14/29
END
`
How It Works:
Pass 2: Expansion
Advantages:
✅ Handles Forward References – A macro call before its definition works correctly.
✅ Efficient Nested Macro Expansion – Allows macros to call other macros.
Disadvantages:
ADD AX, Y
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 15/29
assembly
`START:
MOV AX, 5
ADD AX, 10
END
`
Nested macros are macros that call another macro inside their body.
Since macro expansion happens in Pass 2, nested macros are handled using recursive expansion
during the second pass.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 16/29
1. Scan the source code and store macro definitions in the Macro Definition Table (MDT).
2. Record macro names and their starting addresses in the Macro Name Table (MNT).
assembly
`MACRO INNER X
MOV AX, X
ADD AX, 1
MEND
MACRO OUTER Y
INNER Y ; Calling the INNER macro inside OUTER
MOV BX, AX
MEND
START:
OUTER 10 ; Calls OUTER, which then calls INNER
END
`
INNER 0
OUTER 2
2 INNER Y
3 MOV BX, AX
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 17/29
Pass 2: Macro Expansion (Recursive Expansion)
`START:
MOV AX, 10 ; Expansion of INNER
ADD AX, 1 ; Expansion of INNER
MOV BX, AX ; Expansion of OUTER
END
`
Key Takeaways
✅ Two-Pass Processing Handles Nested Macros Efficiently – Macros are stored first, then expanded
recursively.
✅ Uses Tables for Lookups – The processor stores macros in MNT & MDT, allowing recursive
expansion.
✅ Handles Parameter Substitution Properly – Ensures arguments from outer macros are substituted
correctly inside nested macros.
Would you like a Python implementation of a Two-Pass Macro Processor supporting nested
macros?
A macro processor requires efficient data structures to store and manage macro definitions, names,
parameters, and expansions. Below are the key data structures used:
Structure of MNT
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 18/29
Index Macro Name MDT Index
1 ADDNUM 0
2 MULTIPLY 3
Structure of MDT
Index Instruction
0 MOV AX, X
1 ADD AX, Y
2 MEND
3 MUL BX, Z
4 MEND
Structure of APT
Y 10
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 19/29
Example of MET after Expansion
Instruction
MOV AX, 5
ADD AX, 10
MUL BX, 3
assembly
`MACRO ADDNUM X, Y
MOV AX, X
ADD AX, Y
MEND
MACRO MULTIPLY Z
MUL BX, Z
MEND
START:
ADDNUM 5, 10
MULTIPLY 3
END
`
MULTIPLY 3
Index Instruction
0 MOV AX, X
1 ADD AX, Y
2 MEND
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 20/29
Index Instruction
3 MUL BX, Z
4 MEND
Parameter Value
X 5
Y 10
Z 3
Instruction
MOV AX, 5
ADD AX, 10
MUL BX, 3
assembly
`START:
MOV AX, 5
ADD AX, 10
MUL BX, 3
END
`
Conclusion
Each data structure plays a key role:
✅ MNT – Stores macro names and locations.
✅ MDT – Stores macro bodies.
✅ APT – Handles parameter substitution.
✅ MET – Stores final expanded macro code.
✅ MEC – Helps with nested macro tracking.
Would you like a Python implementation of a Two-Pass Macro Processor using these data
structures?
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 21/29
In a macro processor, several data structures are used to manage macro definitions, names, and
arguments efficiently. The three key data structures are:
Structure of NAMTAB
Usage: Used during macro expansion to replace macro calls with corresponding instructions.
Structure of DEFTAB
Index Instruction
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 22/29
Structure of ARGTAB
Parameter Value
&X 5
&Y 10
&Z 3
assembly
START:
ADDNUM 5, 10
MULTIPLY 3
END
`
Index Instruction
0 MOV AX, &X
1 ADD AX, &Y
2 MEND
3 MUL BX, &Z
4 MEND
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 23/29
Pass 2: Expanding Macros Using ARGTAB
Parameter Value
&X 5
&Y 10
Expanded Code:
assembly
`MOV AX, 5
ADD AX, 10
`
Parameter Value
&Z 3
Expanded Code:
assembly
`MUL BX, 3
`
assembly
`START:
MOV AX, 5
ADD AX, 10
MUL BX, 3
END
`
Key Takeaways
✅ NAMTAB → Helps in macro lookup.
✅ DEFTAB → Stores macro bodies for expansion.
✅ ARGTAB → Used for parameter substitution.
Would you like a Python implementation of this Two-Pass Macro Processor using NAMTAB, DEFTAB,
and ARGTAB?
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 24/29
Explain macro parameter types
In assembly language macro processing, macro parameters allow passing values to macros dynamically.
There are three main types of macro parameters:
1. Positional Parameters
2. Keyword Parameters
3. Default Parameters
1. Positional Parameters
These parameters are specified in the order they are passed.
The first argument replaces the first parameter, the second replaces the second, and so on.
No explicit names are given when calling the macro.
assembly
START:
ADDNUM 5, 10 ; X = 5, Y = 10
END
`
Expansion
assembly
`START:
MOV AX, 5
ADD AX, 10
END
`
✅ Advantages:
Simple and easy to use.
❌ Disadvantages:
Order must be maintained. Incorrect ordering can lead to errors.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 25/29
2. Keyword Parameters
Arguments are passed using explicit names rather than order.
This allows flexibility in the order of parameters.
assembly
START:
ADDNUM &Y=10, &X=5 ; Order can be changed
END
`
Expansion
assembly
`START:
MOV AX, 5
ADD AX, 10
END
`
✅ Advantages:
Order does not matter, reducing errors.
More readable and self-explanatory.
❌ Disadvantages:
Requires explicit naming, making macro calls longer.
3. Default Parameters
Parameters are assigned default values in case no argument is provided.
Helps in optional arguments.
assembly
START:
ADDNUM 5 ; Only X is given, Y takes default value 0
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 26/29
END
`
Expansion
assembly
`START:
MOV AX, 5
ADD AX, 0
END
`
✅ Advantages:
Useful for optional parameters.
Provides fallback values if no argument is passed.
❌ Disadvantages:
Can lead to unexpected results if the default value is not suitable.
Positional ✅ Yes ❌ No ❌ No
Keyword ❌ No ✅ Yes ✅ Yes (optional)
Default ❌ No ✅ Yes ✅ Yes
Conclusion
Nested macros
Nested macros are macros that call another macro within their body.
This means a macro definition itself contains a macro invocation of another macro.
They require recursive expansion during macro processing.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 27/29
How Are Nested Macros Processed?
A macro processor expands macros in two passes:
assembly
`MACRO INNER X
MOV AX, X
ADD AX, 1
MEND
MACRO OUTER Y
INNER Y ; Calls the INNER macro inside OUTER
MOV BX, AX
MEND
START:
OUTER 10 ; Calls OUTER, which then calls INNER
END
`
OUTER 2
Index Instruction
0 MOV AX, X
1 ADD AX, 1
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 28/29
Index Instruction
2 INNER Y
3 MOV BX, AX
`START:
MOV AX, 10 ; Expansion of INNER
ADD AX, 1 ; Expansion of INNER
MOV BX, AX ; Expansion of OUTER
END
`
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 29/29