Accessing MASM Structure Fields in a Multimodule Program

Last reviewed: January 23, 1995
Article ID: Q30887
The information in this article applies to:
  • Microsoft Macro Assembler for MS-DOS, versions 5.0, 5.1, and 6.0
  • Microsoft Macro Assembler for OS/2, version 6.0

SUMMARY

To use a structure, defined in one module with the STRUC (STRUCT in MASM 6.0) operator, in another module with the Macro Assembler, you must take into account the fact that the structure definition must be available to both modules at assemble time. The structure field operator (.) can then be used to access the structure's contents.

MORE INFORMATION

The following program demonstrates the use of a structure in a two-module program:

; Source file 1:

EXTRN changecow:NEAR                ; Near function call

animals    STRUC                    ; Structure definition
    cat    db    "catfield      "
    dog    db    "dogfield      "   ; Field widths of 15 characters
    cow    db    "cowfield      "
animals    ENDS

.MODEL small

.DATA

           PUBLIC zoo
zoo        animals   <"felix","spot","Holstein">  ; Initialize fields

.CODE
main:      mov ax, @data            ; Make data addressable
           mov ds,ax
           mov es,ax                ; For string moves

           call changecow           ; Change "Holstein" to "Daisy"
           retf
           END main

; Source file 2: ; In source file 2, structure definition must be available for the ; field offsets. (Normally it would be placed in an include file, ; then the INCLUDE operator would be used; however, for sake of ; clarity, the structure definition has been placed in both files). ; The structure information must be included here to correctly ; associate the fields with the offset. The structure name does not ; need to be "animals"; however, the structure fields should ; correspond.

animals    STRUC                    ; Structure definition
    cat    db    "catfield      "
    dog    db    "dogfield      "   ; Field widths of 15 characters
    cow    db    "cowfield      "
animals    ENDS


.MODEL small

.DATA

EXTRN zoo:NEAR                       ; Let assembler know about zoo
newcow    db     "Daisy         "    ; Replace cow with new name
lenname   EQU    $ - newcow          ; Length of new name

.CODE
          PUBLIC changecow           ; Changes cow field to "Daisy"
changecow PROC
          mov     di, offset zoo.cow ; es:di should now point to
destination
          mov     si, offset newcow  ; ds:si should now point to the source
          mov     cx, lenname        ; Length of new string
          rep     movsb              ; Do replacement
          ret
changecow ENDP
          END


Additional reference words: kbinf 5.00 5.10 6.00
KBCategory: kbprg
KBSubCategory: MASMLngIss


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: January 23, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.