NASM understands four different types of constant: numeric, character, string and floating-point.
A numeric constant is simply a number. NASM allows you to specify
numbers in a variety of number bases, in a variety of ways: you can suffix H
, Q
or O
, and
B
for hex, octal, and binary, or you can prefix 0x
for hex in
the style of C, or you can prefix $
for hex in the style of
Borland Pascal. Note, though, that the $
prefix does double
duty as a prefix on identifiers (see Section 3.1), so a hex number prefixed with a
$
sign must have a digit after the $
rather than a letter.
Some examples:
mov ax,100 ; decimal mov ax,0a2h ; hex mov ax,$0a2 ; hex again: the 0 is required mov ax,0xa2 ; hex yet again mov ax,777q ; octal mov ax,777o ; octal again mov ax,10010011b ; binary
A character constant consists of up to four characters enclosed in either single or double quotes. The type of quote makes no difference to NASM, except of course that surrounding the constant with single quotes allows double quotes to appear within it and vice versa.
A character constant with more than one character will be arranged with little-endian order in mind: if you code
mov eax,'abcd'
then the constant generated is not 0x61626364
, but
0x64636261
, so that if you were then to store the value into
memory, it would read abcd
rather than dcba
. This is also the sense of character constants understood by the
Pentium’s CPUID
instruction.
String
constants are only acceptable to some pseudo-instructions, namely the DB
family and INCBIN
.
A string constant looks like a character constant, only longer. It is treated as a concatenation of maximum-size character constants for the conditions. So the following are equivalent:
db 'hello' ; string constant db 'h','e','l','l','o' ; equivalent character constants
And the following are also equivalent:
dd 'ninechars' ; doubleword string constant dd 'nine','char','s' ; becomes three doublewords db 'ninechars',0,0,0 ; and really looks like this
Note that when used as an operand to db
, a constant like
'ab'
is treated as a string constant despite being short
enough to be a character constant, because otherwise db 'ab'
would have the same effect as db 'a'
, which would be silly.
Similarly, three-character or four-character constants are treated as strings when they
are operands to dw
.
Floating-point constants are acceptable only as
arguments to DW
, DD
, DQ
and DT
. They
are expressed in the traditional form: digits, then a period, then optionally more
digits, then optionally an E
followed by an exponent. The
period is mandatory, so that NASM can distinguish between dd
1
, which declares an integer constant, and dd 1.0
which declares a floating-point constant.
Some examples:
dw -0.5 ; IEEE half precision dd 1.2 ; an easy one dq 1.e10 ; 10,000,000,000 dq 1.e+10 ; synonymous with 1.e10 dq 1.e-10 ; 0.000 000 000 1 dt 3.141592653589793238462 ; pi
NASM cannot do compile-time arithmetic on floating-point constants. This is because NASM is designed to be portable - although it always generates code to run on x86 processors, the assembler itself can run on any system with an ANSI C compiler. Therefore, the assembler cannot guarantee the presence of a floating-point unit capable of handling the Intel number formats, and so for NASM to be able to do floating arithmetic it would have to include its own complete set of floating-point routines, which would significantly increase the size of the assembler for very little benefit.