Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

declaration of constant strings in x86 assembly language

Tags:

x86

assembly

masm

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?

like image 318
Vasanth Avatar asked Oct 22 '25 03:10

Vasanth


1 Answers

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)
like image 134
Cody Gray Avatar answered Oct 23 '25 23:10

Cody Gray



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!