How to Share a Structure Between C and Assembler ModulesLast reviewed: January 23, 1995Article ID: Q39526 |
The information in this article applies to:
SUMMARYYou 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 INFORMATIONThe 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 .datasample 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_recproc_rec PROC near MOV ax, rec.word_1 MOV bx, rec.word_2 MOV cl, rec.byte_1 MOV ch, rec.byte_2 RETproc_rec ENDP END |
Additional reference words: kbinf 5.00 5.10 6.00 6.00a 6.00b
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |