Hardware Reference
In-Depth Information
allocates a single 32-bit word and calls its address WSIZE . The word is initialized
to either 64 or 32, depending on the value of WORDSIZE , in this case, 32. Typi-
cally this construction would be used to write a program that could be assembled
for either 32-bit mode or 64-bit mode. IF and ENDIF , then by changing a single
definition, WORDSIZE , the program can automatically be set to assemble for either
size. Using this approach, it is possible to maintain one source program for multi-
ple (different) target machines, which makes software development and mainte-
nance easier. In many cases, all the machine-dependent definitions, like WORD-
SIZE , are collected into a single file, with different versions for different machines.
By including the right definitions file, the program can be easily recompiled for
different machines.
The COMMENT pseudoinstruction allows the user to change the comment
delimiter to something other than semicolon. PAGE is used to control the listing
the assembler can produce, if requested. END marks the end of the program.
Many other pseudoinstructions exist in MASM. Other x86 assemblers have a
different collection of pseudoinstructions available because they are dictated not by
the underlying architecture, but by the taste of the assembler writer.
7.2 MACROS
Assembly language programmers frequently need to repeat sequences of in-
structions several times within a program. The most obvious way to do so is sim-
ply to write the required instructions wherever they are needed. If a sequence is
long, however, or must be used a large number of times, writing it repeatedly be-
comes tedious.
An alternative approach is to make the sequence into a procedure and call it
wherever it is needed. This strategy has the disadvantage of requiring a procedure
call instruction and a return instruction to be executed every time a sequence is
needed. If the sequences are short—for example, two instructions—but are used
frequently, the procedure call overhead may significantly slow the program down.
Macros provide an easy and efficient solution to the problem of repeatedly needing
the same or nearly the same sequences of instructions.
7.2.1 Macro Definition, Call, and Expansion
A macro definition is a way to give a name to a piece of text. After a macro
has been defined, the programmer can write the macro name instead of the piece of
program. A macro is, in effect, an abbreviation for a piece of text. Figure 7-3(a)
shows an assembly language program for the x86 that exchanges the contents of
the variables p and q twice. These sequences could be defined as macros, as
shown in Fig. 7-3(b). After its definition, every occurrence of SWAP causes it to
be replaced by the four lines:
 
 
 
Search WWH ::




Custom Search