Yeditepe University Department of Computer Engineering ICS 232 Systems Programming and Assembly Language Lecture Notes #4 Spring 2003 MACROPROCESSORS A macro name 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. Macro definitions expanded source code Macroprocessor Source code with macro calls Assembler Figure 1. Macro expansion on a source program. Ex: Macro Definition User program User program (after macro expansion) INITZ MACRO header MOV AX, @data … INITZ … MOV … MOV MOV … MOV DS, AX MOV ES, AX ENDM A template (code) trailer prototype (macro name) macro call 1 AX, @data DS, AX ES, AX Using parameters in macros: Ex: displaying a message. (Assume that: MES2 DB Macro Definition User program PROMPT MACRO MOV LEA INT ENDM … MESSGE ;dummy argument AH, 09H DX, MESSGE 21H ;end of macro ‘Enter the data as mm/dd/yy’) User program (after macro expansion) … PROMPT MES2 … … MES2 DB ‘Enter the data as mm/dd/yy’ MOV AH, 09H LEA DX, MES2 INT 21H … MES2 DB ‘Enter the data as mm/dd/yy’ Conditional assembly: Only part of the macro is copied out into the code. Which part is copied out will be under the control of the parameters in the macro call. CONMB (condition) branch address Ex: Line no. … 8 9 … 15 … Assembly instructions … CONMB (&C>2), 15 … … If condition is true, skip the code up to line 15. If condition is false, expansion continues from line 9. 2 Design for a simple Macroassembler read macro definitions into a memory buffer more source code Y N Assembler get next line N Y write line to the output file macro call call Macro Expander routine Figure 2. A macroprocessor front end for an assembler Find the appropriate macro definition Build a parameter table to relate dummy and real parameters LP = first line in the template Examine the line N conditional assembly call Y substitute real parameters evaluate the Boolean expression write it to the output file N LP = LP + 1 LP = LP + 1 Y more lines N True Y LP = line number in the statement return Figure 3. Macro Expander Routine 3 LP: line pointer Ex: Source program Macro definition … ALPHA A, 2, C … ALPHA Parameter table Dummy parameter ARG1 ARG2 ARG3 MACRO ARG1, ARG2, ARG3 … ENDM Real parameter A 2 C 4 Macro calls within Macros - Use a stack to keep the order of the macro calls Create a parameter table at each macro call If a dummy parameter appears in the real parameter field of a called macro, search the parameter table of the calling macro and replace it with the appropriate real parameter. Find the appropriate macro definition Build a parameter table to relate dummy and real parameters Push LP onto the stack LP = first line in the template Examine the line ? instruction conditional assembly call substitute real parameters macro call macro end evaluate the Boolean expression write it to the output file N True Find the appropriate macro definition Pop from stack and set LP and current param. table pointer Y Build a parameter table LP = LP + 1 LP = LP + 1 LP = line number in the statement Push LP and the pointer to the last parameter table onto the stack LP = first line in the template Y more lines N return LP: line pointer Figure 4. Macro Expander Routine that allows macro calls within macros 5