0% found this document useful (0 votes)
14 views

ASM Macro Examples (1)

The document explains the concept of macros in Assembly language, detailing their definition, syntax, and usage in both MASM and NASM. It highlights the advantages and disadvantages of macros compared to procedures, including execution speed and code size implications. Additionally, it covers advanced macro techniques such as conditional expansion and the design of a macro processor.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

ASM Macro Examples (1)

The document explains the concept of macros in Assembly language, detailing their definition, syntax, and usage in both MASM and NASM. It highlights the advantages and disadvantages of macros compared to procedures, including execution speed and code size implications. Additionally, it covers advanced macro techniques such as conditional expansion and the design of a macro processor.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

ASM Macro Examples

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).

Syntax in MASM (Microsoft Assembler)

assembly

`NAME MACRO param1, param2


; Macro body (Assembly instructions)
ENDM
`

Syntax in NASM (Netwide Assembler)

assembly

`%macro NAME num_params


; Macro body (Assembly instructions)
%endmacro
`

Example of a Macro in MASM

A macro to print a string in MASM:

assembly

`print MACRO text


mov ah, 09h
lea dx, text
int 21h
ENDM

.code
start:
print myMessage
mov ah, 4Ch
int 21h

myMessage db 'Hello, World!$'

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/29
end start
`

Example of a Macro in NASM

A macro to print a message in NASM:

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

mov eax, 1 ; sys_exit


xor ebx, ebx
int 0x80
`

Key Differences Between Macros and Procedures

Feature Macro Procedure (Function)


Call Overhead None (inline expansion) Has a CALL and RET overhead

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?

Explain macro theoretically and how it works

What is a Macro in Assembly?

A macro in Assembly language is a preprocessor directive that allows a programmer to define a


reusable block of code with a specific name. When the assembler encounters the macro, it expands its
definition inline, replacing the macro call with its actual code before the final machine code is
generated.

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.

How a Macro Works


1. Definition:

A macro is defined using a name and a set of instructions.

It can accept parameters, allowing it to be more flexible.

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.

This happens before the actual assembly process begins.

3. Assembly and Execution:

After expansion, the resulting code is assembled into machine code.


Since macros are expanded inline, they eliminate the overhead of function calls (such as stack
operations).

Macro Processing Stages


1. Preprocessing:

The assembler scans the code for macro definitions.

It stores the macro definitions in memory.


2. Expansion:

Each time a macro is used, its body is copied into the location of the call.

If parameters are used, they are substituted with actual values.

3. Assembly and Linking:


The expanded source code is assembled into object code.

The final executable is generated.

Example to Illustrate How Macros Work


Macro Definition and Expansion

Let's define a simple macro in Assembly that moves a value into the `AX` register.

assembly

`MOVAX MACRO value


MOV AX, value
ENDM
`

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/29
Using the Macro

assembly

`MOVAX 10 ; This expands to: MOV AX, 10


MOVAX 20 ; This expands to: MOV AX, 20
`

How the Assembler Expands It

Before assembling, the macro calls are replaced with:

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:

Macros make the code more structured and easier to understand.


3. No Function Call Overhead:
Unlike procedures (which require stack operations like `CALL` and `RET`), macros are
expanded inline, making execution faster.

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)

Parameter Passing Substituted before assembly Uses stack/registers

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

Conditional Macro Expansion in Assembly

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:

`IF`, `ELSE`, `ENDIF` (MASM)


`%if`, `%else`, `%endif` (NASM)

1. Conditional Macros in MASM


In MASM, you can use `IF` statements inside macros to check parameter values.

Example: Macro with Conditional Expansion

This macro prints either "Positive" or "Negative" based on the input number.

assembly

`CHECK_SIGN MACRO number


IF number GE 0
MOV DX, OFFSET positiveMsg
ELSE
MOV DX, OFFSET negativeMsg
ENDIF
MOV AH, 09h
INT 21h
ENDM

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.

If true, it moves the address of `"Positive Number$"` into `DX`.


Otherwise, it moves `"Negative Number$"`.

2. Conditional Macros in NASM


NASM uses `%if`, `%else`, and `%endif` for conditional macro expansion.

Example: Conditional Macro in NASM

This macro enables debugging messages only if `DEBUG_MODE` is set to `1`.

assembly

`%define DEBUG_MODE 1 ; Set to 0 to disable debugging

%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

If `DEBUG_MODE` is 1, the macro expands and prints a debug message.

If `DEBUG_MODE` is 0, the macro does not expand, reducing unnecessary instructions.

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.

Example: Loop-Based Conditional Macro

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

The macro `REPEAT_MESSAGE` takes two parameters:


`message` (string) and `count` (how many times to print it).

The `%rep %2` directive repeats the instructions based on `count`.

The expanded code will print the message three times.

Conclusion
Conditional macros customize code expansion based on input values or settings, allowing for:

1. Flexible Debugging (enable/disable debug code).

2. Adaptive Code Execution (different behaviors based on parameters).


3. Looping Constructs (repeat blocks of code efficiently).

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 and AGO in Assembly Macros (IBM Assembler - High-Level Macros)

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.

1. AIF (Assembled IF)


AIF is a conditional branching statement used inside macros.
It checks a condition at assembly time and decides whether to branch to a specific label.

Syntax:

assembly

`AIF (condition) label


`

If `condition` is true, the assembler jumps to `label`.

If `condition` is false, execution continues normally.

Example: AIF in a Macro

assembly

`MYMACRO MACRO PARAM


AIF (PARAM EQ 0) SKIP
PRINT 'PARAM is non-zero'
SKIP
MEND
`

How It Works:

If `PARAM == 0`, it jumps to `SKIP`, skipping the `PRINT` statement.


Otherwise, it prints `"PARAM is non-zero"`.

2. AGO (Assembled GO TO)


AGO is an unconditional assembly-time jump.
It always jumps to a specified label during macro expansion.

Syntax:

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 8/29
assembly

`AGO label
`

Unlike `AIF`, it does not check a condition—it always jumps.

Example: AGO in a Macro

assembly

`MYMACRO MACRO PARAM


AIF (PARAM EQ 0) SKIP
PRINT 'This will always execute'
AGO ENDLABEL
SKIP
PRINT 'PARAM is zero'
ENDLABEL
MEND
`

How It Works:

If `PARAM == 0`, execution jumps to SKIP, printing `"PARAM is zero"`.


If `PARAM != 0`, it prints `"This will always execute"` and then skips the `"PARAM is zero"` part by
jumping to `ENDLABEL`.

3. AIF vs. AGO


Directive Type Purpose

AIF Conditional Jumps based on a condition

AGO Unconditional Always jumps

4. Example with Both AIF and AGO


assembly

`CHECKNUM MACRO NUM


AIF (NUM LT 0) NEGATIVE
PRINT 'Number is Positive'
AGO ENDLABEL
NEGATIVE
PRINT 'Number is Negative'
ENDLABEL
MEND
`

Behavior:

If `NUM < 0`, it jumps to `NEGATIVE` and prints `"Number is Negative"`.

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.

AGO is like an unconditional `GOTO`, always jumping to a label.


These directives help control macro expansion dynamically before code execution.

Would you like an example where AIF and AGO are used for looping inside macros?

Macro processor design

Macro Processor Design

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. Functions of a Macro Processor


A macro processor performs the following key tasks:

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.

5. Recursive Macro Processing – Supports nested macro calls.

2. Phases of a Macro Processor


A macro processor operates in two main passes:

Pass 1: Macro Definition Processing

Identify and store macro definitions in a Macro Definition Table (MDT).

Record macro names in a Macro Name Table (MNT) with pointers to their definitions.

Pass 2: Macro Expansion

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.

Expanded code replaces the macro call in the source program.

3. Data Structures Used in a Macro Processor


Data Structure Purpose

Macro Definition Table (MDT) Stores macro body (sequence of instructions).

Macro Name Table (MNT) Stores macro names and their starting addresses in MDT.
Argument Table (APT) Stores actual parameters for substitution during expansion.

Macro Expansion Counter (MEC) Keeps track of recursive macro expansions.

4. Design of a Two-Pass Macro Processor


Pass 1: Macro Definition Processing

1. Scan the source program.

2. If a macro definition (`MACRO ... MEND`) is found:


Store the macro name in the MNT.

Store the macro body in the MDT.

Record parameter names for later substitution.


3. Ignore macro calls in this phase.

Pass 2: Macro Expansion

1. Scan the source program again.

2. If a macro call is found:

Look up the macro in MNT.


Retrieve its body from MDT.

Substitute parameters using APT.

Replace the macro call with expanded code.

5. Example of Macro Processing


Example Source Program with a Macro

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
`

Macro Processor Tables After Pass 1

Macro Name Table (MNT)

Index Macro Name MDT Index


1 INCR 0

Macro Definition Table (MDT)

Index Instruction
0 MOV AX, X

1 ADD AX, 1
2 MOV X, AX

Argument Table (APT) for `INCR VAR`

Parameter Value
X VAR

Expanded Code After Pass 2

assembly

`START:
MOV AX, VAR
ADD AX, 1
MOV VAR, AX
MOV BX, AX
END
`

The macro call `INCR VAR` is replaced with its definition.

`X` is substituted with `VAR` during expansion.

6. Advanced Features in Macro Processors


1. Conditional Expansion (AIF, AGO)

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
`

Expands differently based on the value of `X`.

2. Nested Macros

assembly

`MACRO OUTER
MOV AX, BX
INNER 10
MEND

MACRO INNER Y
ADD AX, Y
MEND
`

When `OUTER` is called, it also expands `INNER`.

7. Single-Pass vs Two-Pass Macro Processors


Feature Single-Pass Processor Two-Pass Processor
Efficiency Faster Slower

Handles Forward References No Yes


Macro Expansion Handling Inline expansion Separate passes

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?

1 pass and 2 pass

1-Pass vs 2-Pass Macro Processor

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.

1. One-Pass Macro Processor


Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 13/29
Definition:

In a one-pass macro processor, definition and expansion occur simultaneously.


The processor expands macros as soon as it encounters them.

It does not store macro definitions separately.

How It Works:

1. Reads the source code line by line.


2. If a macro definition (`MACRO ... MEND`) is encountered:
The macro is stored in memory.

3. If a macro call is found:


It is expanded immediately.
Parameters are replaced in real-time.

4. Continues processing the remaining source code.

Advantages:

✅ Faster Execution – No need for a second pass.


✅ Memory Efficient – No need to store macro definitions separately.
Disadvantages:

❌ 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
`

Expanded Code (Immediately after reading the call)

assembly

`START:
MOV AX, 5
ADD AX, 10

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 14/29
END
`

2. Two-Pass Macro Processor


Definition:

In a two-pass macro processor, macro processing happens in two distinct stages:


Pass 1: Macro definitions are stored.
Pass 2: Macro calls are expanded using the stored definitions.

This allows handling forward references (calls before definitions).

How It Works:

Pass 1: Definition Handling

1. Scans the source program.


2. Stores macro definitions in the Macro Definition Table (MDT).

3. Records macro names in the Macro Name Table (MNT).

Pass 2: Expansion

1. Reads the source program again.


2. Replaces macro calls with the stored definition from MDT.
3. Performs parameter substitution and generates final code.

Advantages:

✅ Handles Forward References – A macro call before its definition works correctly.
✅ Efficient Nested Macro Expansion – Allows macros to call other macros.
Disadvantages:

❌ Slower than One-Pass – Requires scanning the source code twice.


❌ More Memory Usage – Needs tables (MDT, MNT, APT) to store definitions.
Example of Two-Pass Macro Processing

Pass 1: Store Definitions

Macro Name Table (MNT) Macro Definition Table (MDT)


ADDNUM → 0 MOV AX, X

ADD AX, Y

Pass 2: Expand Macros

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 15/29
assembly

`START:
MOV AX, 5
ADD AX, 10
END
`

Comparison Table: 1-Pass vs 2-Pass Macro Processor


Feature One-Pass Processor Two-Pass Processor
Definition & Expansion Done together Done separately

Handles Forward References ❌ No ✅ Yes


Handles Nested Macros ❌ Difficult ✅ Yes
Speed ✅ Faster ❌ Slower
Memory Usage ✅ Low ❌ High
When to Use Which?
One-Pass Macro Processor → Best for simple macro processing without forward references.
Two-Pass Macro Processor → Needed for complex macros, nested macros, and forward
references.

Would you like an implementation of a Two-Pass Macro Processor in Python?

Nested macros in 2 pass

Nested Macros in a Two-Pass Macro Processor

What are Nested Macros?

Nested macros are macros that call another macro inside their body.

This means a macro definition itself contains a macro invocation.

How Are They Processed in a Two-Pass Macro Processor?

Since macro expansion happens in Pass 2, nested macros are handled using recursive expansion
during the second pass.

Processing Nested Macros in a Two-Pass Macro Processor


Pass 1: Macro Definition Storage

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).

Pass 2: Macro Expansion (Recursive Expansion)

1. When a macro is encountered, retrieve its definition from MDT.


2. If another macro call is found inside, it is expanded recursively before replacing the outer macro.

3. The final expanded code is generated after full expansion.

Example of Nested Macros in Two-Pass Macro Processing


Source Code with Nested Macros

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
`

Step-by-Step Processing in a Two-Pass Macro Processor


Pass 1: Store Macro Definitions

Macro Name Table (MNT)

Macro Name MDT Index

INNER 0
OUTER 2

Macro Definition Table (MDT)

MDT Index Instruction


0 MOV AX, X
1 ADD AX, 1

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)

1. OUTER 10 is encountered → Lookup in MNT, found at index 2.

2. Replace INNER Y with its expansion using Y = 10.


3. After full expansion, final code is generated.

Final Expanded Code


assembly

`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?

Data structure in macro

Data Structures Used in a Macro Processor

A macro processor requires efficient data structures to store and manage macro definitions, names,
parameters, and expansions. Below are the key data structures used:

1. Macro Name Table (MNT)


Stores macro names along with a reference to their definitions in the Macro Definition Table
(MDT).

Helps in quick lookup when a macro call is encountered.

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

Index: Entry number in the table.


Macro Name: Name of the macro.

MDT Index: Location of the macro body in the MDT.

2. Macro Definition Table (MDT)


Stores the body of macros (actual instructions inside the macro).
Helps in macro expansion when a macro call is encountered.

Structure of MDT

Index Instruction
0 MOV AX, X

1 ADD AX, Y
2 MEND
3 MUL BX, Z

4 MEND

Index: Line number of macro definition.


Instruction: The actual body of the macro.

3. Argument Table (APT)


Stores actual parameters passed during a macro call.
Used during parameter substitution.

Structure of APT

Parameter Name Value


X 5

Y 10

Parameter Name: The variable used in the macro definition.


Value: The actual argument provided in the macro call.

4. Macro Expansion Table (MET)


Stores expanded macro instructions after substitution.

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

Helps in generating the final expanded code.

5. Macro Expansion Counter (MEC)


Keeps track of how many times a macro has been expanded.
Helps in nested macro handling.

Example: Macro Processing Using These Data Structures


Source Code

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
`

Pass 1: Store Macros in MNT & MDT

Macro Name Table (MNT)

Macro Name MDT Index


ADDNUM 0

MULTIPLY 3

Macro Definition Table (MDT)

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

Pass 2: Macro Expansion Using APT & MET

Argument Table (APT)

Parameter Value
X 5

Y 10
Z 3

Macro Expansion Table (MET)

Instruction
MOV AX, 5

ADD AX, 10
MUL BX, 3

Final Expanded Code

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?

DEFTAB , NAMTAB, ARGTAB

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:

1. NAMTAB (Name Table) – Stores macro names and their locations.


2. DEFTAB (Definition Table) – Stores macro body (instructions).
3. ARGTAB (Argument Table) – Stores actual arguments provided during macro calls.

1. NAMTAB (Name Table)


Purpose: Stores macro names and their starting location in the DEFTAB.
Usage: Helps in quick macro lookup when a macro call is encountered.

Structure of NAMTAB

Macro Name DEFTAB Index


ADDNUM 0
MULTIPLY 3

Macro Name – The name of the macro.


DEFTAB Index – The position in DEFTAB where the macro definition starts.

2. DEFTAB (Definition Table)


Purpose: Stores the actual macro body (instructions).

Usage: Used during macro expansion to replace macro calls with corresponding instructions.

Structure of DEFTAB

Index Instruction

0 MOV AX, &X


1 ADD AX, &Y
2 MEND
3 MUL BX, &Z
4 MEND

Index – Line number in DEFTAB.

Instruction – The macro body with placeholders (`&X`, `&Y`, `&Z`).

3. ARGTAB (Argument Table)


Purpose: Stores actual parameters passed during a macro call.
Usage: Used for parameter substitution when expanding macros.

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

Parameter – The placeholder used in the macro definition.

Value – The actual argument given in the macro call.

Example: Using NAMTAB, DEFTAB, and ARGTAB in a Macro


Processor
Source Code with Macros

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
`

Pass 1: Storing Macros in Tables

NAMTAB (Macro Name Table)

Macro Name DEFTAB Index


ADDNUM 0
MULTIPLY 3

DEFTAB (Macro Definition Table)

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

Macro Call: `ADDNUM 5, 10`

ARGTAB (Argument Table)

Parameter Value
&X 5
&Y 10

Expanded Code:

assembly

`MOV AX, 5
ADD AX, 10
`

Macro Call: `MULTIPLY 3`

ARGTAB (Argument Table)

Parameter Value
&Z 3

Expanded Code:

assembly

`MUL BX, 3
`

Final Expanded Assembly Code

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

Types of Macro Parameters

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.

Example of Positional Parameters

assembly

`MACRO ADDNUM &X, &Y


MOV AX, &X
ADD AX, &Y
MEND

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.

Example of Keyword Parameters

assembly

`MACRO ADDNUM &X=0, &Y=0


MOV AX, &X
ADD AX, &Y
MEND

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.

Example of Default Parameters

assembly

`MACRO ADDNUM &X=0, &Y=0


MOV AX, &X
ADD AX, &Y
MEND

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.

Comparison of Macro Parameter Types


Parameter Type Order Matters? Explicit Naming? Default Values?

Positional ✅ Yes ❌ No ❌ No
Keyword ❌ No ✅ Yes ✅ Yes (optional)
Default ❌ No ✅ Yes ✅ Yes
Conclusion

✅ Positional Parameters → Simple but must follow order.


✅ Keyword Parameters → More flexible but requires explicit names.
✅ Default Parameters → Allows optional values, reducing the need for explicit arguments.
Would you like a Python implementation of a macro processor handling these parameter types?

Nested macros

Nested Macros in Assembly Language

What Are 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:

1. Pass 1 (Definition Storage)


Stores macro definitions in a Macro Definition Table (MDT).

Maintains a Macro Name Table (MNT) for lookups.


2. Pass 2 (Expansion & Recursive Processing)
When a macro call is encountered, the macro processor retrieves its definition from MDT.
If another macro call is found inside, it is expanded first before replacing the outer macro.

Example of Nested Macros


Source Code with Nested Macros

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
`

Step-by-Step Processing of Nested Macros


Pass 1: Store Macro Definitions in Tables

Macro Name Table (MNT)

Macro Name MDT Index


INNER 0

OUTER 2

Macro Definition Table (MDT)

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

Pass 2: Macro Expansion (Recursive Expansion)

1. OUTER 10 is encountered → Lookup in MNT, found at index 2.

2. The first instruction inside OUTER is INNER Y, so lookup INNER.


3. INNER 10 is expanded first.
4. After INNER is expanded, OUTER continues its expansion.

Final Expanded Code


assembly

`START:
MOV AX, 10 ; Expansion of INNER
ADD AX, 1 ; Expansion of INNER
MOV BX, AX ; Expansion of OUTER
END
`

Key Points About Nested Macros


✅ Uses Recursive Expansion – Inner macros must be expanded first.
✅ Requires Two-Pass Processing – The first pass stores macros, and the second expands them.
✅ Uses Tables for Storage & Lookup – MNT, MDT, and APT (Argument Table) are used to manage
nested macro calls.
Would you like a Python implementation of a macro processor that handles nested macros?

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 29/29

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy