The Watch window is not limited to variables. You can enter an expression (that is, any valid combination of variables, constants, and operators) for CodeView to evaluate and display. You can also select the format in which CodeView displays the expression.
Summary: MASM expressions are evaluated using C rules.
CodeView does not include an expression evaluator specifically for MASM. It uses the C expression evaluator instead. This means you must enter MASM variables or expressions in a form the C evaluator recognizes, which is not always the way they appear in a MASM program. (Online help describes the operators and precedence order for C expressions. The last part of this section also gives examples of some of the more commonly used expression forms.)
The Language command from the Options menu offers a choice of Auto, C, Basic, or FORTRAN expression evaluators. However, the Basic and FORTRAN expression evaluators do not support address evaluation, pointer conversions, type casting, or other operations needed when debugging assembly-language code.
Besides arithmetic and memory-reference expressions, CodeView can also display Boolean expressions. For example, if a variable is never supposed to be larger than 100 or less than 25, the expression
(var < 25 || var > 100)
evaluates to one (TRUE) if var goes out of bounds.
By default, CodeView displays expression values in decimal form. You can change the display radix to octal or hexadecimal with the Radix (N) command described at the end of the previous section.
Another way to change the display format is to append a comma and a single-digit format specifier to any watched variable, expression, or address. For example, to display varname in octal form, type varname,o in the Watch expression box. (If varname is already in the Watch window, simply append a comma and the octal specifier ,o and then move the cursor off the line.) The following list describes the use of each specifier:
Specifier | Form Displayed |
c | Least-significant byte of the variable displayed as a single character |
d | Decimal value |
e or E | Eight bytes displayed as a double-precision exponential number |
f | Four bytes displayed as a single-precision floating-point number |
g or G | Eight bytes displayed as a double-precision exponential number |
i | Signed integer value |
o | Unsigned octal value |
s | String; all following bytes displayed as ASCII characters, up to next null character (ASCII 0) |
u | Unsigned decimal value |
x or X | Hexadecimal value, without leading 0x |
Expressions using registers or indexes are more complex. The following sections show how to substitute CodeView expressions using the C expression evaluator for MASM expressions.
The C expression evaluator does not recognize brackets to indicate the memory location pointed to by a register. Instead, use the BY, WO, or DW operator to reference the corresponding byte, word, or doubleword value.
MASM Expression | CodeView Equivalent |
BYTE PTR [bx] | BY bx |
WORD PTR [bp] | WO bp |
DWORD PTR [bp] | DW bp |
Register Indirection with Displacement
To perform based, indexed, or based-indexed indirection with a displacement, use the BY, WO, or DW operator combined with addition.
MASM Expression | CodeView Equivalent |
BYTE PTR [di+6] | BY di+6 |
BYTE PTR Test [bx] | BY &Test+bx |
WORD PTR [si] [bp+6] | WO si+bp+6 |
DWORD PTR [bx] [si] | DW bx+si |
Use the address operator (&) instead of the OFFSET operator.
MASM Expression | CodeView Equivalent |
OFFSET Var | &Var |
Use C type casts, or the BY, WO, and DW operators in conjunction with the address operator (&), to replace the PTR operator.
MASM Expression | CodeView Equivalents |
BYTE PTR Var | BY &Var *(unsigned char*)&Var |
WORD PTR Var | WO &Var *(unsigned *)&Var |
DWORD PTR Var | DW &Var *(unsigned long*)&Var |
Add a comma and the string specifier ,s after the variable name.
MASM Expression | CodeView Equivalent |
Stringvar | Stringvar,s |
Because CodeView uses the C expression evaluator and C strings end with an ASCII null (zero), CodeView displays all characters up to the next null in memory when you request a string display. If you intend to debug a MASM program, you should terminate string variables with a null.
The C expression evaluator equates an array name with the address of its first element. Therefore, you should prefix an array name with the address operator (&), then add the desired offset. The offset can be added directly, or it can appear within parentheses. It can be a number, a register name, or a variable.
The following examples (using byte, word, and doubleword arrays) show how this is done:
MASM Expression | CodeView Equivalents |
String[12] | BY &String+12 *(&String+12) |
aWords[bx+di] | WO &aWords+bx+di *(unsigned*)(&aWords+bx+di) |
aDWords[bx+4] | DW &aDWords+bx+4 *(unsignedlong*)(&aDWords+bx+4) |
MASM 6.0 lets you define pointer-type variables. Since these are the same as C pointers, the C expression evaluator works as it does with C programs.
You dereference a pointer simply by typing its name in the Watch window. The pointer's address is displayed, followed by all the elements of the variable to which the pointer refers. Multiple levels of indirection (that is, pointers referencing other pointers) can be displayed simultaneously.