Kompiuterių architektūra

Overview

  • A macro is similar to a procedure
  • A macro name represents a group of instructions
  • A procedure is called at execution time
  • control transfers to the procedure
  • returns after executing the procedure's statements
  • A macro is invoked at assembly time
  • Assembler copies macro's statements into the program at the point of invocation

Macro Definition and Invocation

  • A macro is a block of text that has been given a name
  • When TASM encounters the name during assembly, it inserts the block into the program
  • The text may consist of instructions, pseudo-ops, comments, or references to other macros

Syntax of a Macro Definition

MACRO macro_name optional-parameter-list
      statements
ENDM  macro_name
  • Example: define a macro to move a word into a word
MACRO Movw  word1,word2
      push [word2]
      pop  [word1]
ENDM Movw

Using a Macro

  • To use a macro in a program, we invoke it
  • The syntax is:
macro_name argument-list
  • When TASM encounters the macro name, it expands the macro
  • As it copies the macro statement, TASM replaces each of the formal parameters with the corresponding actual argument
  • A macro must be defined before it is used

Example

  • Invoke the macro Movw to move B to A, where B and A are word variables
Movw A,B
  • To expand this macro, TASM copies the macro statements into the program at the position of the call, replacing each occurrence of word1 by A and word2 by B
  • The result is
push [B]
pop  [A]

Caution in Use of Macros

  • Macros are simply text substitution -- unexpected things can happen
  • Example: Movw A,bx expands to
push [bx] ; register-indirect mode!!
pop  [A]
  • Example: Movw A+2,B expands to
push [B]
pop  [A+2] ; this is OK
  • Movw A,ax is illegal -- why?

Macros and .LST files

  • The .lst file shows how the macros are expanded
  • This behavior can be turned off using the %NOMACS assembler directive
  • It can be turned on again using the %MACS directive
  • See mac_demo.asmand mac_demo.lstto see how TASM expands macros

Restoring registers

  • Macros should restore any registers they use (unless they contain output values)
  • Example:
MACRO exchw word1,word2
    push ax
    mov ax,[word1]
    xchg ax,[word2]
    mov [word1],ax
    pop ax
ENDM exchw

Local Labels in Macros

  • A macro with a loop may have one or more labels
  • If regular labels are used, the macro could not be used more than once in the program
  • To use local labels, use the LOCAL pseudo-op:
MACRO MyMacro
LOCAL @@foo,@@bar
. . .
ENDM MyMacro
  • TASM replaces @@foo and @@bar with numeric local labels like @@0001 and @@0002

Examples of Useful Macros

;macro to return to DOS
MACRO DOS_Rtn
    mov ah,4Ch
    int 21h
ENDM DOS_Rtn
;macro to do CR & LF
MACRO NewLine
    mov ah,2
    mov dl,0Dh
    int 21h
    mov dl,0Ah
    int 21h
ENDM NewLine
;place largest of two words in ax
MACRO GetBig word1,word2
LOCAL @@exit
    mov ax,[word1]
    cmp ax,[word2]
    jg  @@exit
    mov ax,[word2]
@@exit:
ENDM GetBig

An Interesting Macro

;display a string macro: e.g., DispStr "Howdy!"
MACRO DispStr string
LOCAL @@start,@@msg
      push ax          ;save registers
      push dx
      push ds
      jmp  @@start     ;string is being stored
@@msg db string,'$'    ; in the code segment
@@start:               ; so, skip over it
      mov  ax,cs
      mov  ds,ax       ;set ds to code segment
      mov  ah,9
      lea  dx,[@@msg]
      int  21h
      pop  ds          ;restore registers
      pop  dx
      pop ax
ENDM  DispStr

Including Macro Libraries

  • The macros that a program invokes may be contained in a separate file
  • This makes it possible to create a macro library
  • If the file's name is macros.asm, the pseudo-op
INCLUDE "macros.asm" would cause TASM to copy all the macro definitions from the file macros.asm into the program at the position of the INCLUDE statement

Repetition Macros

  • The REPT macro may be used to repeat a block of statements
  • The syntax is:
REPT expression statements ENDM
  • When TASM encounters this macro, the statements are repeated the number of times given by the value of the expression

REPT Examples

LABEL A    WORD ;declares a word array A
REPT  5         ;   with 5 zeros
      dw   0
ENDM            ; (easier to do A dw 5 DUP (0) )

MACRO Block N   ;initialize a block of memory to
                ; the first N integers
    k=1         ;use = so we can redefine k
    REPT N
        dw  k
        k=k+1   ;this is done at assembly time, so
    ENDM        ;    no code is generated by k=k+1
ENDM  Block

; invoke Block like this
LABEL A   WORD ; A is a word array containing 1,?,100
BLOCK 100

The IRP Macro

  • Another repetition macro is IRP (indefinite repeat)
IRP dummy,<text> statements ENDM
  • The angle brackets are part of the syntax -- they tell TASM to treat a list as one item
  • Example:
MACRO SaveRegs regs
    IRP d,<regs>
        push d
    ENDM
ENDM SaveRegs
  • To save ax, bx, and cx:
    SaveRegs <ax,bx,cx>
  • expands to
  • push ax
    push bx
    push cx

Conditionals

  • Conditional pseudo-ops may be used
  • The usual form is:
conditional
    statements
ELSE
    statements
ENDIF
  • The ELSE is optional
  • Some conditionals include:
Form              True if

IF exp          exp not 0

IFE exp         exp is 0

IFB <arg>       argis blank

IFNB<arg>       argnot blank

IFDEF sym        symnot defined

IFNDEF sym        symis not defined

IFDIF<s1>,<s2>    s1ands2differ

IFIDN<s1>,<s2>    s1 and s2 identical

IF1             1st pass of assembler

IF2             2nd pass of assembler

A Macro that uses IF

  • This macro defines a block of memory words with N entries, consisting of the first K integers, followed by N-K zeros
  • If the macro is invoked:
LABEL A WORD
Block 10,5

it will initialize an array of 10 words to 1,2,3,4,5,0,0,0,0,0

MACRO Block n,k
    i=1
    REPT n
        IF k+1-i
            dw i
            i=i+1
        ELSE
            dw 0
        ENDIF
    ENDM
ENDM Block

Macros vs. Procedures

  • Assembly Time
    • a program containing macros usually takes longer to assemble
  • Execution Time
    • a program containing macros is usually faster
  • Program Size
    • a program with macros is generally larger, since each macro call causes the code to be inserted
  • Other Considerations
    • macros are suitable for small tasks
    • procedures are more suited for large tasks