Q99239: FIX: MASM Generates Incorrect Code for Indirect Addressing

Article: Q99239
Product(s): Microsoft Macro Assembler
Version(s): 6.0,6.0a,6.0b,6.1,6.1a
Operating System(s): 
Keyword(s): 
Last Modified: 06-MAY-2001

-------------------------------------------------------------------------------
The information in this article applies to:

- Microsoft Macro Assembler (MASM), versions 6.0, 6.0a, 6.0b, 6.1, 6.1a 
-------------------------------------------------------------------------------

SYMPTOMS
========

Microsoft Macro Assembler (MASM) generates incorrect code when an indirect
addressing using 32-bit registers uses a label located in a 16-bit segment as
the first argument in the list that forms the indirect address.

CAUSE
=====

When the first operand in the list of operands that forms the indirect address
is a label located in a 16-bit segment, MASM interprets the indirect address as
a 16-bit reference to the segment.

RESOLUTION
==========

To work around this problem, perform one of the following two steps:

- Use a 32-bit register as the first operand in the indirect address.

- Place the label outside the brackets.

The code example below demonstrates both of these techniques.

STATUS
======

Microsoft has confirmed this to be a problem in MASM versions 6.0, 6.0a, 6.0b,
6.1, and 6.1a. This problem was corrected in MASM for MS-DOS version 6.11.

MORE INFORMATION
================

The evaluation order precedence of the operands in indirect addressing is left
to right inside the brackets, followed by the label outside the brackets, if one
is present. MASM generates incorrect code when the code uses any combination of
32-bit registers and a label to calculate an address when the label is located
in a 16-bit segment and the label is listed as the first operand enclosed in
brackets.

To work around this problem, make sure that the first operand MASM processes is a
32-bit register; then MASM calculates the address properly. You can do this by
moving a 32-bit register to the beginning of the list in the brackets or moving
the label outside the brackets. The code example below demonstrates both of
these techniques.

Sample Code
-----------

  ; Assemble options needed:  /Zi
  ; Use CodeView to see the instructions generated by the assembler

  .386
  sseg SEGMENT STACK USE16 'STACK'
      BYTE 4096 dup (?)
  sseg ENDS

  cseg SEGMENT PUBLIC USE16 'CODE'
  ASSUME cs:cseg, ds:cseg, es:cseg
  filler WORD 0
  alabel DWORD 2 dup (?)
  start:
      mov eax, dword ptr [alabel+esi] ; assembles incorrectly
                                      ;  MOV EDI,DWORD PTR [BX+SI+0002]
      mov eax, dword ptr [esi+alabel] ; assembles correctly
                                      ;  MOV EAX,DWORD PTR [ESI+00000002]
      mov eax, dword ptr alabel[esi]  ; assembles correctly
                                      ;  MOV EAX,DWORD PTR [ESI+00000002]
      mov ax, 4C00h
      int 21h
  cseg ENDS
  END start

Additional query words: 6.00 6.00a 6.00b 6.10 6.10a buglist6.00a buglist6.00b buglist6.10 buglist6.10a fixlist6.11

======================================================================
Keywords          :  
Technology        : kbMASMsearch kbAudDeveloper kbMASM600 kbMASM610 kbMASM610a kbMASM600a kbMASM600b
Version           : :6.0,6.0a,6.0b,6.1,6.1a
Solution Type     : kbfix

=============================================================================