declaration of constant strings in x86 assembly language
The "standard" MASM syntax for declaring a constant string is:
VarName DB "String Contents",0
This declares an array of bytes (db
== declare byte) with the symbolic name VarName
. The contents of that array are given by the value enclosed in quotation marks ("String Contents"
), and the array is terminated by a NUL character (0
). Obviously, what this gives you is a standard C-style NUL-terminated string that can be used with almost any C API.
I call this the "standard" syntax for MASM because it's the one I use, the one that most compilers/disassemblers use, and the one that you'll see most code samples written in.
However, as Ped7g points out in a comment, modern versions of MASM actually support using the BYTE
directive in the declaration. It is effectively a synonym for DB
. That would make the book author's code correct, and suitable for use with the version of MASM bundled with any modern version of Visual Studio:
VarName BYTE "String Contents",0
You can use either one you want. If you're learning from a book that uses the latter, you may want to stick with it for convenience. However, you should be aware of the former, too, since as I mentioned, you'll see it all over the place.
Here is the complete list of type specifiers (keywords) that you'll see in data definitions:
-
DB
(Declare Byte), which is equivalent toBYTE
(an 8-bit value the same size as theAL
register) -
DW
(Declare Word), which is equivalent toWORD
(a 16-bit value the same size as theAX
register) -
DD
(Declare DoubleWord), which is equivalent toDWORD
(a 32-bit value the same size as theEAX
register) -
DF
(Declare Float), which is equivalent toFWORD
(a 48-bit value used to store a single-precision floating-point value) -
DQ
(Declare QuadWord), which is equivalent toQWORD
(a 64-bit value the same size as theRAX
register when targeting 64-bit mode x86-64, and also used to store a double-precision floating-point value) -
DT
(Declare Ten-byte), which is equivalent toTBYTE
(an 80-bit value the same size as the x87 FPU's stack-based registers; used to spill a value from there directly to memory without losing precision)
Vasanth
Updated on June 04, 2022Comments
-
Vasanth almost 2 years
Today, while declaring constant strings (using Visual Studio community 2015 in win 10 ) I was facing a problem. Book said,
String BYTE "HELLO",0
while I typed in the same, MASM throws errors like:
syntax error : ,
Then, I removed , and 0 then it showed:
Missing operator in expression
How can I eliminate this error? What is the right way to declare a constant string?
-
Cody Gray over 7 yearsWhat book are you reading? Perhaps it is not teaching you MASM syntax?
BYTE
is illegal here (although it is a keyword used other places). This should beString db "HELLO",0
and probably located in a.data
segment. -
Vasanth over 7 years@Cody Gray I am reading x86 assembly programming by kip Irvine. Your notation is given under legacy data directives and as equally correct.It is indeed in the .data segment. Thank you.
-
Vasanth over 7 yearsMy problem fixed but rather weirdly.. after updating my PC and rebooting.. it is now accepting both the notations. But what is the standard notation supported by most assemblers? THANK YOU.
-
zx485 over 7 yearsThere are no 'most assemblers'. Everyone has its own syntax. For MASM, the line should be
String db "HELLO",0
. -
Ped7g over 7 yearsa modern version of MASM does support also
BYTE
directive (as replacement for old goodDB
), and indeedDB
is now "obsolete"... I expect to have plenty of fun with MASM questions on SO in next decade, as if ambiguity between 16/32/64b mode and MASM/TASM vs NASM/FASM/YASM vs gas/AT&T wasn't already enough... Well played MS, well played. :D -
Ross Ridge over 7 yearsI can't reproduce the problem. The version of MASM included with Visual Studio 2015 assembles
String BYTE "HELLO",0
without errors.
-
-
Peter Cordes over 4 years
TEXTEQU
is an assemble-time directive, like#define
in C. It doesn't assemble any bytes into the output file. -
Peter Cordes over 4 yearsDo you have a citation for DB standing for Declare Byte? I think of it as standing for "Data Byte"; its purpose is to assemble bytes directly into the output file.
-
Cody Gray over 4 yearsNo citation on hand, @Peter. Just consistency, the hobgoblin of my simple mind.
-
Peter Cordes over 4 yearsAre the
<
and>
around the data actually needed for EQU or TEXTEQU to work? I don't think that looks like normal MASM syntax. -
John Rudolph over 4 yearsyes, but usually strings are declared in the .data segment. When the < > are used, the constants are declared in the global scope