Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

SPCC - 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

SPCC – 3

Macro: A macro is an abbreviation, which stands for some related lines of code. Macros are
useful for the following purposes -
● To simplify and reduce the amount of repetitive coding.
● To reduce errors caused by repetitive coding.
● To make an assembly program more readable.
A macro consists of a name, a set of formal parameters and a body of code. The use of
macro names with a set of actual parameters is replaced by some code generated by its
body. This is called macro expansion.

Macro definition: This is the part of the code where you define the macro. It starts with a
directive that indicates the beginning of the macro definition, followed by the actual
assembly language instructions or statements that constitute the macro.
Macro Call: When you want to use the macro in your program, you insert a macro call at the
appropriate place. A macro call is essentially the name of the macro followed by any
arguments it may require. The assembler then replaces the macro call with the
corresponding macro definition.
Macro parameters: These are of two types –
● Formal parameters - Formal parameters are those that are used in macro definition.
● Actual parameters - Actual parameters are those that are substituted during a macro
call.
There are four ways to perform macro substitution -
● Positional Arguments -
o Parameters are substituted in the order in which they appear in the macro
definition.

● Keyword Arguments -
o Keyword parameters allow parameters to be passed by name rather than by
position.
o This method allows greater flexibility and clarity in macro calls, especially when
dealing with macros that have many parameters.
o In keyword parameters, each parameter is associated with a keyword in both
the macro definition and the macro call.

● Keyword default Parameters -


o Default parameters allow you to specify default values for parameters in the
macro definition.
o If a macro call does not provide a value for a parameter with a default value,
the default value is used.
o This can be useful for making certain parameters optional.
● Mixed Parameters -
o Mixed parameters allow a combination of positional and keyword parameters
in a single macro definition.
o This provides flexibility by allowing some parameters to be passed by position
and others by keyword.

Flow control during macro expansion:


● Sequential Execution - By default, instructions within a macro are executed
sequentially, following the order in which they appear in the macro definition. This
sequential execution ensures that each instruction is performed in the intended
sequence.
● Conditional Execution - Macros can include conditional statements, such as
IF-THEN-ELSE constructs, to enable conditional execution of instructions based on
certain conditions. These conditions can be evaluated at macro expansion time to
determine which instructions should be executed.
● Looping Constructs - Macros may also include looping constructs, such as DO-WHILE
or FOR loops, to repeat a sequence of instructions multiple times. Looping constructs
provide a way to iterate over a set of instructions until a certain condition is met.
The flow control during macro expansion is controlled using a macro expansion counter.
Algorithm of macro expansion:
● MEC = Statement number of first statement following the prototype stmt.
● While statement pointed by MEC is not a MEND statement.
o if a model statement, then:
▪ Expand the statement;

▪ MEC = MEC + 1;
o else (i.e. a pre-processor statement)
▪ MEC = new value specified in the statement.
● Exit from macro expansion.
Nested macro: When one macro is contained within the definition of another macro. The
macro containing the nested call is called the outer macro and the macro is called the inner
macro.

Recursive macro call: When a macro expands itself. Macro definition does not contain any
statement to stop any of the inner expansions and return to complete the outer ones. This
leads to an infinite loop.
Advanced Macro Facilities:
● Change of flow -
o Sequencing Symbol – Defined by putting it in the label field of a statment in the
macro body.
o Statments -
▪ AIF (Assembly If) is an assembly language directive that enables
conditional execution of macro instructions during macro expansion. It is
used to conditionally include or exclude blocks of code within a macro
based on specified conditions.
▪ AGO (Assembly Go) is an assembly language directive that functions as
an unconditional branch within a macro. It is used to redirect the control
flow to a specified label within the macro, altering the sequence of
instructions.
▪ ANOP (Assembly NO Operation) is used to introduce a delay or pad the
instruction stream without performing any meaningful operation.
● Expansion Time Variables – They can only be used during the expansion. They can be
changed using SET. They are of two types -
o Local EVs can only be used in one macro.
o Global EVs work like global variables.
● Advanced Directives -
o Remove Directive – Remove macro definition from macro definition table.
o IRP (Indefinitely Repeat) directive - is used by programmer to direct the
assembler to repeatedly duplicate and assemble the sequence several times,
determined by a compound parameter.
o Repeat directive - This is another facility with assembler to duplicate and
assemble the sequence several times during macro expansion.
Macroprocessor:
● Features -
o Associating macro parameters with their arguments
o Delimiting macro parameters
o Directives related to arguments
o Automatic label generation
o Machine independent features
● Functions -
o Identifies macro definitions and calls in the program
o Determines formal parameters and their values
o Keeps track of the values of expansion time variables and sequencing symbols
declared in a macro
o Handles expansion time control flow and performs expansion of model
statements.
Design of a macroprocessor:
● The microprocessor accepts an assembly program containing definitions and calls and
translates it into an assembly program which does not contain any macro definitions
and calls.
● The program output by the macro preprocesor can be handed over to an assembler
to obtain the target program
Data Structures of a macroprocessor:
● Macro Name Table:
o The macro name table is a data structure that maintains a list of all defined
macros in the program.
o It typically stores information such as the name of the macro and a pointer or
reference to its definition.
● Parameter Name Table:
o The parameter name table is used to store information about the parameters
(both formal and actual) used in macros.
o It may include details such as the names of parameters, their types, positions,
and other attributes.
● EV Name Table (Environment Name Table):
o The EV name table maintains a record of environment variables used within
macros.
o Environment variables can include values that affect the behavior of macros or
other parts of the assembly program.
● SS Name Table (Symbol Set Name Table):
o The SS name table keeps track of symbol sets, which are collections of symbols
used in macros.
o Symbol sets help organize and manage symbols within macros, facilitating their
reuse and maintenance.
● Keyword Parameter Default Table:
o This table stores default values for keyword parameters used in macros.
o It allows macro authors to specify default values for parameters that can be
overridden by the caller if needed.
● Macro Definition Table:
o The macro definition table stores the actual definitions of macros, including
their instructions, parameters, and any associated metadata.
o It provides the necessary information for expanding macros during the
preprocessing stage.
● EV Table (Environment Table):
o The EV table contains the values of environment variables used in macros.
o It tracks the current state of environment variables and their corresponding
values.
● Actual Parameter Table:
o The actual parameter table maintains information about the parameters
passed to macros during invocation.
o It includes details such as the values or references of actual parameters and
their correspondence with formal parameters.
● SS Table (Symbol Set Table):
o The SS table stores information about symbol sets, including the symbols they
contain and their properties.
o It helps manage symbol sets used within macros and ensures proper resolution
of symbols during macro expansion.
Design Overview:
● Identify the macro calls in the program
o Macro name table (MNT) is designed to hold the names of all macro definitions
in a program.A macro name is entered in this table when a macro definition is
processed.
o While processing a statement in the source program, the preprocessor
compares the string found in its mnemonic field with the macro names in MNT.
● Determine the values of formal parameter
o Actual Parameter table (APT) holds the values of formal parameters during
expansion of macro call.
o Each entry in the table is a pair: <formal parameter name>, <value>
o Parameter default table (PDT) is designed to hold default values of keyword
parameters.
● Maintain the values of expansion time variables declared in a macro
o Expansion time variables Table (EVT) each entry in the table is a pair <EV
name>, <value>.
● Organise expansion time control flow
o Macro definition table (MDT) is used to store the body of a macro.
o The flow of control during macro expansion determines when a model
statement is to be visited for expansion
● Determine the values of sequencing symbol
o A sequencing symbol table (SST). Each entry in table is a pair <SS name><MDT
entry #>
● Perform expansion of a model statement.
o MEC points to the MDT entry containing the model statement.
o Values of formal parameters and EV’s are available in APT and EVT respectively.
The model statement defining a SS can be identified from SST.
Two Pass Algorithm:
A macro processor, like an assembler, scans and processes lines of text. The macro
processor algorithm will make two systematic scans or passes over the input text, searching
first for macro definitions and then for macro calls. The assembler cannot process a
reference to a symbol before its definitions, in the same way the macro processor cannot
expand a macro call before having and saving the corresponding macro definition. Thus, we
need two-pass over the input text, one to handle definitions and one to handle macro calls.
The first pass examines every operation code and saves all macro definitions in a macro
definition table (MDT). The first pass will also prepare a Macro Name Table (MNT) along
with MDT. The second pass will then examine every mnemonic and replace each macro
name with the appropriate text from the macro definitions.
Pass 1:

The algorithm tests for each input line. If it is a MACRO pseudo operation, the entire macro
definition that follows is saved in the next available location in the macro definition table.
The first line of the definition is the macro name line. The name is entered in to the macro
name table (MNT), along with a pointer to the first location of the MDT entry of the
definition. When the END pseudo op is encountered all of the macro definitions have been
processed, so control transfers to pass 2. The entire algorithm is explained in pass 1
flowchart.
Pass 2:

When a macro call is found, the call processor sets a pointer, the MDTP, to the
corresponding macro definitions stored in the MDT. The initial value of the MDTP is
obtained from the MDT index field of the MNT entry. The macro expander prepares the ALA
consisting of a table of dummy arguments indicates and corresponding arguments to the
call. Reading proceeds from the MDT, as each successive line is read, the values from the
argument lists are substituted for dummy arguments indicates in the macro definition.
Reading of the MEND line in the MDT terminates expansion of the macro and scanning
continues from the input file. When the END op is encountered, the expanded source is
transferred to the assembler for further processing.
Not done from PPT:
1. Processing of macro definitions. ( The long-ass flowchart)

You might also like