FIX: MASM Generates Incorrect Code for Indirect Addressing (99239)



The information in this article applies to:

  • Microsoft Macro Assembler (MASM) 6.0
  • Microsoft Macro Assembler (MASM) 6.0a
  • Microsoft Macro Assembler (MASM) 6.0b
  • Microsoft Macro Assembler (MASM) 6.1
  • Microsoft Macro Assembler (MASM) 6.1a

This article was previously published under Q99239

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
				

Modification Type:MajorLast Reviewed:10/22/2003
Keywords:kbfix KB99239