280 likes | 506 Views
Macros. Macro: a sequence of statements that is copied into a program at assembly time. Used for code that is needed multiple places in a program, so that you need not type it in each time.
E N D
Macros • Macro: a sequence of statements that is copied into a program at assembly time. • Used for code that is needed multiple places in a program, so that you need not type it in each time. • You create the macro once, include it in your program once, and then you invoke (call) it whenever you need it.
Macros • Subroutines versus macros: • A subroutine occurs only once in a given program. • If it is needed ten times, control passes to it ten times.
Macros • A macro definition occurs only once in a program, but each time it is invoked, the macro code is copied into the program. • If a macro is needed in ten different places in the program, ten different copies of the code will end up in the program.
Macros • Advantages to using macros • As opposed to subroutines • Less cumbersome sometimes to use a macro than it is to call a subroutine to perform the code. • Examples: a macro call a subroutine or to do entry linkage. • As opposed to typing the code multiple times: • You create and type the code just once. • Less chance of introducing typos. • Common functions are performed in a standardized way.
Macros • Three aspects to a macro: • The macro definition, which occurs once in the program. • The statements to invoke the macro, which can occur multiple times throughout the program. • The code that is generated each time the macro is invoked. • Sample macro definition...
This is the name that will be used to invoke the macro. PARAMETERS Notifies the assembler that a macro definition follows. Macros A macro comment that will not appear in the generated code. MACRO INCR &MEM_VAL, &INCR_VAL,® .* This macro generates the code required to return from a module. * Generate standard linkage to exit a routine. MOVER ®, &MEM_VAL ADD ®, &INCR_VAL MOVEM ®, &MEM_VAL MEND A comment that will appear in the generated code. Code that will actually be generated. MACRO DEFINITION END
Macros • A macro definition consists of the following, in order: • Header, simply the word MACRO. • Prototype statement, which gives the name of the macro and symbolic parameters (parameters being passed in). • Includes an optional label and parameters that will be substituted into the macro code.
Macros • Model statements, simply the Assembler code to copy into the program. • Trailer, simply the word MEND ("MacroEND")
Macros • Two types of comments can be inserted anywhere after the prototype statement: • Comments beginning with ".*" are for the macro definition only and will not be reproduced when the macro is copied into the code. • Comments beginning with "*" will appear in the code wherever the macro is invoked.
Macros • Some things may vary slightly from the way a macro is needed in one area of your program and the way it is needed in another area. • Example: for a macro which is used to call subroutines, the name of the subroutines would vary.
Macros • So, variable symbols are used to pass information to the macro. • When the assembler statements are generated, the variable symbols are replaced by the values we have assigned to them. • Variable symbols function as a parameter list of sorts.
Macros • Variable symbol names: • "&" followed by one letter and up to six more letters and digits. • Several types of variable symbols; we will look at only symbolic parameters right now. • Examples: &LENGTH, &EOT, &TABLE
Macros • Anything with an "&" in front of it in the definition is a symbolic parameter and will be replaced by a value when the code is copied in.
ConditionalAssembly • We know branching instructions that alter the flow of a program during execution — BM, BO, BC, etc. • Macros have special branching instructions that alter the flow of a program’s assembly. • When a macro is called, parameters tell the macro to generate some instructions in the macro while ignoring others. • This is called conditional assembly.
Conditional Assembly • Advantages to eliminating unneeded code before it is ever generated: • The object program is smaller. • The program code is cleaner, more concise, and easier to understand.
Conditional Assembly • Terminology: • Sequence symbol: a label for a branching instruction. • Consists of a period, a letter, and up to 6 more digits and letters: .UP, .END, .LOOP • Optional on macro statements; use them where you need them. • First, a quick look at a conditional assembly macro; we will return to it later after looking at the rules...
Conditional Assembly • Three macro instructions for conditional assembly: • AIF: if statement. • AGO: an unconditional branching instruction. • ANOP: a placemarker that doesn’t actually do anything. • Let’s look at each one...
Conditional Assembly • AIF: "if "; sets up a conditional branch • Format: .seqsymb AIF (logical expression).seqsymb .CHECK AIF ('&WAY' EQ 'UP').UP Sequence symbol: A branching destination for other AIFs Macro IF: Used for branching within the macro Name of incoming parameter to check Relational operator Value to check for Destination sequence symbol: Branches to this.
Conditional Assembly • Logical expressions in AIFs can contain: • 0s (false) and 1s (true) with or without enclosing parentheses. • Arithmetic expressions enclosed within parentheses, including: • self-defining terms: 'abc', '4', etc. • operators: +, -, *, and / • variable symbols like &WAY
Conditional Assembly • Relational operators: EQ = equal GT = greater than NE = not equal LE = less than or equal to LT = less than GE = greater than or equal to. • Symbolic parameters in apostrophes (like '&WAY'), a relational operand, and a character string in apostrophes (like 'UP'), all enclosed in parentheses: ('&WAY' EQ 'DOWN')
Conditional Assembly • When the logical expression is evaluated, a 1 is assigned if it is true, and a 0 if not true.
Conditional Assembly • AGO — "go to"; an unconditional branch • Format: .seqsymb AGO .seqsymb AGO .END Sequence symbol omitted here An unconditional branch statement Branches to the statement with the sequence symbol .END
Conditional Assembly • ANOP — a placemarker that doesn't do anything. • Same idea as DS OH. • Format: .seqsymb ANOP .UP ANOP Sequence symbol used as a branching destination Placemarker: No code generated.
SET Symbols • Set symbols: variables used by the macro, no code generated. • Global set symbols: assigned an initial value by the system the first time the macro is invoked. • Each time the macro is invoked after that, the value from the last invocation is retained. • Put global definition statements right after prototype statement.
Example – I(5.12) MACRO CLEAR &X, &N LCL &M &M SET 0 MOVER AREG, =‘0’ .MORE MOVEM AREG, &X+&M &M SET &M+1 AIF (&M NE &N) .MORE MEND QUE : EXPNSION OF CLEAR B , 3
Example – I(5.12)ANS + MOVER AREG=‘0’ + MOVEM AREG,B + MOVEM AREG,B+1 + MOVEM AREG,B+2