Accessing C Data

A great convenience of inline assembly is the ability to refer to C variables by name. An __asm block can refer to any symbols—including variable names—that are visible where the block appears. For instance, if the C variable var is visible, the instruction

__asm mov ax, var

stores the value of var in AX.

If a structure or union member has a unique name, an __asm block can refer to it using only the member name, without specifying the C variable or typedef name before the period (.) operator. If the member name is not unique, however, you must place a variable or typedef name immediately before the period (.) operator. For instance, the following structure types share same_name as their member name:

struct first_type

{

char *weasel;

int same_name;

};

struct second_type

{

int wonton;

long same_name;

};

If you declare variables with the types

struct first_type hal;

struct second_type oat;

all references to the member same_name must use the variable name, because same_name is not unique. But the member weasel has a unique name, so you can refer to it using only its member name:

__asm

{

mov bx, OFFSET hal

mov cx, [bx]hal.same_name ; Must use 'hal'

mov si, [bx].weasel ; Can omit 'hal'

}

Note that omitting the variable name is merely a coding convenience. The same assembly instructions are generated whether or not it is present.