[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference 7.286::atarist

Title:Atari ST, TT, & Falcon
Notice:Please read note 1.0 and its replies before posting!
Moderator:FUNYET::ANDERSON
Created:Mon Apr 04 1988
Last Modified:Tue May 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:1433
Total number of notes:10312

406.0. "Structure member alignment woes" by LEDDEV::WALLACE () Thu Mar 02 1989 16:46

    After a few hours of debugging a program on the ST the other day
    I realised there was nothing wrong with my program (in my opinion)
    but with the C compiler (Mark Williams).
    
    The problem is that when you create a structure MWC forces all int's
    to be on word boundries. IE: Given,
    		typdef struct {
    			char	a_byte;
    			int	a_word;
    		} MY_STRUCT;
    The data structure generated by MWC aligns the beginning of the
    structure to a word boundry (I have no problem with that) and aligns
    the member "a_int" to a word boundry. Which means that there is an
    unused (and undeclared) byte of memory between where "a_byte" is stored
    and where "a_word" is stored. 
    
    This "normaly" does not pose a problem and is transparent to the
    programmer (in fact I assume the ST accesses words faster on word
    boundries than on byte boundries). The problem comes into play when you
    are trying to map the C structure to an existing (predefined by someone
    else) data structure from disk (and/or ram). In which case your data
    fields do NOT line up. Which means you have to resort to defining
    all of your structure members as chars or arrays of chars (what
    a kludge).

    Alas it turns out that K&R (2nd edition) does mention that-
    "Because of alignment requirements for different objects, there
    may be unnamed holes in a structure."
    I guess I need to re-read K&R more frequently so that I might remember
    some of these insightfull little details and save myself some debugging
    time.
        
    The VAX C compiler does what I expected MWC to do, that is it does not
    align int size structure members to longword boundries it leaves them
    on whatever byte boundry they fall on based on the structure
    definition. Does anyone know if other ST C compilers have this same
    "feature"? 

    	Ray
T.RTitleUserPersonal
Name
DateLines
406.168000's fault right?CADSYS::DALTONA mind is a terrible thingThu Mar 02 1989 16:587
If memory serves me correctly (no pun intended), doesn't MWC have to do that
because the 68000 has to access words on word boundaries (i.e even adresses)
or you get an exception (2 bombs)? I only vaguely remember this... it's been
so long since I've done any real 68000 assembly.

KD
406.2Can't blame the compiler this timePRNSYS::LOMICKAJJeff LomickaThu Mar 02 1989 17:1512
Yes, in this case it's the MC68000, not the compiler.  MWC did the right
thing, and your code was in error.

The 68000, like the PDP-11, will "odd address trap" if you try to access
a word-sized object on an odd boundary.  This will exhibit itself on the
Atari as three bombs.

Note that if you are reading binary data from a file, you may also find
that you need to swap the bit order in the bytes that make up the int's
in your structure, since the 68000 does it backwards from the VAX and
the 8086.  On the 68000, the high order byte comes first.

406.3Guess it's time to hit the books again :-)LEDDEV::WALLACEThu Mar 02 1989 17:286
    The byte ordering I new about the "odd address trap" I either did
    not know about or forgot about.
    
    Thanks for the explanation.
    
    	Ray
406.4A suggestion for your structSMURF::COUTUHe who will not risk, cannot win.Fri Mar 03 1989 00:5618
    Whenever you have to write code which is truly portable across
    machine architectures you have to worry about this. Usually people
    code for a single type of machine and so this never bites them.
    I have a suggestion for dealing with defining the structures; use
    unions instead. For example define a union which is made up of two
    different structures. One is the structure that you REALLY want with
    appropriate unnamed or dummy fields built into the right places. The
    second is a structure of unsigned chars, this is the one you use to
    read from the file with. (I suppose, come to think of it, that it
    could be most any size of element as long as it works out to exactly the
    right length.) Sure is a lot easier than having to access each element
    of the structure piecemeal!
    
    Oh, you may also run into this problem any time you store data on disk
    that was in a structure. Not all machines store data on disk in the
    same format that they use in memory. Fun eh?
    
    Dan
406.5But I thought there's a MOVEP to do itUTRTSC::TEYEMADoes he know where he's talkin' about, anywayFri Mar 03 1989 06:4517
    Having played around a little with 68k assembly, I think there is
    a "work-around" solution for your problem, provided you can disassemble
    and re-assemble your program.
    
    The way you can handle the odd-address word access, is to change
    all apropriate 
    		MOVE.W <source>,<dest> 
    into 
    		MOVEP.W <source>,<pref.Dn register).
    I hope memory serves me well here, I think this instruction allows the
    odd-address for the source operand. About the destination, I know
    a data-register is allowed.
    
    I hope this works for you, and hope you have a good Search & Replace
    option on your editor !
    
    Casper