Hardware Reference
In-Depth Information
the program. The macro call can also be used to pass parameters (e.g. symbols,
constants, or registers) to the assembler for use during the macro expansion.
As an example of the use of macros, the macro defined in the following code
can be used to exchange the contents of two registers passed to the macro as
parameters reg1 and reg2 :
;
MACRO TO EXCHANGE 16-BIT REGISTER CONTENTS
;
PARAMETERS PASSED:
regl, reg2
;
REGISTERS AFFECTED:
regl, reg2
Swap
MACRO regl,reg2 ; Specify registers to swap
PUSH
regl
; Stack contents of reg1 first
PUSH
reg2
; then stack contents of reg2
POP
regl
; reg1 receives reg2 contents
POP
reg2
; reg2 receives reg1 contents
ENDM
The following line of code shows how the macro call is made:
Swap
AX,CX
; Call the macro
The macro assembler expands the call, replacing it with the code given in its def-
inition. The code generated by the macro assembler (i.e. the macro expansion )
will thus be:
PUSH AX
PUSH CX
POP AX
POP CX
A macro facility can be instrumental in making significant reductions in the
size of source code modules. Furthermore, macros can be nested such that a
macro definition can itself contain references to other macros which, in turn,
can contain references to others. A notable disadvantage of using macros is that
the resulting object code may contain a large number of identical sections of
code and will also occupy more memory space than if an equivalent subroutine
had been used. In practice, therefore, programmers should use macros with
care since there may be occasions where subroutines would be more efficient
even though they may not be quite so easy to implement.
As well as macros, most assemblers also support conditional assembly. This
allows the programmer to specify conditions under which portions of the pro-
gram are either assembled or not assembled. Conditional assembly allows the
programmer to test for specific conditions (using statements such as IF ...
ELSE ... ENDIF ) and use the outcome to control the assembly process.
Assemblers generally make two passes through a source file. During the first
pass, macro calls are expanded and a symbol table is generated. On the second
pass, relocatable code is generated which can be saved in a disk file. Such files
are, however, not directly executable and require the services of a linker in order
to function as self-contained programs.
The Microsoft macro assembler (MASM) provides logical programming syn-
tax which supports the segmented architecture of x86 microprocessors. The
assembler produces relocatable object modules which are linked together using
the Microsoft overlay linker, LINK.
Search WWH ::




Custom Search