1 / 26

Macros

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.

agalia
Download Presentation

Macros

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

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

  6. 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,&REG .* This macro generates the code required to return from a module. * Generate standard linkage to exit a routine. MOVER &REG, &MEM_VAL ADD &REG, &INCR_VAL MOVEM &REG, &MEM_VAL MEND A comment that will appear in the generated code. Code that will actually be generated. MACRO DEFINITION END

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

  8. Macros • Model statements, simply the Assembler code to copy into the program. • Trailer, simply the word MEND ("MacroEND")

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

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

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

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

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

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

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

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

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

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

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

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

  21. Conditional Assembly • When the logical expression is evaluated, a 1 is assigned if it is true, and a 0 if not true.

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

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

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

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

  26. Example – I(5.12)ANS + MOVER AREG=‘0’ + MOVEM AREG,B + MOVEM AREG,B+1 + MOVEM AREG,B+2

More Related