Reputation: 119
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
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
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
AL
register)DW
(Declare Word), which is equivalent to WORD
AX
register)DD
(Declare DoubleWord), which is equivalent to DWORD
EAX
register)DF
(Declare Float), which is equivalent to FWORD
DQ
(Declare QuadWord), which is equivalent to QWORD
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
Upvotes: 8