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?
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 BYTEAL register)DW (Declare Word), which is equivalent to WORDAX register)DD (Declare DoubleWord), which is equivalent to DWORDEAX register)DF (Declare Float), which is equivalent to FWORDDQ (Declare QuadWord), which is equivalent to QWORDRAX 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 TBYTEIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With