www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/09/20/07:16:58

From: Shawn Hargreaves <Shawn AT talula DOT demon DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Structs in Asm
Date: Sat, 20 Sep 1997 10:10:50 +0100
Organization: None
Distribution: world
Message-ID: <aMzFXbAaM5I0Ew60@talula.demon.co.uk>
References: <Pine DOT LNX DOT 3 DOT 91 DOT 970920033312 DOT 1481A-100000 AT aditya DOT unigoa DOT ernet DOT in>
NNTP-Posting-Host: talula.demon.co.uk
MIME-Version: 1.0
Lines: 42
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019