Microsoft KB Archive/39526

From BetaArchive Wiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Knowledge Base


How to Share a Structure Between C and Assembler Modules

Article ID: 39526

Article Last Modified on 10/17/2003



APPLIES TO

  • Microsoft Macro Assembler 5.0
  • Microsoft Macro Assembler 5.1 Standard Edition
  • Microsoft Macro Assembler 6.0 Standard Edition
  • Microsoft Macro Assembler 6.0a
  • Microsoft Macro Assembler 6.0b



This article was previously published under Q39526

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

Keywords: KB39526