Microsoft Macro Assembler Reference Manual (1984)
Microsoft Macro Assembler Reference Manual (1984)
40 Macro Assembler
Reference Manual
0
Mirrosnft Cornoration
Information in this document is subject to change without notice and
does not represent a commitment on the part of Microsoft Corpora-
tion . The software described in this document is furnished under a
license agreement or nondisclosure agreement . The software may be
used or copied only in accordance with the terms of the agreement . It
is against the law to copy this software on magnetic tape, disk, or any
other medium for any purpose other than the purchaser's personal
use .
Contents
0
Chapter 1 Introduction
1 .1 Overview 1-1
1 .2 What You Need 1-2
1 .3 Notational Conventions 1-3
0 Chapter 4
4 .1
Types and Declarations
Introduction 4-1
4 .2 Label Declarations 4-1
4 .3 Data Declarations 4-2
5 .1 Introduction 5-1
5 .2 Operands 5-1
5 .3 Expressions 5-10
5 .4 Forward References 5-23
5 .5 Strong Typing for Memory Operands 5-26
Chapter 6 Global Declarations
6 .1 Introduction 6-1
6 .2 PUBLIC Directive 6-2
6 .3 EXTRN Directive 6-3
6 .4 Program Example 6-4
Chapter 7 Conditional Assembly
7 .1
7 .2
Introduction 7-1
IF and IFE Directives 7-2
0
7 .3 IF1 and IF2I)irectives 7-2
7 .4 IFDEF and IFNDEF Directives 7-3
7 .5 IFB and IFNB Directives 7-3
7 .6 IFIDN and IFDIF Directives 7-4
Chapter 8 Macro Directives
8.1 Introduction 8-1
8 .2 MACRO and ENDM Directives 8-2
8 .3 Macro Calls 8-4
8 .4 LOCAL Directive 8-5
8 .5 PURGE Directive 8-6
8 .6 KEPT and ENDM Directives 8-7
8 .7 IRP and ENDM Directives 8-8
8 .8 IHPC and END -,M Directives 8-10
8 .9 EXITM Directive 8-11
8 .10 Substitute Op erator 8-12
8.11 Literal Text . Operator 8-13
8 .12 Literal Character Operator 8-14
8 .13 Expression Operator 8-14
8 .14 Macro Comment 8-15
Chapter 1
• Introduction
1 .1 Overview 1-1
1 .3 Notational Conventions
S
0
Introduction
0 1 .1 Overview
This manual describes the usage and input syntax of the Microsoft®
Macro Assembler, MASM . The assembler produces relocatable object
modules from 8086, 186, or 286 assembly language source files . The re-
locatable object modules can be linked, using the Microsoft Linker,
LINK, to create executable programs for the MS-DOS operating sys-
tem .
This manual does not teach assembly language programming, nor does
it give a detailed description of 8086, 186, and 286 instructions . For
information on these topics, you will need other references .
Microsoft Macro Assembler Reference Manual
0
Introduction
1 .3 Notational Conventions
This manual uses the following notational conventions to define the
0 assembly language syntax :
Convention Meaning
Roman Indicates command or parameter
names that must be typed as
shown . In most cases, upper and
lowercase letters can he freely in-
termixed .
2 .1 Introduction 2-1
2 .3 Integers 2-1
2 .8 Names 2-5
0 2 .1 Introduction
All assembly language programs consist of one or more statements and
comments . A statement or comment is a combination of characters,
numbers, and names . Names and numbers are used to identify values
in instruction statements . Characters are used to form the names or
numbers, or to form character constants .
2 .2 Character Set
MASM recognizes the following character set :
ABCDEFGHIJKLM-1N0PQRSTUVWXYZ
abcdefghijkImnopgrstuvwxyz
0 0123456789
?@-$ : .f1()<>{}
+-/*&%!'-I\= #' ;,
2 .3 Integers
Syntax
digits
digitsB
digitsQ
digitsO
digitsD
digitsH
e The digits are a combination of one or more digits of the specified ra-
dix : B, Q, 0, D, or H . If no radix is given, MASM uses the current de-
fault radix (typically decimal) . The following table lists the digits
that can be used with each radix . (Radix can be either upper or lower-
case .)
You can set the default radix by using the RADIX directive . See the
section, " .RADIX Directive,'' in Chapter 9 .
Examples
Real numbers can be used only with the DD, DQ, and DT direc-
tives . The maximum number of digits in the number and the max-
imum range of exponent values depends on the directive .
0
Examples
digitsR
Encoded real numbers can be used only with the DD, DQ, and DT
0 directives . The maximum number of digits for the encoded numbers
used with DD, DQ, and DT must be 8, 16, and 20 digits, respec-
tively . (If a leading zero is supplied, the number must be 9, 17, or 21
digits .)
Example
3F800000 ; 1 .0 for DD
3FF0000000000000 ; 1 .0 for DQ
j+ 1-1 digits
Packed decimals have the same format as other decimal integers ex-
cept that they can take an optional plus (+) or minus (-) sign and
can be defined only with the DT directive . A packed decimal must
not have more than 18 digits . 0
Examples
characters '
characters "
Examples
'a'
'ab'
• a'
• This is a message ."
'Can '' t find the file .'
• Specified '"value"' not found ."
2 .8 Names
• Syntax
characters . . .
0 Examples
subrout3
Array
main
2 .9 Reserved Names
A reserved name is any name that has a special, predefined meaning
to the assembler . Reserved names include instruction and directive
mnemonics, register names, and predefined group and segment
names . These names can be used only as defined and must not be
redefined .
C) c
2 .10 Statements
0 Syntax
Examples
count db 0
mov ax, bx
assume cs : TEXT, ds :DGROUP
main proc far
2 .11 Comments
Syntax
;text
Examples
Example
comment *
This comment continues until the
next asterisk .
comment +
The assembler ignores the
following MOV statement
+ mov ax, 1
0
0Q
Chapter 3
Program Structure
3 .1 Introduction 3-1
Program Structure
3 .1 Introduction
The Program Structure directives let a programmer define the organi-
zation that a program's code and data will have when loaded into
memory .
3 .2 Source Files
Every assembly language program consists of one or more source files .
A source file is simply a text file that contains statements that define
the program's data and instructions . MASM reads source files and as-
sembles the statements to create "object modules" that can be
prepared for execution by the system linker .
All source files have the same form -- zero or more program "seg-
ments" followed by an END statement . The END statement, re-
quired in every source file, signals the end of the source file . It also pro-
vides a way to define the program entry point or starting address (if
any) . All other statements in a source file are optional .
.8086
DATA segment
STRING db
DATA ends
'Hello . •, 13, 10, '$'
; Program Data Segment
0
CODE segment Program Code Segment
assume cs :CODE, ds :DATA
START : Program Entry Point
mov ax, seg DATA
mov ds, ax
mov dx, offset STRING
mov ah, 9
int 21h
mov ah, 4ch
1nt 21h
CODE ends
1. The .8086 directive, enabling the 8086 instruction set for as-
sembly
Program Structure
.8086
.8087
.186
.286c
.286p
.287
The .8086 directive enables assembly of instructions for the 8086 mi-
croprocessor . It also disables assembly of 186 and 286 instructions .
Similarly, the .8087 directive enables assembly of instructions for
the 8087 floating point coprocessor and disables assembly of 287 in-
structions . Since MASM assembles 8086 and 8087 instructions by
default, the .8086 and .8087 directives are not required if the source
files contain 8086 and 8087 instructions only .
The .186 directive enables assembly of instructions for the 186 mi-
croprocessor . This directive should be used for programs that will
be executed by an 186 microprocessor .
The .287 directive enables assembly of instructions for the 287 float-
ing point coprocessor . This directive should be used with programs
0
that have floating point instructions and will be executed by a 286
microprocessor .
Even though a source file may contain the .8087 or .287 directive,
MASM also requires the /r or /e option in the MASM command line
to define how to assemble floating point instructions . The /r option
directs the assembler to generate the actual instruction code for the
floating point instruction . The /e option directs it to generate a
software interrupt code to a floating point emulator routine .
The name defines the name of the segment . This name can be
unique or be the same name given to other segments in the program .
Segments with identical names are treated as the same segment .
The optional align, combine, and class define program loading in-
structions that are to be used by the linker when forming the exe-
cutable program . These options are described later .
Program Structure
Example
0 rot
_ main endp
SAMPLE TEXT ends
Note
The optional align defines the alignment of the given segment . The
alignment defines the range of memory addresses from which a
starting address for the segment can be selected . It can be any one
0
of the following :
PUBLIC
Concatenates all segments having the same name
and forms a single, contiguous segment . All in-
struction and data addresses in the new segment
0
are relative to a single segment register, and all
offsets are adjusted to represent the distance
from the beginning of the new segment .
STACK
Concatenates all segments having the same name
and forms a single, contiguous segment . All ad-
dresses in the new segment are relative to the SS
segment register . The Stack Pointer (SP) regis-
ter is set to an address in the segment .
COMMON
Creates overlapping segments by placing the
start of all segments having the same name at
the same address . The length of the resulting
area is the length of the longest segment . All ad-
dresses in the segments are relative to the same
base address . 0
Program Structure
MEMORY
Places all segments having the same name in the
AT address
Causes all label and variable addresses defined in
the segment to be relative to the given address .
The address can be any valid expression, but
must not contain a forward reference, that is, a
reference to a symbol defined later in the source
f ile . A T segments typically contain no code or
initialized data . Instead, they represent address
templates that can be placed over code or data
already in memory, such as code and data found
in ROM devices . The labels and variables in the
AT segments can then be used to access the fixed
instructions and data .
Note
Example
_TEXT
assume cs : TEXT
segment word public 'CODE' 0
TEXT ends
END expression
The END directive marks the end of the module . The assembler ig-
nores any statements following this directive .
0
The optional expression defines the program entry point . The entry
point defines the address at which program execution is to start . If
the program has more than one module, only one of these modules
can define an entry point . The module with the entry point is called
the "main module ." If no entry point is given, none is assumed .
Examples
end
end main
0
_~L A
Program Structure
3 .6 GROUP Directive
0 Syntax
The GROUP directive associates a group name with one or more seg-
ments, and causes all labels and variables defined in the given seg-
ments to have addresses that are relative to the beginning of the
group instead of to the beginning of the segments in which they are
defined . The seg-name must be the name of a segment defined using
the SEGMENT directive, or a SEG expression . The name must be
unique .
The GROUP directive does not affect the order in which segments of
a group are loaded . Loading order depends on each segment's class,
or on the order the object modules are given to the linker .
Note
A group name must not be used in more than one GROUP direc-
tive in any source file . If several segments within the source file
belong to the same group, all segment names must be given in
the same GROUP directive .
q- 0
Example
DGROUF group
assume
_DATA, _BSS
ds :DGROUP 0
DATA segment word public 'DATA'
_DATA ends
_BSS segment word public 'BSS'
BSS ends
end
3 .7 ASSUME Directive
Syntax 0
ASSUME seg-reg : seg-name ,,,
ASSUME NOTHING
Program Structure
Note
The segment override operator ( :) can be used to override the
current segment register selected by the ASSUME directive .
Examples
assume cs :code
assume cs :cgroup,ds :dgroup,ss :nothing,es :nothing
assume nothing
• 3 .8 ORG Directive
Syntax
ORG expression
Examples
org 120H
org $+ 2
3 .9 EVEN Directive
Syntax
EVEN
0
The EVEN directive aligns the next data or instruction byte on a
word boundary . If the current value of the location counter is odd,
the directive increments the location counter to an even value and
generates one NOP instruction (90h) . If the location counter is al-
ready even, the directive is ignored .
Example
org 0
testl db 1
even
test2 dw 513
The PROC and ENDP directives mark the beginning and end of a
procedure . A procedure is a block of instructions that form a pro-
gram subroutine . Every procedure has a name with which it can be
called .
The name must be a unique name, not previously defined in the pro-
gram . The optional type can be either NEAR or FAR . NEAR is as-
sumed if no type is given . The name has the same attributes as a la-
0
bel and can be used as an operand in a jump, call, or loop instruc-
tion .
Program Structure
Example
9 19
S
Chapter 4
Types and Declarations
4 .1 Introduction 4-1
0 4 .1 Introduction
This chapter explains how to generate data for a program, how to de-
clare labels, variables, and other symbols that refer to instruction and
data locations, and how to define types that can be used to generate
data blocks that contain multiple fields, such as structures and
records .
4 .2 Label Declarations
Label declarations create ''labels ." A label is simply a name that
represents the address of a given instruction . Labels can be used in
JMP, CALL, and other execution control instructions to direct pro-
gram execution to the associated instruction .
Examples
start :
loop : inc 4 [bp]
4 .2 .2 Procedure Labels
Syntax
The PROC directive creates a label name and sets its type to NEAR
or FAR . The label then represents the address of the following in-
struction and can be used in JMP, CALL, or LOOP instruction to
direct execution control to the given instruction .
NEAR labels can be used with JMP, CALL, and LOOP instruction
in the enclosing segment only . FAR labels can be used in any seg-
ment of the program .
4 .3 Data Declarations
DB Data Byte
DW Data Word
DD Data Doubleword
DQ Data Quadword
DT Data Ten-byte Word
0
Types and Declarations
4 .3 .1 DB Directive
0 Syntax
(name DB initial-value
integer db 16
string db 'ab'
message db "Enter your name : "
constantexp db 4 * 3
empty db ?
multiple db 1, 2, 3,
duplicate db 10 dup(?)
high byte db 255
4 .3 .2 DW Directive
Syntax
[name DW initial-value,,,
String constants must not exceed two characters in length . The last
(or only) character in the string is placed in the low-order byte, and
either zero or the first character is placed in the high-order byte .
Examples
integer dw 16728
character dw 'a'
string dw 'bc'
constantexp dw 4 * 3
addressexp dw string
empty dw ?
multiple dw 1, 2, 3, '$'
duplicate
high_word
arrayptr
dw
dw
dw
10 dup(?)
65535
array
0
arrayptr2 dw offset DGROUP :array
4 .3 .3 DD Directive
Syntax
[name] DD initial-value
String constants must not exceed two characters in length . The last
(or only) character in the string is placed in the low-order byte, and
the first character (if there are two in the string) is placed in the
0 next byte . Zeroes are placed in all remaining bytes .
Examples
integer dd 16728
character dd a'
string dd 'bc'
real dd 1 .5
encodedreal dd 3f000000R
constantexp dd 4 * 3
addsegexp dd real
empty dd ?
multiple dd 1, 2, 3,
duplicate dd 10 dup(?)
high_dcuble dd 4294967295
4 .3 .4 DQ Directive
Syntax
[name] DQ initial-value
Examples
integer
character
string
dq
dq
dq
16728
a'
'bc'
0
real dq 1 .5
encodedreal dq 3f00000000000000R
constantexp dq 4 * 3
empty dq ?
multiple dq 1, 2, 3, '$'
duplicate dq 10 dup(?)
high_ quad dq 18446744073709551615
4 .3 .5 DT Directive
Syntax
[name] DT initial-value
String constants must not exceed two characters in length . The last
(or only) character in the string is placed in the low-order byte, and
the first character (if there are two in the string) is placed in the
next byte . Zeroes are placed in all remaining bytes .
Note
Examples
0 packeddecimaldt
integer
character
dt
dt
1234567890
16728D
'a'
string dt 'bc'
real dt 1 .5
encodedreal dt 3f00000000000000000OR
empty dt v
multiple dt 1 2 3 $.
duplicate dt 10 dup(?)
high_tbyte dt 1208925819614629174706175D
4 .3 .8 DUP Operator
Syntax
0 The Dt'P operator is a special operator that can be used with the
Data Declaration and other directives to specify multiple oc-
currences of one or more initial values . The count defines the
number of times to repeat the initial-value . An initial value can be
any expression that evalutes to an integer value, a character con-
stant, or another DUP operator . If more than one initial value is
given, the values must be separated by commas () . DUP operators
can be nested up to 17 levels .
Examples
DW 20 DUP( 1,2,3,4 )
This example generates 80 words of data . The first four words have
the values 1, 2, 3, and 4, respectively . This pattern is duplicated for
the remaining words .
This example generates 125 bytes of data, each byte having the
value 1 . 0
DD 14 DUP (? )
4 .4 Symbol Declarations
The Symbol Declaration directives let a programmer create and use
symbols . A symbol is a descriptive name that represents a number,
text, an instruction, or an address . Symbols make programs easier to
read and maintain by letting descriptive names represent values . A
symbol can be used anywhere its corresponding value is allowed .
Assign Absolutes
EQU Equate Absolutes, Aliases, or Text Symbols
LABEL Instruction or Data Labels
4 .4 .1 = Directive
Syntax
name = expression
Examples
0
integer 16728
'ab'
string
constantexp 3 * 4
addressexp string
4 .4 .2 EQU Directive
Syntax
The name must be a unique name, not previously defined . The ex-
pression can be an integer, a string constant, a real number, an en-
coded real number, an instruction mnemonic, a constant expression,
or an address expression . Expressions that evaluate to integer
values in the range 0 to 65,535 create absolute symbols and cause
the assembler to replace the name with a value . All other expres-
sions cause the assembler to replace the name with text .
Examples
integer
real
constantexp
equ
equ
equ
16728
3 .14159
3 * 4
;
;
;
replaced with value
replaced with text
replaced with value
0
memoryop equ [bpl ;replaced with text
mnemonic equ mov ; replaced with text
addressexp equ real replaced with text
string equ 'Type Enter' replaced with text
4 .4 .3 LABEL Directive
Syntax
The name must be unique and not previously defined . The type can
be any one of the following :
BYTE
WORD
DWORD
QWORD
TBYTE
NEAR
FAR
Examples
4 .5 Type Declarations
Syntax
name STRUC
0 field-definitions
name ENDS
The STRUC and ENDS directives mark the beginning and end of a
type definition for a structure . Structure type definitions define the
name of a structure type and the number, type, and default value of
the fields contained in the type . Once defined, structure types may
be used to declare structure variables .
The name defines the new name of the structure type . It must be
unique . The field-definitions define the structure's fields . Any
number of field definitions can be given . The definitions must have
the form
[name] DB default-value,,,
[name] DW default-value,,,
[name] DD default-value,,,
[name] DQ default-value,,,
[name] DT default-value,,,
The optional name defines the field name, the DB, DW, DD, DQ,
and DT directives define the size of each field, and default-value de-
Microsoft Macro Assembler Reference Manual
Example
table strut
count db 10
value dw 10 DUP(?)
name db 'font3'
table ends
In this example, the fields are ''count", "value", and ''name'' . The
"count" field is a single byte value initialized to 10 ; "value" is an
is
array of 10 uninitialized word values ; and "name" is a character ar-
ray of 5 bytes initialized to ''font3 ." The field names ''count,"
"value," and "name" have the offset values 0, 1, and 21, respective-
ly .
4 .5 .2 RECORD Directive
Syntax
If =exp is given, it defines the initial value for the field . If the field
is at least 7 bits wide, you can use an ASCII character for exp . The
exp must not contain a forward reference to any symbol .
In all cases, the first field you declare goes into the most significant
bits of the record . Successively declared fields are placed in the
succeeding bits to the right . If the fields you declare do not total ex-
actly 8 bits or exactly 16 bits, the entire record is shifted right so
that the last bit of the last field is the lowest bit of the record .
Unused bits will be in the high end of the record .
Examples
This example creates a record type "item" having two fields : "char"
and "weight ." These values are initialized to the letter Q and the
number 2, respectively .
4 .6 .1 Structure Declarations
Syntax
Note
You cannot initialize any structure field that has multiple
values if this field was given a default initial value when the
structure was defined .
Examples
0 T his example creates a structure variable named "struct2 ." Its type
is also "table .'' The initial value for the first field is set to zero . The
default values defined by the structure type are used for the remain-
ing two fields .
4 .8 .2 Record Declarations
Syntax
The name is optional . If not given, MASM allocates space for the
record, but does not create a variable that you can use to access the
record .
Examples
This example creates a record variable named "passkey ." Its type is
"encode .'' The initial values for the first two fields are the default
values defined by the record type . The initial value for the third
field is 7 .
0
Chapter 5
Operands and Expressions
5 .1 Introduction 5-1
5 .2 Operands 5-1
5 .2 .1 Constant Operands 5-2
5 .2 .2 Direct Memory Operands 5-2
5 .2 .3 Relocatable Operands 5-3
5 .2 .4 Location Counter 5-3
5 .2 .5 Register Operands 5-4
5 .2 .6 Based Operands 5-5
5 .2 .7 Indexed Operands 5-6
5 .2 .8 Based Indexed Operands 5-7
0 5 .2 .9 Structure Operands 5-8
5 .2 .10 Record Operands 5-9
5 .2 .11 Record Field Operands 5-9
0
Operands and Expressions
0 5 .1 Introduction
This chapter describes the syntax and meaning of operands and ex-
pressions used in assembly language statements and directives .
Operands represent values, registers, or memory locations to be acted
on by instructions or directives . Expressions are combinations of
operands and arithmetic, logical, bitwise, and attribute operators . An
expression evaluates to a value or memory location to be acted on by
an instruction or directive .
5 .2 Operands
An operand is a constant, label, variable, or other symbol that is used
in an instruction or directive to represent a value, register, or memory
location to be acted on .
Constant
Direct Memory
Relocatable
Location Counter
Register
Based
Indexed
Based Indexed
Structure
Record
Record Field
5 .2 .1 Constant Operands
Syntax
Examples
mov ax, 9
mov al, 'c'
mov bx, 65535/3
mov cx, count
Syntax is
segment : offset
Examples
5 .2 .3 Relocatable Operands
• Syntax
symbol
Examples
call main
mov bx, local
mov bx, offset DGROUP :table
5 .2 .4 Location Counter
Example
target equ $
mov ax, 1
imp target
5 .2 .5 Register Operands
Syntax
reg-name
ax ah al bx bh bl
ex ch cl dx dh dl
cs ds ss es sp by
di si
The ax, bx, cx, and dx registers are 16-bit general purpose regis-
ters . They can be used for any data or numeric manipulation . The
ah, bh, ch, dh registers represent the high 8-bits of the correspond-
ing general purpose registers . Similarly, al, bl, cl, and dl represent
the low-order 8-bits of the general purpose registers .
The cs, ds, ss, and es registers are the segment registers . They con-
tain the current segment address of the code, data, stack, and extra
segments, respectively . All instruction and data addresses are rela-
tive to the segment address in one of these registers .
The bx, bp, di, and si registers are 16-bit base and index registers .
These are general purpose registers that are typically used for
pointers to program data .
The 16-bit flag register contains nine 1-bit flags whose positions and
meaning are defined in the following table :
0
5-4
Operands and Expressions
Although no name exists for the 16-bit flag register, the contents of
the register can be accessed using the LAHF, SAHF, PUSHF, and
POPF instructions .
5 .2 .8 Based Operands
Syntax
0 disp(
dispf
by j
bx
The effective address of a based operand is the sum of the disp value
and the contents of the given register . If by is used, the operand's
address is relative to the segment pointed to by the ss register . If
bx is used, the address is relative to the segment pointed to by the
ds register .
displ lbpl
by + disp]
bp1 . disp
by +disp
In each case, the effective address is the sum of disp and the con-
tents of the given register .
Examples
mov
mov
mov
ax,
ax,
ax,
[ by ]
[ bx ]
12 [ bx ]
0
mov ax, fred[ by ]
5 .2 .7 Indexed Operands
Syntax
disp[ si ]
disp( di ]
disp]]di]
di + disp]
d i .disp
di +disp
In each case, the effective address is the sum of disp and the con-
tents of the given register .
Examples
mov ax, [ s1 ]
mov
mov
mov
ax,
ax,
ax,
[ dl ]
12[ dl ]
fred [ si ]
0
0 Syntax
disp by si ]
disp by di ]
disp bx si ]
disp bx di]
disp][bp][di]
by+di+di .•zp]
by+di] . disp
di]+disp+[bp]
In each case, the effective address is the sum of disp and the con-
tents of the given registers .
Examples
mov ax, [ by I [ si ]
mov ax, [ bx + di ]
mov ax, 12[ by + di ]
mov ax, fred[ bx ] [ si ]
5-7
5 .2 .9 Structure Operands
Syntax
variable . field
0
A structure operand rrlpresents the memory address of one member
of a structure . The variable must be the name of a structure or must
be a memory operand that resolves to the address of a structure, and
field must be the name of a field within that structure .
date struc
month dw ?
day dw ?
date
year
ends
dw ?
0
5-8
0 Syntax
Examples
Syntax
record-fieldname
Example
mov dx,recl
mov cl,fleld2
shr dx,el
This example copies 7, the shift count for field2, to cl, then uses the
0
address of ''reel,'' copied to dx, in a shift operation . This operation
adjusts reel so that field2 is now at the lowest bit .
5 .3 Expressions
An expression is a combination of operands and operators that
evaluates to a single value . Operands in expressions can be any of
the operands described in this chapter . The result of an expression
can be a value or a memory location, depending on the types of
operands and operators used .
5 .3 .1 Arithmetic Operators
Syntax
expl * exp2
expl / exp2
expl MOD exp2
expl + exp2
expl exp2
+ exp
- exp
Operator Meaning
* Multiplication .
0 MOD
+
Integer division .
Remainder after division (modulus) .
Addition .
Subtraction .
+ Positive (unary) .
Negative (unary) .
Examples
0 14 * 4
14 / 4
14 MOD 4
;
;
;
equals 56
equals 3
equals 2
14 + 4 ; equals 18
14 - 4 ; equals 10
14 - +4 : equals 10
14 - -4 ; equals 18
alpha + 5 ; add 5 to alpha's offset
alpha - 5 ; subtract 5 from alpha's offset
alpha - beta ; subtract beta's offset from alpha's
Syntax
0 The SIIR and Sill, operators shift the given expression right or left
by count number of bits . Bits shifted off the end of the expression
are lost . If count is greater than or equal to 16, the result is 0 .
Examples
5 .3 .3 Relational Operators
Syntax
expl EQ exp2
expl NE exp2
expl LT exp2
expl LE exp2
expl GT exp2
expl GE exp2
The relational operators compare the expressions expl and exp2 and
return true (OFFFFH) if the given condition is satisfied, or false
(0000H) if it is not . The expressions must resolve to absolute values .
The operators have the following meanings :
0
Operator Condition is satisfied when :
EQ Operands are equal .
NE Operands are not equal .
LT Left operand is less than right .
LE Left operand is less than or equal to right .
GT Left operand is greater than right .
GE Left operand is greater than or equal to right .
Examples
1 EQ 0 ; false
1 NE 0 ; true
1 LT 0 ; false
1 LE 0 ; false
1 GT 0 ; true
1 GE 0 ; true
5 .3 .4 Bitwise Operators
0 Syntax
NOT erp
expl AND exp2
expl OR exp2
expl XOR exp2
Operator Meaning
NOT Inverse .
AND Boolean AND .
OR Boolean OR .
0 Examples
XOR Boolean exclusive OR .
5 .3 .5 Index Operator
Syntax
expression) [ expression2 ]
Examples
Note that the last example is identical to the statement "mov ex,
DGROUP :1 .''
5 .3 .6 PTR Operator
Syntax
BYTE 1
WORD 2
DWORD 4
QWORD 8
TBYTE 10
NEAR OFFFFh
FAR OFFFEh
0 Examples
Syntax
segment-register : expression
segment-name : expression
group-name : expression
Examples
5- 1 5
5 .3 .8 SHORT Operator
Syntax
SHORT label
The SHORT operator sets the type of the given label to SHORT .
Short labels can be used in ''jump'' instructions whenever the dis-
tance from the label to the instruction is not more than 127 bytes .
Instructions using short labels are one byte smaller than identical
instructions using near labels .
Example
5 .3 .9 THIS Operator
Syntax
THIS type
NEAR FAR
BYTE WORD
DWORD QWORD
TBYTE
Examples
5- 1 6
Syntax
11M'11 expression
LOW expression
The HIGH and LOW operators return the high and low 8 bits of the
given expression . The IIIGII operator returns the high 8 bits of the
expression ; the LOW operator returns the low-order 8 bits . The ex-
pression can be any value .
Examples
Syntax
SEG expression
The SEG operator returns the segment value of the given expression .
The expression can be any label, variable, segment name, group
name, or other symbol .
Example
Syntax
OFFSET expression
0
The OFFSET operator returns the offset of the given expression.
The expression can be any label, variable, segment name, or other
symbol . The returned value is the number of bytes between the
item and the beginning of the segment in which it is defined . For a
segment name, the return value is the offset from the start of the
segment to the most recent byte generated for that segment .
Examples
mov
mov
bx, offset subrout3
bx, offset DGROUP :array 0
The returned value is always a relative value that is subject to
change by the linker when the program is actually linked .
Syntax
TYPE expression
Examples
Syntax
.TYPE expression
The TYPE operator returns a byte that defines the mode and scope
of the given expression . If the expression is not valid, TYPE returns
zero .
If both the scope bit and defined bit are zero, the expression is not
valid .
The TYPE operator is typically used with conditional directives,
where an argument may need to be tested to make a decision regard-
ing program flow .
Example
x db 12
z equ .type x
Syntax
LENGTH variable
is
The LENGTH operator returns the number of BYTE, WORD,
DWORD, QWORD, or TBYTE elements in the given variable . The
size of each element depends on the variable's defined type .
Only variables that have been defined using the DUP operator re-
turn values greater than one . The return value is always the
number that precedes the first DUP operator .
Examples
In this example, LENGTH returns 100 . The return value does not
depend on any nested DUP operators .
Syntax
SIZE variable
The SIZE operator returns the total number of bytes allocated for
the given variable . The return value is equal to the return value of
LENGTH times the return value of TYPE .
0
5-20
9 Example
array dv 100 dup(1)
Syntax
The WIDTH operator returns the width (in bits) of the given record
field or record . The record-fieldname must be the name of a record
defined in a field . The record must be the name of a record .
Example
Syntax
Example
Precedence Operators
Highest
0 1 LENGTH, SIZE, WIDTH, MASK
2
3
4
r
5 PTR, OFFSET, SEG, TYPE, THIS
6 HIGH, LOW
7 MOD, SHL, SHR
8 +,-
9 EQ, NE, LT, LE, GT, GE
10 NOT
11 AND
12 OR, XOR
13 SHORT, .TYPE
Lowest
Examples
0 8 / 4 * 2
8 /
8 + 4 * 2
(4 * 2)
equals 4
equals 1
equals 16
(8 + 4) * 2 equals 24
8 EQ 4 AND 2 LT 3 equals 000011 (false)
8 EQ 4 OR 2 LT 3 equals OFFFFH (true)
5 .4 Forward References
Although MASM permits forward references to labels, variable
names, segment names, and other symbols, such references can lead
to assembly errors if not used properly . A forward reference is any
use of a name before it has been formally declared . For example, in
the JMP instruction below, the label "target" is a forward reference .
Imp target
may ax, 0
target :
gister, and uses these assumptions to generate code or data for the
statement . For example, in the JMP instruction above, MASM as-
sumes that "target" is an instruction label having NEAR type . It
generates three bytes of instruction code for the instruction . 0
MASM bases its assumptions on the statement containing the for-
ward reference . Errors can occur when these assumptions are in-
correct . For example, if "target" were really a FAR label and not a
NEAR label, the assumption made by MASM in pass 1 would cause a
phase error . In other words, MASM would generate five bytes of in-
struction code for the JMP instruction in pass 2 but only three in
pass 1 .
Examples
0
mov ax, ss :stacktop
inc data :tlme[1]
add ax, dgroup :_I
Example
r nA
0 type .
Examples
MASM assumes that the label has NEAR type, so PTR need not be
used for NEAR labels . If the label has FAR type, however, and PTR
is not used, a phase error will result .
Example
code segment
If you do not associate a group with the segment name, MASM may
ignore the segment override and use the default segment register for
the variable . This usually results in a phase error in pass 2 .
To avoid this error, the PTR operator must be used to override the
variable's type . The statement
0
Chapter 6
Global Declarations
6 .1 Introduction 6-1
0
S
0
Global Declarations
0 6 .1 Introduction
The Global Declaration directives let a programmer define labels,
variables, and absolute symbols that can he accessed globally, that is,
from all modules in a program . Global declarations transform "local"
symbols (labels, variables, and other symbols that can be used only in
the source files in which they are defined) into "global" symbols that
are available to all other modules .
PUBLIC
EXTRN
6- 1
6 .2 PUBLIC Directive
Syntax
PUBLIC name,,,
Example
0
6-2
Global Declarations
6 .3 EXTRN Directive
• Syntax
BYTE WORD
DWORD QWORD
TBYTE
NEAR FAR
ABS
Although the actual address is not determined until link time, the
assembler may assume a default segment for the external item based
on where the EXTRN directive is placed in the module . If the direc-
tive is placed inside a segment, the external item is assumed to be
relative to that segment . In this case, the item's public declaration
(in some other module) must be in a segment having the same name
and attributes . If the directive is outside all segments, no assump-
tion is made about what segment the item is relative to, and the
item's public declaration can be in any segment in any module . In
either case, the segment override operator ( :) can be used to override
an external variable's or label's default segment .
Example
6 .4 Program Example
The following source files illustrate a program that uses public and
external declarations to access instruction labels . The program con-
sists of two modules, named "startmod" and "printmod ." The
0
"startmod" module is the program's main module . Execution starts
at the instruction labeled "start" in "startmod," and passes to the
instruction labeled ''print" in "printmod" where a DOS system call
is used to print the message "Hello" at the system console . Execu-
tion then returns to the instruction labeled "exit'' in "startmod ."
Startmod Module :
NAME startmod
public start, exit
extrn main :near
data
data
segment word public
ends
'DATA'
0
code segment byte public 'CODE'
assume cs :code, ds :data
start :
mov ax, seg DATA
mov ds, ax
jmp print
exit :
mov ah, 4ch
int 21h
code ends
end start
0
6-4
Global Declarations
Printmod M, dale :
0 NAME printmod
public print
extrn exit :near
end
Before this program can be executed, these source files must be as-
sembled individually, then linked together using the system linker .
S
0
Chapter 7
Conditional Assembly
7 .1 Introduction 7-1
0
Conditional Assembly
0 7 .1 Introduction
The Conditional Assembly directives provide conditional assembly of
blocks of statements within a source file . There are the following con-
ditional directives :
IF
IFE
IF1
IF2
IFDEF
IFNDEF
IFB
IF NB
IFIDN
IFDIF
ELSE
ENDIF
0 The IF directives and the ENDIF and ELSE directives can be used to
enclose the statements to be considered for conditional assembly . The
conditional block takes the form :
IF
statements
ELSE
statements
ENDIF
IF expression
IFE expression
The IF and IFE directives test the value of an expression . The IF direc-
tive grants assembly if expression is non-zero (true) . The IFE directive
grants assembly if expression is 0 (false) . The expression must resolve
to an absolute value and must not contain forward references .
Example
if DEBUG
extrn dump :far
end if
extrn trace :far
extrn breakpoint :far 0
7 .3 IF1 and IF2 Directives
Syntax
IF1
IF2
The IF1 and IF2 directives test the current assembly pass . The IF1
directive grants assembly on pass 1 only . IF2 grants assembly on
pass 2 . The directives take no arguments .
Conditional Assembly
IFDEF name
IFNDEF name
The IFDEF and IFNDEF directives test whether or not the given
name has been defined . The IFDEF directive grants . assembly if
name is a label, variable, or symbol . The IFNDEF directive grants
assembly if name has not yet been defined .
The name can be any valid name . Note that if name is a forward
0 reference, it is considered undefined on pass 1, but defined on pass 2 .
This is a frequent cause of phase errors .
Example
lfndef BUFFER
BUFFER db 10 dup(?)
endif
0 The IFB and IFNB directives test the given arg . The IFB directive
grants assembly if arg is blank . The IFNB directive grants assembly
if arg is not blank . The arg can be any name, number, or expression .
The angle brackets (< >) are required .
Microsoft Macro Assembler Reference Manual
The IFB and IFNB directives are intended to be used in macro defin-
itions . They can be used to control conditional assembly of state-
ments in the macro based on the parameters passed in the macro
call . In such cases, arg should be one of the dummy parameters list-
ed by the MACRO directive .
0
Examples
IFB <X>
This example tests the argument "<X> ." If this is in a macro de-
finition and no parameter was passed for X, the directive would
grant assembly .
The IFIDN and IFDIF directives test argl and arg2. The IFIDN
directive grants assembly if the arguments are identical . The IFDIF
directive grants assembly if the arguments are different . The argu-
ments can be any names, numbers, or expressions . To be identical,
each character in argl must match the corresponding character in
arg2 . The angle brackets (< >) are required .
Examples
This example tests the arguments "<X>" and "<Y> ." If this is
in a macro definition and the parameters passed for X and Y are
identical, the directive grants assembly .
0
Chapter 8
Macro Directives
8 .1 Introduction 8-1
Macro Directives
0 8 .1 Introduction
This chapter explains how to create and use macros in your source
files . There are the following macro directives :
MACRO
LOCAL
PURGE
REPT
IRP
IRPC
EXITM
ENDM
The MACRO directive lets you write a named block of source state-
ments, then use that name in your source file to represent the state-
ments . MASM automatically replaces each occurrence of a macro
name with the statements given in the macro definition . This means
0 you can place a block of statements any where in your source file any
number of times by simply defining it once, then giving the name
where you need it . The LOCAL directive lets you define unique labels
for a macro, and the PURGE directive lets you control the macros you
define .
The REPT, IRP, and IRPC directives also let you create contiguous
blocks of repeated statements . You control the number of times the
statements are repeated . You can repeat them a given number of
times, once for each parameter in a list, or once for each character in a
string .
& Ampersand
;; Double semicolon
! Exclamation mark
Percent sign
The MACRO and ENDM directives create a macro having the given
name and containing the given statements .
The name must be a valid name and must be unique . It is used in the
source file to invoke the macro . The dummy-parameteris a name that
acts as a placeholder for values to be passed to the macro when it is
called . Any number of dummy parameters can be given, but they must
all fit on one line . If you give more than one, you must separate them
with commas (,) . The statements are any valid MASM statements, in-
cluding other MACRO directives . Any number of statements can be
used . The dummy parameter can be used any number of times in these
statements .
Macro definitions can contain calls to other macros . These nested mac-
ro calls are expanded like any other macro call, but only when the
outer macro is called . A macro definition cannot contain a call to it-
self . 0
Macro Directives
Notes
You must be careful when using the word MACRO after the TI-
TLE, StUBTTL, and NAME directives . Since the MACRO direc-
tive overrides these directives, placing the word immediately after
these directives causes MASN1 to begin to create macros named
TITLE, St'BTTL, and NAME . To avoid this problem, you should
alter the word MACRO in some way when using it in a title or
name . For example, add a hyphen to the word, "- MACRO ."
Examples
8 .3 Macro Calls
Syntax
name I actual-parameter,,, I
0
A macro call directs MASM to copy the statements of the macro
name to the point of call and to replace any dummy parameters in
these statements with the corresponding actual-parameters . The
name must be the name of a macro defined earlier in the source file .
The actual-parameter can be any name, number, or other value . Any
number of actual parameters can be given, but they must all fit on
one line . Multiple parameters must be separated with commas,
spaces, or tabs .
MASM replaces the first dummy parameter with the first actual
parameter, the second with the second, and so on . If a macro call
has more actual parameters than dummy parameters, the extra ac-
tual parameters are ignored . If a call has fewer actual parameters,
any remaining dummy parameters are replaced with nothing . This
means MASM removes the dummy parameter name for the macro
statements, but does nothing else .
Examples
ALLOCBLOCK 1,2,3,4,5
ALLOCBLOCK <1,2,3,4,5>
8 .4 LOCAL Directive
0 Syntax
LOCAL dummy-name,,,
The LOCAL directive creates unique names for use in macros . The
dummy-name is a name for a placeholder that is to be replaced by
the unique name when the macro is expanded . At least one dummy-
name is required . If you give more than one, you must separate the
names with commas . A dummy name can be used in any statement
within the macro .
MASM creates a new name for a dummy each time the macro is ex-
panded . The name has the form
??zzxx
Note
Q r
Microsoft Macro Assembler Reference Manual
Example
8 .5 PURGE Directive
Syntax
Macro Directives
Examples
0 PURGE add
This example deletes the macro named ''add'' from the assembler's
memory .
REPT expression
statements
ENDM
Example
X = 0
REPT 10
X = X+1
DB X
ENDM
The IRP and ENDM directives define a block of statements that are
to be repeated once for each parameter in the list enclosed by angle
brackets (< >) . The dummy is a name for a placeholder to be re-
placed by the current parameter . The parameter can be any legal
symbol, string, numeric, or character constant . Any number of
parameters can be given . If you give more than one, you must
separate them with commas . The statements can be any valid as-
sembler statements . The dummy can be used any number of times
in these statements .
Example
IRP X,<1,2,3,4,5,6,7,8,9,10>
DB X
ENDM
0
Macro Directives
Notes
0 If an empty parameter is found in the list, MMASM repeats the
statements once, replacing the dummy with no value . If the en-
tire list is empty, MMASMM skips the repeat block .
alloc MACRO X
IRP Y,<X>
DB Y
ENDM
ENDM
allot <1,2,3,4,5,6,7,8,9,10>
IRP Y,<1,2,3,4,5,6,7,8,9,10>
DB Y
ENDM
That is, the macro removes the brackets from the actual param-
eter before replacing the dummy . This means you must provide
the angle brackets for the parameter list yourself .
Example
IRPC X,0123456789
DB X+1
ENDM
This example repeats the DB directive 10 times, once for each char-
acter in the string "0123156789 ." The resulting statements create 10
bytes of data having the values 1 through 10 .
Macro Directives
8 .9 EXITM Directive
0 Syntax
EXITNI
Example
0 REPT
IFE
times
x-OFFH
EXITM
ELSE
DB x
END IF
x = x+1
ENDM
ENDM
This example defines a macro that creates no more than 255 bytes of
data . The macro contains an IFE directive that checks the expres-
sion "x-OFFh" . When this expression is 0 (x equal to 255), the EX-
ITM directive is processed and expansion of the macro stops .
&dummy-parameter
0
or
dummy-parameter&
Example
errgen MACRO Y, X
error&X db 'Error .tY - &X'
ENDM
In this example, MASM replaces &X with the value of the actual
parameter passed to the macro "errgen ." If the macro is called with
the statement
errgen 1, wait
Note
For complex, nested macros, you can use extra ampersands to
delay the actual replacement of a dummy parameter . In general,
you need to supply as many ampersands as there are levels of
nesting .
Macro Directives
alloc MACRO X
IRP Z,<1,2,3>
0 X&&Z
ENDM
DB
ENDM
Z
alloc VAR
VAR1 DB 1
VAR2 DB 2
VAR3 DB 3
0
8 .11 Literal Text Operator
Syntax
< text>
The literal text operator can also be used to force MASM to treat
special characters such as ; or & literally . For example, the semi-
colon inside angle brackets < ;> becomes a semicolon, not a com-
ment indicator .
MASM removes one set of angle brackets each time the parameter is
used in a macro . When using nested macros, you will need to supply
as many sets of angle brackets as there are levels of nesting .
Microsoft Macro Assembler Reference Manual
!character
text
Example
passes the text literal ''syml + sym2 =" to the dummy parameter
"msg .'' It passes the value 300 (the result of the expression "syml +
sym2") to the dummy "n ."
Macro Directives
0 Syntax
; ;text
The macro comment is any text in a macro definition that does not
need to be copied in the macro expansion . All text following the dou-
ble semicolon ( ; ;) is ignored by the assembler and will appear only in
the macro definition when the source listing is created .
0
S
0
Chapter 9
File Control
9 .1 Introduction 0-1
9 .5 NAME Directive 9- 1
File Control
0 9 .1 Introduction
This chapter describes File Control directives . These directives pro-
vide control of the source, object, and listing files read and created by
MASNI during an assembly .
9 .2 INCLUDE Directive
Syntax
INCLUDE filename
The INCLUDE directive inserts source code from the source file given
by filename into the current source file during assembly . The filename
must name an existing file . A pathname must be given if the file is not
in the current working directory . If the named file is not found,
MASM displays an error message and stops .
0 1
Examples
include entry
include include\record
include \usr\include\as\stdio
9 .3 RADIX Directive
Syntax 0
.RADIX expression
The RADIX directive sets the default input radix for numbers in
the source file . The expression defines whether the numbers are
binary, octal, decimal, hexadecimal, or numbers of some other base .
It must be within the range 2 to 16 . The following lists some com-
mon values :
2 - binary
8 - octal
10 - decimal
16 - hexadecimal
Examples
radix 16
0
radix 2
File Control
0 Notes
The RADIX directive does not affect the DD, DQ, or DT direc-
tives . Numbers entered in the expression of these directives are
always evaluated as decimal unless a numeric suffix is appended
to the value .
The RADIX directive does not affect the optional radix specif-
iers, B and D, used with integers numbers . When B or D ap-
pears at the end of any integer, it is always considered to be a
radix specifier even if the current input radix is 16 . This means
that numbers such as ''Oabcd'' and "234b'' are illegal even when
the input radix is set to 16 . ( "a," "b," and ''c'' are not legal di-
gits for a decimal number ; similarly, "2," ''3,'' and "4" are not
legal for a binary number .)
0 9 .4 %OUT Directive
Syntax
% OUT text
The %OUT directive directs MASM to display the text at the user's
terminal . The directive is useful for displaying messages during
specific points of a long assembly .
9 .5 NAME Directive
Syntax
NAME module-name
Example
name main
9 .6 TITLE Directive
0
Syntax
TITLE text
Example
Note that the first six non-blank characters of the title will be used
as the module name if the module does not contain a NAME direc-
9
tive .
File Control
9 .7 SUBTITLE Directive
• Syntax
SUBTTL text
Examples
0 subttl
9 .8 PAGE Directive
Syntax
The PAGE directive sets the line length and character width of the
program listing, increments section page numbering, or generates a
page break in the listing .
If a length and width are given, PAGE sets the maximum number of
lines per page to length, and the maximum number of characters per
line to width . The length must be in the range 10 to 255 . The de-
fault is 50 . The width must be in the range 60 to 132 . The default is
80 . A width can be given without a length as long as the comma (,)
precedes the width .
Microsoft Macro Assembler Reference Manual
If a plus sign (+) is given, PAGE increments the section number and
resets the page number to 1 . Program listing page numbers have the
form :
section-minor
0
By default, page numbers start at 1-1 .
Examples
PAGE
PAGE 58,60
This example sets the maximum page length to 58 lines, and the
maximum width to 60 characters .
PAGE ,132
0
This example sets the maximum width to 132 characters . The
current page length remains unchanged .
PAGE +
This example increments the current section number and sets the
page number to 1 .
.LIST
.XLIST
The LIST and .XLIST directives control which source program lines
0
are copied to the program listing . The .XLIST directive suppresses
copying of subsequent source lines to the program listing . The LIST
File Control
0 program listing .
Example
.XLIST
;listing suspended here
.LIST
;listing resumes here
.SFCOND
0 .LFCOND
.TFCOND
The TFCONI) directive sets the default mode for listing of condi-
tional blocks . This directive works in conjunction with the -X op-
tion of the assembler . If -X is not given in the NASNI command
line, TFC'OND causes false conditional blocks to be listed by de-
fault . If -X is given, .TFCOND causes false conditional blocks to be
suppressed .
0 7
Microsoft Macro Assembler Reference Manual
Examples
.SFCOND
IF 0
;This block will not be listed .
0
ENDIF
.LFCOND
IF 0
;This block will be listed .
END IF
.LALL
.XALL
.SALL
The LALL, XALL, and SALL directives control the listing of the
statements in macros that have been expanded in the source file .
MASM always lists the full macro definition, but lists macro expan-
0
sions only if the appropriate directive is set .
The .LALL directive causes MASM to list all the source statements
in a macro, including comments preceded by a single semicolon ( ;)
but not those preceded by a double semicolon ( ; ;) . The XALL direc-
tive lists only those source statements that generate code or data, so
comments are ignored .
0
File Control
.CREF
.XCREF name,,,
Example
A .1 Introduction A-1
Instruction Summary
0 A .1 Introduction
MASM is an assembler for the 8086/186/286 family of microproces-
sors, capable of assembling instructions for the 8086, 186, and 286 mi-
croprocessors and the 8087 and 287 floating point coprocessors .
MASM will assemble any program written for an 8086, 186, or 286 mi-
croprocessor environment as long as the program uses the instruction
syntax described in this chapter .
Symbol Meaning
accum accumulator : AX or AL
reg byte or word register
byte : AL, AH, BL, BH, CL, CH, DL, DH
word : AX, BX, CX, DX, SI, DI, BP, SP
segreg segment register : CS, DS, SS, ES
r/m general operand : register, memory address, indexed
operand, based operand, or based indexed operand
immed 8- or 16-bit immediate value : constant or symbol
mem memory operand : label, variable, or symbol
label instruction label
Microsoft Macro Assembler Reference Manual
A .2 8086 Instructions
The following is a complete list of the 8086 instructions . MASM as-
sembles all 8086 instructions by default . 0
Syntax Action
AAA ASCII adjust for addition
AAD ASCII adjust for division
AAM ASCII adjust for multiplication
AAS ASCII adjust for subtraction
ADC accum, immed Add immediate with carry to accumulator
ADC r/m, immed Add immediate with carry to operand
ADC r/m, reg Add register with carry to operand
ADC reg, r/m Add operand with carry to register
ADD accum, immed Add immediate to accumulator
ADD r/m, immed Add immediate to operand
ADD r/m, reg Add register to operand
ADD reg, r/m Add operand to register
AND accum, immed Bitwise And immediate with accumulator
AND r/m, immed Bitwise And immediate with operand
AND r/m, reg Bitwise And register with operand
AND reg, r/m
CALL label
Bitwise And operand with register
Call instruction at label
0
CALL r/m Call instruction indirect
CBW Convert byte to word
CLC Clear carry flag
CLD Clear direction flag
CLI Clear interrupt flag
CMC Complement carry flag
CMP accum, immed Compare immediate with accumulator
CMP r/m, immed Compare immediate with operand
CMP r/m, reg Compare register with operand
CMP reg, r/m Compare operand with register
CMPS src, dent Compare strings
CMPSB Compare strings byte for byte
CMPSW Compare strings word for word
CWD Convert word to double word
DAA Decimal adjust for addition
DAS Decimal adjust for subtraction
DEC r/m
DEC reg
D IV r/m
Decrement operand
Decrement 16-bit register
Divide accumulator by operand
0
ESC immed, r/m Escape with 6-bit immediate and operand
HLT Halt
Instruction Summary
0 JMP label
JMP r/m
JNA label
Jump to instruction at label
Jump to instruction indirect
Jump on not above
JNAE label Jump on not above or equal
JNB label Jump on not below
JNBE label Jump on not below or equal
JNC label Jump on no carry
JNE label Jump on not equal
JNG label Jump on not greater
JNGE label Jump on not greater or equal
JNL label Jump on not less than
JNLE label Jump on not less than or equal
JNO label Jump on not overflow
JNP label Jump on not parity
JNS label Jump on not sign
JNZ label Jump on not zero
JO label Jump on overflow
JP label Jump on parity
JPE label Jump on parity even
JPO label Jump on parity odd
JS lahrl Jump on sign
JZ lahrl Jump on zero
LAHF Load AH with flags
Microsoft Macro Assembler Reference Manual
A 4
Instruction Summary
0 REP NE
REPNZ
REPZ
Repeat if not equal
Repeat if not zero
Repeat if zero
RET [ roamed] Return after popping bytes from stack
ROL r/m, 1 Rotate left by 1 bit
ROL r/m, CL Rotate left by CL
ROR r/m, 1 Rotate right by 1 hit
ROR r/m, CL Rotate right by CL
SAHF Store All into flags
SAL r/m, 1 Shift arithmetic left by 1 bit
SAL r/n, CL Shift arithemetic left by CL
SAR r/m, 1 Shift arithmetic right by I bit
SAR r/m, CL Shift arithmetic right by CL
SBB accum, immed Subtract immediate and carry flag
SBB r/m, immed Subtract immediate and carry flag
SBB r/m, reg Subtract register and carry flag
SBB reg, r/m Subtract operand and carry flag
SCAS deet Scan string
SCASB Scan string for byte in AL
0 SCASW
SHL r/m, 1
SHL r/m, CL
Scan string for word in AX
Shift left by 1 bit
Shift left by CL
S H R r/m, 1 Shift right by 1 bit
S H R r/in, C L Shift right by CL
STC Set carry flag
STD Set direction flag
STI Set interrupt flag
STOS de ,, t Store string
STOSB Store byte in AL at st ring
STOSW Store word in AX at string
SUB accum, immrd Subtract imeediate from accumulator
SUB r/m, immed Subtract immediate from operand
SUB r/m, reg Subtract register from operand
SUB reg, r/m Subtract operand from register
TEST aceum, immed Compare immediate bits with accumulator
TEST r/m, immed Compare immediate bits with operand
TEST r/m, reg Compare register bits with operand
TEST reg, r/m Compare operand bits with register
WAIT Wait
XCHG accum, reg Exchange accumulator with register
XCHG r/m, reg Exchange operand with register
XCHG reg, accum Exchange register with accumulator
The String instructions (CMPS, LODS, MOVS, SCAS, and STOS) use
the DS, SI, ES, and DI registers to compute operand locations . Source
operands are assumed to be at DS :[SI] ; destination operands at
ES :[DI] . The operand type (BYTE or WORD) is defined by the in-
struction mnemonic . For example, CMPSB specifies BYTE operands
and CMPSW specifies WORD operands . For the CMPS, LODS,
MOVS, SCAS, and STOS instructions, the src and dest operands are
dummy operands that define the operand type only . The offsets asso-
ciated with these operands are not used . The src operand can also be
used to specify a segment override . The ES register for the destination
operand cannot be overridden .
Examples
Imps
lods
mov
word ptr string, word ptr es :0
byte ptr string
byte ptr es :O, byte ptr string
0
The REP, REPE, REPNE, REPNZ, or REPZ instructions provide a
way to repeatedly execute a String instruction for a given count or
while a given condition is true . If a Repeat instruction immediately
precedes a String instruction (both instructions must be on the same
line), the instructions are repeated until the specified repeat condi-
tion is false, or the CA register is equal to zero . The Repeat instruc-
tion decrements CX by one for each execution .
Example
mov cx, 10
rep scasb
0
Instruction Summary
Syntax Action
F2XM1 Calculate 2x-1
FABS Take absolute value of top of stack
FADD Add real
FADD fn rm Add real from memory
FADD ST, ST(t) Add real from stack
FADD ST(), ST Add real to stack
FADDP ST i), ST Add real and pop stack
FBLD in r»t Load 10-byte packed decimal on stack
FBSTP ntem Store 10-byte packed decimal and pop
FCHS Change sign on the top stack element
FCLEX Clear exceptions after WAIT
FCOM Compare real
FCOM ST Compare real with top of stack
FCOM ST(,) Compare real with stack
FCOMP Compare real and pop stack
0 FCOMP ST
FCOMP ST()
FCOMPP
Compare real with top of stack and pop
Compare real with stack and pop stack
Compare real and pop stack twice
FDECSTP Decrement stack pointer
FDISI Disable interrupts after AWAIT
FDIV Divide real
FDIV nu in Divide real from memory
FDIV ST, ST(i) Divide real from stack
FDIV ST(i), ST Divide real in stack
FDIVP ST(i), ST Divide real and pop stack
FDIVR Reversed real divide
FDIVR nrr m Reverse real divide from memory
FDIVR ST, ST(i) Reverse real divide from stack
FDIVR ST(i), ST Reverse real divide in stack
FDIVRP ST(i), ST Reversed real divide and pop stack twice
FENI Enable interrupts after AWAIT
FFREE Free stack element
FFREEST Free top of stack element
FFREE ST() Free ith stack element
FIADD m c tit Add 2 or 4-byte integer
FICOM inrm 2 or 4-byte integer compare
FICOMP mem 2 or 4-byte integer compare and pop stack
FIDIV mem 2 or 4-byte integer divide
Microsoft Macro Assembler Reference Manual
0 FSTP mem
FSTSW mem
FSUB
Store 4, 8, or 10-byte real and pop stack
Store 8087 status word after WAIT
Subtract real
FSUB mem Subtract real from memory
FSUB ST, ST(,) Subtract real from stack
FSUB ST(i), ST Subtract real to stack
FSUBP ST(i), ST Subtract real and pop stack
FSUBR Reversed real subtract
FSUBR mum Reversed real subtract from memory
FSUBR ST, ST(i) Reversed real subtract from stack
FSUBR ST(,), ST Reversed real subtract in stack
FSUBRP ST(i), ST Reversed real subtract and pop stack
FTST Test top of stack
FWAIT Wait for last 8087 operation to complete
FXAM Examine top of stack element
FXCH Exchange contents of stack elements
FFREE ST Exchange top of stack element
FFREE ST(i) Exchange top of stack and ith element
FXTRACT Extract exponent and significand
0 FYL2X
FYL2PI
Calculate Y log,,x
Calculate Y log,`(x+1)
Syntax Action
BOUND reg, mem Detect value out of range
ENTER immedl6, immedS Enter procedure
INS mum, DX Input string from port DX
INSB mum, DX Input byte string from port DX
IN SW in( tit, DX Input word string from port I)X
LEAVE Leave procedure
OUTS DX, m e m Output byte/word/string to port DX
OUTSB DX, mun Output byte string to port DX
OUTSW DX, mrm Output word string to port DX
PUSHA Push all registers
POPA Pop all registers
n 0
Instruction Summary
0 The 287 instruction set consists of all 8087 instructions plus the fol-
lowing additional instructions . The .287 directive can be used to en-
able these instructions for assembly .
Syntax Action
FSETPM Set Protected Mode
FSTSW AX Store Status Word in AX (wait)
FNSTSW AX Store Status Word in AX (no-wait)
0
0
0
Appendix B
Directive Summary
0
S
Directive Summary
B .1 Introduction
Directives give the assembler directions and information about input
and output, memory organization, conditional assembly, listing and
cross-reference control, and definitions . There are the following direc-
tives :
Any combination of upper and lowercase letters can be used when giv-
ing directive names in a source file .
name = expression
Assigns the numeric value of expression to
0
name .
( name] DB initial-value,,,
Allocates and initializes a byte (8 bits) of
storage for each initial- value .
0
[name] DW initial-value,,,
Allocates and initializes a word (2 bytes) of
storage for each given initial-value.
[name] DD initial-value,,,
Allocates and initializes a doubleword (4
bytes) of storage for each given initial-value .
[name] DQ initial-value,,,
Allocates and initializes a quadword (8 bytes)
of storage for each given initial-value .
[ name] DT initial-value,,,
Allocates and initializes 10 bytes of storage
for each given initial-value .
Directive Summary
END ( expression]
Marks the end of the module and optionally
0 ENDIF
sets the program entry point to expression .
IFNB < arg > Grants assembly if the argis not blank .
0
IFNDEF name
Grants assembly if name has not yet been de-
fined .
INCLUDE filename
Inserts source code from the source file given
by filename into the current source file during
assembly .
NAME module-name
Sets the name of the current module to
module-name .
ORG expression
Sets the location counter to expression .
PUBLIC name,,,
Makes the variable, label, or absolute symbol
0
given by name available to all other modules
in the program .
Directive Summary
.RADIK expression
Sets the input radix for numbers in the source
s file to expression .
.XCREF name,,,
Suppresses the listing of symbols in the cross-
0
reference listing file .
.XLIST Suppresses listing of subsequent source lines
to the program listing .
0
Appendix C
Segment Names
For High-Level Languages
C .1 Introduction C-1
C .6 Constant Segments
0 C-8
S
0 C .1 Introduction
This appendix describes the naming conventions used to form assem-
bly language source files that are compatible with object modules pro-
duced by the Microsoft C, Pascal, and Fortran language compilers
(version 3 .0 or later) .
C .2 Text Segments
Syntax
assumecs : nameTEXT
Small Model Programs . Only one text segment is allowed . The seg-
mentt must not exceed 64 Kbytes . If the segment's complete definition
is distributed among several modules, the statement
should be used at the beginning of each module to ensure that the seg-
ment is placed in a single 64 Kbyte physical segment . All procedure
0
and statement labels should have the NEAR type .
Example
_main endp
TEXT ends
Middle and Large Model Programs . Multiple text segments are al-
lowed, however, no segment can be greater than 64 Kbytes . To dis-
tinguish one segment from another, each should have its own name .
Since most modules contain only one text segment, the module's
name is often used as part of the text segment's name . All pro-
cedure and statement labels should have the FAR type, unless they
will only be accessed from within the same segment
0 Example
main endp
SAMPLE TEXT ends
A near data segment's name must be "-DATA ." The segment can
contain any combination of data statements defining variables to be
used by the program . The segment must not exceed 64 Kbytes of
data . All data addresses in the segment are relative to the prede-
fined group ''DGROUP" . Therefore, the statements
Example
0 A far data segment defines data or data space that can be accessed
only by specifying an explicit segment value . Only large model pro-
grams can have far data segments .
Example
ARRAY DATA
array dw
dw
segment
0
1
word public 'FAR - DATA'
0
dw 2
dw 4
table dw 1600 dup(?)
ARRAY DATA ends
C .5 Bss Segments
Syntax
0
Segment Names For High-Level Languages
0 Note
The group name DGROUP must not be defined in more than one
GROUP directive in a source file . If a source file contains both a
DATA and BSS segment, the directive
should be used .
Example
0 DGROUP group _BSS
assume ds : DGROUP
C .6 Constant Segments
Syntax
A constant segment defines constant data that will not change dur-
ing program execution . Constant segments are typically used in
large model programs to hold the segment values of far data seg-
ments .
The constant segment's name must be "CONST ." The segment can
contain any combination of data statements defining constants to be
used by the program . The segment must not exceed 64 Kbytes . All
data addresses in the segment are relative to the predefined group
"DGROUP'' . Therefore, the statements
Note
The group name DGROUP must not be defined in more than one
GROUP directive in a source file . If a source file contains a
DATA, BSS, and CONST segment, the directive
should be used .
Example
s
0
Index
segment override Conditional assembly (continued)
operator 5-15 on blank arguments 7-3
= directive 4-8 on defined symbols 7-3
on expressions 7-2
on identical arguments 7-4
ABS type 6-3 Constant Operands 5-2
Absolute (AT) segments 3-7 constants, default radix 9-2
Alignment .CREF directive 9-9
EVEN directive 3-12 Cross-reference listing 9-9
ORG directive 3-11
segments 3-6
AND operator 5-13 Data
Arithmetic operators 5-10 bytes 4-3
Assembly listing doublewords 4-4
false conditionals 9-7 quadwords 4-5
macros 9-8 ten-byte words 4-6
page breaks 9-5 words 4-3
page dimensions 9-5 DB directive 4-3
0 subtitle 9-5
suppressing 9-6
symbols 9-9
DD directive 4-4
Declarations
byte data 4-3
title 9-4 doubleword data 4-4
ASSUME directive 3-10 label 4-1
AT segments 3-7 quadword data 4-5
record 4-15
structure 4-14
Based indexed operands 5-7 ten-byte words 4-6
Based operands 5-5 word data 4-3
Bitwise operators 5-13 Default input radix 9-2
Default segment registers 3-10
Direct memory operands 5-2
Character Set 2-1 Directives, summary B-1
Class argument 3-7 DQ directive 4-5
Combine argument 3-6 DT directive 4-6
COMMENT directive 2-8 DW directive 4-3
Comments 2-7, 2-8
2
Index
0 index 5-13
LENGTH 5-20
LOW 5-17
Register operands 5-4
Relational operators 5-12
Relocatable operands 5-3
MASK 5-22 REPT directive 8-7
OFFSET 5-18
PTR 5-14
relational 5-12 .SALL directive 9-8
SEG 5-17 SEG operator 5-17
segment override ( :) 5-15 SEGMENT directive 3-4
shift 5-11 Segment override operator 5-15
SHL 5-11 Segments
SHORT 5-16 alignment 3-6
SHR 5-11 AT 3-7
SIZE 5-20 class argument 3-7
THIS 5-16 combine types 3-6
TYPE 5-18 COMMON 3-6
.TYPE 5-19 definition 3-4
WIDTH 5-21 groups 3-9
XOR 5-13 loading options 3-6
OR operator 5-13 MEMORY 3-7
ORG directive 3-11 nesting 3-4
%OUT directive 9-3 origin 3-11
PUBLIC 3-6
3
Index
0
4