FIX: Subtracting Two Externs Gives Incorrect Results

Last reviewed: September 16, 1997
Article ID: Q75177
6.00 | 6.00 MS-DOS | OS/2 kbtool kbfixlist kbbuglist

The information in this article applies to:

  • Microsoft Macro Assembler for MS-DOS, version 6.0
  • Microsoft Macro Assembler for OS/2, version 6.0

SYMPTOMS

Subtracting two external variables in the Microsoft Macro Assembler (MASM) version 6.0 results in an incorrect value, and the assembler does not generate any errors. It should produce an error similar to the one received in MASM 5.1, such as:

   A2032: Illegal Use of extern

CAUSE

When subtracting two local variables, the assembler will correctly calculate the difference in their offsets. However, with external variables, it does not know the offset at assemble time, so it cannot correctly determine their difference.

RESOLUTION

The difference in offsets for external variables can be calculated by moving the values into registers or temporary variables and then performing the calculation. The sample code below illustrates this problem.

STATUS

Microsoft has confirmed this to be a problem in MASM version 6.0. This problem was corrected in MASM version 6.0a.

MORE INFORMATION

Sample Code 1

; Assemble options needed: none

.MODEL small .STACK 4096

.DATA extrn Var1 : byte, Var2 : byte ; The extern variables

.CODE

    .startup
    mov ax, offset Var1   ; Gives correct offset of extern var1
    mov bx, offset Var2   ; Gives correct offset of extern var2
    mov cx, (Var2 - Var1) ; Incorrectly calculate a zero for
                          ; the difference
    sub bx, ax            ; This will calculate the correct value
    .exit 0
END

Sample Code 2

; Contains the external variable definitions

PUBLIC   Var1, Var2
.MODEL small .DATA
Var1     byte   '5'
Var2     byte   '9'
END


Additional reference words: 6.00 buglist6.00 fixlist6.00a
KBCategory: kbtool kbfixlist kbbuglist
KBSubCategory: MLIss
Solution Type : kbfix


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 16, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.