Vasanth
Vasanth

Reputation: 119

declaration of constant strings in x86 assembly language

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?

Upvotes: 4

Views: 21773

Answers (2)

John Rudolph
John Rudolph

Reputation: 21

Ok, there are a few ways to declare a string in MASM: First, you can declare the string in the data segment and use the variable throughout your code.

stringName    BYTE   "Hello, this is a string", 0 

To declare a string constant you can use the EQU or TEXTEQU directive. Both are declared before the data segment in the global scope.

constantString  EQU      <"Hello, string content", 0>

The tricky part, is that you then need to declare a variable in data segment that has the value of constant. Like this:

.data
variableName    BYTE    constantString

Then you can go on to use your named variable in the rest of the code.

The TEXTEQU can be a bit trickier. It creates a 'text macro'. It can simply assign the text, assign the contents of another existing text macro, or a const integer expression. For the example, here is the text. Again, the constant must be assigned to a variable in the data segment.

constantName    TEXTEQU    <"string content">

.data
variableName    BYTE   constantName

Hope this helps. Note: the zero byte at the end of the string is important for a string declaration, but in the TEXTEQU you are defining a MACRO, not just a string.

Upvotes: 0

Cody Gray
Cody Gray

Reputation: 244792

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)

Upvotes: 8

Related Questions