You are on page 1of 14

Lecture 13-14.

Macrodefinitions
The program section to which the name is appropriated and which is assembled every
time when the assembler meets this name in the program text is called as a
macrodefinition (or a macro). The macro begins the directive MACRO and ends the
ENDM. For example: let's describe the macrodefinition hex2ascii translating
hexadecimal number, being in the register AL, in ASCII code of the corresponding
hexadecimal digits:
hex2ascii macro
cmp al, 10
sbb al, 69h
das
endm
Now in the program it is possible to use the word hex2ascii as though it was a command
name, and the assembler will replace each such word with three commands containing in
a macrodefinition. Certainly, it is possible to issue the same section of a code in the form
of procedure and to cause it the CALL command— if procedure is caused more once, this
version of the program will take less place, but the option with a macrodefinition will
begin to be carried out quicker as in it there will be no excess CALL and RET
commands.
Lecture 13-14. Macrodefinitions
However performance speed — not the main advantage of macroes. Unlike procedures of
a macrodefinition can be caused with parameters, therefore, depending on a situation, the
included code will differ, for example a little:
s_mov macro register1, register2
push register1
pop register2
endm
Now it is possible to use S_MOV instead of the MOV command to copy value from one
segment register in another.
The following important means which is using in macrodefinitions — directives of
conditional assembling. For example: let's write a macro which is carrying out
multiplication of the register AX on number, and if a multiplier — two degree,
multiplication is carried out by faster command of shift to the left.
fast_mul macro number
if number eq 2
shl ax, 1; Multiplication on 2
elseif number eq 4
shl ax, 2; Multiplication on 4
Lecture 13-14. Macrodefinitions

elseif number eq 8
shl ax,3 ;Multiplication on 8
… … … ;Similarly up to:
elseif number eq 32768
shl ax,15 ;Multiplication on 32768
else
mov dx,number;Multiplication on a number,
mul dx ;which is not two degree.
endif
endm
Lecture 13-14. Macrodefinitions
Blocks of repetitions
The simplest block of repetitions of REPT (WASM isn't supported) carries out
assembling of a section of the program the set number of times. For example, if it is
required to create the massive of bytes initialized by values from 0 to 0FFh, it can be
done by repetition of a pseudoorder of DB as follows:
hexnumber = 0
hextable label byte; Massive name
rept 256; Block beginning
db hexnumber; These two strings are assembled
hexnumber = hexnumber+1; 256 times.
endm
Blocks of repetitions, just as macrodefinitions, can be caused with parameters. The
directives IRP and IRPC are for this purpose used:
irp parameter, <value1, value2...>
… … …
endm
irpc parameter, string
… … …
endm
Lecture 13-14. Macrodefinitions

Blocks of repetitions
The block described by the directive IRP, will be caused so many times, how many
values are specified in the list (in angular brackets), and at each repetition the label with a
name the parameter, equal to the next value from the list will be defined. For example,
the following block of repetitions will keep the registers AX, BX, CX and DX in the
stack:
irp reg, <ax, bx, cx, dx>
push reg
endm
The directive IRPC (FORC in WASM) describes the block which is carried out so many
times, how many symbols contains the specified string, and at each repetition the label
with the parameter name, equal to the next symbol from a string will be defined. If the
string contains gaps or other symbols, excellent from resolved for labels, it has to be
concluded in angular brackets.
Lecture 13-14. Macrodefinitions

Blocks of repetitions
For example, the following block sets a string in memory, having after each symbol of
the string attribute 0Fh (a white symbol on a black background) so this string can be
copied subsequently directly in video memory.
irpc character, <string of symbols>
db '&character&', 0Fh
endm
In this example ampersands that instead of the character parameter value even in quotes
was substituted it are used. The ampersand — is one of macrooperators — special
operators who act only in macrodefinitions and blocks of repetitions.
Lecture 13-14. Macrodefinitions

Macrooperators
Macrooperator & (ampersand) is necessary in order that the parameter transferred in
quality of an operand to a macrodefinition or the block of repetitions, was replaced with
value before processing of a string by the assembler. So, for example, the following
macro will execute the PUSH EAX command if to cause it as PUSHREG A:
pushreg macro letter
push e&letter&x
endm
Sometimes it is possible to use only one ampersand — at the beginning of the parameter
if there are no different interpretations. For example, if number is transferred, and it is
required to create a set of variables with the names terminating in this number:
irp number,<1,2,3,4>
msg&number db ?
endm
Lecture 13-14. Macrodefinitions

Macrooperators
Macrooperator <> (angular brackets) acts so that all text concluded in these brackets, is
considered as a text string even if it contains gaps or other dividers. As we already saw,
this macrooperator is used by transfer of text strings as parameters for macroes. Other
frequent application of angular brackets — transfer of the list of parameters to the
enclosed macrodefinition or the block of repetitions.
Macrooperator ! (exclamation mark) is used to similarly angular brackets, but affects
only one following symbol, so if this symbol — a comma or an angular bracket, he is all
the same given to a macro as part of parameter.
Macrooperator % (percent) specifies that the text which was behind it is expression and
has to be calculated. Usually it is required to transfer in quality of parameter in a macro
not expression, and its result.
Macrooperator ;; (two semicolons) — the macrocomment beginning. Unlike usual
comments the text of the macrocomment doesn't get to listing and to the program text at
macro substitution. It will save memory when assembling program with a large number
of macrodefinitions.
Lecture 13-14. Macrodefinitions

Other directives used in macrodefinitions


The directive EXITM (WASM isn't supported) carries out a premature exit from a
macrodefinition or the block of repetitions. For example, the following macrodefinition
won't execute any actions, that is won't be expanded in commands of the processor if
parameter isn't specified:
pushreg macro reg
ifb <reg>
exitm
endif
push reg
endm
Lecture 13-14. Macrodefinitions

Other directives used in macrodefinitions


LOCAL label... — lists labels which will be applied in a macrodefinition that there
was no mistake "a label is already defined" when using macro more than once or if the
same label is present at the main text of the program (in WASM the directive LOCAL
allows to use a macro with labels several times, but doesn't allow to apply a label with the
same name in the program). Operand for LOCAL — label or the list of labels which will
be used in a macro.
PURGE macro_name — cancels the macro defined earlier (WASM isn't supported). This
directive is often applied right after INCLUDE which has included in the text of the
program the file with a large number of ready macrodefinitions.
Lecture 13-14. Additional directives

File management
INCLUDE file_name — the directive inserting into the text of the program the text
of the file similarly to command of a preprocessor C #include. It is usually used for
inclusion of the files containing definitions of constants, structures and macroes.

INCLUDELIB file_name — the directive, entering to the linker a name of


additional library or the object file which is required by drawing up this program. For
example, if calls of procedures or the appeal to the data defined in other modules are
used. Use of this directive allows not to enter names of additional libraries by a call of the
linker.
Lecture 13-14. Additional directives

Listing management
Usually assemblers, besides creation of the object file, give opportunity of creation of
listing of the program (TASM /L — for TASM, ml /Fl — for MASM).
Listing — is the file containing the text of the assembly program, a code of each
assembled commnd, the list of certain labels, cross references, segments and groups. The
format of the file of listing differs for different assemblers, and directives of management
also strongly differ with a format of this file, but a little most the general directives after
all are supported by all three assemblers considered in this book.
TITLE text — defines listing heading. The heading appears at the beginning of each
page;
SUBTTL text — defines a listing subtitle. The subtitle appears on the next string after
heading;
PAGE height, width — establishes the sizes of pages of listing (height 10 — 255, width
59 — 255). The directive PAGE without arguments begins the new page, the directive
PAGE + begins new section, and numbering of pages is conducted from the very
beginning;
Lecture 13-14. Additional directives

Listing management
NAME text — defines a name of the program module. If NAME isn't specified, as a
name the first 6 symbols from TITLE are used; if there is neither NAME, nor TITLE, the
file name undertakes a name;
. XLIST — to cancel listing delivery;
. LIST — to allow listing delivery;
. SALL — to forbid listing of macrodefinitions;
. SFCOND — to forbid listing of not assembled conditional blocks;
. LFCOND — to allow listing of not assembled conditional blocks;
. TFCOND — to change a mode of listing of conditional blocks to the opposite;
. CREF — to allow listing of cross references;
. XCREF — to forbid listing of cross references.
Lecture 13-14. Additional directives

Comments
Except the usual comments beginning with a symbol ; (semicolon) and endding at the end
of the string, big blocks of the comments described by the special directive COMMENT
are possible.
comment @
any text
@
Operand for COMMENT — any symbol which will be considered as the comment end.
All section of the text, up to the following emergence of this symbol, is completely
ignored by the assembler.

You might also like