declaration of constant strings in x86 assembly language

14,113

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 to BYTE
    (an 8-bit value the same size as the AL register)
  • DW (Declare Word), which is equivalent to WORD
    (a 16-bit value the same size as the AX register)
  • DD (Declare DoubleWord), which is equivalent to DWORD
    (a 32-bit value the same size as the EAX register)
  • DF (Declare Float), which is equivalent to FWORD
    (a 48-bit value used to store a single-precision floating-point value)
  • DQ (Declare QuadWord), which is equivalent to QWORD
    (a 64-bit value the same size as the RAX 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 to TBYTE
    (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)
Share:
14,113
Vasanth
Author by

Vasanth

Updated on June 04, 2022

Comments

  • Vasanth
    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
      Cody Gray over 7 years
      What 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 be String db "HELLO",0 and probably located in a .data segment.
    • Vasanth
      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
      Vasanth over 7 years
      My 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
      zx485 over 7 years
      There are no 'most assemblers'. Everyone has its own syntax. For MASM, the line should be String db "HELLO",0.
    • Ped7g
      Ped7g over 7 years
      a modern version of MASM does support also BYTE directive (as replacement for old good DB), and indeed DB 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
      Ross Ridge over 7 years
      I can't reproduce the problem. The version of MASM included with Visual Studio 2015 assembles String BYTE "HELLO",0 without errors.
  • Peter Cordes
    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
    Peter Cordes over 4 years
    Do 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
    Cody Gray over 4 years
    No citation on hand, @Peter. Just consistency, the hobgoblin of my simple mind.
  • Peter Cordes
    Peter Cordes over 4 years
    Are 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
    John Rudolph over 4 years
    yes, but usually strings are declared in the .data segment. When the < > are used, the constants are declared in the global scope