Mail Archives: djgpp/1997/09/20/07:16:58
Gurunandan R. Bhat writes:
>I need to define a symbol for local structs in an external asm file in a
>way that allows me to refer to the components of the struct by their own
>symbols. Will the following be okay?
>
>------------------------------------
>data
>struct_name:
> component1: .long
> component2: .short
Sure, that will work. But if you are going to have more than one of
these structures, and particularly if you are planning on sharing the
struct between asm and C code, it can be much easier to define a few
offset macros (this requires you to run the code through the C
preprocessor, by compiling it with a capital .S extension or using the
"-x assembler-with-cpp" option to gcc). For example:
#define COMPONENT1 0
#define COMPONENT2 4
struct_name: .space 6
Then you can do things like:
movl $5, struct_name+COMPONENT1;
Or:
movl struct_name, %eax
movw $72, COMPONENT2(%eax)
If the structure is also used by your C code, you can write a little
program to automatically generate a header file containing the offset
defines, by including the header that defines your struct and calling
the offsetof() macro for each field. That way there is never any danger
of your C struct and asm defines getting out of sync with each other...
--
Shawn Hargreaves - shawn AT talula DOT demon DOT co DOT uk - http://www.talula.demon.co.uk/
Beauty is a French phonetic corruption of a short cloth neck ornament.
- Raw text -