Accessing a Structure in Assembly

Question: How do I access a structure defined in C from an assembly (.asm) file?

 

Answer:

A structure and its elements declared in C may be accessed in assembly by adding an underscore to the name of the structure and by using offsets.  For example, if there is a structure:

struct sMyStruct
{
   BYTE bVariable1;
   BYTE bVariable2;
}sMyStruct;

This structure may be accessed in assembly like:

add [_sMyStruct + 0],1 // Access bVariable1
add [_sMyStruct + 1],1 // Access bVariable2

Equates may be created for the elements and may be used instead of absolute offsets.  For example:

VARIABLE1:   EQU   0
VARIABLE2:   EQU   1

mov A,[_sMyStruct + VARIABLE1]
mov A,[_sMyStruct + VARIABLE2]