Chapter 0 BNF Grammar

The BNF grammar gives the full description of the MASM language. The MASM BNF follows the Backus-Naur Form (BNF) for grammar notation.

You can use the BNF to determine the exact syntax for any language component. The BNF format clearly defines recursive definitions and shows all the available options for any placeholder.

Definitions

Terminals are endpoints in a BNF definition. No other resolution of their definition is possible. Terminals include the set of reserved words and user-defined objects.

Nonterminals are placeholders in the BNF definition. All nonterminals are defined elsewhere in the BNF.

The BNF references two types of expressions before they are formally defined: constExpr and immExpr. A constExpr is an expression whose value is not relocatable and not completely known at assembly time. An immExpr is similar to a constExpr, except that it may also be relocatable.

Conventions

The conventions use different font attributes for different items in the BNF. The symbols and formats are as follows:

Attribute Description  
nonterminal Italic type indicates nonterminals.  
RESERVED Terminals in boldface type are literal reserved words and symbols that must be entered as shown. Characters in this context are always case insensitive.  
[[ ]] Objects enclosed in double brackets ([[ ]]) are optional. The brackets do not actually appear in the source code.  
| A vertical bar indicates a choice between the items on each side of the bar.  
Attribute Description  
.8086 Underlined items indicate the default option if one is given.  
default typeface Characters in the set described or listed can be used as terminals in MASM statements.  

How to Use

To illustrate the use of the BNF, Figure B.1 explores the definition of the TYPEDEF directive by starting with the nonterminal typedefDir.

Of course typedefDir is also an option given in the definition for a nonterminal “higher” than typedefDir. Look at the BNF definition for generalDir as an example.

The entries under each horizontal brace in Figure B.1 are terminals (such as NEAR16, NEAR32, FAR16, and FAR32) or nonterminals (such as qualifier, qualifiedType, distance, and protoSpec) that can be further defined. Each nonterminal (italicized word) in the typedefDir definition is also an entry in the BNF. Three vertical dots mean that the BNF description for that nonternminal is not illustrated in this figure (but is in the BNF).

Definitions can be recursive. As an example, note that qualifiedType is used in one of the two possible definitions for qualifiedType and is also a component of the definition for qualifier.

Nonterminal Definition
;; endOfLine | comment
=Dir id = immExpr ;;
addOp + | -
aExpr term | aExpr && term
alpha a thru z | A thru Z | ? | @ | _ | $
altId id
arbitraryText charList
asmInstruction mnemonic [[ exprList ]]
assumeDir ASSUME assumeList ;; | ASSUME NOTHING ;;
assumeList assumeRegister | assumeList , assumeRegister
assumeReg register : assumeVal
assumeRegister assumeSegReg | assumeReg
assumeSegReg segmentRegister : assumeSegVal
assumeSegVal frameExpr | NOTHING | ERROR
assumeVal qualifiedType | NOTHING | ERROR
bcdConst [[ sign ]] decNumber
binaryOp == | != | >= | <= | > | < | &
bitDef bitFieldId : bitFieldSize [[ = constExpr ]]
bitDefList bitDef | bitDefList , [[ ;; ]] bitDef
bitFieldId id
bitFieldSize constExpr
blockStatements directiveList | .CONTINUE [[ .IF cExpr ]] | .BREAK [[ .IF cExpr ]]
byteRegister AL | AH | BL | BH | CL | CH | DL | DH
cExpr aExpr | cExpr || aExpr
character Any character value (ordinal in the range 0–255) except linefeed (10)
charList character | charList character
className string
commDecl [[ nearfar ]] [[ langType ]] id : commType [[ : constExpr ]]
commDir COMM commList ;;
comment ; text ;;
commentDir COMMENT delimiter text text delimiter text ;;
commList commDecl | commList , commDecl
commType type | constExpr
constant digits [[ radixOverride ]]
constExpr expr
contextDir PUSHCONTEXT contextItemList ;; | POPCONTEXT contextItemList ;;
contextItem ASSUMES | RADIX | LISTING | CPU | ALL
contextItemList contextItem | contextItemList , contextItem
controlBlock whileBlock | repeatBlock
controlDir controlIf | controlBlock
controlElseif .ELSEIF cExpr ;; directiveList [[ controlElseif ]]
controlIf .IF cExpr ;; directiveList [[ controlElseif ]] [[ .ELSE ;; directiveList ]] .ENDIF ;;
coprocessor .8087 | .287 | .387 | .NO87
crefDir crefOption ;;
crefOption .CREF | .XCREF [[ idList ]] | .NOCREF [[ idList ]]
cxzExpr expr | ! expr | expr == expr | expr != expr
dataDecl DB | DW | DD | DF | DQ | DT | dataType | typeId
dataDir [[ id ]] dataItem ;;
dataItem dataDecl scalarInstList | structTag structInstList | typeId structInstList | unionTag structInstList | recordTag recordInstList
dataType BYTE | SBYTE | WORD | SWORD | DWORD | SDWORD | FWORD | QWORD | TBYTE | REAL4 | REAL8 | REAL10
decdigit 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
decNumber decdigit | decNumber decdigit
delimiter Any character other than whiteSpaceCharacter
digits decdigit | digits decdigit | digits hexdigit
directive generalDir | segmentDef
directiveList directive | directiveList directive
distance nearfar | NEAR16 | NEAR32 | FAR16 | FAR32
e01 e01 orOp e02 | e02
e02 e02 AND e03 | e03
e03 NOT e04 | e04
e04 e04 relOp e05 | e05
e05 e05 addOp e06 | e06
e06 e06 mulOp e07 | e06 shiftOp e07 | e07
e07 e07 addOp e08 | e08
e08 HIGH e09 | LOW e09 | HIGHWORD e09 | LOWWORD e09 | e09
e09 OFFSET e10 | LROFFSET e10 | TYPE e10 | THIS e10 | e09 PTR e10 | e09 : e10 | e10
e10 e10 . e11 | e10 [[ expr ]] | e11
e11 ( expr ) | [[ expr ]] | WIDTH id | MASK id | SIZE sizeArg | SIZEOF sizeArg | LENGTH id | LENGTHOF id | recordConst | string | constant | type | id | $ | segmentRegister | register | ST | ST ( expr )
echoDir ECHO arbitraryText ;;
elseifBlock elseifStatement ;; directiveList [[ elseifBlock ]]
elseifStatement ELSEIF constExpr | ELSEIFE constExpr | ELSEIFB textItem | ELSEIFNB textItem | ELSEIFDEF id | ELSEIFNDEF id | ELSEIFDIF textItem , textItem | ELSEIFDIFI textItem , textItem | ELSEIFIDN textItem , textItem | ELSEIFIDNI textItem , textItem | ELSEIF1 | ELSEIF2
endDir END [[ immExpr ]] ;;
endpDir procId ENDP ;;
endsDir id ENDS ;;
equDir textMacroId EQU equType ;;
equType immExpr | textLiteral
errorDir errorOpt ;;
errorOpt .ERR [[ textItem ]] | .ERRE constExpr [[ optText ]] | .ERRNZ constExpr [[ optText ]] | .ERRB textItem [[ optText ]] | .ERRNB textItem [[ optText ]] | .ERRDEF id [[ optText ]] | .ERRNDEF id [[ optText ]] | .ERRDIF textItem , textItem [[ optText ]] | .ERRDIFI textItem , textItem [[ optText ]] | .ERRIDN textItem , textItem [[ optText ]] | .ERRIDNI textItem , textItem [[ optText ]] | .ERR1 [[ textItem ]] | .ERR2 [[ textItem ]]
exitDir .EXIT [[ expr ]] ;;
exitmDir: EXITM | EXITM textItem
exponent E [[ sign ]] decNumber
expr SHORT e05 | .TYPE e01 | OPATTR e01 | e01
exprList expr | exprList , expr
externDef [[ langType ]] id [[ ( altId ) ]] : externType
externDir externKey externList ;;
externKey EXTRN | EXTERN | EXTERNDEF
externList externDef | externList , [[ ;; ]] externDef
externType ABS | qualifiedType
fieldAlign constExpr
fieldInit [[ initValue ]] | structInstance
fieldInitList fieldInit | fieldInitList , [[ ;; ]] fieldInit
fileChar Any character value (ordinal in the range 0–255) except backspace (8), tab (9), linefeed (10), vertical tab (11), form feed (12), carriage return (13), ^Z (26), or space (32)
fileCharList fileChar | fileCharList fileChar
fileSpec fileCharList | textLiteral
flagName ZERO? | CARRY? | OVERFLOW? | SIGN? | PARITY?
floatNumber [[ sign ]] decNumber . [[ decNumber ]] [[ exponent ]] | digits R | digits r
forcDir FORC | IRPC
forDir FOR | IRP
forParm id [[ : forParmType ]]
forParmType REQ | = textLiteral
frameExpr expr
generalDir modelDir | segOrderDir | nameDir | includeLibDir | commentDir | groupDir | assumeDir | structDir | recordDir | typedefDir | externDir | publicDir | commDir | protoTypeDir | equDir | =Dir | textDir | contextDir | optionDir | processorDir | radixDir | titleDir | pageDir | listDir | crefDir | echoDir | ifDir | errorDir | includeDir | macroDir | macroCall | macroRepeat | purgeDir | macroWhile | macroFor | macroForc | aliasDir
gpRegister AX | EAX | BX | EBX | CX | ECX | DX | EDX | BP | EBP | SP | ESP | DI | EDI | SI | ESI
groupDir groupId GROUP segIdList
groupId id
hexdigit a | b | c | d | e | f | A | B | C | D | E | F
id alpha | id alpha | id decdigit
idList id | idList , id
ifDir ifStatement ;; directiveList [[ elseifBlock ]] [[ ELSE ;; directiveList ]] ENDIF ;;
ifStatement IF constExpr | IFE constExpr | IFB textItem | IFNB textItem | IFDEF id | IFNDEF id | IFDIF textItem , textItem | IFDIFI textItem , textItem | IFIDN textItem , textItem | IFIDNI textItem , textItem | IF1 | IF2
immExpr expr
includeDir INCLUDE fileSpec ;;
includeLibDir INCLUDELIB fileSpec ;;
initValue immExpr | string | ? | constExpr DUP ( scalarInstList ) | floatNumber | bcdConst
inSegDir [[ labelDef ]] inSegmentDir
inSegDirList inSegDir | inSegDirList inSegDir
inSegmentDir instruction | dataDir | controlDir | startupDir | exitDir | offsetDir | labelDir | procDir [[ localDirList ]] [[ inSegDirList ]] endpDir | invokeDir | generalDir
instrPrefix REP | REPE | REPZ | REPNE | REPNZ | LOCK
instruction [[ instrPrefix ]] asmInstruction
invokeArg register :: register | expr | ADDR expr
invokeDir INVOKE expr [[ , [[ ;; ]] invokeList ]] ;;
invokeList invokeArg | invokeList , [[ ;; ]] invokeArg
keyword Any reserved word
keywordList keyword | keyword keywordList
labelDef id : | id :: | @:
labelDir id LABEL qualifiedType ;;
langType C | PASCAL | FORTRAN | BASIC | SYSCALL | STDCALL
listDir listOption ;;
listOption .LIST | .NOLIST | .XLIST | .LISTALL | .LISTIF | .LFCOND | .NOLISTIF | .SFCOND | .TFCOND | .LISTMACROALL | .LALL | .NOLISTMACRO | .SALL | .LISTMACRO | .XALL
localDef LOCAL idList ;;
localDir LOCAL parmList ;;
localDirList localDir | localDirList localDir
localList localDef | localList localDef
macroArg % constExpr | % textMacroId | % macroFuncId ( macroArgList ) | string | arbitraryText | < arbitraryText >
macroArgList macroArg | macroArgList , macroArg
macroBody [[ localList ]] macroStmtList
macroCall id macroArgList ;; | id ( macroArgList )
macroDir id MACRO [[ macroParmList ]] ;; macroBody ENDM ;;
macroFor forDir forParm , < macroArgList > ;; macroBody ENDM ;;
macroForc forcDir id , textLiteral ;; macroBody ENDM ;;
macroFuncId id
macroId macroProcId | macroFuncId
macroIdList macroId | macroIdList , macroId
macroLabel id
macroParm id [[ : parmType ]]
macroParmList macroParm | macroParmList , [[ ;; ]] macroParm
macroProcId id
macroRepeat repeatDir constExpr ;; macroBody ENDM ;;
macroStmt directive | exitmDir | : macroLabel | GOTO macroLabel
macroStmtList macroStmt ;; | macroStmtList macroStmt ;;
macroWhile WHILE constExpr ;; macroBody ENDM ;;
mapType ALL | NONE | NOTPUBLIC
memOption TINY | SMALL | MEDIUM | COMPACT | LARGE | HUGE | FLAT
mnemonic Instruction name
modelDir .MODEL memOption [[ , modelOptlist ]] ;;
modelOpt langType | osType | stackOption
modelOptlist modelOpt | modelOptlist , modelOpt
module [[ directiveList ]] endDir
mulOp * | / | MOD
nameDir NAME id ;;
nearfar NEAR | FAR
nestedStruct structHdr [[ id ]] ;; structBody ENDS ;;
offsetDir offsetDirType ;;
offsetDirType EVEN | ORG immExpr | ALIGN [[ constExpr ]]
offsetType GROUP | SEGMENT | FLAT
oldRecordFieldList [[ constExpr ]] | oldRecordFieldList , [[ constExpr ]]
optionDir OPTION optionList ;;
optionItem CASEMAP : mapType | DOTNAME | NODOTNAME | EMULATOR | NOEMULATOR | EPILOGUE : macroId | LANGUAGE : langType | LJMP | NOLJMP | M510 | NOM510 | NOKEYWORD : < keywordList > | NOSIGNEXTEND | OFFSET : offsetType | OLDMACROS | NOOLDMACROS | OLDSTRUCTS | NOOLDSTRUCTS | PROC : oVisibility | PROLOGUE : macroId | READONLY | NOREADONLY | SCOPED | NOSCOPED | SEGMENT : segSize
optionList optionItem | optionList , [[ ;; ]] optionItem
optText , textItem
orOp OR | XOR
osType OS_DOS | OS_OS2
oVisibility PUBLIC | PRIVATE | EXPORT
pageDir PAGE [[ pageExpr ]] ;;
pageExpr + | [[ pageLength ]] [[ , pageWidth ]]
pageLength constExpr
pageWidth constExpr
parm parmId [[ : qualifiedType ]] | parmId [ constExpr ] [[ : qualifiedType ]]
parmId id
parmList parm | parmList , [[ ;; ]] parm
parmType REQ | = textLiteral | VARARG
pOptions [[ distance ]] [[ langType ]] [[ oVisibility ]]
primary expr binaryOp expr | flagName | expr
procDir procId PROC [[ pOptions ]] [[ < macroArgList > ]] [[ usesRegs ]] [[ procParmList ]]
processor .8086 | .186 | .286 | .286C | .286P | .386 | .386C | .386P | .486 | .486P
processorDir processor ;; | coprocessor ;;
procId id
procParmList [[ [[ , [[ ;; ]] ]] parmList ]] [[ [[ , [[ ;; ]] ]] parmId :VARARG]]
protoArg [[ id ]] : qualifiedType
protoArgList [[ [[ , [[ ;; ]] ]] protoList ]] [[ [[ , [[ ;; ]] ]] [[ id ]] :VARARG ]]
protoList protoArg | protoList , [[ ;; ]] protoArg
protoSpec [[ distance ]] [[ langType ]] [[ protoArgList ]] | typeId
protoTypeDir id PROTO protoSpec
pubDef [[ langType ]] id
publicDir PUBLIC pubList ;;
pubList pubDef | pubList , [[ ;; ]] pubDef
purgeDir PURGE macroIdList
qualifiedType type | [[ distance ]] PTR [[ qualifiedType ]]
qualifier qualifiedType | PROTO protoSpec
quote “ | '
radixDir .RADIX constExpr ;;
radixOverride h | o | q | t | y | H | O | Q | T | Y
recordConst recordTag { oldRecordFieldList } | recordTag < oldRecordFieldList >
recordDir recordTag RECORD bitDefList ;;
recordFieldList [[ constExpr ]] | recordFieldList , [[ ;; ]] [[ constExpr ]]
recordInstance { [[ ;; ]] recordFieldList [[ ;; ]] } | < oldRecordFieldList > | constExpr DUP ( recordInstance )
recordInstList recordInstance | recordInstList , [[ ;; ]] recordInstance
recordTag id
register specialRegister | gpRegister | byteRegister
regList register | regList register
relOp EQ | NE | LT | LE | GT | GE
repeatBlock .REPEAT ;; blockStatements ;; untilDir ;;
repeatDir REPEAT | REPT
scalarInstList initValue | scalarInstList , [[ ;; ]] initValue
segAlign BYTE | WORD | DWORD | PARA | PAGE
segAttrib PUBLIC | STACK | COMMON | MEMORY | AT constExpr | PRIVATE
segDir .CODE [[ segId ]] | .DATA | .DATA? | .CONST | .FARDATA [[ segId ]] | .FARDATA? [[ segId ]] | .STACK [[ constExpr ]]
segId id
segIdList segId | segIdList , segId
segmentDef segmentDir [[ inSegDirList ]] endsDir | simpleSegDir [[ inSegDirList ]] [[ endsDir ]]
segmentDir segId SEGMENT [[ segOptionList ]] ;;
segmentRegister CS | DS | ES | FS | GS | SS
segOption segAlign | segRO | segAttrib | segSize | className
segOptionList segOption | segOptionList segOption
segOrderDir .ALPHA | .SEQ | .DOSSEG | DOSSEG
segRO READONLY
segSize USE16 | USE32 | FLAT
shiftOp SHR | SHL
sign - | +
simpleExpr ( cExpr ) | primary
simpleSegDir segDir ;;
sizeArg id | type | e10
specialChars : | . | [[ | ]] | ( | ) | < | > | { | } | + | - | / | * | & | % | ! | ' | \ | = | ; | , | " | white space (8, 9, 11–13, 26, 32) | endOfLine
specialRegister CR0 | CR2 | CR3 | DR0 | DR1 | DR2 | DR3 | DR6 | DR7 | TR3 | TR4 | TR5 | TR6 | TR7
stackOption NEARSTACK | FARSTACK
startupDir .STARTUP ;;
stext stringChar | stext stringChar
string quote [[ stext ]] quote
stringChar quote quote | Any character value (ordinal in the range 0–255) except linefeed (10) and elements of quote
structBody structItem ;; | structBody structItem ;;
structDir structTag structHdr [[ fieldAlign ]] [[, NONUNIQUE ]] ;; structBody structTag ENDS ;;
structHdr STRUC | STRUCT | UNION
structInstance < [[ fieldInitList ]] > | { [[ ;; ]] [[ fieldInitList ]] [[ ;; ]] } | constExpr DUP ( structInstList )
structInstList structInstance | structInstList , [[ ;; ]] structInstance
structItem dataDir | generalDir | offsetDir | nestedStruct
structTag id
term simpleExpr | ! simpleExpr
text textLiteral | text character | ! character text | character | ! character
textDir id textMacroDir ;;
textItem textLiteral | textMacroId | % constExpr
textLen constExpr
textList textItem | textList , [[ ;; ]] textItem
textLiteral < text >;;
textMacroDir CATSTR [[ textList ]] | TEXTEQU [[ textList ]] | SIZESTR textItem | SUBSTR textItem , textStart [[ , textLen ]] | INSTR [[ textStart , ]] textItem , textItem
textMacroId id
textStart constExpr
titleDir titleType arbitraryText ;;
titleType TITLE | SUBTITLE | SUBTTL
type structTag | unionTag | recordTag | distance | dataType | typeId
typedefDir typeId TYPEDEF qualifier
typeId id
unionTag id
untilDir .UNTIL cExpr ;; .UNTILCXZ [[ cxzExpr ]] ;;
usesRegs USES regList
whileBlock .WHILE cExpr ;; blockStatements ;; .ENDW
whiteSpaceCharacter ASCII 8, 9, 11–13, 32