How to Share a Structure Between C and Assembler Modules

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

SUMMARY

You may declare a type with STRUC directive in your MASM module with the same memory-storage template as the "struct" type declared in your C module. You also can declare the variable as external with WORD type if it is a near data, or DWORD type if it is a far data.

In C, the structure may be packed on a 1, 2, or 4-byte boundary. The 2-byte boundary is the default. As a result, the template constructed in your MASM module may not match the memory storage exactly, unless pad bytes are specifically added to the template. See the example below.

In MASM 5.10 and earlier, any template you may create in with the STRUC directive has to have unique field names throughout the module. The field names represent the offset relative to the beginning of the structure. They do not have to be literally the same field names of the structure defined in the C module.

MORE INFORMATION

The sample program below demonstrates this information. It consists of a C module and an Assembler module.

Sample Code

/* Compile options needed: none
*/

struct sample {
   char byte_1;
   unsigned int word_1;
   char byte_2;
   unsigned int word_2;
} rec = {0x41, 0xffff, 0x42, 0xeeee};

void proc_rec( void );

void main()
{
   proc_rec( );
}

; Assemble options needed: /Mx (or /Cx with 6.0 and later)

   .model small,c
   .data

sample STRUC
   byte_1  db   ?      ; if dw is used, next padding byte is not needed.
   junk_1  db   ?      ; necessary padding, unused byte
   word_1  dw   ?
   byte_2  db   ?
   junk_2  db   ?      ; necessary padding, unused byte
   word_2  dw   ?
sample ENDS

EXTRN      rec:word    ; can use EXTRN  rec:sample with 6.0 or later

   .code
PUBLIC     proc_rec

proc_rec PROC near
   MOV ax, rec.word_1
   MOV bx, rec.word_2
   MOV cl, rec.byte_1
   MOV ch, rec.byte_2
   RET
proc_rec ENDP
        END


Additional reference words: kbinf 5.00 5.10 6.00 6.00a 6.00b
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.