Macros and Conditional Assembly

Dr. Tim McGuire 
CS 272
Sam Houston State University

Overview

Macro Definition and Invocation

Syntax of a Macro Definition

MACRO macro_name optional-parameter-list
      statements
ENDM  macro_name
MACRO Movw  word1,word2
      push [word2]
      pop  [word1]
ENDM Movw

Using a Macro

macro_name argument-list

Example

Movw A,B push [B]
pop  [A]

Caution in Use of Macros

push [bx] ; register-indirect mode!!
pop  [A]
push [B]
pop  [A+2] ; this is OK

Macros and .LST files

Restoring registers

MACRO exchw word1,word2
    push ax
    mov ax,[word1]
    xchg ax,[word2]
    mov [word1],ax
    pop ax
ENDM exchw

Local Labels in Macros

MACRO MyMacro
LOCAL @@foo,@@bar
. . .
ENDM MyMacro

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

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

REPT expression statements ENDM

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

IRP dummy,<text> statements ENDM MACRO SaveRegs regs
    IRP d,<regs>
        push d
    ENDM
ENDM SaveRegs

Conditionals

conditional
    statements
ELSE
    statements
ENDIF
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

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